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