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 // getActiveProjects - returns an array of active projects for team.
143 static function getActiveProjects($team_id)
146 $mdb2 = getConnection();
148 $sql = "select id, name, description, tasks from tt_projects
149 where team_id = $team_id and status = 1 order by upper(name)";
150 $res = $mdb2->query($sql);
152 if (!is_a($res, 'PEAR_Error')) {
153 while ($val = $res->fetchRow()) {
160 // getInactiveProjects - returns an array of inactive projects for team.
161 static function getInactiveProjects($team_id)
164 $mdb2 = getConnection();
166 $sql = "select id, name, description, tasks from tt_projects
167 where team_id = $team_id and status = 0 order by upper(name)";
168 $res = $mdb2->query($sql);
170 if (!is_a($res, 'PEAR_Error')) {
171 while ($val = $res->fetchRow()) {
178 // The getAllProjects obtains all projects in a given team.
179 static function getAllProjects($team_id, $all_fields = false) {
180 $mdb2 = getConnection();
183 $sql = "select * from tt_projects where team_id = $team_id order by status, upper(name)";
185 $sql = "select id, name from tt_projects where team_id = $team_id order by status, upper(name)";
186 $res = $mdb2->query($sql);
188 if (!is_a($res, 'PEAR_Error')) {
189 while ($val = $res->fetchRow()) {
197 // getActiveTasks - returns an array of active tasks for team.
198 static function getActiveTasks($team_id)
201 $mdb2 = getConnection();
203 $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by upper(name)";
204 $res = $mdb2->query($sql);
206 if (!is_a($res, 'PEAR_Error')) {
207 while ($val = $res->fetchRow()) {
214 // getInactiveTasks - returns an array of inactive tasks for team.
215 static function getInactiveTasks($team_id)
218 $mdb2 = getConnection();
220 $sql = "select id, name, description from tt_tasks
221 where team_id = $team_id and status = 0 order by upper(name)";
222 $res = $mdb2->query($sql);
224 if (!is_a($res, 'PEAR_Error')) {
225 while ($val = $res->fetchRow()) {
232 // The getAllTasks obtains all tasks in a given team.
233 static function getAllTasks($team_id, $all_fields = false) {
234 $mdb2 = getConnection();
237 $sql = "select * from tt_tasks where team_id = $team_id order by status, upper(name)";
239 $sql = "select id, name from tt_tasks where team_id = $team_id order by status, upper(name)";
240 $res = $mdb2->query($sql);
242 if (!is_a($res, 'PEAR_Error')) {
243 while ($val = $res->fetchRow()) {
251 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
252 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
253 static function getActiveRolesForUser()
257 $mdb2 = getConnection();
259 $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";
260 $res = $mdb2->query($sql);
262 if (!is_a($res, 'PEAR_Error')) {
263 while ($val = $res->fetchRow()) {
264 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
265 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
266 continue; // Skip adding a client role.
273 // getActiveRoles - returns an array of active roles for team.
274 static function getActiveRoles($team_id)
277 $mdb2 = getConnection();
279 $sql = "select id, name, description, rank, rights from tt_roles where team_id = $team_id and status = 1 order by rank";
280 $res = $mdb2->query($sql);
282 if (!is_a($res, 'PEAR_Error')) {
283 while ($val = $res->fetchRow()) {
284 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
291 // getInactiveRoles - returns an array of inactive roles for team.
292 static function getInactiveRoles($team_id)
295 $mdb2 = getConnection();
297 $sql = "select id, name, rank, description from tt_roles
298 where team_id = $team_id and status = 0 order by rank";
299 $res = $mdb2->query($sql);
301 if (!is_a($res, 'PEAR_Error')) {
302 while ($val = $res->fetchRow()) {
309 // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
310 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
311 static function getInactiveRolesForUser()
315 $mdb2 = getConnection();
317 $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";
318 $res = $mdb2->query($sql);
320 if (!is_a($res, 'PEAR_Error')) {
321 while ($val = $res->fetchRow()) {
322 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
323 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
324 continue; // Skip adding a client role.
331 // The getActiveClients returns an array of active clients for team.
332 static function getActiveClients($team_id, $all_fields = false)
335 $mdb2 = getConnection();
338 $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
340 $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
342 $res = $mdb2->query($sql);
344 if (!is_a($res, 'PEAR_Error')) {
345 while ($val = $res->fetchRow()) {
352 // The getInactiveClients returns an array of inactive clients for team.
353 static function getInactiveClients($team_id, $all_fields = false)
356 $mdb2 = getConnection();
359 $sql = "select * from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
361 $sql = "select id, name from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
363 $res = $mdb2->query($sql);
365 if (!is_a($res, 'PEAR_Error')) {
366 while ($val = $res->fetchRow()) {
373 // The getAllClients obtains all clients in a given team.
374 static function getAllClients($team_id, $all_fields = false) {
375 $mdb2 = getConnection();
378 $sql = "select * from tt_clients where team_id = $team_id order by status, upper(name)";
380 $sql = "select id, name from tt_clients where team_id = $team_id order by status, upper(name)";
382 $res = $mdb2->query($sql);
384 if (!is_a($res, 'PEAR_Error')) {
385 while ($val = $res->fetchRow()) {
393 // The getActiveInvoices returns an array of active invoices for team.
394 static function getActiveInvoices($localizeDates = true)
397 $addPaidStatus = $user->isPluginEnabled('ps');
400 $mdb2 = getConnection();
402 if ($user->isClient())
403 $client_part = " and i.client_id = $user->client_id";
405 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
406 left join tt_clients c on (c.id = i.client_id)
407 where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
408 $res = $mdb2->query($sql);
410 if (!is_a($res, 'PEAR_Error')) {
411 $dt = new DateAndTime(DB_DATEFORMAT);
412 while ($val = $res->fetchRow()) {
413 if ($localizeDates) {
414 $dt->parseVal($val['date']);
415 $val['date'] = $dt->toString($user->date_format);
418 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
425 // The getAllInvoices returns an array of all invoices for team.
426 static function getAllInvoices()
431 $mdb2 = getConnection();
433 $sql = "select * from tt_invoices where team_id = $user->team_id";
434 $res = $mdb2->query($sql);
436 if (!is_a($res, 'PEAR_Error')) {
437 $dt = new DateAndTime(DB_DATEFORMAT);
438 while ($val = $res->fetchRow()) {
445 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
446 static function getRecentInvoices($team_id, $client_id)
451 $mdb2 = getConnection();
453 $sql = "select i.id, i.name from tt_invoices i
454 left join tt_clients c on (c.id = i.client_id)
455 where i.team_id = $team_id and i.status = 1 and c.id = $client_id
456 order by i.id desc limit 3";
457 $res = $mdb2->query($sql);
459 if (!is_a($res, 'PEAR_Error')) {
460 $dt = new DateAndTime(DB_DATEFORMAT);
461 while ($val = $res->fetchRow()) {
468 // getUserToProjectBinds - obtains all user to project binds for a team.
469 static function getUserToProjectBinds($team_id) {
470 $mdb2 = getConnection();
473 $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";
474 $res = $mdb2->query($sql);
476 if (!is_a($res, 'PEAR_Error')) {
477 while ($val = $res->fetchRow()) {
485 // The getAllCustomFields obtains all custom fields in a given team.
486 static function getAllCustomFields($team_id) {
487 $mdb2 = getConnection();
489 $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
491 $res = $mdb2->query($sql);
493 if (!is_a($res, 'PEAR_Error')) {
494 while ($val = $res->fetchRow()) {
502 // The getAllCustomFieldOptions obtains all custom field options in a given team.
503 static function getAllCustomFieldOptions($team_id) {
504 $mdb2 = getConnection();
506 $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";
508 $res = $mdb2->query($sql);
510 if (!is_a($res, 'PEAR_Error')) {
511 while ($val = $res->fetchRow()) {
519 // The getCustomFieldLog obtains all custom field log entries for a given team.
520 static function getCustomFieldLog($team_id) {
521 $mdb2 = getConnection();
523 $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";
525 $res = $mdb2->query($sql);
527 if (!is_a($res, 'PEAR_Error')) {
528 while ($val = $res->fetchRow()) {
536 // getFavReports - obtains all favorite reports for all users in team.
537 static function getFavReports($team_id) {
538 $mdb2 = getConnection();
541 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
542 $res = $mdb2->query($sql);
544 if (!is_a($res, 'PEAR_Error')) {
545 while ($val = $res->fetchRow()) {
553 // getExpenseItems - obtains all expense items for all users in team.
554 static function getExpenseItems($team_id) {
555 $mdb2 = getConnection();
558 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
559 $res = $mdb2->query($sql);
561 if (!is_a($res, 'PEAR_Error')) {
562 while ($val = $res->fetchRow()) {
570 // getPredefinedExpenses - obtains predefined expenses for team.
571 static function getPredefinedExpenses($team_id) {
573 $replaceDecimalMark = ('.' != $user->decimal_mark);
575 $mdb2 = getConnection();
578 $sql = "select id, name, cost from tt_predefined_expenses where team_id = $team_id";
579 $res = $mdb2->query($sql);
581 if (!is_a($res, 'PEAR_Error')) {
582 while ($val = $res->fetchRow()) {
583 if ($replaceDecimalMark)
584 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
592 // getNotifications - obtains notification descriptions for team.
593 static function getNotifications($team_id) {
594 $mdb2 = getConnection();
597 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
598 left join tt_fav_reports fr on (fr.id = c.report_id)
599 where c.team_id = $team_id and c.status = 1 and fr.status = 1";
600 $res = $mdb2->query($sql);
602 if (!is_a($res, 'PEAR_Error')) {
603 while ($val = $res->fetchRow()) {
611 // getMonthlyQuotas - obtains monthly quotas for team.
612 static function getMonthlyQuotas($team_id) {
613 $mdb2 = getConnection();
616 $sql = "select year, month, minutes from tt_monthly_quotas where team_id = $team_id";
617 $res = $mdb2->query($sql);
619 if (!is_a($res, 'PEAR_Error')) {
620 while ($val = $res->fetchRow()) {
628 // The getTeams function returns an array of all active teams on the server.
629 static function getTeams() {
631 $mdb2 = getConnection();
633 $sql = "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
634 $res = $mdb2->query($sql);
636 if (!is_a($res, 'PEAR_Error')) {
637 while ($val = $res->fetchRow()) {
638 $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
646 // The markDeleted function marks the team and everything in it as deleted.
647 static function markDeleted($team_id) {
649 // Iterate through team users and mark them as deleted.
650 $users = ttTeamHelper::getAllUsers($team_id);
651 foreach ($users as $one_user) {
652 if (!ttUserHelper::markDeleted($one_user['id'])) return false;
655 // Mark tasks deleted.
656 if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
658 $mdb2 = getConnection();
660 // Mark projects deleted.
661 $sql = "update tt_projects set status = NULL where team_id = $team_id";
662 $affected = $mdb2->exec($sql);
663 if (is_a($affected, 'PEAR_Error')) return false;
665 // Mark clients deleted.
666 $sql = "update tt_clients set status = NULL where team_id = $team_id";
667 $affected = $mdb2->exec($sql);
668 if (is_a($affected, 'PEAR_Error')) return false;
670 // Mark custom fields deleted.
671 $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
672 $affected = $mdb2->exec($sql);
673 if (is_a($affected, 'PEAR_Error')) return false;
675 // Mark team deleted.
676 $sql = "update tt_teams set status = NULL where id = $team_id";
677 $affected = $mdb2->exec($sql);
678 if (is_a($affected, 'PEAR_Error')) return false;
683 // The getTeamDetails function returns team details.
684 static function getTeamDetails($team_id) {
686 $mdb2 = getConnection();
688 $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
690 inner join tt_users u on (u.team_id = t.id)
691 inner join tt_roles r on (r.id = u.role_id and r.rank = 512)
692 where t.id = $team_id";
694 $res = $mdb2->query($sql);
695 if (!is_a($res, 'PEAR_Error')) {
696 $val = $res->fetchRow();
703 // The insert function creates a new team.
704 static function insert($fields) {
706 $mdb2 = getConnection();
708 // Start with team name and currency.
709 $columns = 'name, currency';
710 $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
712 if ($fields['decimal_mark']) {
713 $columns .= ', decimal_mark';
714 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
717 $lang = $fields['lang'];
722 $columns .= ', lang';
723 $values .= ', '.$mdb2->quote($lang);
725 if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
726 $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
727 $columns .= ', date_format';
728 $values .= ', '.$mdb2->quote($date_format);
731 if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
732 $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
733 $columns .= ', time_format';
734 $values .= ', '.$mdb2->quote($time_format);
737 if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
738 $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
739 $columns .= ', week_start';
740 $values .= ', '.(int)$week_start;
743 if ($fields['tracking_mode']) {
744 $columns .= ', tracking_mode';
745 $values .= ', '.(int)$fields['tracking_mode'];
748 if ($fields['project_required']) {
749 $columns .= ', project_required';
750 $values .= ', '.(int)$fields['project_required'];
753 if ($fields['task_required']) {
754 $columns .= ', task_required';
755 $values .= ', '.(int)$fields['task_required'];
758 if ($fields['record_type']) {
759 $columns .= ', record_type';
760 $values .= ', '.(int)$fields['record_type'];
763 if ($fields['bcc_email']) {
764 $columns .= ', bcc_email';
765 $values .= ', '.$mdb2->quote($fields['bcc_email']);
768 if ($fields['plugins']) {
769 $columns .= ', plugins';
770 $values .= ', '.$mdb2->quote($fields['plugins']);
773 if ($fields['lock_spec']) {
774 $columns .= ', lock_spec';
775 $values .= ', '.$mdb2->quote($fields['lock_spec']);
778 if ($fields['workday_minutes']) {
779 $columns .= ', workday_minutes';
780 $values .= ', '.(int)$fields['workday_minutes'];
783 if ($fields['config']) {
784 $columns .= ', config';
785 $values .= ', '.$mdb2->quote($fields['config']);
788 $sql = "insert into tt_teams ($columns) values($values)";
789 $affected = $mdb2->exec($sql);
791 if (!is_a($affected, 'PEAR_Error')) {
792 $team_id = $mdb2->lastInsertID('tt_teams', 'id');
799 // The update function updates team information.
800 static function update($team_id, $fields)
802 $mdb2 = getConnection();
803 $name_part = 'name = '.$mdb2->quote($fields['name']);
806 $decimal_mark_part = '';
807 $date_format_part = '';
808 $time_format_part = '';
809 $week_start_part = '';
810 $tracking_mode_part = '';
811 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
812 $record_type_part = '';
813 $bcc_email_part = '';
816 $lock_spec_part = '';
817 $workday_minutes_part = '';
819 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
820 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
821 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
822 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
823 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
824 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
825 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
826 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
827 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
828 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
829 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
830 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
831 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
833 $sql = "update tt_teams set $name_part $currency_part $lang_part $decimal_mark_part
834 $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
835 $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part where id = $team_id";
836 $affected = $mdb2->exec($sql);
837 if (is_a($affected, 'PEAR_Error')) return false;
842 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
843 static function getInactiveTeams() {
844 $inactive_teams = array();
845 $mdb2 = getConnection();
847 // Get all team ids for teams created or modified more than 6 months ago.
848 // $ts = date('Y-m-d', strtotime('-1 year'));
849 $ts = date('Y-m-d', strtotime('-6 month'));
850 $sql = "select id from tt_teams where timestamp < '$ts' order by id";
851 $res = $mdb2->query($sql);
854 if (!is_a($res, 'PEAR_Error')) {
855 while ($val = $res->fetchRow()) {
856 $team_id = $val['id'];
857 if (ttTeamHelper::isTeamActive($team_id) == false) {
859 $inactive_teams[] = $team_id;
860 // Limit the array size for perfomance by allowing this operation on small chunks only.
861 if ($count >= 100) break;
864 return $inactive_teams;
869 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
870 static function isTeamActive($team_id) {
873 $mdb2 = getConnection();
874 $sql = "select id from tt_users where team_id = $team_id";
875 $res = $mdb2->query($sql);
876 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
877 while ($val = $res->fetchRow()) {
878 $users[] = $val['id'];
880 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
882 return false; // No users in team.
885 $ts = date('Y-m-d', strtotime('-2 years'));
886 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
887 $res = $mdb2->query($sql);
888 if (!is_a($res, 'PEAR_Error')) {
889 if ($val = $res->fetchRow()) {
890 $count = $val['cnt'];
895 return false; // No time entries for the last 2 years.
898 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
899 $count_last_year = 0;
900 $ts = date('Y-m-d', strtotime('-1 year'));
901 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
902 $res = $mdb2->query($sql);
903 if (!is_a($res, 'PEAR_Error')) {
904 if ($val = $res->fetchRow()) {
905 $count_last_year = $val['cnt'];
907 if ($count_last_year == 0)
908 return false; // No time entries for the last year and only a few entries before that.
914 // The delete function permanently deletes all data for a team.
915 static function delete($team_id) {
916 $mdb2 = getConnection();
919 $sql = "select id from tt_users where team_id = $team_id";
920 $res = $mdb2->query($sql);
921 if (is_a($res, 'PEAR_Error')) return false;
922 while ($val = $res->fetchRow()) {
923 $user_id = $val['id'];
924 if (!ttUserHelper::delete($user_id)) return false;
928 if (!ttTeamHelper::deleteTasks($team_id)) return false;
930 // Delete client to project binds.
931 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
932 $affected = $mdb2->exec($sql);
933 if (is_a($affected, 'PEAR_Error')) return false;
936 $sql = "delete from tt_projects where team_id = $team_id";
937 $affected = $mdb2->exec($sql);
938 if (is_a($affected, 'PEAR_Error')) return false;
941 $sql = "delete from tt_clients where team_id = $team_id";
942 $affected = $mdb2->exec($sql);
943 if (is_a($affected, 'PEAR_Error')) return false;
946 $sql = "delete from tt_invoices where team_id = $team_id";
947 $affected = $mdb2->exec($sql);
948 if (is_a($affected, 'PEAR_Error')) return false;
950 // Delete custom fields.
951 if (!ttTeamHelper::deleteCustomFields($team_id)) return false;
954 $sql = "delete from tt_roles where team_id = $team_id";
955 $affected = $mdb2->exec($sql);
956 if (is_a($affected, 'PEAR_Error')) return false;
959 $sql = "delete from tt_teams where id = $team_id";
960 $affected = $mdb2->exec($sql);
961 if (is_a($affected, 'PEAR_Error')) return false;
966 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
967 static function markTasksDeleted($team_id) {
968 $mdb2 = getConnection();
969 $sql = "select id from tt_tasks where team_id = $team_id";
970 $res = $mdb2->query($sql);
971 if (is_a($res, 'PEAR_Error')) return false;
973 while ($val = $res->fetchRow()) {
975 // Delete task binds.
976 $task_id = $val['id'];
977 $sql = "delete from tt_project_task_binds where task_id = $task_id";
978 $affected = $mdb2->exec($sql);
979 if (is_a($affected, 'PEAR_Error')) return false;
981 // Mark task as deleted.
982 $sql = "update tt_tasks set status = NULL where id = $task_id";
983 $affected = $mdb2->exec($sql);
984 if (is_a($affected, 'PEAR_Error')) return false;
990 // The deleteTasks deletes all tasks and task binds for an inactive team.
991 static function deleteTasks($team_id) {
992 $mdb2 = getConnection();
993 $sql = "select id from tt_tasks where team_id = $team_id";
994 $res = $mdb2->query($sql);
995 if (is_a($res, 'PEAR_Error')) return false;
997 while ($val = $res->fetchRow()) {
999 // Delete task binds.
1000 $task_id = $val['id'];
1001 $sql = "delete from tt_project_task_binds where task_id = $task_id";
1002 $affected = $mdb2->exec($sql);
1003 if (is_a($affected, 'PEAR_Error')) return false;
1006 $sql = "delete from tt_tasks where id = $task_id";
1007 $affected = $mdb2->exec($sql);
1008 if (is_a($affected, 'PEAR_Error')) return false;
1014 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
1015 static function deleteCustomFields($team_id) {
1016 $mdb2 = getConnection();
1017 $sql = "select id from tt_custom_fields where team_id = $team_id";
1018 $res = $mdb2->query($sql);
1019 if (is_a($res, 'PEAR_Error')) return false;
1021 while ($val = $res->fetchRow()) {
1022 $field_id = $val['id'];
1024 // Clean up tt_custom_field_log.
1025 $sql = "delete from tt_custom_field_log where field_id = $field_id";
1026 $affected = $mdb2->exec($sql);
1027 if (is_a($affected, 'PEAR_Error')) return false;
1029 // Clean up tt_custom_field_options.
1030 $sql = "delete from tt_custom_field_options where field_id = $field_id";
1031 $affected = $mdb2->exec($sql);
1032 if (is_a($affected, 'PEAR_Error')) return false;
1034 // Delete custom field.
1035 $sql = "delete from tt_custom_fields where id = $field_id";
1036 $affected = $mdb2->exec($sql);
1037 if (is_a($affected, 'PEAR_Error')) return false;
1043 // enablePlugin either enables or disables a specific plugin for team.
1044 static function enablePlugin($plugin, $enable = true)
1047 if (!$user->can('manage_features'))
1050 $plugin_array = explode(',', $user->plugins);
1051 if ($enable && !in_array($plugin, $plugin_array))
1052 $plugin_array[] = $plugin; // Add plugin to array.
1054 if (!$enable && in_array($plugin, $plugin_array)) {
1055 $key = array_search($plugin, $plugin_array);
1057 unset($plugin_array[$key]); // Remove plugin from array.
1060 $plugins = implode(',', $plugin_array);
1061 if ($plugins != $user->plugins) {
1062 if (!ttTeamHelper::update($user->team_id, array('name' => $user->team,'plugins' => $plugins)))
1064 $user->plugins = $plugins;