2aab936a0895e31c9bef56f281a1e079fd9dfd8b
[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   // getActiveTasks - returns an array of active tasks for a group.
161   static function getActiveTasks($group_id)
162   {
163     $result = array();
164     $mdb2 = getConnection();
165
166     $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
167     $res = $mdb2->query($sql);
168     $result = array();
169     if (!is_a($res, 'PEAR_Error')) {
170       while ($val = $res->fetchRow()) {
171         $result[] = $val;
172       }
173     }
174     return $result;
175   }
176
177   // getInactiveTasks - returns an array of inactive tasks for a group.
178   static function getInactiveTasks($group_id)
179   {
180     $result = array();
181     $mdb2 = getConnection();
182
183     $sql = "select id, name, description from tt_tasks
184       where group_id = $group_id and status = 0 order by upper(name)";
185     $res = $mdb2->query($sql);
186     $result = array();
187     if (!is_a($res, 'PEAR_Error')) {
188       while ($val = $res->fetchRow()) {
189         $result[] = $val;
190       }
191     }
192     return $result;
193   }
194
195   // The getAllTasks obtains all tasks in a group.
196   static function getAllTasks($group_id, $all_fields = false) {
197     $mdb2 = getConnection();
198
199     if ($all_fields)
200       $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
201     else
202       $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
203     $res = $mdb2->query($sql);
204     $result = array();
205     if (!is_a($res, 'PEAR_Error')) {
206       while ($val = $res->fetchRow()) {
207         $result[] = $val;
208       }
209       return $result;
210     }
211     return false;
212   }
213
214   // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
215   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
216   static function getActiveRolesForUser()
217   {
218     global $user;
219     $result = array();
220     $mdb2 = getConnection();
221
222     $group_id = $user->getGroup();
223     $org_id = $user->org_id;
224
225     // Determine max rank. If we are working in on behalf group
226     // then rank restriction does not apply.
227     $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
228
229     $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";
230     $res = $mdb2->query($sql);
231     $result = array();
232     if (!is_a($res, 'PEAR_Error')) {
233       while ($val = $res->fetchRow()) {
234         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
235         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
236           continue; // Skip adding a client role.
237         $result[] = $val;
238       }
239     }
240     return $result;
241   }
242
243   // getActiveRoles - returns an array of active roles for a group.
244   static function getActiveRoles($group_id)
245   {
246     $result = array();
247     $mdb2 = getConnection();
248
249     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
250     $res = $mdb2->query($sql);
251     $result = array();
252     if (!is_a($res, 'PEAR_Error')) {
253       while ($val = $res->fetchRow()) {
254         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
255         $result[] = $val;
256       }
257     }
258     return $result;
259   }
260
261   // getInactiveRoles - returns an array of inactive roles for a group.
262   static function getInactiveRoles($group_id)
263   {
264     $result = array();
265     $mdb2 = getConnection();
266
267     $sql = "select id, name, rank, description from tt_roles
268       where group_id = $group_id and status = 0 order by rank";
269     $res = $mdb2->query($sql);
270     $result = array();
271     if (!is_a($res, 'PEAR_Error')) {
272       while ($val = $res->fetchRow()) {
273         $result[] = $val;
274       }
275     }
276     return $result;
277   }
278
279   // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
280   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
281   static function getInactiveRolesForUser()
282   {
283     global $user;
284     $result = array();
285     $mdb2 = getConnection();
286
287     $group_id = $user->getGroup();
288     $org_id = $user->org_id;
289
290     // Determine max rank. If we are working in on behalf group
291     // then rank restriction does not apply.
292     $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
293
294     $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";
295     $res = $mdb2->query($sql);
296     $result = array();
297     if (!is_a($res, 'PEAR_Error')) {
298       while ($val = $res->fetchRow()) {
299         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
300         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
301           continue; // Skip adding a client role.
302         $result[] = $val;
303       }
304     }
305     return $result;
306   }
307
308   // The getAllClients obtains all clients in a group.
309   static function getAllClients($group_id, $all_fields = false) {
310     $mdb2 = getConnection();
311
312     if ($all_fields)
313       $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
314     else
315       $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
316
317     $res = $mdb2->query($sql);
318     $result = array();
319     if (!is_a($res, 'PEAR_Error')) {
320       while ($val = $res->fetchRow()) {
321         $result[] = $val;
322       }
323       return $result;
324     }
325     return false;
326   }
327
328   // The getAllInvoices returns an array of all invoices for a group.
329   static function getAllInvoices()
330   {
331     global $user;
332
333     $result = array();
334     $mdb2 = getConnection();
335
336     $sql = "select * from tt_invoices where group_id = $user->group_id";
337     $res = $mdb2->query($sql);
338     $result = array();
339     if (!is_a($res, 'PEAR_Error')) {
340       $dt = new DateAndTime(DB_DATEFORMAT);
341       while ($val = $res->fetchRow()) {
342         $result[] = $val;
343       }
344     }
345     return $result;
346   }
347
348   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
349   static function getRecentInvoices($group_id, $client_id)
350   {
351     global $user;
352
353     $result = array();
354     $mdb2 = getConnection();
355
356     $sql = "select i.id, i.name from tt_invoices i
357       left join tt_clients c on (c.id = i.client_id)
358       where i.group_id = $group_id and i.status = 1 and c.id = $client_id
359       order by i.id desc limit 3";
360     $res = $mdb2->query($sql);
361     $result = array();
362     if (!is_a($res, 'PEAR_Error')) {
363       $dt = new DateAndTime(DB_DATEFORMAT);
364       while ($val = $res->fetchRow()) {
365         $result[] = $val;
366       }
367     }
368     return $result;
369   }
370
371   // getUserToProjectBinds - obtains all user to project binds for a group.
372   static function getUserToProjectBinds($group_id) {
373     $mdb2 = getConnection();
374
375     $result = array();
376     $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";
377     $res = $mdb2->query($sql);
378     $result = array();
379     if (!is_a($res, 'PEAR_Error')) {
380       while ($val = $res->fetchRow()) {
381         $result[] = $val;
382       }
383       return $result;
384     }
385     return false;
386   }
387
388   // The getAllCustomFields obtains all custom fields in a group.
389   static function getAllCustomFields($group_id) {
390     $mdb2 = getConnection();
391
392     $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
393
394     $res = $mdb2->query($sql);
395     $result = array();
396     if (!is_a($res, 'PEAR_Error')) {
397       while ($val = $res->fetchRow()) {
398         $result[] = $val;
399       }
400       return $result;
401     }
402     return false;
403   }
404
405   // The getAllCustomFieldOptions obtains all custom field options in a group.
406   static function getAllCustomFieldOptions($group_id) {
407     $mdb2 = getConnection();
408
409     $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";
410
411     $res = $mdb2->query($sql);
412     $result = array();
413     if (!is_a($res, 'PEAR_Error')) {
414       while ($val = $res->fetchRow()) {
415         $result[] = $val;
416       }
417       return $result;
418     }
419     return false;
420   }
421
422   // The getCustomFieldLog obtains all custom field log entries for a group.
423   static function getCustomFieldLog($group_id) {
424     $mdb2 = getConnection();
425
426     $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";
427
428     $res = $mdb2->query($sql);
429     $result = array();
430     if (!is_a($res, 'PEAR_Error')) {
431       while ($val = $res->fetchRow()) {
432         $result[] = $val;
433       }
434       return $result;
435     }
436     return false;
437   }
438
439   // getFavReports - obtains all favorite reports for all users in a group.
440   static function getFavReports($group_id) {
441     $mdb2 = getConnection();
442
443     $result = array();
444     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
445     $res = $mdb2->query($sql);
446     $result = array();
447     if (!is_a($res, 'PEAR_Error')) {
448       while ($val = $res->fetchRow()) {
449         $result[] = $val;
450       }
451       return $result;
452     }
453     return false;
454   }
455
456   // getExpenseItems - obtains all expense items for all users in a group.
457   static function getExpenseItems($group_id) {
458     $mdb2 = getConnection();
459
460     $result = array();
461     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
462     $res = $mdb2->query($sql);
463     $result = array();
464     if (!is_a($res, 'PEAR_Error')) {
465       while ($val = $res->fetchRow()) {
466         $result[] = $val;
467       }
468       return $result;
469     }
470     return false;
471   }
472
473   // getMonthlyQuotas - obtains monthly quotas for a group.
474   static function getMonthlyQuotas($group_id) {
475     $mdb2 = getConnection();
476
477     $result = array();
478     $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
479     $res = $mdb2->query($sql);
480     $result = array();
481     if (!is_a($res, 'PEAR_Error')) {
482       while ($val = $res->fetchRow()) {
483         $result[] = $val;
484       }
485       return $result;
486     }
487     return false;
488   }
489
490   // The delete function permanently deletes all data for a group.
491   static function delete($group_id) {
492     $mdb2 = getConnection();
493
494     // Delete users.
495     $sql = "select id from tt_users where group_id = $group_id";
496     $res = $mdb2->query($sql);
497     if (is_a($res, 'PEAR_Error')) return false;
498     while ($val = $res->fetchRow()) {
499       $user_id = $val['id'];
500       if (!ttUserHelper::delete($user_id)) return false;
501     }
502
503     // Delete tasks.
504     if (!ttTeamHelper::deleteTasks($group_id)) return false;
505
506     // Delete client to project binds.
507     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
508     $affected = $mdb2->exec($sql);
509     if (is_a($affected, 'PEAR_Error')) return false;
510
511     // Delete projects.
512     $sql = "delete from tt_projects where group_id = $group_id";
513     $affected = $mdb2->exec($sql);
514     if (is_a($affected, 'PEAR_Error')) return false;
515
516     // Delete clients.
517     $sql = "delete from tt_clients where group_id = $group_id";
518     $affected = $mdb2->exec($sql);
519     if (is_a($affected, 'PEAR_Error')) return false;
520
521     // Delete invoices.
522     $sql = "delete from tt_invoices where group_id = $group_id";
523     $affected = $mdb2->exec($sql);
524     if (is_a($affected, 'PEAR_Error')) return false;
525
526     // Delete custom fields.
527     if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
528
529     // Delete roles.
530     $sql = "delete from tt_roles where group_id = $group_id";
531     $affected = $mdb2->exec($sql);
532     if (is_a($affected, 'PEAR_Error')) return false;
533
534     // Delete cron entries.
535     $sql = "delete from tt_cron where group_id = $group_id";
536     $affected = $mdb2->exec($sql);
537     if (is_a($affected, 'PEAR_Error')) return false;
538
539     // Delete predefined expenses.
540     $sql = "delete from tt_predefined_expenses where group_id = $group_id";
541     $affected = $mdb2->exec($sql);
542     if (is_a($affected, 'PEAR_Error')) return false;
543
544     // Delete monthly quotas.
545     $sql = "delete from tt_monthly_quotas where group_id = $group_id";
546     $affected = $mdb2->exec($sql);
547     if (is_a($affected, 'PEAR_Error')) return false;
548
549     // Delete group.
550     $sql = "delete from tt_groups where id = $group_id";
551     $affected = $mdb2->exec($sql);
552     if (is_a($affected, 'PEAR_Error')) return false;
553
554     return true;
555   }
556
557   // The deleteTasks deletes all tasks and task binds for an inactive group.
558   static function deleteTasks($group_id) {
559     $mdb2 = getConnection();
560     $sql = "select id from tt_tasks where group_id = $group_id";
561     $res = $mdb2->query($sql);
562     if (is_a($res, 'PEAR_Error')) return false;
563
564     while ($val = $res->fetchRow()) {
565
566       // Delete task binds.
567       $task_id = $val['id'];
568       $sql = "delete from tt_project_task_binds where task_id = $task_id";
569       $affected = $mdb2->exec($sql);
570       if (is_a($affected, 'PEAR_Error')) return false;
571
572       // Delete task.
573       $sql = "delete from tt_tasks where id = $task_id";
574       $affected = $mdb2->exec($sql);
575       if (is_a($affected, 'PEAR_Error')) return false;
576     }
577
578     return true;
579   }
580
581   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
582   static function deleteCustomFields($group_id) {
583     $mdb2 = getConnection();
584     $sql = "select id from tt_custom_fields where group_id = $group_id";
585     $res = $mdb2->query($sql);
586     if (is_a($res, 'PEAR_Error')) return false;
587
588     while ($val = $res->fetchRow()) {
589       $field_id = $val['id'];
590
591       // Clean up tt_custom_field_log.
592       $sql = "delete from tt_custom_field_log where field_id = $field_id";
593       $affected = $mdb2->exec($sql);
594       if (is_a($affected, 'PEAR_Error')) return false;
595
596       // Clean up tt_custom_field_options.
597       $sql = "delete from tt_custom_field_options where field_id = $field_id";
598       $affected = $mdb2->exec($sql);
599       if (is_a($affected, 'PEAR_Error')) return false;
600
601       // Delete custom field.
602       $sql = "delete from tt_custom_fields where id = $field_id";
603       $affected = $mdb2->exec($sql);
604       if (is_a($affected, 'PEAR_Error')) return false;
605     }
606
607     return true;
608   }
609 }