Work in progress including IP in fav reports.
[timetracker.git] / WEB-INF / lib / ttFavReportHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('ttTeamHelper');
30
31 // Class ttFavReportHelper is used to help with favorite report related tasks.
32 class ttFavReportHelper {
33
34   // getReports - returns an array of favorite reports for user.
35   static function getReports($user_id) {
36     $mdb2 = getConnection();
37
38     $result = array();
39     $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1";
40     $res = $mdb2->query($sql);
41     if (!is_a($res, 'PEAR_Error')) {
42       while ($val = $res->fetchRow()) {
43         $result[] = $val;
44       }
45       return mu_sort($result, 'name');
46     }
47     return false;
48   }
49
50   // getReport - returns a report identified by its id.
51   static function getReport($id) {
52     $mdb2 = getConnection();
53
54     $sql = "select * from tt_fav_reports where id = $id and status = 1";
55     $res = $mdb2->query($sql);
56     if (!is_a($res, 'PEAR_Error')) {
57       if ($val = $res->fetchRow()) {
58         return $val;
59       }
60     }
61     return false;
62   }
63
64   // getReportByName - returns a report identified by its name.
65   static function getReportByName($user_id, $report_name) {
66     $mdb2 = getConnection();
67
68     $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1 and name = ".$mdb2->quote($report_name);
69     $res = $mdb2->query($sql);
70     if (!is_a($res, 'PEAR_Error')) {
71       if ($val = $res->fetchRow()) {
72         return $val;
73       }
74     }
75     return false;
76   }
77
78   // insertReport - stores reports settings in database.
79   static function insertReport($fields) {
80     $mdb2 = getConnection();
81
82     $sql = "insert into tt_fav_reports (name, user_id, client_id, cf_1_option_id, project_id, task_id,
83       billable, invoice, paid_status, users, period, period_start, period_end,
84       show_client, show_invoice, show_paid, show_ip,
85       show_project, show_start, show_duration, show_cost,
86       show_task, show_end, show_note, show_custom_field_1,
87       group_by, show_totals_only)
88       values(".
89       $mdb2->quote($fields['name']).", ".$fields['user_id'].", ".
90       $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
91       $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
92       $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['invoice']).", ".
93       $mdb2->quote($fields['paid_status']).", ".
94       $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
95       $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
96       $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
97       $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
98       $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".
99       $mdb2->quote($fields['group_by']).", ".$fields['chtotalsonly'].")";
100     $affected = $mdb2->exec($sql);
101     if (is_a($affected, 'PEAR_Error'))
102       return false;
103
104     $sql = "select last_insert_id() as last_id";
105     $res = $mdb2->query($sql);
106     if (is_a($res, 'PEAR_Error'))
107       return false;
108
109     $val = $res->fetchRow();
110     return $val['last_id'];
111   }
112
113   // updateReport - updates report options in the database.
114   function updateReport($fields) {
115     $mdb2 = getConnection();
116     $sql = "update tt_fav_reports set ".
117       "name = ".$mdb2->quote($fields['name']).", ".
118       "client_id = ".$mdb2->quote($fields['client']).", ".
119       "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
120       "project_id = ".$mdb2->quote($fields['project']).", ".
121       "task_id = ".$mdb2->quote($fields['task']).", ".
122       "billable = ".$mdb2->quote($fields['billable']).", ".
123       "invoice = ".$mdb2->quote($fields['invoice']).", ".
124       "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
125       "users = ".$mdb2->quote($fields['users']).", ".
126       "period = ".$mdb2->quote($fields['period']).", ".
127       "period_start = ".$mdb2->quote($fields['from']).", ".
128       "period_end = ".$mdb2->quote($fields['to']).", ".
129       "show_client = ".$fields['chclient'].", ".
130       "show_invoice = ".$fields['chinvoice'].", ".
131       "show_paid = ".$fields['chpaid'].", ".
132       "show_ip = ".$fields['chip'].", ".
133       "show_project = ".$fields['chproject'].", ".
134       "show_start = ".$fields['chstart'].", ".
135       "show_duration = ".$fields['chduration'].", ".
136       "show_cost = ".$fields['chcost'].", ".
137       "show_task = ".$fields['chtask'].", ".
138       "show_end = ".$fields['chfinish'].", ".
139       "show_note = ".$fields['chnote'].", ".
140       "show_custom_field_1 = ".$fields['chcf_1'].", ".
141       "group_by = ".$mdb2->quote($fields['group_by']).", ".
142       "show_totals_only = ".$fields['chtotalsonly'].
143       " where id = ".$fields['id'];
144     $affected = $mdb2->exec($sql);
145     if (is_a($affected, 'PEAR_Error'))
146       return false;
147
148     return $fields['id'];
149   }
150
151   // saveReport - saves report options in the database.
152   static function saveReport($user_id, $bean) {
153     global $user;
154
155     //  Set default value of 0 for not set checkboxes (in bean).
156     //  Later in this function we use it to construct $fields array to update database.
157     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
158     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
159     if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
160     if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
161     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
162     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
163     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
164     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
165     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
166     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
167     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
168     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
169     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
170
171     if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) {
172       $users_in_bean = $bean->getAttribute('users');
173
174       // If all users are selected - use a null value (which means "all users").
175       $all_users_selected = true;
176       if ($user->can('view_reports')) {
177         $all = ttTeamHelper::getActiveUsers();
178         foreach ($all as $one) {
179           if (!in_array($one['id'], $users_in_bean)) {
180             $all_users_selected = false;
181             break;
182           }
183         }
184       }
185       if ($all_users_selected)
186         $users = null;
187       else
188         $users = join(',', $users_in_bean);
189     }
190     if ($bean->getAttribute('start_date')) {
191       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
192       $from = $dt->toString(DB_DATEFORMAT);
193     }
194     if ($bean->getAttribute('end_date')) {
195       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
196       $to = $dt->toString(DB_DATEFORMAT);
197     }
198
199     $fields = array(
200       'name'=>$bean->getAttribute('new_fav_report'),
201       'client'=>$bean->getAttribute('client'),
202       'option'=>$bean->getAttribute('option'),
203       'project'=>$bean->getAttribute('project'),
204       'task'=>$bean->getAttribute('task'),
205       'billable'=>$bean->getAttribute('include_records'),
206       'invoice'=>$bean->getAttribute('invoice'),
207       'paid_status'=>$bean->getAttribute('paid_status'),
208       'users'=>$users,
209       'period'=>$bean->getAttribute('period'),
210       'from'=>$from,
211       'to'=>$to,
212       'chclient'=>$bean->getAttribute('chclient'),
213       'chinvoice'=>$bean->getAttribute('chinvoice'),
214       'chpaid'=>$bean->getAttribute('chpaid'),
215       'chip'=>$bean->getAttribute('chip'),
216       'chproject'=>$bean->getAttribute('chproject'),
217       'chstart'=>$bean->getAttribute('chstart'),
218       'chduration'=>$bean->getAttribute('chduration'),
219       'chcost'=>$bean->getAttribute('chcost'),
220       'chtask'=>$bean->getAttribute('chtask'),
221       'chfinish'=>$bean->getAttribute('chfinish'),
222       'chnote'=>$bean->getAttribute('chnote'),
223       'chcf_1'=>$bean->getAttribute('chcf_1'),
224       'group_by'=>$bean->getAttribute('group_by'),
225       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
226
227     $id = false;
228     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
229     if ($report) {
230       $fields['id'] = $report['id'];
231       $id = ttFavReportHelper::updateReport($fields);
232     } else {
233       $fields['user_id'] = $user_id;
234       $id = ttFavReportHelper::insertReport($fields);
235     }
236
237     return $id;
238   }
239
240   // deleteReport - deletes a favorite report.
241   static function deleteReport($id) {
242     $mdb2 = getConnection();
243
244     $sql = "delete from tt_fav_reports where id = $id";
245     $affected = $mdb2->exec($sql);
246     return (!is_a($affected, 'PEAR_Error'));
247   }
248
249   // loadReport - loads report options from database into a bean.
250   static function loadReport($user_id, &$bean) {
251     global $user;
252
253     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
254     if ($val) {
255       $bean->setAttribute('client', $val['client_id']);
256       $bean->setAttribute('option', $val['cf_1_option_id']);
257       $bean->setAttribute('project', $val['project_id']);
258       $bean->setAttribute('task', $val['task_id']);
259       $bean->setAttribute('include_records', $val['billable']);
260       $bean->setAttribute('invoice', $val['invoice']);
261       $bean->setAttribute('paid_status', $val['paid_status']);
262       if ($val['users'])
263         $bean->setAttribute('users', explode(',', $val['users']));
264       else {
265         // Null users value means "all users". Add them to the bean.
266         if ($user->can('view_reports')) {
267           $all = ttTeamHelper::getActiveUsers();
268           foreach ($all as $one) {
269             $all_user_ids[] = $one['id'];
270           }
271           $bean->setAttribute('users', $all_user_ids);
272         }
273       }
274       $bean->setAttribute('period', $val['period']);
275       if ($val['period_start']) {
276         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
277         $bean->setAttribute('start_date', $dt->toString($user->date_format));
278       }
279       if ($val['period_end']) {
280         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
281         $bean->setAttribute('end_date', $dt->toString($user->date_format));
282       }
283       $bean->setAttribute('chclient', $val['show_client']);
284       $bean->setAttribute('chinvoice', $val['show_invoice']);
285       $bean->setAttribute('chpaid', $val['show_paid']);
286       $bean->setAttribute('chip', $val['show_ip']);
287       $bean->setAttribute('chproject', $val['show_project']);
288       $bean->setAttribute('chstart', $val['show_start']);
289       $bean->setAttribute('chduration', $val['show_duration']);
290       $bean->setAttribute('chcost', $val['show_cost']);
291       $bean->setAttribute('chtask', $val['show_task']);
292       $bean->setAttribute('chfinish', $val['show_end']);
293       $bean->setAttribute('chnote', $val['show_note']);
294       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
295       $bean->setAttribute('group_by', $val['group_by']);
296       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
297       $bean->setAttribute('new_fav_report', $val['name']);
298     } else {
299       $attrs = $bean->getAttributes();
300       $attrs = array_merge($attrs, array(
301         'client'=>'',
302         'option'=>'',
303         'project'=>'',
304         'task'=>'',
305         'include_records'=>'',
306         'invoice'=>'',
307         'users'=>$user_id,
308         'period'=>'',
309         'chclient'=>'1',
310         'chinvoice'=>'',
311         'chproject'=>'1',
312         'chstart'=>'1',
313         'chduration'=>'1',
314         'chcost'=>'',
315         'chtask'=>'1',
316         'chfinish'=>'1',
317         'chnote'=>'1',
318         'chcf_1'=>'',
319         'group_by'=>'',
320         'chtotalsonly'=>'',
321         'new_fav_report'=>''));
322       $bean->setAttributes($attrs);
323     }
324   }
325 }