Fixed saving fav report in a subgroup.
[timetracker.git] / WEB-INF / lib / ttTeamHelper.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('ttUserHelper');
30 import('DateAndTime');
31 import('ttInvoiceHelper');
32
33 // Class ttTeamHelper - contains helper functions that operate with groups.
34 class ttTeamHelper {
35
36   // The getUsersForClient obtains all active and inactive users in a group that are relevant to a client.
37   static function getUsersForClient() {
38     global $user;
39     $mdb2 = getConnection();
40
41     $sql = "select u.id, u.name from tt_user_project_binds upb".
42       " inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)".
43       " inner join tt_users u on (u.id = upb.user_id)".
44       " where (u.status = 1 or u.status = 0)".
45       " group by u.id".
46       " order by upper(u.name)";
47     $res = $mdb2->query($sql);
48     $user_list = array();
49     if (is_a($res, 'PEAR_Error'))
50       return false;
51     while ($val = $res->fetchRow()) {
52       $user_list[] = $val;
53     }
54     return $user_list;
55   }
56
57   // The swapRolesWith swaps existing user role with that of another user.
58   static function swapRolesWith($user_id) {
59     global $user;
60     $mdb2 = getConnection();
61
62     // Obtain role id for the user we are swapping ourselves with.
63     $sql = "select u.id, u.role_id from tt_users u left join tt_roles r on (u.role_id = r.id) where u.id = $user_id and u.group_id = $user->group_id and u.status = 1 and r.rank < $user->rank";
64     $res = $mdb2->query($sql);
65     if (is_a($res, 'PEAR_Error'))
66       return false;
67     $val = $res->fetchRow();
68     if (!$val['id'] || !$val['role_id'])
69       return false;
70
71     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
72
73     // Promote user.
74     $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
75     $affected = $mdb2->exec($sql);
76     if (is_a($affected, 'PEAR_Error')) return false;
77
78     // Demote self.
79     $role_id = $val['role_id'];
80     $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
81     $affected = $mdb2->exec($sql);
82     if (is_a($affected, 'PEAR_Error')) return false;
83
84     return true;
85   }
86
87   // The getUsersForSwap obtains all users a current user can swap roles with.
88   static function getUsersForSwap() {
89     global $user;
90     $mdb2 = getConnection();
91
92     $sql = "select u.id, u.name, r.rank, r.rights from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $user->group_id and u.status = 1 and r.rank < $user->rank order by upper(u.name)";
93     $res = $mdb2->query($sql);
94     $user_list = array();
95     if (is_a($res, 'PEAR_Error'))
96       return false;
97     while ($val = $res->fetchRow()) {
98       $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
99       if ($isClient)
100         continue; // Skip adding clients.
101       $user_list[] = $val;
102     }
103
104     return $user_list;
105   }
106
107   // The getUsers obtains all active and inactive (but not deleted) users in a group.
108   static function getUsers() {
109     global $user;
110     $mdb2 = getConnection();
111     $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
112     $res = $mdb2->query($sql);
113     $user_list = array();
114     if (is_a($res, 'PEAR_Error'))
115       return false;
116     while ($val = $res->fetchRow()) {
117       $user_list[] = $val;
118     }
119     return $user_list;
120   }
121
122   // The getInactiveUsers obtains all inactive users in a group.
123   static function getInactiveUsers($group_id, $all_fields = false) {
124     $mdb2 = getConnection();
125
126     if ($all_fields)
127       $sql = "select u.*, r.name as role_name from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $group_id and u.status = 0 order by upper(u.name)";
128     else
129       $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
130     $res = $mdb2->query($sql);
131     $result = array();
132     if (!is_a($res, 'PEAR_Error')) {
133       while ($val = $res->fetchRow()) {
134         $result[] = $val;
135       }
136       return $result;
137     }
138     return false;
139   }
140
141   // The getAllProjects obtains all projects in a group.
142   static function getAllProjects($group_id, $all_fields = false) {
143     $mdb2 = getConnection();
144
145     if ($all_fields)
146       $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
147     else
148       $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
149     $res = $mdb2->query($sql);
150     $result = array();
151     if (!is_a($res, 'PEAR_Error')) {
152       while ($val = $res->fetchRow()) {
153         $result[] = $val;
154       }
155       return $result;
156     }
157     return false;
158   }
159
160   // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
161   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
162   static function getActiveRolesForUser()
163   {
164     global $user;
165     $result = array();
166     $mdb2 = getConnection();
167
168     $group_id = $user->getGroup();
169     $org_id = $user->org_id;
170
171     // Determine max rank. If we are working in on behalf group
172     // then rank restriction does not apply.
173     $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
174
175     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $max_rank and status = 1 order by rank";
176     $res = $mdb2->query($sql);
177     $result = array();
178     if (!is_a($res, 'PEAR_Error')) {
179       while ($val = $res->fetchRow()) {
180         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
181         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
182           continue; // Skip adding a client role.
183         $result[] = $val;
184       }
185     }
186     return $result;
187   }
188
189   // getActiveRoles - returns an array of active roles for a group.
190   static function getActiveRoles($group_id)
191   {
192     $result = array();
193     $mdb2 = getConnection();
194
195     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
196     $res = $mdb2->query($sql);
197     $result = array();
198     if (!is_a($res, 'PEAR_Error')) {
199       while ($val = $res->fetchRow()) {
200         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
201         $result[] = $val;
202       }
203     }
204     return $result;
205   }
206
207   // getInactiveRoles - returns an array of inactive roles for a group.
208   static function getInactiveRoles($group_id)
209   {
210     $result = array();
211     $mdb2 = getConnection();
212
213     $sql = "select id, name, rank, description from tt_roles
214       where group_id = $group_id and status = 0 order by rank";
215     $res = $mdb2->query($sql);
216     $result = array();
217     if (!is_a($res, 'PEAR_Error')) {
218       while ($val = $res->fetchRow()) {
219         $result[] = $val;
220       }
221     }
222     return $result;
223   }
224
225   // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
226   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
227   static function getInactiveRolesForUser()
228   {
229     global $user;
230     $result = array();
231     $mdb2 = getConnection();
232
233     $group_id = $user->getGroup();
234     $org_id = $user->org_id;
235
236     // Determine max rank. If we are working in on behalf group
237     // then rank restriction does not apply.
238     $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
239
240     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $max_rank and status = 0 order by rank";
241     $res = $mdb2->query($sql);
242     $result = array();
243     if (!is_a($res, 'PEAR_Error')) {
244       while ($val = $res->fetchRow()) {
245         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
246         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
247           continue; // Skip adding a client role.
248         $result[] = $val;
249       }
250     }
251     return $result;
252   }
253
254   // The getAllClients obtains all clients in a group.
255   static function getAllClients($group_id, $all_fields = false) {
256     $mdb2 = getConnection();
257
258     if ($all_fields)
259       $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
260     else
261       $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
262
263     $res = $mdb2->query($sql);
264     $result = array();
265     if (!is_a($res, 'PEAR_Error')) {
266       while ($val = $res->fetchRow()) {
267         $result[] = $val;
268       }
269       return $result;
270     }
271     return false;
272   }
273
274   // The getAllInvoices returns an array of all invoices for a group.
275   static function getAllInvoices()
276   {
277     global $user;
278
279     $result = array();
280     $mdb2 = getConnection();
281
282     $sql = "select * from tt_invoices where group_id = $user->group_id";
283     $res = $mdb2->query($sql);
284     $result = array();
285     if (!is_a($res, 'PEAR_Error')) {
286       $dt = new DateAndTime(DB_DATEFORMAT);
287       while ($val = $res->fetchRow()) {
288         $result[] = $val;
289       }
290     }
291     return $result;
292   }
293
294   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
295   static function getRecentInvoices($group_id, $client_id)
296   {
297     global $user;
298
299     $result = array();
300     $mdb2 = getConnection();
301
302     $sql = "select i.id, i.name from tt_invoices i
303       left join tt_clients c on (c.id = i.client_id)
304       where i.group_id = $group_id and i.status = 1 and c.id = $client_id
305       order by i.id desc limit 3";
306     $res = $mdb2->query($sql);
307     $result = array();
308     if (!is_a($res, 'PEAR_Error')) {
309       $dt = new DateAndTime(DB_DATEFORMAT);
310       while ($val = $res->fetchRow()) {
311         $result[] = $val;
312       }
313     }
314     return $result;
315   }
316
317   // getUserToProjectBinds - obtains all user to project binds for a group.
318   static function getUserToProjectBinds($group_id) {
319     $mdb2 = getConnection();
320
321     $result = array();
322     $sql = "select * from tt_user_project_binds where user_id in (select id from tt_users where group_id = $group_id) order by user_id, status, project_id";
323     $res = $mdb2->query($sql);
324     $result = array();
325     if (!is_a($res, 'PEAR_Error')) {
326       while ($val = $res->fetchRow()) {
327         $result[] = $val;
328       }
329       return $result;
330     }
331     return false;
332   }
333
334   // The getAllCustomFields obtains all custom fields in a group.
335   static function getAllCustomFields($group_id) {
336     $mdb2 = getConnection();
337
338     $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
339
340     $res = $mdb2->query($sql);
341     $result = array();
342     if (!is_a($res, 'PEAR_Error')) {
343       while ($val = $res->fetchRow()) {
344         $result[] = $val;
345       }
346       return $result;
347     }
348     return false;
349   }
350
351   // The getAllCustomFieldOptions obtains all custom field options in a group.
352   static function getAllCustomFieldOptions($group_id) {
353     $mdb2 = getConnection();
354
355     $sql = "select * from tt_custom_field_options where field_id in (select id from tt_custom_fields where group_id = $group_id) order by id";
356
357     $res = $mdb2->query($sql);
358     $result = array();
359     if (!is_a($res, 'PEAR_Error')) {
360       while ($val = $res->fetchRow()) {
361         $result[] = $val;
362       }
363       return $result;
364     }
365     return false;
366   }
367
368   // The getCustomFieldLog obtains all custom field log entries for a group.
369   static function getCustomFieldLog($group_id) {
370     $mdb2 = getConnection();
371
372     $sql = "select * from tt_custom_field_log where field_id in (select id from tt_custom_fields where group_id = $group_id) order by id";
373
374     $res = $mdb2->query($sql);
375     $result = array();
376     if (!is_a($res, 'PEAR_Error')) {
377       while ($val = $res->fetchRow()) {
378         $result[] = $val;
379       }
380       return $result;
381     }
382     return false;
383   }
384
385   // getFavReports - obtains all favorite reports for all users in a group.
386   static function getFavReports($group_id) {
387     $mdb2 = getConnection();
388
389     $result = array();
390     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
391     $res = $mdb2->query($sql);
392     $result = array();
393     if (!is_a($res, 'PEAR_Error')) {
394       while ($val = $res->fetchRow()) {
395         $result[] = $val;
396       }
397       return $result;
398     }
399     return false;
400   }
401
402   // getExpenseItems - obtains all expense items for all users in a group.
403   static function getExpenseItems($group_id) {
404     $mdb2 = getConnection();
405
406     $result = array();
407     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
408     $res = $mdb2->query($sql);
409     $result = array();
410     if (!is_a($res, 'PEAR_Error')) {
411       while ($val = $res->fetchRow()) {
412         $result[] = $val;
413       }
414       return $result;
415     }
416     return false;
417   }
418
419   // getMonthlyQuotas - obtains monthly quotas for a group.
420   static function getMonthlyQuotas($group_id) {
421     $mdb2 = getConnection();
422
423     $result = array();
424     $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
425     $res = $mdb2->query($sql);
426     $result = array();
427     if (!is_a($res, 'PEAR_Error')) {
428       while ($val = $res->fetchRow()) {
429         $result[] = $val;
430       }
431       return $result;
432     }
433     return false;
434   }
435
436   // The delete function permanently deletes all data for a group.
437   static function delete($group_id) {
438     $mdb2 = getConnection();
439
440     // Delete users.
441     $sql = "select id from tt_users where group_id = $group_id";
442     $res = $mdb2->query($sql);
443     if (is_a($res, 'PEAR_Error')) return false;
444     while ($val = $res->fetchRow()) {
445       $user_id = $val['id'];
446       if (!ttUserHelper::delete($user_id)) return false;
447     }
448
449     // Delete tasks.
450     if (!ttTeamHelper::deleteTasks($group_id)) return false;
451
452     // Delete client to project binds.
453     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
454     $affected = $mdb2->exec($sql);
455     if (is_a($affected, 'PEAR_Error')) return false;
456
457     // Delete projects.
458     $sql = "delete from tt_projects where group_id = $group_id";
459     $affected = $mdb2->exec($sql);
460     if (is_a($affected, 'PEAR_Error')) return false;
461
462     // Delete clients.
463     $sql = "delete from tt_clients where group_id = $group_id";
464     $affected = $mdb2->exec($sql);
465     if (is_a($affected, 'PEAR_Error')) return false;
466
467     // Delete invoices.
468     $sql = "delete from tt_invoices where group_id = $group_id";
469     $affected = $mdb2->exec($sql);
470     if (is_a($affected, 'PEAR_Error')) return false;
471
472     // Delete custom fields.
473     if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
474
475     // Delete roles.
476     $sql = "delete from tt_roles where group_id = $group_id";
477     $affected = $mdb2->exec($sql);
478     if (is_a($affected, 'PEAR_Error')) return false;
479
480     // Delete cron entries.
481     $sql = "delete from tt_cron where group_id = $group_id";
482     $affected = $mdb2->exec($sql);
483     if (is_a($affected, 'PEAR_Error')) return false;
484
485     // Delete predefined expenses.
486     $sql = "delete from tt_predefined_expenses where group_id = $group_id";
487     $affected = $mdb2->exec($sql);
488     if (is_a($affected, 'PEAR_Error')) return false;
489
490     // Delete monthly quotas.
491     $sql = "delete from tt_monthly_quotas where group_id = $group_id";
492     $affected = $mdb2->exec($sql);
493     if (is_a($affected, 'PEAR_Error')) return false;
494
495     // Delete group.
496     $sql = "delete from tt_groups where id = $group_id";
497     $affected = $mdb2->exec($sql);
498     if (is_a($affected, 'PEAR_Error')) return false;
499
500     return true;
501   }
502
503   // The deleteTasks deletes all tasks and task binds for an inactive group.
504   static function deleteTasks($group_id) {
505     $mdb2 = getConnection();
506     $sql = "select id from tt_tasks where group_id = $group_id";
507     $res = $mdb2->query($sql);
508     if (is_a($res, 'PEAR_Error')) return false;
509
510     while ($val = $res->fetchRow()) {
511
512       // Delete task binds.
513       $task_id = $val['id'];
514       $sql = "delete from tt_project_task_binds where task_id = $task_id";
515       $affected = $mdb2->exec($sql);
516       if (is_a($affected, 'PEAR_Error')) return false;
517
518       // Delete task.
519       $sql = "delete from tt_tasks where id = $task_id";
520       $affected = $mdb2->exec($sql);
521       if (is_a($affected, 'PEAR_Error')) return false;
522     }
523
524     return true;
525   }
526
527   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
528   static function deleteCustomFields($group_id) {
529     $mdb2 = getConnection();
530     $sql = "select id from tt_custom_fields where group_id = $group_id";
531     $res = $mdb2->query($sql);
532     if (is_a($res, 'PEAR_Error')) return false;
533
534     while ($val = $res->fetchRow()) {
535       $field_id = $val['id'];
536
537       // Clean up tt_custom_field_log.
538       $sql = "delete from tt_custom_field_log where field_id = $field_id";
539       $affected = $mdb2->exec($sql);
540       if (is_a($affected, 'PEAR_Error')) return false;
541
542       // Clean up tt_custom_field_options.
543       $sql = "delete from tt_custom_field_options where field_id = $field_id";
544       $affected = $mdb2->exec($sql);
545       if (is_a($affected, 'PEAR_Error')) return false;
546
547       // Delete custom field.
548       $sql = "delete from tt_custom_fields where id = $field_id";
549       $affected = $mdb2->exec($sql);
550       if (is_a($affected, 'PEAR_Error')) return false;
551     }
552
553     return true;
554   }
555 }