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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttUserHelper');
30 import('DateAndTime');
31 import('ttInvoiceHelper');
33 // Class ttTeamHelper - contains helper functions that operate with teams.
36 // The getUserCount function returns number of people in team.
37 static function getUserCount($team_id) {
38 $mdb2 = getConnection();
40 $sql = "select count(id) as cnt from tt_users where team_id = $team_id and status = 1";
41 $res = $mdb2->query($sql);
43 if (!is_a($res, 'PEAR_Error')) {
44 $val = $res->fetchRow();
50 // The getUsersForClient obtains all active and inactive users in a team that are relevant to a client.
51 static function getUsersForClient() {
53 $mdb2 = getConnection();
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)
60 order by upper(u.name)";
61 $res = $mdb2->query($sql);
63 if (is_a($res, 'PEAR_Error'))
65 while ($val = $res->fetchRow()) {
71 // The getActiveUsers obtains all active users in a given team.
72 static function getActiveUsers($options = null) {
75 $mdb2 = getConnection();
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.team_id = $user->team_id and u.status = 1 order by upper(u.name)";
80 $sql = "select id, name from tt_users where team_id = $user->team_id and status = 1 order by upper(name)";
81 $res = $mdb2->query($sql);
83 if (is_a($res, 'PEAR_Error'))
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->getKey('role.top_manager.label');
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.
106 // The getUsers obtains all active and inactive (but not deleted) users in a given team.
107 static function getUsers() {
109 $mdb2 = getConnection();
111 $sql = "select id, name from tt_users where team_id = $user->team_id and (status = 1 or status = 0) order by upper(name)";
112 $res = $mdb2->query($sql);
113 $user_list = array();
114 if (is_a($res, 'PEAR_Error'))
116 while ($val = $res->fetchRow()) {
123 // The getInactiveUsers obtains all inactive users in a given team.
124 static function getInactiveUsers($team_id, $all_fields = false) {
125 $mdb2 = getConnection();
128 $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.team_id = $team_id and u.status = 0 order by upper(u.name)";
130 $sql = "select id, name from tt_users where team_id = $team_id and status = 0 order by upper(name)";
131 $res = $mdb2->query($sql);
133 if (!is_a($res, 'PEAR_Error')) {
134 while ($val = $res->fetchRow()) {
142 // The getAllUsers obtains all users in a given team.
143 static function getAllUsers($team_id, $all_fields = false) {
144 $mdb2 = getConnection();
147 $sql = "select * from tt_users where team_id = $team_id order by upper(name)";
149 $sql = "select id, name from tt_users where team_id = $team_id order by upper(name)";
150 $res = $mdb2->query($sql);
152 if (!is_a($res, 'PEAR_Error')) {
153 while ($val = $res->fetchRow()) {
161 // getActiveProjects - returns an array of active projects for team.
162 static function getActiveProjects($team_id)
165 $mdb2 = getConnection();
167 $sql = "select id, name, description, tasks from tt_projects
168 where team_id = $team_id and status = 1 order by upper(name)";
169 $res = $mdb2->query($sql);
171 if (!is_a($res, 'PEAR_Error')) {
172 while ($val = $res->fetchRow()) {
179 // getInactiveProjects - returns an array of inactive projects for team.
180 static function getInactiveProjects($team_id)
183 $mdb2 = getConnection();
185 $sql = "select id, name, description, tasks from tt_projects
186 where team_id = $team_id and status = 0 order by upper(name)";
187 $res = $mdb2->query($sql);
189 if (!is_a($res, 'PEAR_Error')) {
190 while ($val = $res->fetchRow()) {
197 // The getAllProjects obtains all projects in a given team.
198 static function getAllProjects($team_id, $all_fields = false) {
199 $mdb2 = getConnection();
202 $sql = "select * from tt_projects where team_id = $team_id order by status, upper(name)";
204 $sql = "select id, name from tt_projects where team_id = $team_id order by status, upper(name)";
205 $res = $mdb2->query($sql);
207 if (!is_a($res, 'PEAR_Error')) {
208 while ($val = $res->fetchRow()) {
216 // getActiveTasks - returns an array of active tasks for team.
217 static function getActiveTasks($team_id)
220 $mdb2 = getConnection();
222 $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by upper(name)";
223 $res = $mdb2->query($sql);
225 if (!is_a($res, 'PEAR_Error')) {
226 while ($val = $res->fetchRow()) {
233 // getInactiveTasks - returns an array of inactive tasks for team.
234 static function getInactiveTasks($team_id)
237 $mdb2 = getConnection();
239 $sql = "select id, name, description from tt_tasks
240 where team_id = $team_id and status = 0 order by upper(name)";
241 $res = $mdb2->query($sql);
243 if (!is_a($res, 'PEAR_Error')) {
244 while ($val = $res->fetchRow()) {
251 // The getAllTasks obtains all tasks in a given team.
252 static function getAllTasks($team_id, $all_fields = false) {
253 $mdb2 = getConnection();
256 $sql = "select * from tt_tasks where team_id = $team_id order by status, upper(name)";
258 $sql = "select id, name from tt_tasks where team_id = $team_id order by status, upper(name)";
259 $res = $mdb2->query($sql);
261 if (!is_a($res, 'PEAR_Error')) {
262 while ($val = $res->fetchRow()) {
270 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
271 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
272 static function getActiveRolesForUser()
276 $mdb2 = getConnection();
278 $sql = "select id, name, description, rank, rights from tt_roles where team_id = $user->team_id and rank < $user->rank and status = 1 order by rank";
279 $res = $mdb2->query($sql);
281 if (!is_a($res, 'PEAR_Error')) {
282 while ($val = $res->fetchRow()) {
283 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
284 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
285 continue; // Skip adding a client role.
292 // getActiveRoles - returns an array of active roles for team.
293 static function getActiveRoles($team_id)
296 $mdb2 = getConnection();
298 $sql = "select id, name, description, rank, rights from tt_roles where team_id = $team_id and status = 1 order by rank";
299 $res = $mdb2->query($sql);
301 if (!is_a($res, 'PEAR_Error')) {
302 while ($val = $res->fetchRow()) {
303 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
310 // getInactiveRoles - returns an array of inactive roles for team.
311 static function getInactiveRoles($team_id)
314 $mdb2 = getConnection();
316 $sql = "select id, name, rank, description from tt_roles
317 where team_id = $team_id and status = 0 order by rank";
318 $res = $mdb2->query($sql);
320 if (!is_a($res, 'PEAR_Error')) {
321 while ($val = $res->fetchRow()) {
328 // getAllRoles - obtains all roles defined for team.
329 static function getAllRoles($team_id) {
330 $mdb2 = getConnection();
333 $sql = "select * from tt_roles where team_id = $team_id";
334 $res = $mdb2->query($sql);
336 if (!is_a($res, 'PEAR_Error')) {
337 while ($val = $res->fetchRow()) {
345 // The getActiveClients returns an array of active clients for team.
346 static function getActiveClients($team_id, $all_fields = false)
349 $mdb2 = getConnection();
352 $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
354 $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
356 $res = $mdb2->query($sql);
358 if (!is_a($res, 'PEAR_Error')) {
359 while ($val = $res->fetchRow()) {
366 // The getInactiveClients returns an array of inactive clients for team.
367 static function getInactiveClients($team_id, $all_fields = false)
370 $mdb2 = getConnection();
373 $sql = "select * from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
375 $sql = "select id, name from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
377 $res = $mdb2->query($sql);
379 if (!is_a($res, 'PEAR_Error')) {
380 while ($val = $res->fetchRow()) {
387 // The getAllClients obtains all clients in a given team.
388 static function getAllClients($team_id, $all_fields = false) {
389 $mdb2 = getConnection();
392 $sql = "select * from tt_clients where team_id = $team_id order by status, upper(name)";
394 $sql = "select id, name from tt_clients where team_id = $team_id order by status, upper(name)";
396 $res = $mdb2->query($sql);
398 if (!is_a($res, 'PEAR_Error')) {
399 while ($val = $res->fetchRow()) {
407 // The getActiveInvoices returns an array of active invoices for team.
408 static function getActiveInvoices($localizeDates = true)
411 $addPaidStatus = $user->isPluginEnabled('ps');
414 $mdb2 = getConnection();
416 if ($user->isClient())
417 $client_part = " and i.client_id = $user->client_id";
419 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
420 left join tt_clients c on (c.id = i.client_id)
421 where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
422 $res = $mdb2->query($sql);
424 if (!is_a($res, 'PEAR_Error')) {
425 $dt = new DateAndTime(DB_DATEFORMAT);
426 while ($val = $res->fetchRow()) {
427 if ($localizeDates) {
428 $dt->parseVal($val['date']);
429 $val['date'] = $dt->toString($user->date_format);
432 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
439 // The getAllInvoices returns an array of all invoices for team.
440 static function getAllInvoices()
445 $mdb2 = getConnection();
447 $sql = "select * from tt_invoices where team_id = $user->team_id";
448 $res = $mdb2->query($sql);
450 if (!is_a($res, 'PEAR_Error')) {
451 $dt = new DateAndTime(DB_DATEFORMAT);
452 while ($val = $res->fetchRow()) {
459 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
460 static function getRecentInvoices($team_id, $client_id)
465 $mdb2 = getConnection();
467 $sql = "select i.id, i.name from tt_invoices i
468 left join tt_clients c on (c.id = i.client_id)
469 where i.team_id = $team_id and i.status = 1 and c.id = $client_id
470 order by i.id desc limit 3";
471 $res = $mdb2->query($sql);
473 if (!is_a($res, 'PEAR_Error')) {
474 $dt = new DateAndTime(DB_DATEFORMAT);
475 while ($val = $res->fetchRow()) {
482 // getUserToProjectBinds - obtains all user to project binds for a team.
483 static function getUserToProjectBinds($team_id) {
484 $mdb2 = getConnection();
487 $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";
488 $res = $mdb2->query($sql);
490 if (!is_a($res, 'PEAR_Error')) {
491 while ($val = $res->fetchRow()) {
499 // The getAllCustomFields obtains all custom fields in a given team.
500 static function getAllCustomFields($team_id) {
501 $mdb2 = getConnection();
503 $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
505 $res = $mdb2->query($sql);
507 if (!is_a($res, 'PEAR_Error')) {
508 while ($val = $res->fetchRow()) {
516 // The getAllCustomFieldOptions obtains all custom field options in a given team.
517 static function getAllCustomFieldOptions($team_id) {
518 $mdb2 = getConnection();
520 $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";
522 $res = $mdb2->query($sql);
524 if (!is_a($res, 'PEAR_Error')) {
525 while ($val = $res->fetchRow()) {
533 // The getCustomFieldLog obtains all custom field log entries for a given team.
534 static function getCustomFieldLog($team_id) {
535 $mdb2 = getConnection();
537 $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";
539 $res = $mdb2->query($sql);
541 if (!is_a($res, 'PEAR_Error')) {
542 while ($val = $res->fetchRow()) {
550 // getFavReports - obtains all favorite reports for all users in team.
551 static function getFavReports($team_id) {
552 $mdb2 = getConnection();
555 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
556 $res = $mdb2->query($sql);
558 if (!is_a($res, 'PEAR_Error')) {
559 while ($val = $res->fetchRow()) {
567 // getExpenseItems - obtains all expense items for all users in team.
568 static function getExpenseItems($team_id) {
569 $mdb2 = getConnection();
572 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
573 $res = $mdb2->query($sql);
575 if (!is_a($res, 'PEAR_Error')) {
576 while ($val = $res->fetchRow()) {
584 // getPredefinedExpenses - obtains predefined expenses for team.
585 static function getPredefinedExpenses($team_id) {
587 $replaceDecimalMark = ('.' != $user->decimal_mark);
589 $mdb2 = getConnection();
592 $sql = "select id, name, cost from tt_predefined_expenses where team_id = $team_id";
593 $res = $mdb2->query($sql);
595 if (!is_a($res, 'PEAR_Error')) {
596 while ($val = $res->fetchRow()) {
597 if ($replaceDecimalMark)
598 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
606 // getNotifications - obtains notification descriptions for team.
607 static function getNotifications($team_id) {
608 $mdb2 = getConnection();
611 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
612 left join tt_fav_reports fr on (fr.id = c.report_id)
613 where c.team_id = $team_id and c.status = 1 and fr.status = 1";
614 $res = $mdb2->query($sql);
616 if (!is_a($res, 'PEAR_Error')) {
617 while ($val = $res->fetchRow()) {
625 // getMonthlyQuotas - obtains monthly quotas for team.
626 static function getMonthlyQuotas($team_id) {
627 $mdb2 = getConnection();
630 $sql = "select year, month, minutes from tt_monthly_quotas where team_id = $team_id";
631 $res = $mdb2->query($sql);
633 if (!is_a($res, 'PEAR_Error')) {
634 while ($val = $res->fetchRow()) {
642 // The getTeams function returns an array of all active teams on the server.
643 static function getTeams() {
645 $mdb2 = getConnection();
647 $sql = "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
648 $res = $mdb2->query($sql);
650 if (!is_a($res, 'PEAR_Error')) {
651 while ($val = $res->fetchRow()) {
652 $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
660 // The markDeleted function marks the team and everything in it as deleted.
661 static function markDeleted($team_id) {
663 // Iterate through team users and mark them as deleted.
664 $users = ttTeamHelper::getAllUsers($team_id);
665 foreach ($users as $one_user) {
666 if (!ttUserHelper::markDeleted($one_user['id'])) return false;
669 // Mark tasks deleted.
670 if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
672 $mdb2 = getConnection();
674 // Mark projects deleted.
675 $sql = "update tt_projects set status = NULL where team_id = $team_id";
676 $affected = $mdb2->exec($sql);
677 if (is_a($affected, 'PEAR_Error')) return false;
679 // Mark clients deleted.
680 $sql = "update tt_clients set status = NULL where team_id = $team_id";
681 $affected = $mdb2->exec($sql);
682 if (is_a($affected, 'PEAR_Error')) return false;
684 // Mark custom fields deleted.
685 $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
686 $affected = $mdb2->exec($sql);
687 if (is_a($affected, 'PEAR_Error')) return false;
689 // Mark team deleted.
690 $sql = "update tt_teams set status = NULL where id = $team_id";
691 $affected = $mdb2->exec($sql);
692 if (is_a($affected, 'PEAR_Error')) return false;
697 // The getTeamDetails function returns team details.
698 static function getTeamDetails($team_id) {
700 $mdb2 = getConnection();
702 $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
704 inner join tt_users u on (u.team_id = t.id)
705 inner join tt_roles r on (r.id = u.role_id and r.rank = 512)
706 where t.id = $team_id";
708 $res = $mdb2->query($sql);
709 if (!is_a($res, 'PEAR_Error')) {
710 $val = $res->fetchRow();
717 // The insert function creates a new team.
718 static function insert($fields) {
720 $mdb2 = getConnection();
722 // Start with team name and currency.
723 $columns = 'name, currency';
724 $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
726 if ($fields['decimal_mark']) {
727 $columns .= ', decimal_mark';
728 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
731 $lang = $fields['lang'];
736 $columns .= ', lang';
737 $values .= ', '.$mdb2->quote($lang);
739 if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
740 $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
741 $columns .= ', date_format';
742 $values .= ', '.$mdb2->quote($date_format);
745 if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
746 $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
747 $columns .= ', time_format';
748 $values .= ', '.$mdb2->quote($time_format);
751 if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
752 $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
753 $columns .= ', week_start';
754 $values .= ', '.(int)$week_start;
757 if ($fields['tracking_mode']) {
758 $columns .= ', tracking_mode';
759 $values .= ', '.(int)$fields['tracking_mode'];
762 if ($fields['project_required']) {
763 $columns .= ', project_required';
764 $values .= ', '.(int)$fields['project_required'];
767 if ($fields['task_required']) {
768 $columns .= ', task_required';
769 $values .= ', '.(int)$fields['task_required'];
772 if ($fields['record_type']) {
773 $columns .= ', record_type';
774 $values .= ', '.(int)$fields['record_type'];
777 if ($fields['bcc_email']) {
778 $columns .= ', bcc_email';
779 $values .= ', '.$mdb2->quote($fields['bcc_email']);
782 if ($fields['plugins']) {
783 $columns .= ', plugins';
784 $values .= ', '.$mdb2->quote($fields['plugins']);
787 if ($fields['lock_spec']) {
788 $columns .= ', lock_spec';
789 $values .= ', '.$mdb2->quote($fields['lock_spec']);
792 if ($fields['workday_minutes']) {
793 $columns .= ', workday_minutes';
794 $values .= ', '.(int)$fields['workday_minutes'];
797 if ($fields['config']) {
798 $columns .= ', config';
799 $values .= ', '.$mdb2->quote($fields['config']);
802 $sql = "insert into tt_teams ($columns) values($values)";
803 $affected = $mdb2->exec($sql);
805 if (!is_a($affected, 'PEAR_Error')) {
806 $team_id = $mdb2->lastInsertID('tt_teams', 'id');
813 // The update function updates team information.
814 static function update($team_id, $fields)
816 $mdb2 = getConnection();
817 $name_part = 'name = '.$mdb2->quote($fields['name']);
820 $decimal_mark_part = '';
821 $date_format_part = '';
822 $time_format_part = '';
823 $week_start_part = '';
824 $tracking_mode_part = '';
825 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
826 $record_type_part = '';
827 $bcc_email_part = '';
830 $lock_spec_part = '';
831 $workday_minutes_part = '';
833 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
834 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
835 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
836 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
837 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
838 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
839 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
840 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
841 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
842 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
843 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
844 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
845 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
847 $sql = "update tt_teams set $name_part $currency_part $lang_part $decimal_mark_part
848 $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
849 $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part where id = $team_id";
850 $affected = $mdb2->exec($sql);
851 if (is_a($affected, 'PEAR_Error')) return false;
856 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
857 static function getInactiveTeams() {
858 $inactive_teams = array();
859 $mdb2 = getConnection();
861 // Get all team ids for teams created or modified more than 6 months ago.
862 // $ts = date('Y-m-d', strtotime('-1 year'));
863 $ts = date('Y-m-d', strtotime('-6 month'));
864 $sql = "select id from tt_teams where timestamp < '$ts' order by id";
865 $res = $mdb2->query($sql);
868 if (!is_a($res, 'PEAR_Error')) {
869 while ($val = $res->fetchRow()) {
870 $team_id = $val['id'];
871 if (ttTeamHelper::isTeamActive($team_id) == false) {
873 $inactive_teams[] = $team_id;
874 // Limit the array size for perfomance by allowing this operation on small chunks only.
875 if ($count >= 100) break;
878 return $inactive_teams;
883 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
884 static function isTeamActive($team_id) {
887 $mdb2 = getConnection();
888 $sql = "select id from tt_users where team_id = $team_id";
889 $res = $mdb2->query($sql);
890 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
891 while ($val = $res->fetchRow()) {
892 $users[] = $val['id'];
894 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
896 return false; // No users in team.
899 $ts = date('Y-m-d', strtotime('-2 years'));
900 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
901 $res = $mdb2->query($sql);
902 if (!is_a($res, 'PEAR_Error')) {
903 if ($val = $res->fetchRow()) {
904 $count = $val['cnt'];
909 return false; // No time entries for the last 2 years.
912 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
913 $count_last_year = 0;
914 $ts = date('Y-m-d', strtotime('-1 year'));
915 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
916 $res = $mdb2->query($sql);
917 if (!is_a($res, 'PEAR_Error')) {
918 if ($val = $res->fetchRow()) {
919 $count_last_year = $val['cnt'];
921 if ($count_last_year == 0)
922 return false; // No time entries for the last year and only a few entries before that.
928 // The delete function permanently deletes all data for a team.
929 static function delete($team_id) {
930 $mdb2 = getConnection();
933 $sql = "select id from tt_users where team_id = $team_id";
934 $res = $mdb2->query($sql);
935 if (is_a($res, 'PEAR_Error')) return false;
936 while ($val = $res->fetchRow()) {
937 $user_id = $val['id'];
938 if (!ttUserHelper::delete($user_id)) return false;
942 if (!ttTeamHelper::deleteTasks($team_id)) return false;
944 // Delete client to project binds.
945 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
946 $affected = $mdb2->exec($sql);
947 if (is_a($affected, 'PEAR_Error')) return false;
950 $sql = "delete from tt_projects where team_id = $team_id";
951 $affected = $mdb2->exec($sql);
952 if (is_a($affected, 'PEAR_Error')) return false;
955 $sql = "delete from tt_clients where team_id = $team_id";
956 $affected = $mdb2->exec($sql);
957 if (is_a($affected, 'PEAR_Error')) return false;
960 $sql = "delete from tt_invoices where team_id = $team_id";
961 $affected = $mdb2->exec($sql);
962 if (is_a($affected, 'PEAR_Error')) return false;
964 // Delete custom fields.
965 if (!ttTeamHelper::deleteCustomFields($team_id)) return false;
968 $sql = "delete from tt_roles where team_id = $team_id";
969 $affected = $mdb2->exec($sql);
970 if (is_a($affected, 'PEAR_Error')) return false;
973 $sql = "delete from tt_teams where id = $team_id";
974 $affected = $mdb2->exec($sql);
975 if (is_a($affected, 'PEAR_Error')) return false;
980 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
981 static function markTasksDeleted($team_id) {
982 $mdb2 = getConnection();
983 $sql = "select id from tt_tasks where team_id = $team_id";
984 $res = $mdb2->query($sql);
985 if (is_a($res, 'PEAR_Error')) return false;
987 while ($val = $res->fetchRow()) {
989 // Delete task binds.
990 $task_id = $val['id'];
991 $sql = "delete from tt_project_task_binds where task_id = $task_id";
992 $affected = $mdb2->exec($sql);
993 if (is_a($affected, 'PEAR_Error')) return false;
995 // Mark task as deleted.
996 $sql = "update tt_tasks set status = NULL where id = $task_id";
997 $affected = $mdb2->exec($sql);
998 if (is_a($affected, 'PEAR_Error')) return false;
1004 // The deleteTasks deletes all tasks and task binds for an inactive team.
1005 static function deleteTasks($team_id) {
1006 $mdb2 = getConnection();
1007 $sql = "select id from tt_tasks where team_id = $team_id";
1008 $res = $mdb2->query($sql);
1009 if (is_a($res, 'PEAR_Error')) return false;
1011 while ($val = $res->fetchRow()) {
1013 // Delete task binds.
1014 $task_id = $val['id'];
1015 $sql = "delete from tt_project_task_binds where task_id = $task_id";
1016 $affected = $mdb2->exec($sql);
1017 if (is_a($affected, 'PEAR_Error')) return false;
1020 $sql = "delete from tt_tasks where id = $task_id";
1021 $affected = $mdb2->exec($sql);
1022 if (is_a($affected, 'PEAR_Error')) return false;
1028 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
1029 static function deleteCustomFields($team_id) {
1030 $mdb2 = getConnection();
1031 $sql = "select id from tt_custom_fields where team_id = $team_id";
1032 $res = $mdb2->query($sql);
1033 if (is_a($res, 'PEAR_Error')) return false;
1035 while ($val = $res->fetchRow()) {
1036 $field_id = $val['id'];
1038 // Clean up tt_custom_field_log.
1039 $sql = "delete from tt_custom_field_log where field_id = $field_id";
1040 $affected = $mdb2->exec($sql);
1041 if (is_a($affected, 'PEAR_Error')) return false;
1043 // Clean up tt_custom_field_options.
1044 $sql = "delete from tt_custom_field_options where field_id = $field_id";
1045 $affected = $mdb2->exec($sql);
1046 if (is_a($affected, 'PEAR_Error')) return false;
1048 // Delete custom field.
1049 $sql = "delete from tt_custom_fields where id = $field_id";
1050 $affected = $mdb2->exec($sql);
1051 if (is_a($affected, 'PEAR_Error')) return false;
1057 // enablePlugin either enables or disables a specific plugin for team.
1058 static function enablePlugin($plugin, $enable = true)
1061 if (!$user->can('manage_features'))
1064 $plugin_array = explode(',', $user->plugins);
1065 if ($enable && !in_array($plugin, $plugin_array))
1066 $plugin_array[] = $plugin; // Add plugin to array.
1068 if (!$enable && in_array($plugin, $plugin_array)) {
1069 $key = array_search($plugin, $plugin_array);
1071 unset($plugin_array[$key]); // Remove plugin from array.
1074 $plugins = implode(',', $plugin_array);
1075 if ($plugins != $user->plugins) {
1076 if (!ttTeamHelper::update($user->team_id, array('name' => $user->team,'plugins' => $plugins)))
1078 $user->plugins = $plugins;