Initial repo created
[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";
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";
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 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     }
224         else {
225           $fields['user_id'] = $user_id;
226           $id = ttFavReportHelper::insertReport($fields);
227         }
228         
229         return $id;
230   }
231   
232   // deleteReport - deletes a favorite report.
233   static function deleteReport($id) {
234     $mdb2 = getConnection();
235     
236     $sql = "delete from tt_fav_reports where id = $id";
237     $affected = $mdb2->exec($sql);
238     return (!is_a($affected, 'PEAR_Error'));
239   }
240   
241   // loadReport - loads report options from database into a bean.
242   static function loadReport($user_id, &$bean) {
243         global $user;
244         
245         $val = ttFavReportHelper::getReport($bean->getAttribute('favorite_report'));
246         if ($val) {
247       $bean->setAttribute('client', $val['client_id']);
248       $bean->setAttribute('option', $val['cf_1_option_id']);
249       $bean->setAttribute('project', $val['project_id']);
250       $bean->setAttribute('task', $val['task_id']);
251       $bean->setAttribute('include_records', $val['billable']);
252       $bean->setAttribute('invoice', $val['invoice']);
253       if ($val['users'])
254         $bean->setAttribute('users', explode(',', $val['users']));
255       else {
256         // Null users value means "all users". Add them to the bean.
257         if ($user->canManageTeam()) {
258           $all = ttTeamHelper::getActiveUsers();
259           foreach ($all as $one) {
260             $all_user_ids[] = $one['id'];
261           }
262           $bean->setAttribute('users', $all_user_ids);
263         }
264       }
265       $bean->setAttribute('period', $val['period']);
266       if ($val['period_start']) {
267         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_start']);
268         $bean->setAttribute('start_date', $dt->toString($user->date_format));
269       }
270       if ($val['period_end']) {
271         $dt = new DateAndTime(DB_DATEFORMAT, $val['period_end']);
272         $bean->setAttribute('end_date', $dt->toString($user->date_format));
273       }
274       $bean->setAttribute('chclient', $val['show_client']);
275       $bean->setAttribute('chinvoice', $val['show_invoice']);      
276       $bean->setAttribute('chproject', $val['show_project']);
277       $bean->setAttribute('chstart', $val['show_start']);
278       $bean->setAttribute('chduration', $val['show_duration']);      
279       $bean->setAttribute('chcost', $val['show_cost']);      
280       $bean->setAttribute('chtask', $val['show_task']);
281       $bean->setAttribute('chfinish', $val['show_end']);
282       $bean->setAttribute('chnote', $val['show_note']);
283       $bean->setAttribute('chcf_1', $val['show_custom_field_1']);
284       $bean->setAttribute('group_by', $val['group_by']);
285       $bean->setAttribute('chtotalsonly', $val['show_totals_only']);      
286       $bean->setAttribute('new_fav_report', $val['name']);
287         } else {
288           $attrs = $bean->getAttributes();
289           $attrs = array_merge($attrs, array(
290             'client'=>'',
291             'option'=>'',
292             'project'=>'',
293             'task'=>'',
294             'include_records'=>'',
295             'invoice'=>'',
296             'users'=>$user_id,
297             'period'=>'',
298             'chclient'=>'1',
299             'chinvoice'=>'',
300             'chproject'=>'1',
301             'chstart'=>'1',
302             'chduration'=>'1',
303             'chcost'=>'',
304         'chtask'=>'1',
305             'chfinish'=>'1',
306             'chnote'=>'1',
307         'chcf_1'=>'',
308             'group_by'=>'',
309             'chtotalsonly'=>'',
310             'new_fav_report'=>''
311           ));
312           $bean->setAttributes($attrs);
313         }
314   }
315 }
316 ?>