updated according to comments from anuko
[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
32 // Class ttTeamHelper - contains helper functions that operate with teams.
33 class ttTeamHelper {
34
35   // The getUserCount function returns number of people in team.
36   static function getUserCount($team_id) {
37     $mdb2 = getConnection();
38
39     $sql = "select count(id) as cnt from tt_users where team_id = $team_id and status = 1";
40     $res = $mdb2->query($sql);
41
42     if (!is_a($res, 'PEAR_Error')) {
43       $val = $res->fetchRow();
44       return $val['cnt'];
45     }
46     return false;
47   }
48
49   // The getUsersForClient obtains all active and inactive users in a team that are relevant to a client.
50   static function getUsersForClient() {
51     global $user;
52     $mdb2 = getConnection();
53
54     $sql = "select u.id, u.name from tt_user_project_binds upb
55       inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)
56       inner join tt_users u on (u.id = upb.user_id)
57       where (u.status = 1 or u.status = 0)
58       group by u.id
59       order by u.name";
60     $res = $mdb2->query($sql);
61     $user_list = array();
62     if (is_a($res, 'PEAR_Error'))
63       return false;
64     while ($val = $res->fetchRow()) {
65       $user_list[] = $val;
66     }
67     return $user_list;
68   }
69
70   // The getActiveUsers obtains all active users in a given team.
71   static function getActiveUsers($options = null) {
72     global $user;
73     $mdb2 = getConnection();
74
75     if (isset($options['getAllFields']))
76       $sql = "select * from tt_users where team_id = $user->team_id and status = 1 order by name";
77     else
78       $sql = "select id, name from tt_users where team_id = $user->team_id and status = 1 order by name";
79     $res = $mdb2->query($sql);
80     $user_list = array();
81     if (is_a($res, 'PEAR_Error'))
82       return false;
83     while ($val = $res->fetchRow()) {
84       $user_list[] = $val;
85     }
86
87     if (isset($options['putSelfFirst'])) {
88       // Put own entry at the front.
89       $cnt = count($user_list);
90       for($i = 0; $i < $cnt; $i++) {
91         if ($user_list[$i]['id'] == $user->id) {
92           $self = $user_list[$i]; // Found self.
93           array_unshift($user_list, $self); // Put own entry at the front.
94           array_splice($user_list, $i+1, 1); // Remove duplicate.
95         }
96       }
97     }
98     return $user_list;
99   }
100
101   // The getUsers obtains all active and inactive (but not deleted) users in a given team.
102   static function getUsers() {
103     global $user;
104     $mdb2 = getConnection();
105
106     $sql = "select id, name from tt_users where team_id = $user->team_id and (status = 1 or status = 0) order by name";
107     $res = $mdb2->query($sql);
108     $user_list = array();
109     if (is_a($res, 'PEAR_Error'))
110       return false;
111     while ($val = $res->fetchRow()) {
112       $user_list[] = $val;
113     }
114
115     return $user_list;
116   }
117
118   // The getInactiveUsers obtains all inactive users in a given team.
119   static function getInactiveUsers($team_id, $all_fields = false) {
120     $mdb2 = getConnection();
121
122     if ($all_fields)
123       $sql = "select * from tt_users where team_id = $team_id and status = 0 order by name";
124     else
125       $sql = "select id, name from tt_users where team_id = $team_id and status = 0 order by name";
126     $res = $mdb2->query($sql);
127     $result = array();
128     if (!is_a($res, 'PEAR_Error')) {
129       while ($val = $res->fetchRow()) {
130         $result[] = $val;
131       }
132       return $result;
133     }
134     return false;
135   }
136
137   // The getAllUsers obtains all users in a given team.
138   static function getAllUsers($team_id, $all_fields = false) {
139     $mdb2 = getConnection();
140
141     if ($all_fields)
142       $sql = "select * from tt_users where team_id = $team_id order by name";
143     else
144       $sql = "select id, name from tt_users where team_id = $team_id order by name";
145     $res = $mdb2->query($sql);
146     $result = array();
147     if (!is_a($res, 'PEAR_Error')) {
148       while ($val = $res->fetchRow()) {
149         $result[] = $val;
150       }
151       return $result;
152     }
153     return false;
154   }
155
156   // getActiveProjects - returns an array of active projects for team.
157   static function getActiveProjects($team_id)
158   {
159     $result = array();
160     $mdb2 = getConnection();
161
162     $sql = "select id, name, description, tasks from tt_projects
163       where team_id = $team_id and status = 1 order by name";
164     $res = $mdb2->query($sql);
165     $result = array();
166     if (!is_a($res, 'PEAR_Error')) {
167       while ($val = $res->fetchRow()) {
168         $result[] = $val;
169       }
170     }
171     return $result;
172   }
173
174   // getInactiveProjects - returns an array of inactive projects for team.
175   static function getInactiveProjects($team_id)
176   {
177     $result = array();
178     $mdb2 = getConnection();
179
180     $sql = "select id, name, description, tasks from tt_projects
181       where team_id = $team_id and status = 0 order by name";
182     $res = $mdb2->query($sql);
183     $result = array();
184     if (!is_a($res, 'PEAR_Error')) {
185       while ($val = $res->fetchRow()) {
186         $result[] = $val;
187       }
188     }
189     return $result;
190   }
191
192   // The getAllProjects obtains all projects in a given team.
193   static function getAllProjects($team_id, $all_fields = false) {
194     $mdb2 = getConnection();
195
196     if ($all_fields)
197       $sql = "select * from tt_projects where team_id = $team_id order by status, name";
198     else
199       $sql = "select id, name from tt_projects where team_id = $team_id order by status, name";
200     $res = $mdb2->query($sql);
201     $result = array();
202     if (!is_a($res, 'PEAR_Error')) {
203       while ($val = $res->fetchRow()) {
204         $result[] = $val;
205       }
206       return $result;
207     }
208     return false;
209   }
210
211   // getActiveTasks - returns an array of active tasks for team.
212   static function getActiveTasks($team_id)
213   {
214     $result = array();
215     $mdb2 = getConnection();
216
217     $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by name";
218     $res = $mdb2->query($sql);
219     $result = array();
220     if (!is_a($res, 'PEAR_Error')) {
221       while ($val = $res->fetchRow()) {
222         $result[] = $val;
223       }
224     }
225     return $result;
226   }
227
228   // getInactiveTasks - returns an array of inactive tasks for team.
229   static function getInactiveTasks($team_id)
230   {
231     $result = array();
232     $mdb2 = getConnection();
233
234     $sql = "select id, name, description from tt_tasks
235       where team_id = $team_id and status = 0 order by name";
236     $res = $mdb2->query($sql);
237     $result = array();
238     if (!is_a($res, 'PEAR_Error')) {
239       while ($val = $res->fetchRow()) {
240         $result[] = $val;
241       }
242     }
243     return $result;
244   }
245
246   // The getAllTasks obtains all tasks in a given team.
247   static function getAllTasks($team_id, $all_fields = false) {
248     $mdb2 = getConnection();
249
250     if ($all_fields)
251       $sql = "select * from tt_tasks where team_id = $team_id order by status, name";
252     else
253       $sql = "select id, name from tt_tasks where team_id = $team_id order by status, name";
254     $res = $mdb2->query($sql);
255     $result = array();
256     if (!is_a($res, 'PEAR_Error')) {
257       while ($val = $res->fetchRow()) {
258         $result[] = $val;
259       }
260       return $result;
261     }
262     return false;
263   }
264
265   // The getActiveClients returns an array of active clients for team.
266   static function getActiveClients($team_id, $all_fields = false)
267   {
268     $result = array();
269     $mdb2 = getConnection();
270
271     if ($all_fields)
272       $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by name";
273     else
274       $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by name";
275
276     $res = $mdb2->query($sql);
277     $result = array();
278     if (!is_a($res, 'PEAR_Error')) {
279       while ($val = $res->fetchRow()) {
280         $result[] = $val;
281       }
282     }
283     return $result;
284   }
285
286   // The getInactiveClients returns an array of inactive clients for team.
287   static function getInactiveClients($team_id, $all_fields = false)
288   {
289     $result = array();
290     $mdb2 = getConnection();
291
292     if ($all_fields)
293       $sql = "select * from tt_clients where team_id = $team_id and status = 0 order by name";
294     else
295       $sql = "select id, name from tt_clients where team_id = $team_id and status = 0 order by name";
296
297     $res = $mdb2->query($sql);
298     $result = array();
299     if (!is_a($res, 'PEAR_Error')) {
300       while ($val = $res->fetchRow()) {
301         $result[] = $val;
302       }
303     }
304     return $result;
305   }
306
307   // The getAllClients obtains all clients in a given team.
308   static function getAllClients($team_id, $all_fields = false) {
309     $mdb2 = getConnection();
310
311     if ($all_fields)
312       $sql = "select * from tt_clients where team_id = $team_id order by status, name";
313     else
314       $sql = "select id, name from tt_clients where team_id = $team_id order by status, name";
315
316     $res = $mdb2->query($sql);
317     $result = array();
318     if (!is_a($res, 'PEAR_Error')) {
319       while ($val = $res->fetchRow()) {
320         $result[] = $val;
321       }
322       return $result;
323     }
324     return false;
325   }
326
327   // The getActiveInvoices returns an array of active invoices for team.
328   static function getActiveInvoices($localizeDates = true)
329   {
330     global $user;
331
332     $result = array();
333     $mdb2 = getConnection();
334
335     if (ROLE_CLIENT == $user->role && $user->client_id)
336       $client_part = " and i.client_id = $user->client_id";
337
338     $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
339       left join tt_clients c on (c.id = i.client_id)
340       where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
341     $res = $mdb2->query($sql);
342     $result = array();
343     if (!is_a($res, 'PEAR_Error')) {
344       $dt = new DateAndTime(DB_DATEFORMAT);
345       while ($val = $res->fetchRow()) {
346         if ($localizeDates) {
347           $dt->parseVal($val['date']);
348           $val['date'] = $dt->toString($user->date_format);
349         }
350         $result[] = $val;
351       }
352     }
353     return $result;
354   }
355
356   // The getAllInvoices returns an array of all invoices for team.
357   static function getAllInvoices()
358   {
359     global $user;
360
361     $result = array();
362     $mdb2 = getConnection();
363
364     $sql = "select * from tt_invoices where team_id = $user->team_id";
365     $res = $mdb2->query($sql);
366     $result = array();
367     if (!is_a($res, 'PEAR_Error')) {
368       $dt = new DateAndTime(DB_DATEFORMAT);
369       while ($val = $res->fetchRow()) {
370         $result[] = $val;
371       }
372     }
373     return $result;
374   }
375
376   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
377   static function getRecentInvoices($team_id, $client_id)
378   {
379     global $user;
380
381     $result = array();
382     $mdb2 = getConnection();
383
384     $sql = "select i.id, i.name from tt_invoices i
385       left join tt_clients c on (c.id = i.client_id)
386       where i.team_id = $team_id and i.status = 1 and c.id = $client_id
387       order by i.id desc limit 3";
388     $res = $mdb2->query($sql);
389     $result = array();
390     if (!is_a($res, 'PEAR_Error')) {
391       $dt = new DateAndTime(DB_DATEFORMAT);
392       while ($val = $res->fetchRow()) {
393         $result[] = $val;
394       }
395     }
396     return $result;
397   }
398
399   // getUserToProjectBinds - obtains all user to project binds for a team.
400   static function getUserToProjectBinds($team_id) {
401     $mdb2 = getConnection();
402
403     $result = array();
404     $sql = "select * from tt_user_project_binds where user_id in (select id from tt_users where team_id = $team_id) order by user_id, status, project_id";
405     $res = $mdb2->query($sql);
406     $result = array();
407     if (!is_a($res, 'PEAR_Error')) {
408       while ($val = $res->fetchRow()) {
409         $result[] = $val;
410       }
411       return $result;
412     }
413     return false;
414   }
415
416   // The getAllCustomFields obtains all custom fields in a given team.
417   static function getAllCustomFields($team_id) {
418     $mdb2 = getConnection();
419
420     $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
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 getAllCustomFieldOptions obtains all custom field options in a given team.
434   static function getAllCustomFieldOptions($team_id) {
435     $mdb2 = getConnection();
436
437     $sql = "select * from tt_custom_field_options where field_id in (select id from tt_custom_fields where team_id = $team_id) order by id";
438
439     $res = $mdb2->query($sql);
440     $result = array();
441     if (!is_a($res, 'PEAR_Error')) {
442       while ($val = $res->fetchRow()) {
443         $result[] = $val;
444       }
445       return $result;
446     }
447     return false;
448   }
449
450   // The getCustomFieldLog obtains all custom field log entries for a given team.
451   static function getCustomFieldLog($team_id) {
452     $mdb2 = getConnection();
453
454     $sql = "select * from tt_custom_field_log where field_id in (select id from tt_custom_fields where team_id = $team_id) order by id";
455
456     $res = $mdb2->query($sql);
457     $result = array();
458     if (!is_a($res, 'PEAR_Error')) {
459       while ($val = $res->fetchRow()) {
460         $result[] = $val;
461       }
462       return $result;
463     }
464     return false;
465   }
466
467   // getFavReports - obtains all favorite reports for all users in team.
468   static function getFavReports($team_id) {
469     $mdb2 = getConnection();
470
471     $result = array();
472     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
473     $res = $mdb2->query($sql);
474     $result = array();
475     if (!is_a($res, 'PEAR_Error')) {
476       while ($val = $res->fetchRow()) {
477         $result[] = $val;
478       }
479       return $result;
480     }
481     return false;
482   }
483
484   // getExpenseItems - obtains all expense items for all users in team.
485   static function getExpenseItems($team_id) {
486     $mdb2 = getConnection();
487
488     $result = array();
489     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
490     $res = $mdb2->query($sql);
491     $result = array();
492     if (!is_a($res, 'PEAR_Error')) {
493       while ($val = $res->fetchRow()) {
494         $result[] = $val;
495       }
496       return $result;
497     }
498     return false;
499   }
500
501   // getNotifications - obtains notification descriptions for team.
502   static function getNotifications($team_id) {
503     $mdb2 = getConnection();
504
505     $result = array();
506     $sql = "select c.id, c.cron_spec, c.email, fr.name from tt_cron c
507       left join tt_fav_reports fr on (fr.id = c.report_id)
508       where c.team_id = $team_id and c.status is not null";
509     $res = $mdb2->query($sql);
510     $result = array();
511     if (!is_a($res, 'PEAR_Error')) {
512       while ($val = $res->fetchRow()) {
513         $result[] = $val;
514       }
515       return $result;
516     }
517     return false;
518   }
519
520   // The getTeams function returns an array of all active teams on the server.
521   static function getTeams() {
522     $result = array();
523     $mdb2 = getConnection();
524
525     $sql =  "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
526     $res = $mdb2->query($sql);
527     $result = array();
528     if (!is_a($res, 'PEAR_Error')) {
529       while ($val = $res->fetchRow()) {
530         $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
531         $result[] = $val;
532       }
533       return $result;
534     }
535     return false;
536   }
537
538   // The markDeleted function marks the team and everything in it as deleted.
539   static function markDeleted($team_id) {
540
541     // Iterate through team users and mark them as deleted.
542     $users = ttTeamHelper::getAllUsers($team_id);
543     foreach ($users as $one_user) {
544       if (!ttUserHelper::markDeleted($one_user['id'])) return false;
545     }
546
547     // Mark tasks deleted.
548     if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
549
550     $mdb2 = getConnection();
551
552     // Mark projects deleted.
553     $sql = "update tt_projects set status = NULL where team_id = $team_id";
554     $affected = $mdb2->exec($sql);
555     if (is_a($affected, 'PEAR_Error')) return false;
556
557     // Mark clients deleted.
558     $sql = "update tt_clients set status = NULL where team_id = $team_id";
559     $affected = $mdb2->exec($sql);
560     if (is_a($affected, 'PEAR_Error')) return false;
561
562     // Mark custom fields deleted.
563     $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
564     $affected = $mdb2->exec($sql);
565     if (is_a($affected, 'PEAR_Error')) return false;
566
567     // Mark team deleted.
568     $sql = "update tt_teams set status = NULL where id = $team_id";
569     $affected = $mdb2->exec($sql);
570     if (is_a($affected, 'PEAR_Error')) return false;
571
572     return true;
573   }
574
575   // The getTeamDetails function returns team details.
576   static function getTeamDetails($team_id) {
577     $result = array();
578     $mdb2 = getConnection();
579
580     $role_manager = ROLE_MANAGER;
581     $sql = "select t.name as team_name, u.id as manager_id, u.name as manager_name, u.login as manager_login, u.email as manager_email
582       from tt_teams t
583       inner join tt_users u on (u.team_id = t.id and u.role = $role_manager)
584       where t.id = $team_id";
585
586     $res = $mdb2->query($sql);
587     if (!is_a($res, 'PEAR_Error')) {
588       $val = $res->fetchRow();
589       return $val;
590     }
591
592     return false;
593   }
594
595   // The insert function creates a new team.
596   static function insert($fields) {
597
598     $mdb2 = getConnection();
599
600     $lock_spec = $fields['lock_spec'];
601     if ($lock_spec !== null) {
602       $lockspec_f = ', lock_spec';
603       $lockspec_v = ', ' . $mdb2->quote($lock_spec);
604     } else {
605       $lockspec_f = '';
606       $lockspec_v = '';
607     }
608
609     $lang = $fields['lang'];
610     if (!$lang) {
611       global $i18n;
612       $lang = $i18n->lang;
613     }
614
615     $decimal_mark = $fields['decimal_mark'];
616     if ($decimal_mark !== null) {
617       $decimal_mark_f = ', decimal_mark';
618       $decimal_mark_v = ', ' . $mdb2->quote($decimal_mark);
619     } else {
620       $decimal_mark_f = '';
621       $decimal_mark_v = '';
622     }
623
624     $date_format = $fields['date_format'];
625     if ($date_format !== null) {
626       $date_format_f = ', date_format';
627       $date_format_v = ', ' . $mdb2->quote($date_format);
628     } elseif (defined('DATE_FORMAT_DEFAULT')) {
629       $date_format_f = ', date_format';
630       $date_format_v = ', ' . $mdb2->quote(DATE_FORMAT_DEFAULT);
631     } else {
632       $date_format_f = '';
633       $date_format_v = '';
634     }
635
636     $time_format = $fields['time_format'];
637     if ($time_format !== null) {
638       $time_format_f = ', time_format';
639       $time_format_v = ', ' . $mdb2->quote($time_format);
640     } elseif (defined('TIME_FORMAT_DEFAULT')) {
641       $time_format_f = ', time_format';
642       $time_format_v = ', ' . $mdb2->quote(TIME_FORMAT_DEFAULT);
643     } else {
644       $time_format_f = '';
645       $time_format_v = '';
646     }
647
648     $week_start = $fields['week_start'];
649     if ($week_start !== null) {
650       $week_start_f = ', week_start';
651       $week_start_v = ', ' . (int)$week_start;
652     } elseif (defined('WEEK_START_DEFAULT')) {
653       $week_start_f = ', week_start';
654       $week_start_v = ', ' . (int)WEEK_START_DEFAULT;
655     } else {
656       $week_start_f = '';
657       $week_start_v = '';
658     }
659
660     $plugins = $fields['plugins'];
661     if ($plugins !== null) {
662       $plugins_f = ', plugins';
663       $plugins_v = ', ' . $mdb2->quote($plugins);
664     } else {
665       $plugins_f = '';
666       $plugins_v = '';
667     }
668
669     $tracking_mode = $fields['tracking_mode'];
670     if ($tracking_mode !== null) {
671       $tracking_mode_f = ', tracking_mode';
672       $tracking_mode_v = ', ' . (int)$tracking_mode;
673     } else {
674       $tracking_mode_f = '';
675       $tracking_mode_v = '';
676     }
677
678     $record_type = $fields['record_type'];
679     if ($record_type !== null) {
680       $record_type_f = ', record_type';
681       $record_type_v = ', ' . (int)$record_type;
682     } else {
683       $record_type_f = '';
684       $record_type_v = '';
685     }
686
687     $sql = "insert into tt_teams (name, address, currency $lockspec_f, lang $decimal_mark_f $date_format_f $time_format_f $week_start_f $plugins_f $tracking_mode_f $record_type_f)
688       values(".$mdb2->quote(trim($fields['name'])).
689       ", ".$mdb2->quote(trim($fields['address'])).
690       ", ".$mdb2->quote(trim($fields['currency']))." $lockspec_v, ".$mdb2->quote($lang).
691       "$decimal_mark_v $date_format_v $time_format_v $week_start_v $plugins_v $tracking_mode_v $record_type_v)";
692     $affected = $mdb2->exec($sql);
693
694     if (!is_a($affected, 'PEAR_Error')) {
695       $team_id = $mdb2->lastInsertID('tt_teams', 'id');
696       return $team_id;
697     }
698
699     return false;
700   }
701
702   // The update function updates team information.
703   static function update($team_id, $fields)
704   {
705     // We'll require team name to be always set.
706     if (!isset($fields['name']) || $fields['name'] == "") return false;
707
708     $mdb2 = getConnection();
709     $name_part = 'name = '.$mdb2->quote($fields['name']);
710     $currency_part = '';
711     $addr_part = '';
712     $lang_part = '';
713     $decimal_mark_part = '';
714     $date_format_part = '';
715     $time_format_part = '';
716     $week_start_part = '';
717     $tracking_mode_part = '';
718     $record_type_part = '';
719     $plugins_part = '';
720     $lock_spec_part = '';
721     $working_hours_part = '';
722
723     if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
724     if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
725     if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
726     if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
727     if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
728     if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
729     if (isset($fields['week_start'])) $week_start_part = ', week_start = '.intval($fields['week_start']);
730     if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.intval($fields['tracking_mode']);
731     if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
732     if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
733     if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
734     if (isset($fields['working_hours'])) $working_hours_part = ', daily_working_hours = '.$mdb2->quote($fields['working_hours']);
735
736     $sql = "update tt_teams set $name_part $addr_part $currency_part $lang_part $decimal_mark_part
737       $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part
738       $plugins_part $lock_spec_part $working_hours_part where id = $team_id";
739     $affected = $mdb2->exec($sql);
740     if (is_a($affected, 'PEAR_Error')) return false;
741
742     return true;
743   }
744
745   // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 25).
746   static function getInactiveTeams() {
747     $inactive_teams = array();
748     $mdb2 = getConnection();
749
750     // Get all team ids for teams created or modified more than 1 year ago.
751     $ts = date('Y-m-d', strtotime('-1 year'));
752     $sql =  "select id from tt_teams where timestamp < '$ts' order by id";
753     $res = $mdb2->query($sql);
754
755     $count = 0;
756     if (!is_a($res, 'PEAR_Error')) {
757       while ($val = $res->fetchRow()) {
758         $team_id = $val['id'];
759         if (ttTeamHelper::isTeamActive($team_id) == false) {
760           $count++;
761           $inactive_teams[] = $team_id;
762           // Limit the array size for perfomance by allowing this operation on small chunks only.
763           if ($count >= 25) break;
764         }
765       }
766       return $inactive_teams;
767     }
768     return false;
769   }
770
771   // The isTeamActive determines if a team is using Time Tracker or abandoned it.
772   static function isTeamActive($team_id) {
773     $users = array();
774
775     $mdb2 = getConnection();
776     $sql = "select id from tt_users where team_id = $team_id";
777     $res = $mdb2->query($sql);
778     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
779     while ($val = $res->fetchRow()) {
780       $users[] = $val['id'];
781     }
782     $user_list = implode(',', $users); // This is a comma-separated list of user ids.
783     if (!$user_list)
784       return false; // No users in team.
785
786     $count = 0;
787     $ts = date('Y-m-d', strtotime('-2 years'));
788     $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
789     $res = $mdb2->query($sql);
790     if (!is_a($res, 'PEAR_Error')) {
791       if ($val = $res->fetchRow()) {
792         $count = $val['cnt'];
793       }
794     }
795
796     if ($count == 0)
797       return false;  // No time entries for the last 2 years.
798
799     if ($count <= 5) {
800       // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
801       $count_last_year = 0;
802       $ts = date('Y-m-d', strtotime('-1 year'));
803       $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
804       $res = $mdb2->query($sql);
805       if (!is_a($res, 'PEAR_Error')) {
806         if ($val = $res->fetchRow()) {
807           $count_last_year = $val['cnt'];
808         }
809         if ($count_last_year == 0)
810           return false;  // No time entries for the last year and only a few entries before that.
811       }
812     }
813     return true;
814   }
815
816   // The delete function permanently deletes all data for a team.
817   static function delete($team_id) {
818     $mdb2 = getConnection();
819
820     // Delete users.
821     $sql = "select id from tt_users where team_id = $team_id";
822     $res = $mdb2->query($sql);
823     if (is_a($res, 'PEAR_Error')) return false;
824     while ($val = $res->fetchRow()) {
825       $user_id = $val['id'];
826       if (!ttUserHelper::delete($user_id)) return false;
827     }
828
829     // Delete tasks.
830     if (!ttTeamHelper::deleteTasks($team_id)) return false;
831
832     // Delete client to project binds.
833     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
834     $affected = $mdb2->exec($sql);
835     if (is_a($affected, 'PEAR_Error')) return false;
836
837     // Delete projects.
838     $sql = "delete from tt_projects where team_id = $team_id";
839     $affected = $mdb2->exec($sql);
840     if (is_a($affected, 'PEAR_Error')) return false;
841
842     // Delete clients.
843     $sql = "delete from tt_clients where team_id = $team_id";
844     $affected = $mdb2->exec($sql);
845     if (is_a($affected, 'PEAR_Error')) return false;
846
847     // Delete invoices.
848     $sql = "delete from tt_invoices where team_id = $team_id";
849     $affected = $mdb2->exec($sql);
850     if (is_a($affected, 'PEAR_Error')) return false;
851
852     // Delete custom fields.
853     if (!ttTeamHelper::deleteCustomFields($team_id)) return false;
854
855     // Delete team.
856     $sql = "delete from tt_teams where id = $team_id";
857     $affected = $mdb2->exec($sql);
858     if (is_a($affected, 'PEAR_Error')) return false;
859
860     return true;
861   }
862
863   // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
864   static function markTasksDeleted($team_id) {
865     $mdb2 = getConnection();
866     $sql = "select id from tt_tasks where team_id = $team_id";
867     $res = $mdb2->query($sql);
868     if (is_a($res, 'PEAR_Error')) return false;
869
870     while ($val = $res->fetchRow()) {
871
872       // Delete task binds.
873       $task_id = $val['id'];
874       $sql = "delete from tt_project_task_binds where task_id = $task_id";
875       $affected = $mdb2->exec($sql);
876       if (is_a($affected, 'PEAR_Error')) return false;
877
878       // Mark task as deleted.
879       $sql = "update tt_tasks set status = NULL where id = $task_id";
880       $affected = $mdb2->exec($sql);
881       if (is_a($affected, 'PEAR_Error')) return false;
882     }
883
884     return true;
885   }
886
887   // The deleteTasks deletes all tasks and task binds for an inactive team.
888   static function deleteTasks($team_id) {
889     $mdb2 = getConnection();
890     $sql = "select id from tt_tasks where team_id = $team_id";
891     $res = $mdb2->query($sql);
892     if (is_a($res, 'PEAR_Error')) return false;
893
894     while ($val = $res->fetchRow()) {
895
896       // Delete task binds.
897       $task_id = $val['id'];
898       $sql = "delete from tt_project_task_binds where task_id = $task_id";
899       $affected = $mdb2->exec($sql);
900       if (is_a($affected, 'PEAR_Error')) return false;
901
902       // Delete task.
903       $sql = "delete from tt_tasks where id = $task_id";
904       $affected = $mdb2->exec($sql);
905       if (is_a($affected, 'PEAR_Error')) return false;
906     }
907
908     return true;
909   }
910
911   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
912   static function deleteCustomFields($team_id) {
913     $mdb2 = getConnection();
914     $sql = "select id from tt_custom_fields where team_id = $team_id";
915     $res = $mdb2->query($sql);
916     if (is_a($res, 'PEAR_Error')) return false;
917
918     while ($val = $res->fetchRow()) {
919       $field_id = $val['id'];
920
921       // Clean up tt_custom_field_log.
922       $sql = "delete from tt_custom_field_log where field_id = $field_id";
923       $affected = $mdb2->exec($sql);
924       if (is_a($affected, 'PEAR_Error')) return false;
925
926       // Clean up tt_custom_field_options.
927       $sql = "delete from tt_custom_field_options where field_id = $field_id";
928       $affected = $mdb2->exec($sql);
929       if (is_a($affected, 'PEAR_Error')) return false;
930
931       // Delete custom field.
932       $sql = "delete from tt_custom_fields where id = $field_id";
933       $affected = $mdb2->exec($sql);
934       if (is_a($affected, 'PEAR_Error')) return false;
935     }
936
937     return true;
938   }
939 }