Separated users section on reports in 2 for active and inactive users.
[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('ttGroupHelper');
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($report_name) {
95     global $user;
96     $mdb2 = getConnection();
97
98     $user_id = $user->getUser();
99     $group_id = $user->getGroup();
100     $org_id = $user->org_id;
101
102     $sql = "select id from tt_fav_reports".
103       " where user_id = $user_id and group_id = $group_id and org_id = $org_id and status = 1 and name = ".$mdb2->quote($report_name);
104     $res = $mdb2->query($sql);
105     if (!is_a($res, 'PEAR_Error')) {
106       if ($val = $res->fetchRow()) {
107         return $val;
108       }
109     }
110     return false;
111   }
112
113   // insertReport - stores reports settings in database.
114   static function insertReport($fields) {
115     global $user;
116     $mdb2 = getConnection();
117
118     $user_id = $user->getUser();
119     $group_id = $user->getGroup();
120     $org_id = $user->org_id;
121
122     $sql = "insert into tt_fav_reports".
123       " (name, user_id, group_id, org_id, client_id, cf_1_option_id, project_id, task_id,".
124       " billable, approved, invoice, timesheet, paid_status, users, period, period_start,".
125       " period_end, show_client, show_invoice, show_paid, show_ip,".
126       " show_project, show_timesheet, show_start, show_duration, show_cost,".
127       " show_task, show_end, show_note, show_approved, show_custom_field_1, show_work_units,".
128       " group_by1, group_by2, group_by3, show_totals_only)".
129       " values(".
130       $mdb2->quote($fields['name']).", $user_id, $group_id, $org_id, ".
131       $mdb2->quote($fields['client']).", ".$mdb2->quote($fields['option']).", ".
132       $mdb2->quote($fields['project']).", ".$mdb2->quote($fields['task']).", ".
133       $mdb2->quote($fields['billable']).", ".$mdb2->quote($fields['approved']).", ".
134       $mdb2->quote($fields['invoice']).", ".$mdb2->quote($fields['timesheet']).", ".
135       $mdb2->quote($fields['paid_status']).", ".
136       $mdb2->quote($fields['users']).", ".$mdb2->quote($fields['period']).", ".
137       $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
138       $fields['chclient'].", ".$fields['chinvoice'].", ".$fields['chpaid'].", ".$fields['chip'].", ".
139       $fields['chproject'].", ".$fields['chtimesheet'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
140       $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chapproved'].", ".$fields['chcf_1'].", ".$fields['chunits'].", ".
141       $mdb2->quote($fields['group_by1']).", ".$mdb2->quote($fields['group_by2']).", ".
142       $mdb2->quote($fields['group_by3']).", ".$fields['chtotalsonly'].")";
143     $affected = $mdb2->exec($sql);
144     if (is_a($affected, 'PEAR_Error'))
145       return false;
146
147     $last_id = $mdb2->lastInsertID('tt_fav_reports', 'id');
148     return $last_id;
149   }
150
151   // updateReport - updates report options in the database.
152   static function updateReport($fields) {
153     global $user;
154     $mdb2 = getConnection();
155
156     $user_id = $user->getUser();
157     $group_id = $user->getGroup();
158     $org_id = $user->org_id;
159
160     $sql = "update tt_fav_reports set ".
161       "name = ".$mdb2->quote($fields['name']).", ".
162       "client_id = ".$mdb2->quote($fields['client']).", ".
163       "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
164       "project_id = ".$mdb2->quote($fields['project']).", ".
165       "task_id = ".$mdb2->quote($fields['task']).", ".
166       "billable = ".$mdb2->quote($fields['billable']).", ".
167       "approved = ".$mdb2->quote($fields['approved']).", ".
168       "invoice = ".$mdb2->quote($fields['invoice']).", ".
169       "timesheet = ".$mdb2->quote($fields['timesheet']).", ".
170       "paid_status = ".$mdb2->quote($fields['paid_status']).", ".
171       "users = ".$mdb2->quote($fields['users']).", ".
172       "period = ".$mdb2->quote($fields['period']).", ".
173       "period_start = ".$mdb2->quote($fields['from']).", ".
174       "period_end = ".$mdb2->quote($fields['to']).", ".
175       "show_client = ".$fields['chclient'].", ".
176       "show_invoice = ".$fields['chinvoice'].", ".
177       "show_paid = ".$fields['chpaid'].", ".
178       "show_ip = ".$fields['chip'].", ".
179       "show_project = ".$fields['chproject'].", ".
180       "show_timesheet = ".$fields['chtimesheet'].", ".
181       "show_start = ".$fields['chstart'].", ".
182       "show_duration = ".$fields['chduration'].", ".
183       "show_cost = ".$fields['chcost'].", ".
184       "show_task = ".$fields['chtask'].", ".
185       "show_end = ".$fields['chfinish'].", ".
186       "show_note = ".$fields['chnote'].", ".
187       "show_approved = ".$fields['chapproved'].", ".
188       "show_custom_field_1 = ".$fields['chcf_1'].", ".
189       "show_work_units = ".$fields['chunits'].", ".
190       "group_by1 = ".$mdb2->quote($fields['group_by1']).", ".
191       "group_by2 = ".$mdb2->quote($fields['group_by2']).", ".
192       "group_by3 = ".$mdb2->quote($fields['group_by3']).", ".
193       "show_totals_only = ".$fields['chtotalsonly'].
194       " where id = ".$fields['id']." and user_id = $user_id and group_id = $group_id and org_id = $org_id";
195     $affected = $mdb2->exec($sql);
196     if (is_a($affected, 'PEAR_Error'))
197       return false;
198
199     return $fields['id'];
200   }
201
202   // saveReport - saves report options in the database.
203   static function saveReport($bean) {
204     global $user;
205
206     //  Set default value of 0 for not set checkboxes (in bean).
207     //  Later in this function we use it to construct $fields array to update database.
208     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
209     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
210     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
211     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
212  
213     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
214     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
215     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
216     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
217
218     if (!$bean->getAttribute('chtimesheet')) $bean->setAttribute('chtimesheet', 0);
219     if (!$bean->getAttribute('chip')) $bean->setAttribute('chip', 0);
220     if (!$bean->getAttribute('chapproved')) $bean->setAttribute('chapproved', 0);
221     if (!$bean->getAttribute('chpaid')) $bean->setAttribute('chpaid', 0);
222
223     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
224     if (!$bean->getAttribute('chunits')) $bean->setAttribute('chunits', 0);
225     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
226
227     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
228
229     $active_users_in_bean = $bean->getAttribute('users_active');
230     if ($active_users_in_bean && is_array($active_users_in_bean)) {
231       $users = join(',', $active_users_in_bean);
232     }
233     $inactive_users_in_bean = $bean->getAttribute('users_inactive');
234     if ($inactive_users_in_bean && is_array($inactive_users_in_bean)) {
235       if ($users) $users .= ',';
236       $users .= join(',', $inactive_users_in_bean);
237     }
238
239     if ($bean->getAttribute('start_date')) {
240       $dt = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
241       $from = $dt->toString(DB_DATEFORMAT);
242     }
243     if ($bean->getAttribute('end_date')) {
244       $dt = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
245       $to = $dt->toString(DB_DATEFORMAT);
246     }
247
248     $fields = array(
249       'name'=>$bean->getAttribute('new_fav_report'),
250       'client'=>$bean->getAttribute('client'),
251       'option'=>$bean->getAttribute('option'),
252       'project'=>$bean->getAttribute('project'),
253       'task'=>$bean->getAttribute('task'),
254       'billable'=>$bean->getAttribute('include_records'),
255       'approved'=>$bean->getAttribute('approved'),
256       'paid_status'=>$bean->getAttribute('paid_status'),
257       'invoice'=>$bean->getAttribute('invoice'),
258       'timesheet'=>$bean->getAttribute('timesheet'),
259       'users'=>$users,
260       'period'=>$bean->getAttribute('period'),
261       'from'=>$from,
262       'to'=>$to,
263       'chclient'=>$bean->getAttribute('chclient'),
264       'chstart'=>$bean->getAttribute('chstart'),
265       'chfinish'=>$bean->getAttribute('chfinish'),
266       'chduration'=>$bean->getAttribute('chduration'),
267       'chproject'=>$bean->getAttribute('chproject'),
268       'chtask'=>$bean->getAttribute('chtask'),
269       'chnote'=>$bean->getAttribute('chnote'),
270       'chcost'=>$bean->getAttribute('chcost'),
271       'chtimesheet'=>$bean->getAttribute('chtimesheet'),
272       'chip'=>$bean->getAttribute('chip'),
273       'chapproved'=>$bean->getAttribute('chapproved'),
274       'chpaid'=>$bean->getAttribute('chpaid'),
275       'chcf_1'=>$bean->getAttribute('chcf_1'),
276       'chunits'=>$bean->getAttribute('chunits'),
277       'chinvoice'=>$bean->getAttribute('chinvoice'),
278       'group_by1'=>$bean->getAttribute('group_by1'),
279       'group_by2'=>$bean->getAttribute('group_by2'),
280       'group_by3'=>$bean->getAttribute('group_by3'),
281       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
282
283     $id = false;
284     $report = ttFavReportHelper::getReportByName($fields['name']);
285     if ($report) {
286       $fields['id'] = $report['id'];
287       $id = ttFavReportHelper::updateReport($fields);
288     } else {
289       $id = ttFavReportHelper::insertReport($fields);
290     }
291
292     return $id;
293   }
294
295   // deleteReport - deletes a favorite report.
296   static function deleteReport($id) {
297     global $user;
298     $mdb2 = getConnection();
299
300     $user_id = $user->getUser();
301     $group_id = $user->getGroup();
302     $org_id = $user->org_id;
303
304     $sql = "delete from tt_cron".
305       " where report_id = $id and group_id = $group_id and org_id = $org_id";
306     $affected = $mdb2->exec($sql);
307     if (is_a($affected, 'PEAR_Error'))
308       return false;
309
310     $sql = "delete from tt_fav_reports".
311       " where id = $id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
312     $affected = $mdb2->exec($sql);
313     return (!is_a($affected, 'PEAR_Error'));
314   }
315
316   // loadReport - loads report options from database into a bean.
317   static function loadReport(&$bean) {
318     global $user;
319     $user_id = $user->getUser();
320
321     $val = ttFavReportHelper::get($bean->getAttribute('favorite_report'));
322     if ($val) {
323       $bean->setAttribute('client', $val['client_id']);
324       $bean->setAttribute('option', $val['cf_1_option_id']);
325       $bean->setAttribute('project', $val['project_id']);
326       $bean->setAttribute('task', $val['task_id']);
327       $bean->setAttribute('include_records', $val['billable']);
328       $bean->setAttribute('approved', $val['approved']);
329       $bean->setAttribute('invoice', $val['invoice']);
330       $bean->setAttribute('paid_status', $val['paid_status']);
331       $bean->setAttribute('timesheet', $val['timesheet']);
332       $bean->setAttribute('users_active', explode(',', $val['users']));
333       $bean->setAttribute('users_inactive', explode(',', $val['users']));
334       $bean->setAttribute('period', $val['period']);
335       if ($val['period_start']) {
336         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
337         $bean->setAttribute('start_date', $dt->toString($user->getDateFormat()));
338       }
339       if ($val['period_end']) {
340         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
341         $bean->setAttribute('end_date', $dt->toString($user->getDateFormat()));
342       }
343       $bean->setAttribute('chclient', $val['show_client']);
344       $bean->setAttribute('chinvoice', $val['show_invoice']);
345       $bean->setAttribute('chpaid', $val['show_paid']);
346       $bean->setAttribute('chip', $val['show_ip']);
347       $bean->setAttribute('chproject', $val['show_project']);
348       $bean->setAttribute('chtimesheet', $val['show_timesheet']);
349       $bean->setAttribute('chstart', $val['show_start']);
350       $bean->setAttribute('chduration', $val['show_duration']);
351       $bean->setAttribute('chcost', $val['show_cost']);
352       $bean->setAttribute('chtask', $val['show_task']);
353       $bean->setAttribute('chfinish', $val['show_end']);
354       $bean->setAttribute('chnote', $val['show_note']);
355       $bean->setAttribute('chapproved', $val['show_approved']);
356       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
357       $bean->setAttribute('chunits', $val['show_work_units']);
358       $bean->setAttribute('group_by1', $val['group_by1']);
359       $bean->setAttribute('group_by2', $val['group_by2']);
360       $bean->setAttribute('group_by3', $val['group_by3']);
361       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
362       $bean->setAttribute('new_fav_report', $val['name']);
363     } else {
364       $attrs = $bean->getAttributes();
365       $attrs = array_merge($attrs, array(
366         'client'=>'',
367         'option'=>'',
368         'project'=>'',
369         'task'=>'',
370         'include_records'=>'',
371         'approved'=>'',
372         'paid_status'=>'',
373         'invoice'=>'',
374         'timesheet'=>'',
375         'users'=>$user_id,
376         'period'=>'',
377         'chclient'=>'1',
378         'chstart'=>'1',
379         'chfinish'=>'1',
380         'chduration'=>'1',
381         'chproject'=>'1',
382         'chtask'=>'1',
383         'chnote'=>'1',
384         'chcost'=>'',
385         'chtimesheet'=>'',
386         'chip'=>'',
387         'chapproved'=>'',
388         'chpaid'=>'',
389         'chcf_1'=>'',
390         'chunits'=>'',
391         'chinvoice'=>'',
392         'group_by1'=>'',
393         'group_by2'=>'',
394         'group_by3'=>'',
395         'chtotalsonly'=>'',
396         'new_fav_report'=>''));
397       $bean->setAttributes($attrs);
398     }
399   }
400
401   // getReportOptions - returns an array of fav report options from database data.
402   // Note: this function is a part of refactoring to simplify maintenance of report
403   // generating functions, as we currently have 2 sets: normal reporting (from bean),
404   // and fav report emailing (from db fields). Using options obtained from either db or bean
405   // shall allow us to use only one set of functions.
406   static function getReportOptions($id) {
407
408     // Start with getting the fields from the database.
409     $db_fields = ttFavReportHelper::getReport($id);
410     if (!$db_fields) return false;
411
412     // Prepare an array of report options.
413     $options = $db_fields; // For now, use db field names as options.
414     // Drop things we don't need in reports.
415     unset($options['id']);
416     unset($options['report_spec']); // Currently not used.
417     unset($options['status']);
418
419     // Note: special handling for NULL users field is done in cron.php
420
421     // $options now is a subset of db fields from tt_fav_reports table.
422     return $options;
423   }
424
425   // adjustOptions takes an array or report options and adjusts them for current user
426   // (and group) settings. This is needed in situations when a fav report is stored in db
427   // long ago, but user or group attributes are now changed, so we have to adjust.
428   static function adjustOptions($options) {
429     global $user;
430
431     // Check and optionally adjust users.
432     // Special handling of the NULL $options['users'] field (this used to mean "all users").
433     if (!$options['users']) {
434       if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
435         if ($user->can('view_reports') || $user->can('view_all_reports')) {
436           $max_rank = $user->rank-1;
437           if ($user->can('view_all_reports')) $max_rank = 512;
438           if ($user->can('view_own_reports'))
439             $user_options = array('max_rank'=>$max_rank,'include_self'=>true);
440           else
441             $user_options = array('max_rank'=>$max_rank);
442           $users = $user->getUsers($user_options); // Active and inactive users.
443         } elseif ($user->isClient()) {
444           $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
445         }
446         foreach ($users as $single_user) {
447           $user_ids[] = $single_user['id'];
448         }
449         $options['users'] = implode(',', $user_ids);
450       }
451     } else {
452       $users_to_adjust = explode(',', $options['users']); // Users to adjust.
453       if ($user->isClient()) {
454         $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
455         foreach ($users as $single_user) {
456           $user_ids[] = $single_user['id'];
457         }
458         foreach ($users_to_adjust as $user_to_adjust) {
459           if (in_array($user_to_adjust, $user_ids)) {
460             $adjusted_user_ids[] = $user_to_adjust;
461           }
462         }
463         $options['users'] = implode(',', $adjusted_user_ids);
464       }
465       // TODO: add checking the existing user list for potentially changed access rights for user.
466     }
467
468     if ($user->isPluginEnabled('ap') && $user->isClient() && !$user->can('view_client_unapproved'))
469       $options['approved'] = 1; // Restrict clients to approved records only.
470
471     return $options;
472   }
473 }