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