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