Some 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 getActiveUsers obtains all active users in a given group.
58   static function getActiveUsers($options = null) {
59     global $user;
60     global $i18n;
61     $mdb2 = getConnection();
62
63     $group_id = $user->getGroup();
64     $org_id = $user->org_id;
65
66     if (isset($options['getAllFields']))
67       $sql = "select u.*, r.name as role_name, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $group_id and u.org_id = $org_id and u.status = 1 order by upper(u.name)";
68     else
69       $sql = "select id, name from tt_users where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
70     $res = $mdb2->query($sql);
71     $user_list = array();
72     if (is_a($res, 'PEAR_Error'))
73       return false;
74     while ($val = $res->fetchRow()) {
75       // Localize top manager role name, as it is not localized in db.
76       if ($val['rank'] == 512)
77         $val['role_name'] = $i18n->get('role.top_manager.label');
78       $user_list[] = $val;
79     }
80
81     if (isset($options['putSelfFirst'])) {
82       // Put own entry at the front.
83       $cnt = count($user_list);
84       for($i = 0; $i < $cnt; $i++) {
85         if ($user_list[$i]['id'] == $user->id) {
86           $self = $user_list[$i]; // Found self.
87           array_unshift($user_list, $self); // Put own entry at the front.
88           array_splice($user_list, $i+1, 1); // Remove duplicate.
89         }
90       }
91     }
92     return $user_list;
93   }
94
95   // The swapRolesWith swaps existing user role with that of another user.
96   static function swapRolesWith($user_id) {
97     global $user;
98     $mdb2 = getConnection();
99
100     // Obtain role id for the user we are swapping ourselves with.
101     $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";
102     $res = $mdb2->query($sql);
103     if (is_a($res, 'PEAR_Error'))
104       return false;
105     $val = $res->fetchRow();
106     if (!$val['id'] || !$val['role_id'])
107       return false;
108
109     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
110
111     // Promote user.
112     $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
113     $affected = $mdb2->exec($sql);
114     if (is_a($affected, 'PEAR_Error')) return false;
115
116     // Demote self.
117     $role_id = $val['role_id'];
118     $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
119     $affected = $mdb2->exec($sql);
120     if (is_a($affected, 'PEAR_Error')) return false;
121
122     return true;
123   }
124
125   // The getUsersForSwap obtains all users a current user can swap roles with.
126   static function getUsersForSwap() {
127     global $user;
128     $mdb2 = getConnection();
129
130     $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)";
131     $res = $mdb2->query($sql);
132     $user_list = array();
133     if (is_a($res, 'PEAR_Error'))
134       return false;
135     while ($val = $res->fetchRow()) {
136       $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
137       if ($isClient)
138         continue; // Skip adding clients.
139       $user_list[] = $val;
140     }
141
142     return $user_list;
143   }
144
145   // The getUsers obtains all active and inactive (but not deleted) users in a group.
146   static function getUsers() {
147     global $user;
148     $mdb2 = getConnection();
149     $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
150     $res = $mdb2->query($sql);
151     $user_list = array();
152     if (is_a($res, 'PEAR_Error'))
153       return false;
154     while ($val = $res->fetchRow()) {
155       $user_list[] = $val;
156     }
157     return $user_list;
158   }
159
160   // The getInactiveUsers obtains all inactive users in a group.
161   static function getInactiveUsers($group_id, $all_fields = false) {
162     $mdb2 = getConnection();
163
164     if ($all_fields)
165       $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)";
166     else
167       $sql = "select id, name from tt_users 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       return $result;
175     }
176     return false;
177   }
178
179   // getActiveProjects - returns an array of active projects for a group.
180   static function getActiveProjects($group_id)
181   {
182     $result = array();
183     $mdb2 = getConnection();
184
185     $sql = "select id, name, description, tasks from tt_projects
186       where group_id = $group_id and status = 1 order by upper(name)";
187     $res = $mdb2->query($sql);
188     $result = array();
189     if (!is_a($res, 'PEAR_Error')) {
190       while ($val = $res->fetchRow()) {
191         $result[] = $val;
192       }
193     }
194     return $result;
195   }
196
197   // getInactiveProjects - returns an array of inactive projects for a group.
198   static function getInactiveProjects($group_id)
199   {
200     $result = array();
201     $mdb2 = getConnection();
202
203     $sql = "select id, name, description, tasks from tt_projects
204       where group_id = $group_id and status = 0 order by upper(name)";
205     $res = $mdb2->query($sql);
206     $result = array();
207     if (!is_a($res, 'PEAR_Error')) {
208       while ($val = $res->fetchRow()) {
209         $result[] = $val;
210       }
211     }
212     return $result;
213   }
214
215   // The getAllProjects obtains all projects in a group.
216   static function getAllProjects($group_id, $all_fields = false) {
217     $mdb2 = getConnection();
218
219     if ($all_fields)
220       $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
221     else
222       $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
223     $res = $mdb2->query($sql);
224     $result = array();
225     if (!is_a($res, 'PEAR_Error')) {
226       while ($val = $res->fetchRow()) {
227         $result[] = $val;
228       }
229       return $result;
230     }
231     return false;
232   }
233
234   // getActiveTasks - returns an array of active tasks for a group.
235   static function getActiveTasks($group_id)
236   {
237     $result = array();
238     $mdb2 = getConnection();
239
240     $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
241     $res = $mdb2->query($sql);
242     $result = array();
243     if (!is_a($res, 'PEAR_Error')) {
244       while ($val = $res->fetchRow()) {
245         $result[] = $val;
246       }
247     }
248     return $result;
249   }
250
251   // getInactiveTasks - returns an array of inactive tasks for a group.
252   static function getInactiveTasks($group_id)
253   {
254     $result = array();
255     $mdb2 = getConnection();
256
257     $sql = "select id, name, description from tt_tasks
258       where group_id = $group_id and status = 0 order by upper(name)";
259     $res = $mdb2->query($sql);
260     $result = array();
261     if (!is_a($res, 'PEAR_Error')) {
262       while ($val = $res->fetchRow()) {
263         $result[] = $val;
264       }
265     }
266     return $result;
267   }
268
269   // The getAllTasks obtains all tasks in a group.
270   static function getAllTasks($group_id, $all_fields = false) {
271     $mdb2 = getConnection();
272
273     if ($all_fields)
274       $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
275     else
276       $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
277     $res = $mdb2->query($sql);
278     $result = array();
279     if (!is_a($res, 'PEAR_Error')) {
280       while ($val = $res->fetchRow()) {
281         $result[] = $val;
282       }
283       return $result;
284     }
285     return false;
286   }
287
288   // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
289   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
290   static function getActiveRolesForUser()
291   {
292     global $user;
293     $result = array();
294     $mdb2 = getConnection();
295
296     $group_id = $user->getGroup();
297     $org_id = $user->org_id;
298
299     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $user->rank and status = 1 order by rank";
300     $res = $mdb2->query($sql);
301     $result = array();
302     if (!is_a($res, 'PEAR_Error')) {
303       while ($val = $res->fetchRow()) {
304         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
305         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
306           continue; // Skip adding a client role.
307         $result[] = $val;
308       }
309     }
310     return $result;
311   }
312
313   // getActiveRoles - returns an array of active roles for a group.
314   static function getActiveRoles($group_id)
315   {
316     $result = array();
317     $mdb2 = getConnection();
318
319     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
320     $res = $mdb2->query($sql);
321     $result = array();
322     if (!is_a($res, 'PEAR_Error')) {
323       while ($val = $res->fetchRow()) {
324         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
325         $result[] = $val;
326       }
327     }
328     return $result;
329   }
330
331   // getInactiveRoles - returns an array of inactive roles for a group.
332   static function getInactiveRoles($group_id)
333   {
334     $result = array();
335     $mdb2 = getConnection();
336
337     $sql = "select id, name, rank, description from tt_roles
338       where group_id = $group_id and status = 0 order by rank";
339     $res = $mdb2->query($sql);
340     $result = array();
341     if (!is_a($res, 'PEAR_Error')) {
342       while ($val = $res->fetchRow()) {
343         $result[] = $val;
344       }
345     }
346     return $result;
347   }
348
349   // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
350   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
351   static function getInactiveRolesForUser()
352   {
353     global $user;
354     $result = array();
355     $mdb2 = getConnection();
356
357     $group_id = $user->getGroup();
358     $org_id = $user->org_id;
359
360     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $user->rank and status = 0 order by rank";
361     $res = $mdb2->query($sql);
362     $result = array();
363     if (!is_a($res, 'PEAR_Error')) {
364       while ($val = $res->fetchRow()) {
365         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
366         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
367           continue; // Skip adding a client role.
368         $result[] = $val;
369       }
370     }
371     return $result;
372   }
373
374   // The getActiveClients returns an array of active clients for a group.
375   static function getActiveClients($group_id, $all_fields = false)
376   {
377     $result = array();
378     $mdb2 = getConnection();
379
380     if ($all_fields)
381       $sql = "select * from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
382     else
383       $sql = "select id, name from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
384
385     $res = $mdb2->query($sql);
386     $result = array();
387     if (!is_a($res, 'PEAR_Error')) {
388       while ($val = $res->fetchRow()) {
389         $result[] = $val;
390       }
391     }
392     return $result;
393   }
394
395   // The getInactiveClients returns an array of inactive clients for a group.
396   static function getInactiveClients($group_id, $all_fields = false)
397   {
398     $result = array();
399     $mdb2 = getConnection();
400
401     if ($all_fields)
402       $sql = "select * from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
403     else
404       $sql = "select id, name from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
405
406     $res = $mdb2->query($sql);
407     $result = array();
408     if (!is_a($res, 'PEAR_Error')) {
409       while ($val = $res->fetchRow()) {
410         $result[] = $val;
411       }
412     }
413     return $result;
414   }
415
416   // The getAllClients obtains all clients in a group.
417   static function getAllClients($group_id, $all_fields = false) {
418     $mdb2 = getConnection();
419
420     if ($all_fields)
421       $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
422     else
423       $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
424
425     $res = $mdb2->query($sql);
426     $result = array();
427     if (!is_a($res, 'PEAR_Error')) {
428       while ($val = $res->fetchRow()) {
429         $result[] = $val;
430       }
431       return $result;
432     }
433     return false;
434   }
435
436   // The getActiveInvoices returns an array of active invoices for a group.
437   static function getActiveInvoices($localizeDates = true)
438   {
439     global $user;
440     $addPaidStatus = $user->isPluginEnabled('ps');
441
442     $result = array();
443     $mdb2 = getConnection();
444
445     if ($user->isClient())
446       $client_part = " and i.client_id = $user->client_id";
447
448     $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
449       left join tt_clients c on (c.id = i.client_id)
450       where i.status = 1 and i.group_id = $user->group_id $client_part order by i.name";
451     $res = $mdb2->query($sql);
452     $result = array();
453     if (!is_a($res, 'PEAR_Error')) {
454       $dt = new DateAndTime(DB_DATEFORMAT);
455       while ($val = $res->fetchRow()) {
456         if ($localizeDates) {
457           $dt->parseVal($val['date']);
458           $val['date'] = $dt->toString($user->date_format);
459         }
460         if ($addPaidStatus)
461           $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
462         $result[] = $val;
463       }
464     }
465     return $result;
466   }
467
468   // The getAllInvoices returns an array of all invoices for a group.
469   static function getAllInvoices()
470   {
471     global $user;
472
473     $result = array();
474     $mdb2 = getConnection();
475
476     $sql = "select * from tt_invoices where group_id = $user->group_id";
477     $res = $mdb2->query($sql);
478     $result = array();
479     if (!is_a($res, 'PEAR_Error')) {
480       $dt = new DateAndTime(DB_DATEFORMAT);
481       while ($val = $res->fetchRow()) {
482         $result[] = $val;
483       }
484     }
485     return $result;
486   }
487
488   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
489   static function getRecentInvoices($group_id, $client_id)
490   {
491     global $user;
492
493     $result = array();
494     $mdb2 = getConnection();
495
496     $sql = "select i.id, i.name from tt_invoices i
497       left join tt_clients c on (c.id = i.client_id)
498       where i.group_id = $group_id and i.status = 1 and c.id = $client_id
499       order by i.id desc limit 3";
500     $res = $mdb2->query($sql);
501     $result = array();
502     if (!is_a($res, 'PEAR_Error')) {
503       $dt = new DateAndTime(DB_DATEFORMAT);
504       while ($val = $res->fetchRow()) {
505         $result[] = $val;
506       }
507     }
508     return $result;
509   }
510
511   // getUserToProjectBinds - obtains all user to project binds for a group.
512   static function getUserToProjectBinds($group_id) {
513     $mdb2 = getConnection();
514
515     $result = array();
516     $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";
517     $res = $mdb2->query($sql);
518     $result = array();
519     if (!is_a($res, 'PEAR_Error')) {
520       while ($val = $res->fetchRow()) {
521         $result[] = $val;
522       }
523       return $result;
524     }
525     return false;
526   }
527
528   // The getAllCustomFields obtains all custom fields in a group.
529   static function getAllCustomFields($group_id) {
530     $mdb2 = getConnection();
531
532     $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
533
534     $res = $mdb2->query($sql);
535     $result = array();
536     if (!is_a($res, 'PEAR_Error')) {
537       while ($val = $res->fetchRow()) {
538         $result[] = $val;
539       }
540       return $result;
541     }
542     return false;
543   }
544
545   // The getAllCustomFieldOptions obtains all custom field options in a group.
546   static function getAllCustomFieldOptions($group_id) {
547     $mdb2 = getConnection();
548
549     $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";
550
551     $res = $mdb2->query($sql);
552     $result = array();
553     if (!is_a($res, 'PEAR_Error')) {
554       while ($val = $res->fetchRow()) {
555         $result[] = $val;
556       }
557       return $result;
558     }
559     return false;
560   }
561
562   // The getCustomFieldLog obtains all custom field log entries for a group.
563   static function getCustomFieldLog($group_id) {
564     $mdb2 = getConnection();
565
566     $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";
567
568     $res = $mdb2->query($sql);
569     $result = array();
570     if (!is_a($res, 'PEAR_Error')) {
571       while ($val = $res->fetchRow()) {
572         $result[] = $val;
573       }
574       return $result;
575     }
576     return false;
577   }
578
579   // getFavReports - obtains all favorite reports for all users in a group.
580   static function getFavReports($group_id) {
581     $mdb2 = getConnection();
582
583     $result = array();
584     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
585     $res = $mdb2->query($sql);
586     $result = array();
587     if (!is_a($res, 'PEAR_Error')) {
588       while ($val = $res->fetchRow()) {
589         $result[] = $val;
590       }
591       return $result;
592     }
593     return false;
594   }
595
596   // getExpenseItems - obtains all expense items for all users in a group.
597   static function getExpenseItems($group_id) {
598     $mdb2 = getConnection();
599
600     $result = array();
601     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
602     $res = $mdb2->query($sql);
603     $result = array();
604     if (!is_a($res, 'PEAR_Error')) {
605       while ($val = $res->fetchRow()) {
606         $result[] = $val;
607       }
608       return $result;
609     }
610     return false;
611   }
612
613   // getPredefinedExpenses - obtains predefined expenses for a group.
614   static function getPredefinedExpenses($group_id) {
615     global $user;
616     $replaceDecimalMark = ('.' != $user->decimal_mark);
617
618     $mdb2 = getConnection();
619
620     $result = array();
621     $sql = "select id, name, cost from tt_predefined_expenses where group_id = $group_id";
622     $res = $mdb2->query($sql);
623     $result = array();
624     if (!is_a($res, 'PEAR_Error')) {
625       while ($val = $res->fetchRow()) {
626         if ($replaceDecimalMark)
627           $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
628         $result[] = $val;
629       }
630       return $result;
631     }
632     return false;
633   }
634
635   // getNotifications - obtains notification descriptions for a group.
636   static function getNotifications($group_id) {
637     $mdb2 = getConnection();
638
639     $result = array();
640     $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
641       left join tt_fav_reports fr on (fr.id = c.report_id)
642       where c.group_id = $group_id and c.status = 1 and fr.status = 1";
643     $res = $mdb2->query($sql);
644     $result = array();
645     if (!is_a($res, 'PEAR_Error')) {
646       while ($val = $res->fetchRow()) {
647         $result[] = $val;
648       }
649       return $result;
650     }
651     return false;
652   }
653
654   // getMonthlyQuotas - obtains monthly quotas for a group.
655   static function getMonthlyQuotas($group_id) {
656     $mdb2 = getConnection();
657
658     $result = array();
659     $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
660     $res = $mdb2->query($sql);
661     $result = array();
662     if (!is_a($res, 'PEAR_Error')) {
663       while ($val = $res->fetchRow()) {
664         $result[] = $val;
665       }
666       return $result;
667     }
668     return false;
669   }
670
671   // The delete function permanently deletes all data for a group.
672   static function delete($group_id) {
673     $mdb2 = getConnection();
674
675     // Delete users.
676     $sql = "select id from tt_users where group_id = $group_id";
677     $res = $mdb2->query($sql);
678     if (is_a($res, 'PEAR_Error')) return false;
679     while ($val = $res->fetchRow()) {
680       $user_id = $val['id'];
681       if (!ttUserHelper::delete($user_id)) return false;
682     }
683
684     // Delete tasks.
685     if (!ttTeamHelper::deleteTasks($group_id)) return false;
686
687     // Delete client to project binds.
688     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
689     $affected = $mdb2->exec($sql);
690     if (is_a($affected, 'PEAR_Error')) return false;
691
692     // Delete projects.
693     $sql = "delete from tt_projects where group_id = $group_id";
694     $affected = $mdb2->exec($sql);
695     if (is_a($affected, 'PEAR_Error')) return false;
696
697     // Delete clients.
698     $sql = "delete from tt_clients where group_id = $group_id";
699     $affected = $mdb2->exec($sql);
700     if (is_a($affected, 'PEAR_Error')) return false;
701
702     // Delete invoices.
703     $sql = "delete from tt_invoices where group_id = $group_id";
704     $affected = $mdb2->exec($sql);
705     if (is_a($affected, 'PEAR_Error')) return false;
706
707     // Delete custom fields.
708     if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
709
710     // Delete roles.
711     $sql = "delete from tt_roles where group_id = $group_id";
712     $affected = $mdb2->exec($sql);
713     if (is_a($affected, 'PEAR_Error')) return false;
714
715     // Delete cron entries.
716     $sql = "delete from tt_cron where group_id = $group_id";
717     $affected = $mdb2->exec($sql);
718     if (is_a($affected, 'PEAR_Error')) return false;
719
720     // Delete predefined expenses.
721     $sql = "delete from tt_predefined_expenses where group_id = $group_id";
722     $affected = $mdb2->exec($sql);
723     if (is_a($affected, 'PEAR_Error')) return false;
724
725     // Delete monthly quotas.
726     $sql = "delete from tt_monthly_quotas where group_id = $group_id";
727     $affected = $mdb2->exec($sql);
728     if (is_a($affected, 'PEAR_Error')) return false;
729
730     // Delete group.
731     $sql = "delete from tt_groups where id = $group_id";
732     $affected = $mdb2->exec($sql);
733     if (is_a($affected, 'PEAR_Error')) return false;
734
735     return true;
736   }
737
738   // The deleteTasks deletes all tasks and task binds for an inactive group.
739   static function deleteTasks($group_id) {
740     $mdb2 = getConnection();
741     $sql = "select id from tt_tasks where group_id = $group_id";
742     $res = $mdb2->query($sql);
743     if (is_a($res, 'PEAR_Error')) return false;
744
745     while ($val = $res->fetchRow()) {
746
747       // Delete task binds.
748       $task_id = $val['id'];
749       $sql = "delete from tt_project_task_binds where task_id = $task_id";
750       $affected = $mdb2->exec($sql);
751       if (is_a($affected, 'PEAR_Error')) return false;
752
753       // Delete task.
754       $sql = "delete from tt_tasks where id = $task_id";
755       $affected = $mdb2->exec($sql);
756       if (is_a($affected, 'PEAR_Error')) return false;
757     }
758
759     return true;
760   }
761
762   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
763   static function deleteCustomFields($group_id) {
764     $mdb2 = getConnection();
765     $sql = "select id from tt_custom_fields where group_id = $group_id";
766     $res = $mdb2->query($sql);
767     if (is_a($res, 'PEAR_Error')) return false;
768
769     while ($val = $res->fetchRow()) {
770       $field_id = $val['id'];
771
772       // Clean up tt_custom_field_log.
773       $sql = "delete from tt_custom_field_log where field_id = $field_id";
774       $affected = $mdb2->exec($sql);
775       if (is_a($affected, 'PEAR_Error')) return false;
776
777       // Clean up tt_custom_field_options.
778       $sql = "delete from tt_custom_field_options where field_id = $field_id";
779       $affected = $mdb2->exec($sql);
780       if (is_a($affected, 'PEAR_Error')) return false;
781
782       // Delete custom field.
783       $sql = "delete from tt_custom_fields where id = $field_id";
784       $affected = $mdb2->exec($sql);
785       if (is_a($affected, 'PEAR_Error')) return false;
786     }
787
788     return true;
789   }
790 }