Some more refactoring in notification config.
[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() {
36     global $user;
37     $mdb2 = getConnection();
38
39     $user_id = $user->getUser();
40     $group_id = $user->getGroup();
41     $org_id = $user->org_id;
42
43     $result = array();
44     $sql = "select * from tt_fav_reports".
45       " where user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
46     $res = $mdb2->query($sql);
47     if (!is_a($res, 'PEAR_Error')) {
48       while ($val = $res->fetchRow()) {
49         $result[] = $val;
50       }
51       return mu_sort($result, 'name');
52     }
53     return false;
54   }
55
56   // get - returns a report identified by its id for user.
57   static function get($id) {
58     global $user;
59     $mdb2 = getConnection();
60
61     $user_id = $user->getUser();
62     $group_id = $user->getGroup();
63     $org_id = $user->org_id;
64
65     $sql = "select * from tt_fav_reports".
66       " where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1";
67     $res = $mdb2->query($sql);
68     if (!is_a($res, 'PEAR_Error')) {
69       if ($val = $res->fetchRow()) {
70         return $val;
71       }
72     }
73     return false;
74   }
75
76   // getReport - returns a report identified by its id.
77   // TODO: get rid of this function by encapsulating all cron related tasks in its own class.
78   // Because cron works for all orgs and we want this class to always work in context of
79   // a logged on user, for better security.
80   static function getReport($id) {
81     $mdb2 = getConnection();
82
83     $sql = "select * from tt_fav_reports where id = $id and status = 1";
84     $res = $mdb2->query($sql);
85     if (!is_a($res, 'PEAR_Error')) {
86       if ($val = $res->fetchRow()) {
87         return $val;
88       }
89     }
90     return false;
91   }
92
93   // getReportByName - returns a report identified by its name.
94   static function getReportByName($user_id, $report_name) {
95     $mdb2 = getConnection();
96
97     $sql = "select * from tt_fav_reports where user_id = $user_id and status = 1 and name = ".$mdb2->quote($report_name);
98     $res = $mdb2->query($sql);
99     if (!is_a($res, 'PEAR_Error')) {
100       if ($val = $res->fetchRow()) {
101         return $val;
102       }
103     }
104     return false;
105   }
106
107   // insertReport - stores reports settings in database.
108   static function insertReport($fields) {
109     global $user;
110     $mdb2 = getConnection();
111
112     $group_id = $user->getGroup();
113     $org_id = $user->org_id;
114
115     $sql = "insert into tt_fav_reports".
116       " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
117       " billable, invoice, paid_status, users, period, period_start, period_end,".
118       " show_client, show_invoice, show_paid, show_ip,".
119       " show_project, show_start, show_duration, show_cost,".
120       " show_task, show_end, show_note, show_custom_field_1, show_work_units,".
121       " group_by1, group_by2, group_by3, show_totals_only)".
122       " values(".
123       $mdb2->quote($fields['name']).", ".$fields['user_id'].", $group_id, $org_id, ".
124       $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
125       $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
126       $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['invoice']).", ".
127       $mdb2->quote($fields['paid_status']).", ".
128       $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
129       $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
130       $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
131       $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
132       $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
133       $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
134       $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
135     $affected = $mdb2->exec($sql);
136     if (is_a($affected, 'PEAR_Error'))
137       return false;
138
139     $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
140     return $last_id;
141   }
142
143   // updateReport - updates report options in the database.
144   function updateReport($fields) {
145     $mdb2 = getConnection();
146     $sql = "update tt_fav_reports set ".
147       "name = ".$mdb2->quote($fields['name']).", ".
148       "client_id = ".$mdb2->quote($fields['client']).", ".
149       "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
150       "project_id = ".$mdb2->quote($fields['project']).", ".
151       "task_id = ".$mdb2->quote($fields['task']).", ".
152       "billable = ".$mdb2->quote($fields['billable']).", ".
153       "invoice = ".$mdb2->quote($fields['invoice']).", ".
154       "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
155       "users = ".$mdb2->quote($fields['users']).", ".
156       "period = ".$mdb2->quote($fields['period']).", ".
157       "period_start = ".$mdb2->quote($fields['from']).", ".
158       "period_end = ".$mdb2->quote($fields['to']).", ".
159       "show_client = ".$fields['chclient'].", ".
160       "show_invoice = ".$fields['chinvoice'].", ".
161       "show_paid = ".$fields['chpaid'].", ".
162       "show_ip = ".$fields['chip'].", ".
163       "show_project = ".$fields['chproject'].", ".
164       "show_start = ".$fields['chstart'].", ".
165       "show_duration = ".$fields['chduration'].", ".
166       "show_cost = ".$fields['chcost'].", ".
167       "show_task = ".$fields['chtask'].", ".
168       "show_end = ".$fields['chfinish'].", ".
169       "show_note = ".$fields['chnote'].", ".
170       "show_custom_field_1 = ".$fields['chcf_1'].", ".
171       "show_work_units = ".$fields['chunits'].", ".
172       "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
173       "group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
174       "group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
175       "show_totals_only = ".$fields['chtotalsonly'].
176       " where id = ".$fields['id'];
177     $affected = $mdb2->exec($sql);
178     if (is_a($affected, 'PEAR_Error'))
179       return false;
180
181     return $fields['id'];
182   }
183
184   // saveReport - saves report options in the database.
185   static function saveReport($user_id, $bean) {
186     global $user;
187
188     //  Set default value of 0 for not set checkboxes (in bean).
189     //  Later in this function we use it to construct $fields array to update database.
190     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
191     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
192     if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
193     if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
194     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
195     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
196     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
197     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
198     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
199     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
200     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
201     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
202     if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
203     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
204
205     $users_in_bean = $bean->getAttribute('users');
206     if ($users_in_bean && is_array($users_in_bean)) {
207       $users = join(',', $users_in_bean);
208     }
209     if ($bean->getAttribute('start_date')) {
210       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
211       $from = $dt->toString(DB_DATEFORMAT);
212     }
213     if ($bean->getAttribute('end_date')) {
214       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
215       $to = $dt->toString(DB_DATEFORMAT);
216     }
217
218     $fields = array(
219       'name'=>$bean->getAttribute('new_fav_report'),
220       'client'=>$bean->getAttribute('client'),
221       'option'=>$bean->getAttribute('option'),
222       'project'=>$bean->getAttribute('project'),
223       'task'=>$bean->getAttribute('task'),
224       'billable'=>$bean->getAttribute('include_records'),
225       'invoice'=>$bean->getAttribute('invoice'),
226       'paid_status'=>$bean->getAttribute('paid_status'),
227       'users'=>$users,
228       'period'=>$bean->getAttribute('period'),
229       'from'=>$from,
230       'to'=>$to,
231       'chclient'=>$bean->getAttribute('chclient'),
232       'chinvoice'=>$bean->getAttribute('chinvoice'),
233       'chpaid'=>$bean->getAttribute('chpaid'),
234       'chip'=>$bean->getAttribute('chip'),
235       'chproject'=>$bean->getAttribute('chproject'),
236       'chstart'=>$bean->getAttribute('chstart'),
237       'chduration'=>$bean->getAttribute('chduration'),
238       'chcost'=>$bean->getAttribute('chcost'),
239       'chtask'=>$bean->getAttribute('chtask'),
240       'chfinish'=>$bean->getAttribute('chfinish'),
241       'chnote'=>$bean->getAttribute('chnote'),
242       'chcf_1'=>$bean->getAttribute('chcf_1'),
243       'chunits'=>$bean->getAttribute('chunits'),
244       'group_by1'=>$bean->getAttribute('group_by1'),
245       'group_by2'=>$bean->getAttribute('group_by2'),
246       'group_by3'=>$bean->getAttribute('group_by3'),
247       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
248
249     $id = false;
250     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
251     if ($report) {
252       $fields['id'] = $report['id'];
253       $id = ttFavReportHelper::updateReport($fields);
254     } else {
255       $fields['user_id'] = $user_id;
256       $id = ttFavReportHelper::insertReport($fields);
257     }
258
259     return $id;
260   }
261
262   // deleteReport - deletes a favorite report.
263   static function deleteReport($id) {
264     $mdb2 = getConnection();
265
266     $sql = "delete from tt_fav_reports where id = $id";
267     $affected = $mdb2->exec($sql);
268     return (!is_a($affected, 'PEAR_Error'));
269   }
270
271   // loadReport - loads report options from database into a bean.
272   static function loadReport($user_id, &$bean) {
273     global $user;
274
275     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
276     if ($val) {
277       $bean->setAttribute('client', $val['client_id']);
278       $bean->setAttribute('option', $val['cf_1_option_id']);
279       $bean->setAttribute('project', $val['project_id']);
280       $bean->setAttribute('task', $val['task_id']);
281       $bean->setAttribute('include_records', $val['billable']);
282       $bean->setAttribute('invoice', $val['invoice']);
283       $bean->setAttribute('paid_status', $val['paid_status']);
284       $bean->setAttribute('users', explode(',', $val['users']));
285       $bean->setAttribute('period', $val['period']);
286       if ($val['period_start']) {
287         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
288         $bean->setAttribute('start_date', $dt->toString($user->date_format));
289       }
290       if ($val['period_end']) {
291         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
292         $bean->setAttribute('end_date', $dt->toString($user->date_format));
293       }
294       $bean->setAttribute('chclient', $val['show_client']);
295       $bean->setAttribute('chinvoice', $val['show_invoice']);
296       $bean->setAttribute('chpaid', $val['show_paid']);
297       $bean->setAttribute('chip', $val['show_ip']);
298       $bean->setAttribute('chproject', $val['show_project']);
299       $bean->setAttribute('chstart', $val['show_start']);
300       $bean->setAttribute('chduration', $val['show_duration']);
301       $bean->setAttribute('chcost', $val['show_cost']);
302       $bean->setAttribute('chtask', $val['show_task']);
303       $bean->setAttribute('chfinish', $val['show_end']);
304       $bean->setAttribute('chnote', $val['show_note']);
305       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
306       $bean->setAttribute('chunits', $val['show_work_units']);
307       $bean->setAttribute('group_by1', $val['group_by1']);
308       $bean->setAttribute('group_by2', $val['group_by2']);
309       $bean->setAttribute('group_by3', $val['group_by3']);
310       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
311       $bean->setAttribute('new_fav_report', $val['name']);
312     } else {
313       $attrs = $bean->getAttributes();
314       $attrs = array_merge($attrs, array(
315         'client'=>'',
316         'option'=>'',
317         'project'=>'',
318         'task'=>'',
319         'include_records'=>'',
320         'invoice'=>'',
321         'users'=>$user_id,
322         'period'=>'',
323         'chclient'=>'1',
324         'chinvoice'=>'',
325         'chproject'=>'1',
326         'chstart'=>'1',
327         'chduration'=>'1',
328         'chcost'=>'',
329         'chtask'=>'1',
330         'chfinish'=>'1',
331         'chnote'=>'1',
332         'chcf_1'=>'',
333         'chunits'=>'',
334         'group_by1'=>'',
335         'group_by2'=>'',
336         'group_by3'=>'',
337         'chtotalsonly'=>'',
338         'new_fav_report'=>''));
339       $bean->setAttributes($attrs);
340     }
341   }
342
343   // getReportOptions - returns an array of fav report options from database data.
344   // Note: this function is a part of refactoring to simplify maintenance of report
345   // generating functions, as we currently have 2 sets: normal reporting (from bean),
346   // and fav report emailing (from db fields). Using options obtained from either db or bean
347   // shall allow us to use only one set of functions.
348   static function getReportOptions($id) {
349
350     // Start with getting the fields from the database.
351     $db_fields = ttFavReportHelper::getReport($id);
352     if (!$db_fields) return false;
353
354     // Prepare an array of report options.
355     $options = $db_fields; // For now, use db field names as options.
356     // Drop things we don't need in reports.
357     unset($options['id']);
358     unset($options['report_spec']); // Currently not used.
359     unset($options['status']);
360
361     // Note: special handling for NULL users field is done in cron.php
362
363     // $options now is a subset of db fields from tt_fav_reports table.
364     return $options;
365   }
366
367   // adjustOptions takes and array or report options and adjusts them for current user
368   // (and group) settings. This is needed in situations when a fav report is stored in db
369   // long ago, but user or group attributes are now changed, so we have to adjust.
370   static function adjustOptions($options) {
371     global $user;
372
373     // Check and optionally adjust users.
374     // Special handling of the NULL $options['users'] field (this used to mean "all users").
375     if (!$options['users']) {
376       if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
377         if ($user->can('view_reports') || $user->can('view_all_reports')) {
378           $max_rank = $user->rank-1;
379           if ($user->can('view_all_reports')) $max_rank = 512;
380           if ($user->can('view_own_reports'))
381             $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
382           else
383             $user_options = array('max_rank'=>$max_rank);
384           $users = $user->getUsers($user_options); // Active and inactive users.
385         } elseif ($user->isClient()) {
386           $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
387         }
388         foreach ($users as $single_user) {
389           $user_ids[] = $single_user['id'];
390         }
391         $options['users'] = implode(',', $user_ids);
392       }
393     } else {
394       $users_to_adjust = explode(',', $options['users']); // Users to adjust.
395       if ($user->isClient()) {
396         $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
397         foreach ($users as $single_user) {
398           $user_ids[] = $single_user['id'];
399         }
400         foreach ($users_to_adjust as $user_to_adjust) {
401           if (in_array($user_to_adjust['id'], $user_ids)) {
402             $adjusted_user_ids[] = $user_to_adjust['id'];
403           }
404         }
405         $options['users'] = implode(',', $adjusted_user_ids);
406       }
407       // TODO: add checking the existing user list for potentially changed access rights for user.
408     }
409
410     return $options;
411   }
412 }