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