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