Started to use the status field in tt_fav_reports.
[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, users, period, period_start, period_end,
84       show_client, show_invoice,
85       show_project, show_start, show_duration, show_cost,
86       show_task, show_end, show_note, show_custom_field_1,
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['users']).", ".$mdb2->quote($fields['period']).", ".
94       $mdb2->quote($fields['from']).", ".$mdb2->quote($fields['to']).", ".
95       $fields['chclient'].", ".$fields['chinvoice'].", ".
96       $fields['chproject'].", ".$fields['chstart'].", ".$fields['chduration'].", ".$fields['chcost'].", ".
97       $fields['chtask'].", ".$fields['chfinish'].", ".$fields['chnote'].", ".$fields['chcf_1'].", ".
98       $mdb2->quote($fields['group_by']).", ".$fields['chtotalsonly'].")";
99     $affected = $mdb2->exec($sql);
100     if (is_a($affected, 'PEAR_Error'))
101       return false;
102
103     $sql = "select last_insert_id() as last_id";
104     $res = $mdb2->query($sql);
105     if (is_a($res, 'PEAR_Error'))
106       return false;
107
108     $val = $res->fetchRow();
109     return $val['last_id'];
110   }
111
112   // updateReport - updates report options in the database.
113   function updateReport($fields) {
114     $mdb2 = getConnection();
115     $sql = "update tt_fav_reports set ".
116       "name = ".$mdb2->quote($fields['name']).", ".
117       "client_id = ".$mdb2->quote($fields['client']).", ".
118       "cf_1_option_id = ".$mdb2->quote($fields['option']).", ".
119       "project_id = ".$mdb2->quote($fields['project']).", ".
120       "task_id = ".$mdb2->quote($fields['task']).", ".
121       "billable = ".$mdb2->quote($fields['billable']).", ".
122       "invoice = ".$mdb2->quote($fields['invoice']).", ".
123       "users = ".$mdb2->quote($fields['users']).", ".
124       "period = ".$mdb2->quote($fields['period']).", ".
125       "period_start = ".$mdb2->quote($fields['from']).", ".
126       "period_end = ".$mdb2->quote($fields['to']).", ".
127       "show_client = ".$fields['chclient'].", ".
128       "show_invoice = ".$fields['chinvoice'].", ".
129       "show_project = ".$fields['chproject'].", ".
130       "show_start = ".$fields['chstart'].", ".
131       "show_duration = ".$fields['chduration'].", ".
132       "show_cost = ".$fields['chcost'].", ".
133       "show_task = ".$fields['chtask'].", ".
134       "show_end = ".$fields['chfinish'].", ".
135       "show_note = ".$fields['chnote'].", ".
136       "show_custom_field_1 = ".$fields['chcf_1'].", ".
137       "group_by = ".$mdb2->quote($fields['group_by']).", ".
138       "show_totals_only = ".$fields['chtotalsonly'].
139       " where id = ".$fields['id'];
140     $affected = $mdb2->exec($sql);
141     if (is_a($affected, 'PEAR_Error'))
142       return false;
143
144     return $fields['id'];
145   }
146
147   // saveReport - saves report options in the database.
148   static function saveReport($user_id, $bean) {
149     global $user;
150
151     //  Set default value of 0 for not set checkboxes (in bean).
152     //  Later in this function we use it to construct $fields array to update database.
153     if (!$bean->getAttribute('chclient')) $bean->setAttribute('chclient', 0);
154     if (!$bean->getAttribute('chinvoice')) $bean->setAttribute('chinvoice', 0);
155     if (!$bean->getAttribute('chproject')) $bean->setAttribute('chproject', 0);
156     if (!$bean->getAttribute('chstart')) $bean->setAttribute('chstart', 0);
157     if (!$bean->getAttribute('chduration')) $bean->setAttribute('chduration', 0);
158     if (!$bean->getAttribute('chcost')) $bean->setAttribute('chcost', 0);
159     if (!$bean->getAttribute('chtask')) $bean->setAttribute('chtask', 0);
160     if (!$bean->getAttribute('chfinish')) $bean->setAttribute('chfinish', 0);
161     if (!$bean->getAttribute('chnote')) $bean->setAttribute('chnote', 0);
162     if (!$bean->getAttribute('chcf_1')) $bean->setAttribute('chcf_1', 0);
163     if (!$bean->getAttribute('chtotalsonly')) $bean->setAttribute('chtotalsonly', 0);
164
165     if ($bean->getAttribute('users') && is_array($bean->getAttribute('users'))) {
166       $users_in_bean = $bean->getAttribute('users');
167
168       // If all users are selected - use a null value (which means "all users").
169       $all_users_selected = true;
170       if ($user->canManageTeam()) {
171         $all = ttTeamHelper::getActiveUsers();
172         foreach ($all as $one) {
173           if (!in_array($one['id'], $users_in_bean)) {
174             $all_users_selected = false;
175             break;
176           }
177         }
178       }
179       if ($all_users_selected)
180         $users = null;
181       else
182         $users = join(',', $users_in_bean);
183     }
184     if ($bean->getAttribute('start_date')) {
185       $dt = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
186       $from = $dt->toString(DB_DATEFORMAT);
187     }
188     if ($bean->getAttribute('end_date')) {
189       $dt = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
190       $to = $dt->toString(DB_DATEFORMAT);
191     }
192
193     $fields = array(
194       'name'=>$bean->getAttribute('new_fav_report'),
195       'client'=>$bean->getAttribute('client'),
196       'option'=>$bean->getAttribute('option'),
197       'project'=>$bean->getAttribute('project'),
198       'task'=>$bean->getAttribute('task'),
199       'billable'=>$bean->getAttribute('include_records'),
200       'invoice'=>$bean->getAttribute('invoice'),
201       'users'=>$users,
202       'period'=>$bean->getAttribute('period'),
203       'from'=>$from,
204       'to'=>$to,
205       'chclient'=>$bean->getAttribute('chclient'),
206       'chinvoice'=>$bean->getAttribute('chinvoice'),
207       'chproject'=>$bean->getAttribute('chproject'),
208       'chstart'=>$bean->getAttribute('chstart'),
209       'chduration'=>$bean->getAttribute('chduration'),
210       'chcost'=>$bean->getAttribute('chcost'),
211       'chtask'=>$bean->getAttribute('chtask'),
212       'chfinish'=>$bean->getAttribute('chfinish'),
213       'chnote'=>$bean->getAttribute('chnote'),
214       'chcf_1'=>$bean->getAttribute('chcf_1'),
215       'group_by'=>$bean->getAttribute('group_by'),
216       'chtotalsonly'=>$bean->getAttribute('chtotalsonly'));
217
218     $id = false;
219     $report = ttFavReportHelper::getReportByName($user_id, $fields['name']);
220     if ($report) {
221       $fields['id'] = $report['id'];
222       $id = ttFavReportHelper::updateReport($fields);
223     } else {
224       $fields['user_id'] = $user_id;
225       $id = ttFavReportHelper::insertReport($fields);
226     }
227
228     return $id;
229   }
230
231   // deleteReport - deletes a favorite report.
232   static function deleteReport($id) {
233     $mdb2 = getConnection();
234
235     $sql = "delete from tt_fav_reports where id = $id";
236     $affected = $mdb2->exec($sql);
237     return (!is_a($affected, 'PEAR_Error'));
238   }
239
240   // loadReport - loads report options from database into a bean.
241   static function loadReport($user_id, &$bean) {
242     global $user;
243
244     $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
245     if ($val) {
246       $bean->setAttribute('client', $val['client_id']);
247       $bean->setAttribute('option', $val['cf_1_option_id']);
248       $bean->setAttribute('project', $val['project_id']);
249       $bean->setAttribute('task', $val['task_id']);
250       $bean->setAttribute('include_records', $val['billable']);
251       $bean->setAttribute('invoice', $val['invoice']);
252       if ($val['users'])
253         $bean->setAttribute('users', explode(',', $val['users']));
254       else {
255         // Null users value means "all users". Add them to the bean.
256         if ($user->canManageTeam()) {
257           $all = ttTeamHelper::getActiveUsers();
258           foreach ($all as $one) {
259             $all_user_ids[] = $one['id'];
260           }
261           $bean->setAttribute('users', $all_user_ids);
262         }
263       }
264       $bean->setAttribute('period', $val['period']);
265       if ($val['period_start']) {
266         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
267         $bean->setAttribute('start_date', $dt->toString($user->date_format));
268       }
269       if ($val['period_end']) {
270         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
271         $bean->setAttribute('end_date', $dt->toString($user->date_format));
272       }
273       $bean->setAttribute('chclient', $val['show_client']);
274       $bean->setAttribute('chinvoice', $val['show_invoice']);
275       $bean->setAttribute('chproject', $val['show_project']);
276       $bean->setAttribute('chstart', $val['show_start']);
277       $bean->setAttribute('chduration', $val['show_duration']);
278       $bean->setAttribute('chcost', $val['show_cost']);
279       $bean->setAttribute('chtask', $val['show_task']);
280       $bean->setAttribute('chfinish', $val['show_end']);
281       $bean->setAttribute('chnote', $val['show_note']);
282       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
283       $bean->setAttribute('group_by', $val['group_by']);
284       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);
285       $bean->setAttribute('new_fav_report', $val['name']);
286     } else {
287       $attrs = $bean->getAttributes();
288       $attrs = array_merge($attrs, array(
289         'client'=>'',
290         'option'=>'',
291         'project'=>'',
292         'task'=>'',
293         'include_records'=>'',
294         'invoice'=>'',
295         'users'=>$user_id,
296         'period'=>'',
297         'chclient'=>'1',
298         'chinvoice'=>'',
299         'chproject'=>'1',
300         'chstart'=>'1',
301         'chduration'=>'1',
302         'chcost'=>'',
303         'chtask'=>'1',
304         'chfinish'=>'1',
305         'chnote'=>'1',
306         'chcf_1'=>'',
307         'group_by'=>'',
308         'chtotalsonly'=>'',
309         'new_fav_report'=>''));
310       $bean->setAttributes($attrs);
311     }
312   }
313 }