]> wagnertech.de Git - timetracker.git/blob - WEB-INF/lib/ttTeamHelper.class.php
Cleanup. Removed unused function.
[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 getUserCount function returns number of active users in a group.
37   static function getUserCount($group_id) {
38     $mdb2 = getConnection();
39
40     $sql = "select count(id) as cnt from tt_users where group_id = $group_id and status = 1";
41     $res = $mdb2->query($sql);
42
43     if (!is_a($res, 'PEAR_Error')) {
44       $val = $res->fetchRow();
45       return $val['cnt'];
46     }
47     return false;
48   }
49
50   // The getUsersForClient obtains all active and inactive users in a group that are relevant to a client.
51   static function getUsersForClient() {
52     global $user;
53     $mdb2 = getConnection();
54
55     $sql = "select u.id, u.name from tt_user_project_binds upb".
56       " inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)".
57       " inner join tt_users u on (u.id = upb.user_id)".
58       " where (u.status = 1 or u.status = 0)".
59       " group by u.id".
60       " order by upper(u.name)";
61     $res = $mdb2->query($sql);
62     $user_list = array();
63     if (is_a($res, 'PEAR_Error'))
64       return false;
65     while ($val = $res->fetchRow()) {
66       $user_list[] = $val;
67     }
68     return $user_list;
69   }
70
71   // The getActiveUsers obtains all active users in a given group.
72   static function getActiveUsers($options = null) {
73     global $user;
74     global $i18n;
75     $mdb2 = getConnection();
76
77     if (isset($options['getAllFields']))
78       $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)";
79     else
80       $sql = "select id, name from tt_users where group_id = $user->group_id and status = 1 order by upper(name)";
81     $res = $mdb2->query($sql);
82     $user_list = array();
83     if (is_a($res, 'PEAR_Error'))
84       return false;
85     while ($val = $res->fetchRow()) {
86       // Localize top manager role name, as it is not localized in db.
87       if ($val['rank'] == 512)
88         $val['role_name'] = $i18n->get('role.top_manager.label');
89       $user_list[] = $val;
90     }
91
92     if (isset($options['putSelfFirst'])) {
93       // Put own entry at the front.
94       $cnt = count($user_list);
95       for($i = 0; $i < $cnt; $i++) {
96         if ($user_list[$i]['id'] == $user->id) {
97           $self = $user_list[$i]; // Found self.
98           array_unshift($user_list, $self); // Put own entry at the front.
99           array_splice($user_list, $i+1, 1); // Remove duplicate.
100         }
101       }
102     }
103     return $user_list;
104   }
105
106   // The swapRolesWith swaps existing user role with that of another user.
107   static function swapRolesWith($user_id) {
108     global $user;
109     $mdb2 = getConnection();
110
111     // Obtain role id for the user we are swapping ourselves with.
112     $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";
113     $res = $mdb2->query($sql);
114     if (is_a($res, 'PEAR_Error'))
115       return false;
116     $val = $res->fetchRow();
117     if (!$val['id'] || !$val['role_id'])
118       return false;
119
120     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
121
122     // Promote user.
123     $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
124     $affected = $mdb2->exec($sql);
125     if (is_a($affected, 'PEAR_Error')) return false;
126
127     // Demote self.
128     $role_id = $val['role_id'];
129     $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
130     $affected = $mdb2->exec($sql);
131     if (is_a($affected, 'PEAR_Error')) return false;
132
133     return true;
134   }
135
136   // The getUsersForSwap obtains all users a current user can swap roles with.
137   static function getUsersForSwap() {
138     global $user;
139     $mdb2 = getConnection();
140
141     $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)";
142     $res = $mdb2->query($sql);
143     $user_list = array();
144     if (is_a($res, 'PEAR_Error'))
145       return false;
146     while ($val = $res->fetchRow()) {
147       $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
148       if ($isClient)
149         continue; // Skip adding clients.
150       $user_list[] = $val;
151     }
152
153     return $user_list;
154   }
155
156   // The getUsers obtains all active and inactive (but not deleted) users in a group.
157   static function getUsers() {
158     global $user;
159     $mdb2 = getConnection();
160     $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
161     $res = $mdb2->query($sql);
162     $user_list = array();
163     if (is_a($res, 'PEAR_Error'))
164       return false;
165     while ($val = $res->fetchRow()) {
166       $user_list[] = $val;
167     }
168     return $user_list;
169   }
170
171   // The getInactiveUsers obtains all inactive users in a group.
172   static function getInactiveUsers($group_id, $all_fields = false) {
173     $mdb2 = getConnection();
174
175     if ($all_fields)
176       $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)";
177     else
178       $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
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       return $result;
186     }
187     return false;
188   }
189
190   // The getAllUsers obtains all users in a group.
191   static function getAllUsers($group_id, $all_fields = false) {
192     $mdb2 = getConnection();
193     if ($all_fields)
194       $sql = "select * from tt_users where group_id = $group_id order by upper(name)";
195     else
196       $sql = "select id, name from tt_users where group_id = $group_id order by upper(name)";
197     $res = $mdb2->query($sql);
198     $result = array();
199     if (!is_a($res, 'PEAR_Error')) {
200       while ($val = $res->fetchRow()) {
201         $result[] = $val;
202       }
203       return $result;
204     }
205     return false;
206   }
207
208   // getActiveProjects - returns an array of active projects for a group.
209   static function getActiveProjects($group_id)
210   {
211     $result = array();
212     $mdb2 = getConnection();
213
214     $sql = "select id, name, description, tasks from tt_projects
215       where group_id = $group_id and status = 1 order by upper(name)";
216     $res = $mdb2->query($sql);
217     $result = array();
218     if (!is_a($res, 'PEAR_Error')) {
219       while ($val = $res->fetchRow()) {
220         $result[] = $val;
221       }
222     }
223     return $result;
224   }
225
226   // getInactiveProjects - returns an array of inactive projects for a group.
227   static function getInactiveProjects($group_id)
228   {
229     $result = array();
230     $mdb2 = getConnection();
231
232     $sql = "select id, name, description, tasks from tt_projects
233       where group_id = $group_id and status = 0 order by upper(name)";
234     $res = $mdb2->query($sql);
235     $result = array();
236     if (!is_a($res, 'PEAR_Error')) {
237       while ($val = $res->fetchRow()) {
238         $result[] = $val;
239       }
240     }
241     return $result;
242   }
243
244   // The getAllProjects obtains all projects in a group.
245   static function getAllProjects($group_id, $all_fields = false) {
246     $mdb2 = getConnection();
247
248     if ($all_fields)
249       $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
250     else
251       $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
252     $res = $mdb2->query($sql);
253     $result = array();
254     if (!is_a($res, 'PEAR_Error')) {
255       while ($val = $res->fetchRow()) {
256         $result[] = $val;
257       }
258       return $result;
259     }
260     return false;
261   }
262
263   // getActiveTasks - returns an array of active tasks for a group.
264   static function getActiveTasks($group_id)
265   {
266     $result = array();
267     $mdb2 = getConnection();
268
269     $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
270     $res = $mdb2->query($sql);
271     $result = array();
272     if (!is_a($res, 'PEAR_Error')) {
273       while ($val = $res->fetchRow()) {
274         $result[] = $val;
275       }
276     }
277     return $result;
278   }
279
280   // getInactiveTasks - returns an array of inactive tasks for a group.
281   static function getInactiveTasks($group_id)
282   {
283     $result = array();
284     $mdb2 = getConnection();
285
286     $sql = "select id, name, description from tt_tasks
287       where group_id = $group_id and status = 0 order by upper(name)";
288     $res = $mdb2->query($sql);
289     $result = array();
290     if (!is_a($res, 'PEAR_Error')) {
291       while ($val = $res->fetchRow()) {
292         $result[] = $val;
293       }
294     }
295     return $result;
296   }
297
298   // The getAllTasks obtains all tasks in a group.
299   static function getAllTasks($group_id, $all_fields = false) {
300     $mdb2 = getConnection();
301
302     if ($all_fields)
303       $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
304     else
305       $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
306     $res = $mdb2->query($sql);
307     $result = array();
308     if (!is_a($res, 'PEAR_Error')) {
309       while ($val = $res->fetchRow()) {
310         $result[] = $val;
311       }
312       return $result;
313     }
314     return false;
315   }
316
317   // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
318   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
319   static function getActiveRolesForUser()
320   {
321     global $user;
322     $result = array();
323     $mdb2 = getConnection();
324
325     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $user->group_id and rank < $user->rank and status = 1 order by rank";
326     $res = $mdb2->query($sql);
327     $result = array();
328     if (!is_a($res, 'PEAR_Error')) {
329       while ($val = $res->fetchRow()) {
330         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
331         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
332           continue; // Skip adding a client role.
333         $result[] = $val;
334       }
335     }
336     return $result;
337   }
338
339   // getActiveRoles - returns an array of active roles for a group.
340   static function getActiveRoles($group_id)
341   {
342     $result = array();
343     $mdb2 = getConnection();
344
345     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
346     $res = $mdb2->query($sql);
347     $result = array();
348     if (!is_a($res, 'PEAR_Error')) {
349       while ($val = $res->fetchRow()) {
350         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
351         $result[] = $val;
352       }
353     }
354     return $result;
355   }
356
357   // getInactiveRoles - returns an array of inactive roles for a group.
358   static function getInactiveRoles($group_id)
359   {
360     $result = array();
361     $mdb2 = getConnection();
362
363     $sql = "select id, name, rank, description from tt_roles
364       where group_id = $group_id and status = 0 order by rank";
365     $res = $mdb2->query($sql);
366     $result = array();
367     if (!is_a($res, 'PEAR_Error')) {
368       while ($val = $res->fetchRow()) {
369         $result[] = $val;
370       }
371     }
372     return $result;
373   }
374
375   // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
376   // "Relevant" means that client roles are filtered out if Client plugin is disabled.
377   static function getInactiveRolesForUser()
378   {
379     global $user;
380     $result = array();
381     $mdb2 = getConnection();
382
383     $sql = "select id, name, description, rank, rights from tt_roles where group_id = $user->group_id and rank < $user->rank and status = 0 order by rank";
384     $res = $mdb2->query($sql);
385     $result = array();
386     if (!is_a($res, 'PEAR_Error')) {
387       while ($val = $res->fetchRow()) {
388         $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
389         if ($val['is_client'] && !$user->isPluginEnabled('cl'))
390           continue; // Skip adding a client role.
391         $result[] = $val;
392       }
393     }
394     return $result;
395   }
396
397   // The getActiveClients returns an array of active clients for a group.
398   static function getActiveClients($group_id, $all_fields = false)
399   {
400     $result = array();
401     $mdb2 = getConnection();
402
403     if ($all_fields)
404       $sql = "select * from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
405     else
406       $sql = "select id, name from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
407
408     $res = $mdb2->query($sql);
409     $result = array();
410     if (!is_a($res, 'PEAR_Error')) {
411       while ($val = $res->fetchRow()) {
412         $result[] = $val;
413       }
414     }
415     return $result;
416   }
417
418   // The getInactiveClients returns an array of inactive clients for a group.
419   static function getInactiveClients($group_id, $all_fields = false)
420   {
421     $result = array();
422     $mdb2 = getConnection();
423
424     if ($all_fields)
425       $sql = "select * from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
426     else
427       $sql = "select id, name from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
428
429     $res = $mdb2->query($sql);
430     $result = array();
431     if (!is_a($res, 'PEAR_Error')) {
432       while ($val = $res->fetchRow()) {
433         $result[] = $val;
434       }
435     }
436     return $result;
437   }
438
439   // The getAllClients obtains all clients in a group.
440   static function getAllClients($group_id, $all_fields = false) {
441     $mdb2 = getConnection();
442
443     if ($all_fields)
444       $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
445     else
446       $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
447
448     $res = $mdb2->query($sql);
449     $result = array();
450     if (!is_a($res, 'PEAR_Error')) {
451       while ($val = $res->fetchRow()) {
452         $result[] = $val;
453       }
454       return $result;
455     }
456     return false;
457   }
458
459   // The getActiveInvoices returns an array of active invoices for a group.
460   static function getActiveInvoices($localizeDates = true)
461   {
462     global $user;
463     $addPaidStatus = $user->isPluginEnabled('ps');
464
465     $result = array();
466     $mdb2 = getConnection();
467
468     if ($user->isClient())
469       $client_part = " and i.client_id = $user->client_id";
470
471     $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
472       left join tt_clients c on (c.id = i.client_id)
473       where i.status = 1 and i.group_id = $user->group_id $client_part order by i.name";
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         if ($localizeDates) {
480           $dt->parseVal($val['date']);
481           $val['date'] = $dt->toString($user->date_format);
482         }
483         if ($addPaidStatus)
484           $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
485         $result[] = $val;
486       }
487     }
488     return $result;
489   }
490
491   // The getAllInvoices returns an array of all invoices for a group.
492   static function getAllInvoices()
493   {
494     global $user;
495
496     $result = array();
497     $mdb2 = getConnection();
498
499     $sql = "select * from tt_invoices where group_id = $user->group_id";
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   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
512   static function getRecentInvoices($group_id, $client_id)
513   {
514     global $user;
515
516     $result = array();
517     $mdb2 = getConnection();
518
519     $sql = "select i.id, i.name from tt_invoices i
520       left join tt_clients c on (c.id = i.client_id)
521       where i.group_id = $group_id and i.status = 1 and c.id = $client_id
522       order by i.id desc limit 3";
523     $res = $mdb2->query($sql);
524     $result = array();
525     if (!is_a($res, 'PEAR_Error')) {
526       $dt = new DateAndTime(DB_DATEFORMAT);
527       while ($val = $res->fetchRow()) {
528         $result[] = $val;
529       }
530     }
531     return $result;
532   }
533
534   // getUserToProjectBinds - obtains all user to project binds for a group.
535   static function getUserToProjectBinds($group_id) {
536     $mdb2 = getConnection();
537
538     $result = array();
539     $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";
540     $res = $mdb2->query($sql);
541     $result = array();
542     if (!is_a($res, 'PEAR_Error')) {
543       while ($val = $res->fetchRow()) {
544         $result[] = $val;
545       }
546       return $result;
547     }
548     return false;
549   }
550
551   // The getAllCustomFields obtains all custom fields in a group.
552   static function getAllCustomFields($group_id) {
553     $mdb2 = getConnection();
554
555     $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
556
557     $res = $mdb2->query($sql);
558     $result = array();
559     if (!is_a($res, 'PEAR_Error')) {
560       while ($val = $res->fetchRow()) {
561         $result[] = $val;
562       }
563       return $result;
564     }
565     return false;
566   }
567
568   // The getAllCustomFieldOptions obtains all custom field options in a group.
569   static function getAllCustomFieldOptions($group_id) {
570     $mdb2 = getConnection();
571
572     $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";
573
574     $res = $mdb2->query($sql);
575     $result = array();
576     if (!is_a($res, 'PEAR_Error')) {
577       while ($val = $res->fetchRow()) {
578         $result[] = $val;
579       }
580       return $result;
581     }
582     return false;
583   }
584
585   // The getCustomFieldLog obtains all custom field log entries for a group.
586   static function getCustomFieldLog($group_id) {
587     $mdb2 = getConnection();
588
589     $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";
590
591     $res = $mdb2->query($sql);
592     $result = array();
593     if (!is_a($res, 'PEAR_Error')) {
594       while ($val = $res->fetchRow()) {
595         $result[] = $val;
596       }
597       return $result;
598     }
599     return false;
600   }
601
602   // getFavReports - obtains all favorite reports for all users in a group.
603   static function getFavReports($group_id) {
604     $mdb2 = getConnection();
605
606     $result = array();
607     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
608     $res = $mdb2->query($sql);
609     $result = array();
610     if (!is_a($res, 'PEAR_Error')) {
611       while ($val = $res->fetchRow()) {
612         $result[] = $val;
613       }
614       return $result;
615     }
616     return false;
617   }
618
619   // getExpenseItems - obtains all expense items for all users in a group.
620   static function getExpenseItems($group_id) {
621     $mdb2 = getConnection();
622
623     $result = array();
624     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
625     $res = $mdb2->query($sql);
626     $result = array();
627     if (!is_a($res, 'PEAR_Error')) {
628       while ($val = $res->fetchRow()) {
629         $result[] = $val;
630       }
631       return $result;
632     }
633     return false;
634   }
635
636   // getPredefinedExpenses - obtains predefined expenses for a group.
637   static function getPredefinedExpenses($group_id) {
638     global $user;
639     $replaceDecimalMark = ('.' != $user->decimal_mark);
640
641     $mdb2 = getConnection();
642
643     $result = array();
644     $sql = "select id, name, cost from tt_predefined_expenses where group_id = $group_id";
645     $res = $mdb2->query($sql);
646     $result = array();
647     if (!is_a($res, 'PEAR_Error')) {
648       while ($val = $res->fetchRow()) {
649         if ($replaceDecimalMark)
650           $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
651         $result[] = $val;
652       }
653       return $result;
654     }
655     return false;
656   }
657
658   // getNotifications - obtains notification descriptions for a group.
659   static function getNotifications($group_id) {
660     $mdb2 = getConnection();
661
662     $result = array();
663     $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
664       left join tt_fav_reports fr on (fr.id = c.report_id)
665       where c.group_id = $group_id and c.status = 1 and fr.status = 1";
666     $res = $mdb2->query($sql);
667     $result = array();
668     if (!is_a($res, 'PEAR_Error')) {
669       while ($val = $res->fetchRow()) {
670         $result[] = $val;
671       }
672       return $result;
673     }
674     return false;
675   }
676
677   // getMonthlyQuotas - obtains monthly quotas for a group.
678   static function getMonthlyQuotas($group_id) {
679     $mdb2 = getConnection();
680
681     $result = array();
682     $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
683     $res = $mdb2->query($sql);
684     $result = array();
685     if (!is_a($res, 'PEAR_Error')) {
686       while ($val = $res->fetchRow()) {
687         $result[] = $val;
688       }
689       return $result;
690     }
691     return false;
692   }
693
694   // The insert function creates a new group.
695   static function insert($fields) {
696
697     global $user;
698     $mdb2 = getConnection();
699
700     // Start with group name and currency.
701     $columns = 'name, currency';
702     $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
703
704     if ($fields['decimal_mark']) {
705       $columns .= ', decimal_mark';
706       $values .= ', '.$mdb2->quote($fields['decimal_mark']);
707     }
708
709     $lang = $fields['lang'];
710     if (!$lang) {
711       global $i18n;
712       $lang = $i18n->lang;
713     }
714     $columns .= ', lang';
715     $values .= ', '.$mdb2->quote($lang);
716
717     if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
718       $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
719       $columns .= ', date_format';
720       $values .= ', '.$mdb2->quote($date_format);
721     }
722
723     if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
724       $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
725       $columns .= ', time_format';
726       $values .= ', '.$mdb2->quote($time_format);
727     }
728
729     if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
730       $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
731       $columns .= ', week_start';
732       $values .= ', '.(int)$week_start;
733     }
734
735     if ($fields['tracking_mode']) {
736       $columns .= ', tracking_mode';
737       $values .= ', '.(int)$fields['tracking_mode'];
738     }
739
740     if ($fields['project_required']) {
741       $columns .= ', project_required';
742       $values .= ', '.(int)$fields['project_required'];
743     }
744
745     if ($fields['task_required']) {
746       $columns .= ', task_required';
747       $values .= ', '.(int)$fields['task_required'];
748     }
749
750     if ($fields['record_type']) {
751       $columns .= ', record_type';
752       $values .= ', '.(int)$fields['record_type'];
753     }
754
755     if ($fields['bcc_email']) {
756       $columns .= ', bcc_email';
757       $values .= ', '.$mdb2->quote($fields['bcc_email']);
758     }
759
760     if ($fields['plugins']) {
761       $columns .= ', plugins';
762       $values .= ', '.$mdb2->quote($fields['plugins']);
763     }
764
765     if ($fields['lock_spec']) {
766       $columns .= ', lock_spec';
767       $values .= ', '.$mdb2->quote($fields['lock_spec']);
768     }
769
770     if ($fields['workday_minutes']) {
771       $columns .= ', workday_minutes';
772       $values .= ', '.(int)$fields['workday_minutes'];
773     }
774
775     if ($fields['config']) {
776       $columns .= ', config';
777       $values .= ', '.$mdb2->quote($fields['config']);
778     }
779
780     $columns .= ', created, created_ip, created_by';
781     $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
782
783     $sql = "insert into tt_groups ($columns) values($values)";
784     $affected = $mdb2->exec($sql);
785
786     if (!is_a($affected, 'PEAR_Error')) {
787       $group_id = $mdb2->lastInsertID('tt_groups', 'id');
788       return $group_id;
789     }
790
791     return false;
792   }
793
794   // The getInactiveGroups is a maintenance function that returns an array of inactive group ids (max 100).
795   static function getInactiveGroups() {
796     $inactive_groups = array();
797     $mdb2 = getConnection();
798
799     // Get all group ids for groups created or modified more than 8 months ago.
800     // $ts = date('Y-m-d', strtotime('-1 year'));
801     $ts = $mdb2->quote(date('Y-m-d', strtotime('-8 month')));
802     $sql =  "select id from tt_groups where created < $ts and (modified is null or modified < $ts) order by id";
803     $res = $mdb2->query($sql);
804
805     $count = 0;
806     if (!is_a($res, 'PEAR_Error')) {
807       while ($val = $res->fetchRow()) {
808         $group_id = $val['id'];
809         if (ttTeamHelper::isGroupActive($group_id) == false) {
810           $count++;
811           $inactive_groups[] = $group_id;
812           // Limit the array size for perfomance by allowing this operation on small chunks only.
813           if ($count >= 100) break;
814         }
815       }
816       return $inactive_groups;
817     }
818     return false;
819   }
820
821   // The isGroupActive determines if a group is using Time Tracker or abandoned it.
822   static function isGroupActive($group_id) {
823     $users = array();
824
825     $mdb2 = getConnection();
826     $sql = "select id from tt_users where group_id = $group_id";
827     $res = $mdb2->query($sql);
828     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
829     while ($val = $res->fetchRow()) {
830       $users[] = $val['id'];
831     }
832     $user_list = implode(',', $users); // This is a comma-separated list of user ids.
833     if (!$user_list)
834       return false; // No users in group.
835
836     $count = 0;
837     $ts = date('Y-m-d', strtotime('-2 years'));
838     $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
839     $res = $mdb2->query($sql);
840     if (!is_a($res, 'PEAR_Error')) {
841       if ($val = $res->fetchRow()) {
842         $count = $val['cnt'];
843       }
844     }
845
846     if ($count == 0)
847       return false;  // No time entries for the last 2 years.
848
849     if ($count <= 5) {
850       // We will consider a group inactive if it has 5 or less time entries made more than 1 year ago.
851       $count_last_year = 0;
852       $ts = date('Y-m-d', strtotime('-1 year'));
853       $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
854       $res = $mdb2->query($sql);
855       if (!is_a($res, 'PEAR_Error')) {
856         if ($val = $res->fetchRow()) {
857           $count_last_year = $val['cnt'];
858         }
859         if ($count_last_year == 0)
860           return false;  // No time entries for the last year and only a few entries before that.
861       }
862     }
863     return true;
864   }
865
866   // The delete function permanently deletes all data for a group.
867   static function delete($group_id) {
868     $mdb2 = getConnection();
869
870     // Delete users.
871     $sql = "select id from tt_users where group_id = $group_id";
872     $res = $mdb2->query($sql);
873     if (is_a($res, 'PEAR_Error')) return false;
874     while ($val = $res->fetchRow()) {
875       $user_id = $val['id'];
876       if (!ttUserHelper::delete($user_id)) return false;
877     }
878
879     // Delete tasks.
880     if (!ttTeamHelper::deleteTasks($group_id)) return false;
881
882     // Delete client to project binds.
883     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
884     $affected = $mdb2->exec($sql);
885     if (is_a($affected, 'PEAR_Error')) return false;
886
887     // Delete projects.
888     $sql = "delete from tt_projects where group_id = $group_id";
889     $affected = $mdb2->exec($sql);
890     if (is_a($affected, 'PEAR_Error')) return false;
891
892     // Delete clients.
893     $sql = "delete from tt_clients where group_id = $group_id";
894     $affected = $mdb2->exec($sql);
895     if (is_a($affected, 'PEAR_Error')) return false;
896
897     // Delete invoices.
898     $sql = "delete from tt_invoices where group_id = $group_id";
899     $affected = $mdb2->exec($sql);
900     if (is_a($affected, 'PEAR_Error')) return false;
901
902     // Delete custom fields.
903     if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
904
905     // Delete roles.
906     $sql = "delete from tt_roles where group_id = $group_id";
907     $affected = $mdb2->exec($sql);
908     if (is_a($affected, 'PEAR_Error')) return false;
909
910     // Delete group.
911     $sql = "delete from tt_groups where id = $group_id";
912     $affected = $mdb2->exec($sql);
913     if (is_a($affected, 'PEAR_Error')) return false;
914
915     return true;
916   }
917
918   // The markTasksDeleted deletes task binds and marks the tasks as deleted for a group.
919   static function markTasksDeleted($group_id) {
920     $mdb2 = getConnection();
921     $sql = "select id from tt_tasks where group_id = $group_id";
922     $res = $mdb2->query($sql);
923     if (is_a($res, 'PEAR_Error')) return false;
924
925     while ($val = $res->fetchRow()) {
926
927       // Delete task binds.
928       $task_id = $val['id'];
929       $sql = "delete from tt_project_task_binds where task_id = $task_id";
930       $affected = $mdb2->exec($sql);
931       if (is_a($affected, 'PEAR_Error')) return false;
932
933       // Mark task as deleted.
934       $sql = "update tt_tasks set status = NULL where id = $task_id";
935       $affected = $mdb2->exec($sql);
936       if (is_a($affected, 'PEAR_Error')) return false;
937     }
938
939     return true;
940   }
941
942   // The deleteTasks deletes all tasks and task binds for an inactive group.
943   static function deleteTasks($group_id) {
944     $mdb2 = getConnection();
945     $sql = "select id from tt_tasks where group_id = $group_id";
946     $res = $mdb2->query($sql);
947     if (is_a($res, 'PEAR_Error')) return false;
948
949     while ($val = $res->fetchRow()) {
950
951       // Delete task binds.
952       $task_id = $val['id'];
953       $sql = "delete from tt_project_task_binds where task_id = $task_id";
954       $affected = $mdb2->exec($sql);
955       if (is_a($affected, 'PEAR_Error')) return false;
956
957       // Delete task.
958       $sql = "delete from tt_tasks where id = $task_id";
959       $affected = $mdb2->exec($sql);
960       if (is_a($affected, 'PEAR_Error')) return false;
961     }
962
963     return true;
964   }
965
966   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
967   static function deleteCustomFields($group_id) {
968     $mdb2 = getConnection();
969     $sql = "select id from tt_custom_fields where group_id = $group_id";
970     $res = $mdb2->query($sql);
971     if (is_a($res, 'PEAR_Error')) return false;
972
973     while ($val = $res->fetchRow()) {
974       $field_id = $val['id'];
975
976       // Clean up tt_custom_field_log.
977       $sql = "delete from tt_custom_field_log where field_id = $field_id";
978       $affected = $mdb2->exec($sql);
979       if (is_a($affected, 'PEAR_Error')) return false;
980
981       // Clean up tt_custom_field_options.
982       $sql = "delete from tt_custom_field_options where field_id = $field_id";
983       $affected = $mdb2->exec($sql);
984       if (is_a($affected, 'PEAR_Error')) return false;
985
986       // Delete custom field.
987       $sql = "delete from tt_custom_fields where id = $field_id";
988       $affected = $mdb2->exec($sql);
989       if (is_a($affected, 'PEAR_Error')) return false;
990     }
991
992     return true;
993   }
994 }