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();
146 $sql = "select * from tt_users where team_id = $team_id order by upper(name)";
148 $sql = "select id, name from tt_users where team_id = $team_id order by upper(name)";
149 $res = $mdb2->query($sql);
151 if (!is_a($res, 'PEAR_Error')) {
152 while ($val = $res->fetchRow()) {
160 // getActiveProjects - returns an array of active projects for team.
161 static function getActiveProjects($team_id)
164 $mdb2 = getConnection();
166 $sql = "select id, name, description, tasks from tt_projects
167 where team_id = $team_id and status = 1 order by upper(name)";
168 $res = $mdb2->query($sql);
170 if (!is_a($res, 'PEAR_Error')) {
171 while ($val = $res->fetchRow()) {
178 // getInactiveProjects - returns an array of inactive projects for team.
179 static function getInactiveProjects($team_id)
182 $mdb2 = getConnection();
184 $sql = "select id, name, description, tasks from tt_projects
185 where team_id = $team_id and status = 0 order by upper(name)";
186 $res = $mdb2->query($sql);
188 if (!is_a($res, 'PEAR_Error')) {
189 while ($val = $res->fetchRow()) {
196 // The getAllProjects obtains all projects in a given team.
197 static function getAllProjects($team_id, $all_fields = false) {
198 $mdb2 = getConnection();
201 $sql = "select * from tt_projects where team_id = $team_id order by status, upper(name)";
203 $sql = "select id, name from tt_projects where team_id = $team_id order by status, upper(name)";
204 $res = $mdb2->query($sql);
206 if (!is_a($res, 'PEAR_Error')) {
207 while ($val = $res->fetchRow()) {
215 // getActiveTasks - returns an array of active tasks for team.
216 static function getActiveTasks($team_id)
219 $mdb2 = getConnection();
221 $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by upper(name)";
222 $res = $mdb2->query($sql);
224 if (!is_a($res, 'PEAR_Error')) {
225 while ($val = $res->fetchRow()) {
232 // getInactiveTasks - returns an array of inactive tasks for team.
233 static function getInactiveTasks($team_id)
236 $mdb2 = getConnection();
238 $sql = "select id, name, description from tt_tasks
239 where team_id = $team_id and status = 0 order by upper(name)";
240 $res = $mdb2->query($sql);
242 if (!is_a($res, 'PEAR_Error')) {
243 while ($val = $res->fetchRow()) {
250 // The getAllTasks obtains all tasks in a given team.
251 static function getAllTasks($team_id, $all_fields = false) {
252 $mdb2 = getConnection();
255 $sql = "select * from tt_tasks where team_id = $team_id order by status, upper(name)";
257 $sql = "select id, name from tt_tasks where team_id = $team_id order by status, upper(name)";
258 $res = $mdb2->query($sql);
260 if (!is_a($res, 'PEAR_Error')) {
261 while ($val = $res->fetchRow()) {
269 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
270 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
271 static function getActiveRolesForUser()
275 $mdb2 = getConnection();
277 $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";
278 $res = $mdb2->query($sql);
280 if (!is_a($res, 'PEAR_Error')) {
281 while ($val = $res->fetchRow()) {
282 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
283 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
284 continue; // Skip adding a client role.
291 // getActiveRoles - returns an array of active roles for team.
292 static function getActiveRoles($team_id)
295 $mdb2 = getConnection();
297 $sql = "select id, name, description, rank, rights from tt_roles where team_id = $team_id and status = 1 order by rank";
298 $res = $mdb2->query($sql);
300 if (!is_a($res, 'PEAR_Error')) {
301 while ($val = $res->fetchRow()) {
302 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
309 // getInactiveRoles - returns an array of inactive roles for team.
310 static function getInactiveRoles($team_id)
313 $mdb2 = getConnection();
315 $sql = "select id, name, rank, description from tt_roles
316 where team_id = $team_id and status = 0 order by rank";
317 $res = $mdb2->query($sql);
319 if (!is_a($res, 'PEAR_Error')) {
320 while ($val = $res->fetchRow()) {
327 // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
328 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
329 static function getInactiveRolesForUser()
333 $mdb2 = getConnection();
335 $sql = "select id, name, description, rank, rights from tt_roles where team_id = $user->team_id and rank < $user->rank and status = 0 order by rank";
336 $res = $mdb2->query($sql);
338 if (!is_a($res, 'PEAR_Error')) {
339 while ($val = $res->fetchRow()) {
340 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
341 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
342 continue; // Skip adding a client role.
349 // The getActiveClients returns an array of active clients for team.
350 static function getActiveClients($team_id, $all_fields = false)
353 $mdb2 = getConnection();
356 $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
358 $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
360 $res = $mdb2->query($sql);
362 if (!is_a($res, 'PEAR_Error')) {
363 while ($val = $res->fetchRow()) {
370 // The getInactiveClients returns an array of inactive clients for team.
371 static function getInactiveClients($team_id, $all_fields = false)
374 $mdb2 = getConnection();
377 $sql = "select * from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
379 $sql = "select id, name from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
381 $res = $mdb2->query($sql);
383 if (!is_a($res, 'PEAR_Error')) {
384 while ($val = $res->fetchRow()) {
391 // The getAllClients obtains all clients in a given team.
392 static function getAllClients($team_id, $all_fields = false) {
393 $mdb2 = getConnection();
396 $sql = "select * from tt_clients where team_id = $team_id order by status, upper(name)";
398 $sql = "select id, name from tt_clients where team_id = $team_id order by status, upper(name)";
400 $res = $mdb2->query($sql);
402 if (!is_a($res, 'PEAR_Error')) {
403 while ($val = $res->fetchRow()) {
411 // The getActiveInvoices returns an array of active invoices for team.
412 static function getActiveInvoices($localizeDates = true)
415 $addPaidStatus = $user->isPluginEnabled('ps');
418 $mdb2 = getConnection();
420 if ($user->isClient())
421 $client_part = " and i.client_id = $user->client_id";
423 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
424 left join tt_clients c on (c.id = i.client_id)
425 where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
426 $res = $mdb2->query($sql);
428 if (!is_a($res, 'PEAR_Error')) {
429 $dt = new DateAndTime(DB_DATEFORMAT);
430 while ($val = $res->fetchRow()) {
431 if ($localizeDates) {
432 $dt->parseVal($val['date']);
433 $val['date'] = $dt->toString($user->date_format);
436 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
443 // The getAllInvoices returns an array of all invoices for team.
444 static function getAllInvoices()
449 $mdb2 = getConnection();
451 $sql = "select * from tt_invoices where team_id = $user->team_id";
452 $res = $mdb2->query($sql);
454 if (!is_a($res, 'PEAR_Error')) {
455 $dt = new DateAndTime(DB_DATEFORMAT);
456 while ($val = $res->fetchRow()) {
463 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
464 static function getRecentInvoices($team_id, $client_id)
469 $mdb2 = getConnection();
471 $sql = "select i.id, i.name from tt_invoices i
472 left join tt_clients c on (c.id = i.client_id)
473 where i.team_id = $team_id and i.status = 1 and c.id = $client_id
474 order by i.id desc limit 3";
475 $res = $mdb2->query($sql);
477 if (!is_a($res, 'PEAR_Error')) {
478 $dt = new DateAndTime(DB_DATEFORMAT);
479 while ($val = $res->fetchRow()) {
486 // getUserToProjectBinds - obtains all user to project binds for a team.
487 static function getUserToProjectBinds($team_id) {
488 $mdb2 = getConnection();
491 $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";
492 $res = $mdb2->query($sql);
494 if (!is_a($res, 'PEAR_Error')) {
495 while ($val = $res->fetchRow()) {
503 // The getAllCustomFields obtains all custom fields in a given team.
504 static function getAllCustomFields($team_id) {
505 $mdb2 = getConnection();
507 $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
509 $res = $mdb2->query($sql);
511 if (!is_a($res, 'PEAR_Error')) {
512 while ($val = $res->fetchRow()) {
520 // The getAllCustomFieldOptions obtains all custom field options in a given team.
521 static function getAllCustomFieldOptions($team_id) {
522 $mdb2 = getConnection();
524 $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";
526 $res = $mdb2->query($sql);
528 if (!is_a($res, 'PEAR_Error')) {
529 while ($val = $res->fetchRow()) {
537 // The getCustomFieldLog obtains all custom field log entries for a given team.
538 static function getCustomFieldLog($team_id) {
539 $mdb2 = getConnection();
541 $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";
543 $res = $mdb2->query($sql);
545 if (!is_a($res, 'PEAR_Error')) {
546 while ($val = $res->fetchRow()) {
554 // getFavReports - obtains all favorite reports for all users in team.
555 static function getFavReports($team_id) {
556 $mdb2 = getConnection();
559 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
560 $res = $mdb2->query($sql);
562 if (!is_a($res, 'PEAR_Error')) {
563 while ($val = $res->fetchRow()) {
571 // getExpenseItems - obtains all expense items for all users in team.
572 static function getExpenseItems($team_id) {
573 $mdb2 = getConnection();
576 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
577 $res = $mdb2->query($sql);
579 if (!is_a($res, 'PEAR_Error')) {
580 while ($val = $res->fetchRow()) {
588 // getPredefinedExpenses - obtains predefined expenses for team.
589 static function getPredefinedExpenses($team_id) {
591 $replaceDecimalMark = ('.' != $user->decimal_mark);
593 $mdb2 = getConnection();
596 $sql = "select id, name, cost from tt_predefined_expenses where team_id = $team_id";
597 $res = $mdb2->query($sql);
599 if (!is_a($res, 'PEAR_Error')) {
600 while ($val = $res->fetchRow()) {
601 if ($replaceDecimalMark)
602 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
610 // getNotifications - obtains notification descriptions for team.
611 static function getNotifications($team_id) {
612 $mdb2 = getConnection();
615 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
616 left join tt_fav_reports fr on (fr.id = c.report_id)
617 where c.team_id = $team_id and c.status = 1 and fr.status = 1";
618 $res = $mdb2->query($sql);
620 if (!is_a($res, 'PEAR_Error')) {
621 while ($val = $res->fetchRow()) {
629 // getMonthlyQuotas - obtains monthly quotas for team.
630 static function getMonthlyQuotas($team_id) {
631 $mdb2 = getConnection();
634 $sql = "select year, month, minutes from tt_monthly_quotas where team_id = $team_id";
635 $res = $mdb2->query($sql);
637 if (!is_a($res, 'PEAR_Error')) {
638 while ($val = $res->fetchRow()) {
646 // The getTeams function returns an array of all active teams on the server.
647 static function getTeams() {
649 $mdb2 = getConnection();
651 $sql = "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
652 $res = $mdb2->query($sql);
654 if (!is_a($res, 'PEAR_Error')) {
655 while ($val = $res->fetchRow()) {
656 $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
664 // The markDeleted function marks the team and everything in it as deleted.
665 static function markDeleted($team_id) {
667 // Iterate through team users and mark them as deleted.
668 $users = ttTeamHelper::getAllUsers($team_id);
669 foreach ($users as $one_user) {
670 if (!ttUserHelper::markDeleted($one_user['id'])) return false;
673 // Mark tasks deleted.
674 if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
676 $mdb2 = getConnection();
678 // Mark projects deleted.
679 $sql = "update tt_projects set status = NULL where team_id = $team_id";
680 $affected = $mdb2->exec($sql);
681 if (is_a($affected, 'PEAR_Error')) return false;
683 // Mark clients deleted.
684 $sql = "update tt_clients set status = NULL where team_id = $team_id";
685 $affected = $mdb2->exec($sql);
686 if (is_a($affected, 'PEAR_Error')) return false;
688 // Mark custom fields deleted.
689 $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
690 $affected = $mdb2->exec($sql);
691 if (is_a($affected, 'PEAR_Error')) return false;
693 // Mark team deleted.
694 $sql = "update tt_teams set status = NULL where id = $team_id";
695 $affected = $mdb2->exec($sql);
696 if (is_a($affected, 'PEAR_Error')) return false;
701 // The getTeamDetails function returns team details.
702 static function getTeamDetails($team_id) {
704 $mdb2 = getConnection();
706 $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
708 inner join tt_users u on (u.team_id = t.id)
709 inner join tt_roles r on (r.id = u.role_id and r.rank = 512)
710 where t.id = $team_id";
712 $res = $mdb2->query($sql);
713 if (!is_a($res, 'PEAR_Error')) {
714 $val = $res->fetchRow();
721 // The insert function creates a new team.
722 static function insert($fields) {
724 $mdb2 = getConnection();
726 // Start with team name and currency.
727 $columns = 'name, currency';
728 $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
730 if ($fields['decimal_mark']) {
731 $columns .= ', decimal_mark';
732 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
735 $lang = $fields['lang'];
740 $columns .= ', lang';
741 $values .= ', '.$mdb2->quote($lang);
743 if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
744 $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
745 $columns .= ', date_format';
746 $values .= ', '.$mdb2->quote($date_format);
749 if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
750 $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
751 $columns .= ', time_format';
752 $values .= ', '.$mdb2->quote($time_format);
755 if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
756 $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
757 $columns .= ', week_start';
758 $values .= ', '.(int)$week_start;
761 if ($fields['tracking_mode']) {
762 $columns .= ', tracking_mode';
763 $values .= ', '.(int)$fields['tracking_mode'];
766 if ($fields['project_required']) {
767 $columns .= ', project_required';
768 $values .= ', '.(int)$fields['project_required'];
771 if ($fields['task_required']) {
772 $columns .= ', task_required';
773 $values .= ', '.(int)$fields['task_required'];
776 if ($fields['record_type']) {
777 $columns .= ', record_type';
778 $values .= ', '.(int)$fields['record_type'];
781 if ($fields['bcc_email']) {
782 $columns .= ', bcc_email';
783 $values .= ', '.$mdb2->quote($fields['bcc_email']);
786 if ($fields['plugins']) {
787 $columns .= ', plugins';
788 $values .= ', '.$mdb2->quote($fields['plugins']);
791 if ($fields['lock_spec']) {
792 $columns .= ', lock_spec';
793 $values .= ', '.$mdb2->quote($fields['lock_spec']);
796 if ($fields['workday_minutes']) {
797 $columns .= ', workday_minutes';
798 $values .= ', '.(int)$fields['workday_minutes'];
801 if ($fields['config']) {
802 $columns .= ', config';
803 $values .= ', '.$mdb2->quote($fields['config']);
806 $sql = "insert into tt_teams ($columns) values($values)";
807 $affected = $mdb2->exec($sql);
809 if (!is_a($affected, 'PEAR_Error')) {
810 $team_id = $mdb2->lastInsertID('tt_teams', 'id');
817 // The update function updates team information.
818 static function update($team_id, $fields)
820 $mdb2 = getConnection();
821 $name_part = 'name = '.$mdb2->quote($fields['name']);
824 $decimal_mark_part = '';
825 $date_format_part = '';
826 $time_format_part = '';
827 $week_start_part = '';
828 $tracking_mode_part = '';
829 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
830 $record_type_part = '';
831 $bcc_email_part = '';
834 $lock_spec_part = '';
835 $workday_minutes_part = '';
837 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
838 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
839 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
840 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
841 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
842 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
843 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
844 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
845 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
846 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
847 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
848 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
849 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
851 $sql = "update tt_teams set $name_part $currency_part $lang_part $decimal_mark_part
852 $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
853 $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part where id = $team_id";
854 $affected = $mdb2->exec($sql);
855 if (is_a($affected, 'PEAR_Error')) return false;
860 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
861 static function getInactiveTeams() {
862 $inactive_teams = array();
863 $mdb2 = getConnection();
865 // Get all team ids for teams created or modified more than 6 months ago.
866 // $ts = date('Y-m-d', strtotime('-1 year'));
867 $ts = date('Y-m-d', strtotime('-6 month'));
868 $sql = "select id from tt_teams where timestamp < '$ts' order by id";
869 $res = $mdb2->query($sql);
872 if (!is_a($res, 'PEAR_Error')) {
873 while ($val = $res->fetchRow()) {
874 $team_id = $val['id'];
875 if (ttTeamHelper::isTeamActive($team_id) == false) {
877 $inactive_teams[] = $team_id;
878 // Limit the array size for perfomance by allowing this operation on small chunks only.
879 if ($count >= 100) break;
882 return $inactive_teams;
887 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
888 static function isTeamActive($team_id) {
891 $mdb2 = getConnection();
892 $sql = "select id from tt_users where team_id = $team_id";
893 $res = $mdb2->query($sql);
894 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
895 while ($val = $res->fetchRow()) {
896 $users[] = $val['id'];
898 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
900 return false; // No users in team.
903 $ts = date('Y-m-d', strtotime('-2 years'));
904 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
905 $res = $mdb2->query($sql);
906 if (!is_a($res, 'PEAR_Error')) {
907 if ($val = $res->fetchRow()) {
908 $count = $val['cnt'];
913 return false; // No time entries for the last 2 years.
916 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
917 $count_last_year = 0;
918 $ts = date('Y-m-d', strtotime('-1 year'));
919 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
920 $res = $mdb2->query($sql);
921 if (!is_a($res, 'PEAR_Error')) {
922 if ($val = $res->fetchRow()) {
923 $count_last_year = $val['cnt'];
925 if ($count_last_year == 0)
926 return false; // No time entries for the last year and only a few entries before that.
932 // The delete function permanently deletes all data for a team.
933 static function delete($team_id) {
934 $mdb2 = getConnection();
937 $sql = "select id from tt_users where team_id = $team_id";
938 $res = $mdb2->query($sql);
939 if (is_a($res, 'PEAR_Error')) return false;
940 while ($val = $res->fetchRow()) {
941 $user_id = $val['id'];
942 if (!ttUserHelper::delete($user_id)) return false;
946 if (!ttTeamHelper::deleteTasks($team_id)) return false;
948 // Delete client to project binds.
949 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
950 $affected = $mdb2->exec($sql);
951 if (is_a($affected, 'PEAR_Error')) return false;
954 $sql = "delete from tt_projects where team_id = $team_id";
955 $affected = $mdb2->exec($sql);
956 if (is_a($affected, 'PEAR_Error')) return false;
959 $sql = "delete from tt_clients where team_id = $team_id";
960 $affected = $mdb2->exec($sql);
961 if (is_a($affected, 'PEAR_Error')) return false;
964 $sql = "delete from tt_invoices where team_id = $team_id";
965 $affected = $mdb2->exec($sql);
966 if (is_a($affected, 'PEAR_Error')) return false;
968 // Delete custom fields.
969 if (!ttTeamHelper::deleteCustomFields($team_id)) return false;
972 $sql = "delete from tt_roles where team_id = $team_id";
973 $affected = $mdb2->exec($sql);
974 if (is_a($affected, 'PEAR_Error')) return false;
977 $sql = "delete from tt_teams where id = $team_id";
978 $affected = $mdb2->exec($sql);
979 if (is_a($affected, 'PEAR_Error')) return false;
984 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
985 static function markTasksDeleted($team_id) {
986 $mdb2 = getConnection();
987 $sql = "select id from tt_tasks where team_id = $team_id";
988 $res = $mdb2->query($sql);
989 if (is_a($res, 'PEAR_Error')) return false;
991 while ($val = $res->fetchRow()) {
993 // Delete task binds.
994 $task_id = $val['id'];
995 $sql = "delete from tt_project_task_binds where task_id = $task_id";
996 $affected = $mdb2->exec($sql);
997 if (is_a($affected, 'PEAR_Error')) return false;
999 // Mark task as deleted.
1000 $sql = "update tt_tasks set status = NULL where id = $task_id";
1001 $affected = $mdb2->exec($sql);
1002 if (is_a($affected, 'PEAR_Error')) return false;
1008 // The deleteTasks deletes all tasks and task binds for an inactive team.
1009 static function deleteTasks($team_id) {
1010 $mdb2 = getConnection();
1011 $sql = "select id from tt_tasks where team_id = $team_id";
1012 $res = $mdb2->query($sql);
1013 if (is_a($res, 'PEAR_Error')) return false;
1015 while ($val = $res->fetchRow()) {
1017 // Delete task binds.
1018 $task_id = $val['id'];
1019 $sql = "delete from tt_project_task_binds where task_id = $task_id";
1020 $affected = $mdb2->exec($sql);
1021 if (is_a($affected, 'PEAR_Error')) return false;
1024 $sql = "delete from tt_tasks where id = $task_id";
1025 $affected = $mdb2->exec($sql);
1026 if (is_a($affected, 'PEAR_Error')) return false;
1032 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
1033 static function deleteCustomFields($team_id) {
1034 $mdb2 = getConnection();
1035 $sql = "select id from tt_custom_fields where team_id = $team_id";
1036 $res = $mdb2->query($sql);
1037 if (is_a($res, 'PEAR_Error')) return false;
1039 while ($val = $res->fetchRow()) {
1040 $field_id = $val['id'];
1042 // Clean up tt_custom_field_log.
1043 $sql = "delete from tt_custom_field_log where field_id = $field_id";
1044 $affected = $mdb2->exec($sql);
1045 if (is_a($affected, 'PEAR_Error')) return false;
1047 // Clean up tt_custom_field_options.
1048 $sql = "delete from tt_custom_field_options where field_id = $field_id";
1049 $affected = $mdb2->exec($sql);
1050 if (is_a($affected, 'PEAR_Error')) return false;
1052 // Delete custom field.
1053 $sql = "delete from tt_custom_fields where id = $field_id";
1054 $affected = $mdb2->exec($sql);
1055 if (is_a($affected, 'PEAR_Error')) return false;
1061 // enablePlugin either enables or disables a specific plugin for team.
1062 static function enablePlugin($plugin, $enable = true)
1065 if (!$user->can('manage_features'))
1068 $plugin_array = explode(',', $user->plugins);
1069 if ($enable && !in_array($plugin, $plugin_array))
1070 $plugin_array[] = $plugin; // Add plugin to array.
1072 if (!$enable && in_array($plugin, $plugin_array)) {
1073 $key = array_search($plugin, $plugin_array);
1075 unset($plugin_array[$key]); // Remove plugin from array.
1078 $plugins = implode(',', $plugin_array);
1079 if ($plugins != $user->plugins) {
1080 if (!ttTeamHelper::update($user->team_id, array('name' => $user->team,'plugins' => $plugins)))
1082 $user->plugins = $plugins;