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