Some fixes associated with renaming the group_by field.
[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, show_work_units,
87       group_by1, 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'].", ".$fields['chunits'].", ".
99       $mdb2->quote($fields['group_by1']).", ".$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       "show_work_units = ".$fields['chunits'].", ".
142       "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
143       "show_totals_only = ".$fields['chtotalsonly'].
144       " where id = ".$fields['id'];
145     $affected = $mdb2->exec($sql);
146     if (is_a($affected, 'PEAR_Error'))
147       return false;
148
149     return $fields['id'];
150   }
151
152   // saveReport - saves report options in the database.
153   static function saveReport($user_id, $bean) {
154     global $user;
155
156     //  Set default value of 0 for not set checkboxes (in bean).
157     //  Later in this function we use it to construct $fields array to update database.
158     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
159     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
160     if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
161     if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
162     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
163     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
164     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
165     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
166     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
167     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
168     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
169     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
170     if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
171     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
172
173     $users_in_bean = $bean->getAttribute('users');
174     if ($users_in_bean && is_array($users_in_bean)) {
175       $users = join(',', $users_in_bean);
176     }
177     if ($bean->getAttribute('start_date')) {
178       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
179       $from = $dt->toString(DB_DATEFORMAT);
180     }
181     if ($bean->getAttribute('end_date')) {
182       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
183       $to = $dt->toString(DB_DATEFORMAT);
184     }
185
186     $fields = array(
187       'name'=>$bean->getAttribute('new_fav_report'),
188       'client'=>$bean->getAttribute('client'),
189       'option'=>$bean->getAttribute('option'),
190       'project'=>$bean->getAttribute('project'),
191       'task'=>$bean->getAttribute('task'),
192       'billable'=>$bean->getAttribute('include_records'),
193       'invoice'=>$bean->getAttribute('invoice'),
194       'paid_status'=>$bean->getAttribute('paid_status'),
195       'users'=>$users,
196       'period'=>$bean->getAttribute('period'),
197       'from'=>$from,
198       'to'=>$to,
199       'chclient'=>$bean->getAttribute('chclient'),
200       'chinvoice'=>$bean->getAttribute('chinvoice'),
201       'chpaid'=>$bean->getAttribute('chpaid'),
202       'chip'=>$bean->getAttribute('chip'),
203       'chproject'=>$bean->getAttribute('chproject'),
204       'chstart'=>$bean->getAttribute('chstart'),
205       'chduration'=>$bean->getAttribute('chduration'),
206       'chcost'=>$bean->getAttribute('chcost'),
207       'chtask'=>$bean->getAttribute('chtask'),
208       'chfinish'=>$bean->getAttribute('chfinish'),
209       'chnote'=>$bean->getAttribute('chnote'),
210       'chcf_1'=>$bean->getAttribute('chcf_1'),
211       'chunits'=>$bean->getAttribute('chunits'),
212       'group_by1'=>$bean->getAttribute('group_by1'),
213       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
214
215     $id = false;
216     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
217     if ($report) {
218       $fields['id'] = $report['id'];
219       $id = ttFavReportHelper::updateReport($fields);
220     } else {
221       $fields['user_id'] = $user_id;
222       $id = ttFavReportHelper::insertReport($fields);
223     }
224
225     return $id;
226   }
227
228   // deleteReport - deletes a favorite report.
229   static function deleteReport($id) {
230     $mdb2 = getConnection();
231
232     $sql = "delete from tt_fav_reports where id = $id";
233     $affected = $mdb2->exec($sql);
234     return (!is_a($affected, 'PEAR_Error'));
235   }
236
237   // loadReport - loads report options from database into a bean.
238   static function loadReport($user_id, &$bean) {
239     global $user;
240
241     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
242     if ($val) {
243       $bean->setAttribute('client', $val['client_id']);
244       $bean->setAttribute('option', $val['cf_1_option_id']);
245       $bean->setAttribute('project', $val['project_id']);
246       $bean->setAttribute('task', $val['task_id']);
247       $bean->setAttribute('include_records', $val['billable']);
248       $bean->setAttribute('invoice', $val['invoice']);
249       $bean->setAttribute('paid_status', $val['paid_status']);
250       $bean->setAttribute('users', explode(',', $val['users']));
251       $bean->setAttribute('period', $val['period']);
252       if ($val['period_start']) {
253         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
254         $bean->setAttribute('start_date', $dt->toString($user->date_format));
255       }
256       if ($val['period_end']) {
257         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
258         $bean->setAttribute('end_date', $dt->toString($user->date_format));
259       }
260       $bean->setAttribute('chclient', $val['show_client']);
261       $bean->setAttribute('chinvoice', $val['show_invoice']);
262       $bean->setAttribute('chpaid', $val['show_paid']);
263       $bean->setAttribute('chip', $val['show_ip']);
264       $bean->setAttribute('chproject', $val['show_project']);
265       $bean->setAttribute('chstart', $val['show_start']);
266       $bean->setAttribute('chduration', $val['show_duration']);
267       $bean->setAttribute('chcost', $val['show_cost']);
268       $bean->setAttribute('chtask', $val['show_task']);
269       $bean->setAttribute('chfinish', $val['show_end']);
270       $bean->setAttribute('chnote', $val['show_note']);
271       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
272       $bean->setAttribute('chunits', $val['show_work_units']);
273       $bean->setAttribute('group_by1', $val['group_by1']);
274       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
275       $bean->setAttribute('new_fav_report', $val['name']);
276     } else {
277       $attrs = $bean->getAttributes();
278       $attrs = array_merge($attrs, array(
279         'client'=>'',
280         'option'=>'',
281         'project'=>'',
282         'task'=>'',
283         'include_records'=>'',
284         'invoice'=>'',
285         'users'=>$user_id,
286         'period'=>'',
287         'chclient'=>'1',
288         'chinvoice'=>'',
289         'chproject'=>'1',
290         'chstart'=>'1',
291         'chduration'=>'1',
292         'chcost'=>'',
293         'chtask'=>'1',
294         'chfinish'=>'1',
295         'chnote'=>'1',
296         'chcf_1'=>'',
297         'chunits'=>'',
298         'group_by1'=>'',
299         'chtotalsonly'=>'',
300         'new_fav_report'=>''));
301       $bean->setAttributes($attrs);
302     }
303   }
304
305   // getReportOptions - returns an array of fav report options from database data.
306   // Note: this function is a part of refactoring to simplify maintenance of report
307   // generating functions, as we currently have 2 sets: normal reporting (from bean),
308   // and fav report emailing (from db fields). Using options obtained from either db or bean
309   // shall allow us to use only one set of functions.
310   static function getReportOptions($id) {
311
312     // Start with getting the fields from the database.
313     $db_fields = ttFavReportHelper::getReport($id);
314     if (!$db_fields) return false;
315
316     // Prepare an array of report options.
317     $options = $db_fields; // For now, use db field names as options.
318     // Drop things we don't need in reports.
319     unset($options['id']);
320     unset($options['report_spec']); // Currently not used.
321     unset($options['status']);
322
323     // Note: special handling for NULL users field is done in cron.php
324
325     // $options now is a subset of db fields from tt_fav_reports table.
326     return $options;
327   }
328
329   // adjustOptions takes and array or report options and adjusts them for current user
330   // (and group) settings. This is needed in situations when a fav report is stored in db
331   // long ago, but user or group attributes are now changed, so we have to adjust.
332   static function adjustOptions($options) {
333     global $user;
334
335     // Check and optionally adjust users.
336     // Special handling of the NULL $options['users'] field (this used to mean "all users").
337     if (!$options['users']) {
338       if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
339         if ($user->can('view_reports') || $user->can('view_all_reports')) {
340           $max_rank = $user->rank-1;
341           if ($user->can('view_all_reports')) $max_rank = 512;
342           if ($user->can('view_own_reports'))
343             $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
344           else
345             $user_options = array('max_rank'=>$max_rank);
346           $users = $user->getUsers($user_options); // Active and inactive users.
347         } elseif ($user->isClient()) {
348           $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
349         }
350         foreach ($users as $single_user) {
351           $user_ids[] = $single_user['id'];
352         }
353         $options['users'] = implode(',', $user_ids);
354       }
355     } else {
356       $users_to_adjust = explode(',', $options['users']); // Users to adjust.
357       if ($user->isClient()) {
358         $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
359         foreach ($users as $single_user) {
360           $user_ids[] = $single_user['id'];
361         }
362         foreach ($users_to_adjust as $user_to_adjust) {
363           if (in_array($user_to_adjust['id'], $user_ids)) {
364             $adjusted_user_ids[] = $user_to_adjust['id'];
365           }
366         }
367         $options['users'] = implode(',', $adjusted_user_ids);
368       }
369       // TODO: add checking the existing user list for potentially changed access rights for user.
370     }
371
372     return $options;
373   }
374 }