Added handling of show work units in fav reports and export / import.
[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_by, 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_by']).", ".$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_by = ".$mdb2->quote($fields['group_by']).", ".
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     if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) {
174       $users_in_bean = $bean->getAttribute('users');
175
176       // If all users are selected - use a null value (which means "all users").
177       $all_users_selected = true;
178       if ($user->can('view_reports')) {
179         $all = ttTeamHelper::getActiveUsers();
180         foreach ($all as $one) {
181           if (!in_array($one['id'], $users_in_bean)) {
182             $all_users_selected = false;
183             break;
184           }
185         }
186       }
187       if ($all_users_selected)
188         $users = null;
189       else
190         $users = join(',', $users_in_bean);
191     }
192     if ($bean->getAttribute('start_date')) {
193       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
194       $from = $dt->toString(DB_DATEFORMAT);
195     }
196     if ($bean->getAttribute('end_date')) {
197       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
198       $to = $dt->toString(DB_DATEFORMAT);
199     }
200
201     $fields = array(
202       'name'=>$bean->getAttribute('new_fav_report'),
203       'client'=>$bean->getAttribute('client'),
204       'option'=>$bean->getAttribute('option'),
205       'project'=>$bean->getAttribute('project'),
206       'task'=>$bean->getAttribute('task'),
207       'billable'=>$bean->getAttribute('include_records'),
208       'invoice'=>$bean->getAttribute('invoice'),
209       'paid_status'=>$bean->getAttribute('paid_status'),
210       'users'=>$users,
211       'period'=>$bean->getAttribute('period'),
212       'from'=>$from,
213       'to'=>$to,
214       'chclient'=>$bean->getAttribute('chclient'),
215       'chinvoice'=>$bean->getAttribute('chinvoice'),
216       'chpaid'=>$bean->getAttribute('chpaid'),
217       'chip'=>$bean->getAttribute('chip'),
218       'chproject'=>$bean->getAttribute('chproject'),
219       'chstart'=>$bean->getAttribute('chstart'),
220       'chduration'=>$bean->getAttribute('chduration'),
221       'chcost'=>$bean->getAttribute('chcost'),
222       'chtask'=>$bean->getAttribute('chtask'),
223       'chfinish'=>$bean->getAttribute('chfinish'),
224       'chnote'=>$bean->getAttribute('chnote'),
225       'chcf_1'=>$bean->getAttribute('chcf_1'),
226       'chunits'=>$bean->getAttribute('chunits'),
227       'group_by'=>$bean->getAttribute('group_by'),
228       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
229
230     $id = false;
231     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
232     if ($report) {
233       $fields['id'] = $report['id'];
234       $id = ttFavReportHelper::updateReport($fields);
235     } else {
236       $fields['user_id'] = $user_id;
237       $id = ttFavReportHelper::insertReport($fields);
238     }
239
240     return $id;
241   }
242
243   // deleteReport - deletes a favorite report.
244   static function deleteReport($id) {
245     $mdb2 = getConnection();
246
247     $sql = "delete from tt_fav_reports where id = $id";
248     $affected = $mdb2->exec($sql);
249     return (!is_a($affected, 'PEAR_Error'));
250   }
251
252   // loadReport - loads report options from database into a bean.
253   static function loadReport($user_id, &$bean) {
254     global $user;
255
256     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
257     if ($val) {
258       $bean->setAttribute('client', $val['client_id']);
259       $bean->setAttribute('option', $val['cf_1_option_id']);
260       $bean->setAttribute('project', $val['project_id']);
261       $bean->setAttribute('task', $val['task_id']);
262       $bean->setAttribute('include_records', $val['billable']);
263       $bean->setAttribute('invoice', $val['invoice']);
264       $bean->setAttribute('paid_status', $val['paid_status']);
265       if ($val['users'])
266         $bean->setAttribute('users', explode(',', $val['users']));
267       else {
268         // Null users value means "all users". Add them to the bean.
269         if ($user->can('view_reports')) {
270           $all = ttTeamHelper::getActiveUsers();
271           foreach ($all as $one) {
272             $all_user_ids[] = $one['id'];
273           }
274           $bean->setAttribute('users', $all_user_ids);
275         }
276       }
277       $bean->setAttribute('period', $val['period']);
278       if ($val['period_start']) {
279         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
280         $bean->setAttribute('start_date', $dt->toString($user->date_format));
281       }
282       if ($val['period_end']) {
283         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
284         $bean->setAttribute('end_date', $dt->toString($user->date_format));
285       }
286       $bean->setAttribute('chclient', $val['show_client']);
287       $bean->setAttribute('chinvoice', $val['show_invoice']);
288       $bean->setAttribute('chpaid', $val['show_paid']);
289       $bean->setAttribute('chip', $val['show_ip']);
290       $bean->setAttribute('chproject', $val['show_project']);
291       $bean->setAttribute('chstart', $val['show_start']);
292       $bean->setAttribute('chduration', $val['show_duration']);
293       $bean->setAttribute('chcost', $val['show_cost']);
294       $bean->setAttribute('chtask', $val['show_task']);
295       $bean->setAttribute('chfinish', $val['show_end']);
296       $bean->setAttribute('chnote', $val['show_note']);
297       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
298       $bean->setAttribute('chunits', $val['show_work_units']);
299       $bean->setAttribute('group_by', $val['group_by']);
300       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
301       $bean->setAttribute('new_fav_report', $val['name']);
302     } else {
303       $attrs = $bean->getAttributes();
304       $attrs = array_merge($attrs, array(
305         'client'=>'',
306         'option'=>'',
307         'project'=>'',
308         'task'=>'',
309         'include_records'=>'',
310         'invoice'=>'',
311         'users'=>$user_id,
312         'period'=>'',
313         'chclient'=>'1',
314         'chinvoice'=>'',
315         'chproject'=>'1',
316         'chstart'=>'1',
317         'chduration'=>'1',
318         'chcost'=>'',
319         'chtask'=>'1',
320         'chfinish'=>'1',
321         'chnote'=>'1',
322         'chcf_1'=>'',
323         'chunits'=>'',
324         'group_by'=>'',
325         'chtotalsonly'=>'',
326         'new_fav_report'=>''));
327       $bean->setAttributes($attrs);
328     }
329   }
330 }