Refactored getRecentInvoices and moved to another class.
[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 where user_id in (select id from tt_users where group_id = $group_id) order by user_id, status, project_id";
264     $res = $mdb2->query($sql);
265     $result = array();
266     if (!is_a($res, 'PEAR_Error')) {
267       while ($val = $res->fetchRow()) {
268         $result[] = $val;
269       }
270       return $result;
271     }
272     return false;
273   }
274
275   // The getAllCustomFields obtains all custom fields in a group.
276   static function getAllCustomFields($group_id) {
277     $mdb2 = getConnection();
278
279     $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
280
281     $res = $mdb2->query($sql);
282     $result = array();
283     if (!is_a($res, 'PEAR_Error')) {
284       while ($val = $res->fetchRow()) {
285         $result[] = $val;
286       }
287       return $result;
288     }
289     return false;
290   }
291
292   // The getAllCustomFieldOptions obtains all custom field options in a group.
293   static function getAllCustomFieldOptions($group_id) {
294     $mdb2 = getConnection();
295
296     $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";
297
298     $res = $mdb2->query($sql);
299     $result = array();
300     if (!is_a($res, 'PEAR_Error')) {
301       while ($val = $res->fetchRow()) {
302         $result[] = $val;
303       }
304       return $result;
305     }
306     return false;
307   }
308
309   // The getCustomFieldLog obtains all custom field log entries for a group.
310   static function getCustomFieldLog($group_id) {
311     $mdb2 = getConnection();
312
313     $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";
314
315     $res = $mdb2->query($sql);
316     $result = array();
317     if (!is_a($res, 'PEAR_Error')) {
318       while ($val = $res->fetchRow()) {
319         $result[] = $val;
320       }
321       return $result;
322     }
323     return false;
324   }
325
326   // getFavReports - obtains all favorite reports for all users in a group.
327   static function getFavReports($group_id) {
328     $mdb2 = getConnection();
329
330     $result = array();
331     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
332     $res = $mdb2->query($sql);
333     $result = array();
334     if (!is_a($res, 'PEAR_Error')) {
335       while ($val = $res->fetchRow()) {
336         $result[] = $val;
337       }
338       return $result;
339     }
340     return false;
341   }
342
343   // getExpenseItems - obtains all expense items for all users in a group.
344   static function getExpenseItems($group_id) {
345     $mdb2 = getConnection();
346
347     $result = array();
348     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
349     $res = $mdb2->query($sql);
350     $result = array();
351     if (!is_a($res, 'PEAR_Error')) {
352       while ($val = $res->fetchRow()) {
353         $result[] = $val;
354       }
355       return $result;
356     }
357     return false;
358   }
359
360   // getMonthlyQuotas - obtains monthly quotas for a group.
361   static function getMonthlyQuotas($group_id) {
362     $mdb2 = getConnection();
363
364     $result = array();
365     $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
366     $res = $mdb2->query($sql);
367     $result = array();
368     if (!is_a($res, 'PEAR_Error')) {
369       while ($val = $res->fetchRow()) {
370         $result[] = $val;
371       }
372       return $result;
373     }
374     return false;
375   }
376
377   // The delete function permanently deletes all data for a group.
378   static function delete($group_id) {
379     $mdb2 = getConnection();
380
381     // Delete users.
382     $sql = "select id from tt_users where group_id = $group_id";
383     $res = $mdb2->query($sql);
384     if (is_a($res, 'PEAR_Error')) return false;
385     while ($val = $res->fetchRow()) {
386       $user_id = $val['id'];
387       if (!ttUserHelper::delete($user_id)) return false;
388     }
389
390     // Delete tasks.
391     if (!ttTeamHelper::deleteTasks($group_id)) return false;
392
393     // Delete client to project binds.
394     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
395     $affected = $mdb2->exec($sql);
396     if (is_a($affected, 'PEAR_Error')) return false;
397
398     // Delete projects.
399     $sql = "delete from tt_projects where group_id = $group_id";
400     $affected = $mdb2->exec($sql);
401     if (is_a($affected, 'PEAR_Error')) return false;
402
403     // Delete clients.
404     $sql = "delete from tt_clients where group_id = $group_id";
405     $affected = $mdb2->exec($sql);
406     if (is_a($affected, 'PEAR_Error')) return false;
407
408     // Delete invoices.
409     $sql = "delete from tt_invoices where group_id = $group_id";
410     $affected = $mdb2->exec($sql);
411     if (is_a($affected, 'PEAR_Error')) return false;
412
413     // Delete custom fields.
414     if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
415
416     // Delete roles.
417     $sql = "delete from tt_roles where group_id = $group_id";
418     $affected = $mdb2->exec($sql);
419     if (is_a($affected, 'PEAR_Error')) return false;
420
421     // Delete cron entries.
422     $sql = "delete from tt_cron where group_id = $group_id";
423     $affected = $mdb2->exec($sql);
424     if (is_a($affected, 'PEAR_Error')) return false;
425
426     // Delete predefined expenses.
427     $sql = "delete from tt_predefined_expenses where group_id = $group_id";
428     $affected = $mdb2->exec($sql);
429     if (is_a($affected, 'PEAR_Error')) return false;
430
431     // Delete monthly quotas.
432     $sql = "delete from tt_monthly_quotas where group_id = $group_id";
433     $affected = $mdb2->exec($sql);
434     if (is_a($affected, 'PEAR_Error')) return false;
435
436     // Delete group.
437     $sql = "delete from tt_groups where id = $group_id";
438     $affected = $mdb2->exec($sql);
439     if (is_a($affected, 'PEAR_Error')) return false;
440
441     return true;
442   }
443
444   // The deleteTasks deletes all tasks and task binds for an inactive group.
445   static function deleteTasks($group_id) {
446     $mdb2 = getConnection();
447     $sql = "select id from tt_tasks where group_id = $group_id";
448     $res = $mdb2->query($sql);
449     if (is_a($res, 'PEAR_Error')) return false;
450
451     while ($val = $res->fetchRow()) {
452
453       // Delete task binds.
454       $task_id = $val['id'];
455       $sql = "delete from tt_project_task_binds where task_id = $task_id";
456       $affected = $mdb2->exec($sql);
457       if (is_a($affected, 'PEAR_Error')) return false;
458
459       // Delete task.
460       $sql = "delete from tt_tasks where id = $task_id";
461       $affected = $mdb2->exec($sql);
462       if (is_a($affected, 'PEAR_Error')) return false;
463     }
464
465     return true;
466   }
467
468   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
469   static function deleteCustomFields($group_id) {
470     $mdb2 = getConnection();
471     $sql = "select id from tt_custom_fields where group_id = $group_id";
472     $res = $mdb2->query($sql);
473     if (is_a($res, 'PEAR_Error')) return false;
474
475     while ($val = $res->fetchRow()) {
476       $field_id = $val['id'];
477
478       // Clean up tt_custom_field_log.
479       $sql = "delete from tt_custom_field_log where field_id = $field_id";
480       $affected = $mdb2->exec($sql);
481       if (is_a($affected, 'PEAR_Error')) return false;
482
483       // Clean up tt_custom_field_options.
484       $sql = "delete from tt_custom_field_options where field_id = $field_id";
485       $affected = $mdb2->exec($sql);
486       if (is_a($affected, 'PEAR_Error')) return false;
487
488       // Delete custom field.
489       $sql = "delete from tt_custom_fields where id = $field_id";
490       $affected = $mdb2->exec($sql);
491       if (is_a($affected, 'PEAR_Error')) return false;
492     }
493
494     return true;
495   }
496 }