4dd2f9aaafd8cd8863b692e3b760574311c92b80
[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 getInactiveUsers obtains all inactive users in a group.
108   static function getInactiveUsers($group_id, $all_fields = false) {
109     $mdb2 = getConnection();
110
111     if ($all_fields)
112       $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)";
113     else
114       $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
115     $res = $mdb2->query($sql);
116     $result = array();
117     if (!is_a($res, 'PEAR_Error')) {
118       while ($val = $res->fetchRow()) {
119         $result[] = $val;
120       }
121       return $result;
122     }
123     return false;
124   }
125
126   // The getAllProjects obtains all projects in a group.
127   static function getAllProjects($group_id, $all_fields = false) {
128     $mdb2 = getConnection();
129
130     if ($all_fields)
131       $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
132     else
133       $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
134     $res = $mdb2->query($sql);
135     $result = array();
136     if (!is_a($res, 'PEAR_Error')) {
137       while ($val = $res->fetchRow()) {
138         $result[] = $val;
139       }
140       return $result;
141     }
142     return false;
143   }
144
145   // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
146   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
147   static function getActiveRolesForUser()
148   {
149     global $user;
150     $result = array();
151     $mdb2 = getConnection();
152
153     $group_id = $user->getGroup();
154     $org_id = $user->org_id;
155
156     // Determine max rank. If we are working in on behalf group
157     // then rank restriction does not apply.
158     $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
159
160     $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";
161     $res = $mdb2->query($sql);
162     $result = array();
163     if (!is_a($res, 'PEAR_Error')) {
164       while ($val = $res->fetchRow()) {
165         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
166         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
167           continue; // Skip adding a client role.
168         $result[] = $val;
169       }
170     }
171     return $result;
172   }
173
174   // getActiveRoles - returns an array of active roles for a group.
175   static function getActiveRoles($group_id)
176   {
177     $result = array();
178     $mdb2 = getConnection();
179
180     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
181     $res = $mdb2->query($sql);
182     $result = array();
183     if (!is_a($res, 'PEAR_Error')) {
184       while ($val = $res->fetchRow()) {
185         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
186         $result[] = $val;
187       }
188     }
189     return $result;
190   }
191
192   // getInactiveRoles - returns an array of inactive roles for a group.
193   static function getInactiveRoles($group_id)
194   {
195     $result = array();
196     $mdb2 = getConnection();
197
198     $sql = "select id, name, rank, description from tt_roles
199       where group_id = $group_id and status = 0 order by rank";
200     $res = $mdb2->query($sql);
201     $result = array();
202     if (!is_a($res, 'PEAR_Error')) {
203       while ($val = $res->fetchRow()) {
204         $result[] = $val;
205       }
206     }
207     return $result;
208   }
209
210   // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
211   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
212   static function getInactiveRolesForUser()
213   {
214     global $user;
215     $result = array();
216     $mdb2 = getConnection();
217
218     $group_id = $user->getGroup();
219     $org_id = $user->org_id;
220
221     // Determine max rank. If we are working in on behalf group
222     // then rank restriction does not apply.
223     $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
224
225     $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";
226     $res = $mdb2->query($sql);
227     $result = array();
228     if (!is_a($res, 'PEAR_Error')) {
229       while ($val = $res->fetchRow()) {
230         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
231         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
232           continue; // Skip adding a client role.
233         $result[] = $val;
234       }
235     }
236     return $result;
237   }
238
239   // The getAllClients obtains all clients in a group.
240   static function getAllClients($group_id, $all_fields = false) {
241     $mdb2 = getConnection();
242
243     if ($all_fields)
244       $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
245     else
246       $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
247
248     $res = $mdb2->query($sql);
249     $result = array();
250     if (!is_a($res, 'PEAR_Error')) {
251       while ($val = $res->fetchRow()) {
252         $result[] = $val;
253       }
254       return $result;
255     }
256     return false;
257   }
258
259   // The getAllInvoices returns an array of all invoices for a group.
260   static function getAllInvoices()
261   {
262     global $user;
263
264     $result = array();
265     $mdb2 = getConnection();
266
267     $sql = "select * from tt_invoices where group_id = $user->group_id";
268     $res = $mdb2->query($sql);
269     $result = array();
270     if (!is_a($res, 'PEAR_Error')) {
271       $dt = new DateAndTime(DB_DATEFORMAT);
272       while ($val = $res->fetchRow()) {
273         $result[] = $val;
274       }
275     }
276     return $result;
277   }
278
279   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
280   static function getRecentInvoices($group_id, $client_id)
281   {
282     global $user;
283
284     $result = array();
285     $mdb2 = getConnection();
286
287     $sql = "select i.id, i.name from tt_invoices i
288       left join tt_clients c on (c.id = i.client_id)
289       where i.group_id = $group_id and i.status = 1 and c.id = $client_id
290       order by i.id desc limit 3";
291     $res = $mdb2->query($sql);
292     $result = array();
293     if (!is_a($res, 'PEAR_Error')) {
294       $dt = new DateAndTime(DB_DATEFORMAT);
295       while ($val = $res->fetchRow()) {
296         $result[] = $val;
297       }
298     }
299     return $result;
300   }
301
302   // getUserToProjectBinds - obtains all user to project binds for a group.
303   static function getUserToProjectBinds($group_id) {
304     $mdb2 = getConnection();
305
306     $result = array();
307     $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";
308     $res = $mdb2->query($sql);
309     $result = array();
310     if (!is_a($res, 'PEAR_Error')) {
311       while ($val = $res->fetchRow()) {
312         $result[] = $val;
313       }
314       return $result;
315     }
316     return false;
317   }
318
319   // The getAllCustomFields obtains all custom fields in a group.
320   static function getAllCustomFields($group_id) {
321     $mdb2 = getConnection();
322
323     $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
324
325     $res = $mdb2->query($sql);
326     $result = array();
327     if (!is_a($res, 'PEAR_Error')) {
328       while ($val = $res->fetchRow()) {
329         $result[] = $val;
330       }
331       return $result;
332     }
333     return false;
334   }
335
336   // The getAllCustomFieldOptions obtains all custom field options in a group.
337   static function getAllCustomFieldOptions($group_id) {
338     $mdb2 = getConnection();
339
340     $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";
341
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 getCustomFieldLog obtains all custom field log entries for a group.
354   static function getCustomFieldLog($group_id) {
355     $mdb2 = getConnection();
356
357     $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";
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   // getFavReports - obtains all favorite reports for all users in a group.
371   static function getFavReports($group_id) {
372     $mdb2 = getConnection();
373
374     $result = array();
375     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
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   // getExpenseItems - obtains all expense items for all users in a group.
388   static function getExpenseItems($group_id) {
389     $mdb2 = getConnection();
390
391     $result = array();
392     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
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   // getMonthlyQuotas - obtains monthly quotas for a group.
405   static function getMonthlyQuotas($group_id) {
406     $mdb2 = getConnection();
407
408     $result = array();
409     $sql = "select year, month, minutes from tt_monthly_quotas 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   // The delete function permanently deletes all data for a group.
422   static function delete($group_id) {
423     $mdb2 = getConnection();
424
425     // Delete users.
426     $sql = "select id from tt_users where group_id = $group_id";
427     $res = $mdb2->query($sql);
428     if (is_a($res, 'PEAR_Error')) return false;
429     while ($val = $res->fetchRow()) {
430       $user_id = $val['id'];
431       if (!ttUserHelper::delete($user_id)) return false;
432     }
433
434     // Delete tasks.
435     if (!ttTeamHelper::deleteTasks($group_id)) return false;
436
437     // Delete client to project binds.
438     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
439     $affected = $mdb2->exec($sql);
440     if (is_a($affected, 'PEAR_Error')) return false;
441
442     // Delete projects.
443     $sql = "delete from tt_projects where group_id = $group_id";
444     $affected = $mdb2->exec($sql);
445     if (is_a($affected, 'PEAR_Error')) return false;
446
447     // Delete clients.
448     $sql = "delete from tt_clients where group_id = $group_id";
449     $affected = $mdb2->exec($sql);
450     if (is_a($affected, 'PEAR_Error')) return false;
451
452     // Delete invoices.
453     $sql = "delete from tt_invoices where group_id = $group_id";
454     $affected = $mdb2->exec($sql);
455     if (is_a($affected, 'PEAR_Error')) return false;
456
457     // Delete custom fields.
458     if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
459
460     // Delete roles.
461     $sql = "delete from tt_roles where group_id = $group_id";
462     $affected = $mdb2->exec($sql);
463     if (is_a($affected, 'PEAR_Error')) return false;
464
465     // Delete cron entries.
466     $sql = "delete from tt_cron where group_id = $group_id";
467     $affected = $mdb2->exec($sql);
468     if (is_a($affected, 'PEAR_Error')) return false;
469
470     // Delete predefined expenses.
471     $sql = "delete from tt_predefined_expenses where group_id = $group_id";
472     $affected = $mdb2->exec($sql);
473     if (is_a($affected, 'PEAR_Error')) return false;
474
475     // Delete monthly quotas.
476     $sql = "delete from tt_monthly_quotas where group_id = $group_id";
477     $affected = $mdb2->exec($sql);
478     if (is_a($affected, 'PEAR_Error')) return false;
479
480     // Delete group.
481     $sql = "delete from tt_groups where id = $group_id";
482     $affected = $mdb2->exec($sql);
483     if (is_a($affected, 'PEAR_Error')) return false;
484
485     return true;
486   }
487
488   // The deleteTasks deletes all tasks and task binds for an inactive group.
489   static function deleteTasks($group_id) {
490     $mdb2 = getConnection();
491     $sql = "select id from tt_tasks where group_id = $group_id";
492     $res = $mdb2->query($sql);
493     if (is_a($res, 'PEAR_Error')) return false;
494
495     while ($val = $res->fetchRow()) {
496
497       // Delete task binds.
498       $task_id = $val['id'];
499       $sql = "delete from tt_project_task_binds where task_id = $task_id";
500       $affected = $mdb2->exec($sql);
501       if (is_a($affected, 'PEAR_Error')) return false;
502
503       // Delete task.
504       $sql = "delete from tt_tasks where id = $task_id";
505       $affected = $mdb2->exec($sql);
506       if (is_a($affected, 'PEAR_Error')) return false;
507     }
508
509     return true;
510   }
511
512   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
513   static function deleteCustomFields($group_id) {
514     $mdb2 = getConnection();
515     $sql = "select id from tt_custom_fields where group_id = $group_id";
516     $res = $mdb2->query($sql);
517     if (is_a($res, 'PEAR_Error')) return false;
518
519     while ($val = $res->fetchRow()) {
520       $field_id = $val['id'];
521
522       // Clean up tt_custom_field_log.
523       $sql = "delete from tt_custom_field_log where field_id = $field_id";
524       $affected = $mdb2->exec($sql);
525       if (is_a($affected, 'PEAR_Error')) return false;
526
527       // Clean up tt_custom_field_options.
528       $sql = "delete from tt_custom_field_options where field_id = $field_id";
529       $affected = $mdb2->exec($sql);
530       if (is_a($affected, 'PEAR_Error')) return false;
531
532       // Delete custom field.
533       $sql = "delete from tt_custom_fields where id = $field_id";
534       $affected = $mdb2->exec($sql);
535       if (is_a($affected, 'PEAR_Error')) return false;
536     }
537
538     return true;
539   }
540 }