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) {
74 $mdb2 = getConnection();
76 if (isset($options['getAllFields']))
77 $sql = "select * from tt_users where team_id = $user->team_id and status = 1 order by upper(name)";
79 $sql = "select id, name from tt_users where team_id = $user->team_id and status = 1 order by upper(name)";
80 $res = $mdb2->query($sql);
82 if (is_a($res, 'PEAR_Error'))
84 while ($val = $res->fetchRow()) {
88 if (isset($options['putSelfFirst'])) {
89 // Put own entry at the front.
90 $cnt = count($user_list);
91 for($i = 0; $i < $cnt; $i++) {
92 if ($user_list[$i]['id'] == $user->id) {
93 $self = $user_list[$i]; // Found self.
94 array_unshift($user_list, $self); // Put own entry at the front.
95 array_splice($user_list, $i+1, 1); // Remove duplicate.
102 // The getUsers obtains all active and inactive (but not deleted) users in a given team.
103 static function getUsers() {
105 $mdb2 = getConnection();
107 $sql = "select id, name from tt_users where team_id = $user->team_id and (status = 1 or status = 0) order by upper(name)";
108 $res = $mdb2->query($sql);
109 $user_list = array();
110 if (is_a($res, 'PEAR_Error'))
112 while ($val = $res->fetchRow()) {
119 // The getInactiveUsers obtains all inactive users in a given team.
120 static function getInactiveUsers($team_id, $all_fields = false) {
121 $mdb2 = getConnection();
124 $sql = "select * from tt_users where team_id = $team_id and status = 0 order by upper(name)";
126 $sql = "select id, name from tt_users where team_id = $team_id and status = 0 order by upper(name)";
127 $res = $mdb2->query($sql);
129 if (!is_a($res, 'PEAR_Error')) {
130 while ($val = $res->fetchRow()) {
138 // The getAllUsers obtains all users in a given team.
139 static function getAllUsers($team_id, $all_fields = false) {
140 $mdb2 = getConnection();
143 $sql = "select * from tt_users where team_id = $team_id order by upper(name)";
145 $sql = "select id, name from tt_users where team_id = $team_id order by upper(name)";
146 $res = $mdb2->query($sql);
148 if (!is_a($res, 'PEAR_Error')) {
149 while ($val = $res->fetchRow()) {
157 // getActiveProjects - returns an array of active projects for team.
158 static function getActiveProjects($team_id)
161 $mdb2 = getConnection();
163 $sql = "select id, name, description, tasks from tt_projects
164 where team_id = $team_id and status = 1 order by upper(name)";
165 $res = $mdb2->query($sql);
167 if (!is_a($res, 'PEAR_Error')) {
168 while ($val = $res->fetchRow()) {
175 // getInactiveProjects - returns an array of inactive projects for team.
176 static function getInactiveProjects($team_id)
179 $mdb2 = getConnection();
181 $sql = "select id, name, description, tasks from tt_projects
182 where team_id = $team_id and status = 0 order by upper(name)";
183 $res = $mdb2->query($sql);
185 if (!is_a($res, 'PEAR_Error')) {
186 while ($val = $res->fetchRow()) {
193 // The getAllProjects obtains all projects in a given team.
194 static function getAllProjects($team_id, $all_fields = false) {
195 $mdb2 = getConnection();
198 $sql = "select * from tt_projects where team_id = $team_id order by status, upper(name)";
200 $sql = "select id, name from tt_projects where team_id = $team_id order by status, upper(name)";
201 $res = $mdb2->query($sql);
203 if (!is_a($res, 'PEAR_Error')) {
204 while ($val = $res->fetchRow()) {
212 // getActiveTasks - returns an array of active tasks for team.
213 static function getActiveTasks($team_id)
216 $mdb2 = getConnection();
218 $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by upper(name)";
219 $res = $mdb2->query($sql);
221 if (!is_a($res, 'PEAR_Error')) {
222 while ($val = $res->fetchRow()) {
229 // getInactiveTasks - returns an array of inactive tasks for team.
230 static function getInactiveTasks($team_id)
233 $mdb2 = getConnection();
235 $sql = "select id, name, description from tt_tasks
236 where team_id = $team_id and status = 0 order by upper(name)";
237 $res = $mdb2->query($sql);
239 if (!is_a($res, 'PEAR_Error')) {
240 while ($val = $res->fetchRow()) {
247 // The getAllTasks obtains all tasks in a given team.
248 static function getAllTasks($team_id, $all_fields = false) {
249 $mdb2 = getConnection();
252 $sql = "select * from tt_tasks where team_id = $team_id order by status, upper(name)";
254 $sql = "select id, name from tt_tasks where team_id = $team_id order by status, upper(name)";
255 $res = $mdb2->query($sql);
257 if (!is_a($res, 'PEAR_Error')) {
258 while ($val = $res->fetchRow()) {
266 // The getActiveClients returns an array of active clients for team.
267 static function getActiveClients($team_id, $all_fields = false)
270 $mdb2 = getConnection();
273 $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
275 $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by upper(name)";
277 $res = $mdb2->query($sql);
279 if (!is_a($res, 'PEAR_Error')) {
280 while ($val = $res->fetchRow()) {
287 // The getInactiveClients returns an array of inactive clients for team.
288 static function getInactiveClients($team_id, $all_fields = false)
291 $mdb2 = getConnection();
294 $sql = "select * from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
296 $sql = "select id, name from tt_clients where team_id = $team_id and status = 0 order by upper(name)";
298 $res = $mdb2->query($sql);
300 if (!is_a($res, 'PEAR_Error')) {
301 while ($val = $res->fetchRow()) {
308 // The getAllClients obtains all clients in a given team.
309 static function getAllClients($team_id, $all_fields = false) {
310 $mdb2 = getConnection();
313 $sql = "select * from tt_clients where team_id = $team_id order by status, upper(name)";
315 $sql = "select id, name from tt_clients where team_id = $team_id order by status, upper(name)";
317 $res = $mdb2->query($sql);
319 if (!is_a($res, 'PEAR_Error')) {
320 while ($val = $res->fetchRow()) {
328 // The getActiveInvoices returns an array of active invoices for team.
329 static function getActiveInvoices($localizeDates = true)
332 $addPaidStatus = $user->isPluginEnabled('ps');
335 $mdb2 = getConnection();
337 if (ROLE_CLIENT == $user->role && $user->client_id)
338 $client_part = " and i.client_id = $user->client_id";
340 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
341 left join tt_clients c on (c.id = i.client_id)
342 where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
343 $res = $mdb2->query($sql);
345 if (!is_a($res, 'PEAR_Error')) {
346 $dt = new DateAndTime(DB_DATEFORMAT);
347 while ($val = $res->fetchRow()) {
348 if ($localizeDates) {
349 $dt->parseVal($val['date']);
350 $val['date'] = $dt->toString($user->date_format);
353 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
360 // The getAllInvoices returns an array of all invoices for team.
361 static function getAllInvoices()
366 $mdb2 = getConnection();
368 $sql = "select * from tt_invoices where team_id = $user->team_id";
369 $res = $mdb2->query($sql);
371 if (!is_a($res, 'PEAR_Error')) {
372 $dt = new DateAndTime(DB_DATEFORMAT);
373 while ($val = $res->fetchRow()) {
380 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
381 static function getRecentInvoices($team_id, $client_id)
386 $mdb2 = getConnection();
388 $sql = "select i.id, i.name from tt_invoices i
389 left join tt_clients c on (c.id = i.client_id)
390 where i.team_id = $team_id and i.status = 1 and c.id = $client_id
391 order by i.id desc limit 3";
392 $res = $mdb2->query($sql);
394 if (!is_a($res, 'PEAR_Error')) {
395 $dt = new DateAndTime(DB_DATEFORMAT);
396 while ($val = $res->fetchRow()) {
403 // getUserToProjectBinds - obtains all user to project binds for a team.
404 static function getUserToProjectBinds($team_id) {
405 $mdb2 = getConnection();
408 $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";
409 $res = $mdb2->query($sql);
411 if (!is_a($res, 'PEAR_Error')) {
412 while ($val = $res->fetchRow()) {
420 // The getAllCustomFields obtains all custom fields in a given team.
421 static function getAllCustomFields($team_id) {
422 $mdb2 = getConnection();
424 $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
426 $res = $mdb2->query($sql);
428 if (!is_a($res, 'PEAR_Error')) {
429 while ($val = $res->fetchRow()) {
437 // The getAllCustomFieldOptions obtains all custom field options in a given team.
438 static function getAllCustomFieldOptions($team_id) {
439 $mdb2 = getConnection();
441 $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";
443 $res = $mdb2->query($sql);
445 if (!is_a($res, 'PEAR_Error')) {
446 while ($val = $res->fetchRow()) {
454 // The getCustomFieldLog obtains all custom field log entries for a given team.
455 static function getCustomFieldLog($team_id) {
456 $mdb2 = getConnection();
458 $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";
460 $res = $mdb2->query($sql);
462 if (!is_a($res, 'PEAR_Error')) {
463 while ($val = $res->fetchRow()) {
471 // getFavReports - obtains all favorite reports for all users in team.
472 static function getFavReports($team_id) {
473 $mdb2 = getConnection();
476 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
477 $res = $mdb2->query($sql);
479 if (!is_a($res, 'PEAR_Error')) {
480 while ($val = $res->fetchRow()) {
488 // getExpenseItems - obtains all expense items for all users in team.
489 static function getExpenseItems($team_id) {
490 $mdb2 = getConnection();
493 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
494 $res = $mdb2->query($sql);
496 if (!is_a($res, 'PEAR_Error')) {
497 while ($val = $res->fetchRow()) {
505 // getPredefinedExpenses - obtains predefined expenses for team.
506 static function getPredefinedExpenses($team_id) {
508 $replaceDecimalMark = ('.' != $user->decimal_mark);
510 $mdb2 = getConnection();
513 $sql = "select id, name, cost from tt_predefined_expenses where team_id = $team_id";
514 $res = $mdb2->query($sql);
516 if (!is_a($res, 'PEAR_Error')) {
517 while ($val = $res->fetchRow()) {
518 if ($replaceDecimalMark)
519 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
527 // getNotifications - obtains notification descriptions for team.
528 static function getNotifications($team_id) {
529 $mdb2 = getConnection();
532 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
533 left join tt_fav_reports fr on (fr.id = c.report_id)
534 where c.team_id = $team_id and c.status = 1 and fr.status = 1";
535 $res = $mdb2->query($sql);
537 if (!is_a($res, 'PEAR_Error')) {
538 while ($val = $res->fetchRow()) {
546 // getMonthlyQuotas - obtains monthly quotas for team.
547 static function getMonthlyQuotas($team_id) {
548 $mdb2 = getConnection();
551 $sql = "select year, month, minutes from tt_monthly_quotas where team_id = $team_id";
552 $res = $mdb2->query($sql);
554 if (!is_a($res, 'PEAR_Error')) {
555 while ($val = $res->fetchRow()) {
563 // The getTeams function returns an array of all active teams on the server.
564 static function getTeams() {
566 $mdb2 = getConnection();
568 $sql = "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
569 $res = $mdb2->query($sql);
571 if (!is_a($res, 'PEAR_Error')) {
572 while ($val = $res->fetchRow()) {
573 $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
581 // The markDeleted function marks the team and everything in it as deleted.
582 static function markDeleted($team_id) {
584 // Iterate through team users and mark them as deleted.
585 $users = ttTeamHelper::getAllUsers($team_id);
586 foreach ($users as $one_user) {
587 if (!ttUserHelper::markDeleted($one_user['id'])) return false;
590 // Mark tasks deleted.
591 if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
593 $mdb2 = getConnection();
595 // Mark projects deleted.
596 $sql = "update tt_projects set status = NULL where team_id = $team_id";
597 $affected = $mdb2->exec($sql);
598 if (is_a($affected, 'PEAR_Error')) return false;
600 // Mark clients deleted.
601 $sql = "update tt_clients set status = NULL where team_id = $team_id";
602 $affected = $mdb2->exec($sql);
603 if (is_a($affected, 'PEAR_Error')) return false;
605 // Mark custom fields deleted.
606 $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
607 $affected = $mdb2->exec($sql);
608 if (is_a($affected, 'PEAR_Error')) return false;
610 // Mark team deleted.
611 $sql = "update tt_teams set status = NULL where id = $team_id";
612 $affected = $mdb2->exec($sql);
613 if (is_a($affected, 'PEAR_Error')) return false;
618 // The getTeamDetails function returns team details.
619 static function getTeamDetails($team_id) {
621 $mdb2 = getConnection();
623 $role_manager = ROLE_MANAGER;
624 $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
626 inner join tt_users u on (u.team_id = t.id and u.role = $role_manager)
627 where t.id = $team_id";
629 $res = $mdb2->query($sql);
630 if (!is_a($res, 'PEAR_Error')) {
631 $val = $res->fetchRow();
638 // The insert function creates a new team.
639 static function insert($fields) {
641 $mdb2 = getConnection();
643 // Start with team name and currency.
644 $columns = 'name, currency';
645 $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
647 if ($fields['decimal_mark']) {
648 $columns .= ', decimal_mark';
649 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
652 $lang = $fields['lang'];
657 $columns .= ', lang';
658 $values .= ', '.$mdb2->quote($lang);
660 if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
661 $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
662 $columns .= ', date_format';
663 $values .= ', '.$mdb2->quote($date_format);
666 $time_format = $fields['time_format'];
667 if ($time_format !== null) {
668 $time_format_f = ', time_format';
669 $time_format_v = ', ' . $mdb2->quote($time_format);
670 } elseif (defined('TIME_FORMAT_DEFAULT')) {
671 $time_format_f = ', time_format';
672 $time_format_v = ', ' . $mdb2->quote(TIME_FORMAT_DEFAULT);
678 $week_start = $fields['week_start'];
679 if ($week_start !== null) {
680 $week_start_f = ', week_start';
681 $week_start_v = ', ' . (int)$week_start;
682 } elseif (defined('WEEK_START_DEFAULT')) {
683 $week_start_f = ', week_start';
684 $week_start_v = ', ' . (int)WEEK_START_DEFAULT;
690 $tracking_mode = $fields['tracking_mode'];
691 if ($tracking_mode !== null) {
692 $tracking_mode_f = ', tracking_mode';
693 $tracking_mode_v = ', ' . (int)$tracking_mode;
695 $tracking_mode_f = '';
696 $tracking_mode_v = '';
699 $project_required = $fields['project_required'];
700 if ($project_required !== null) {
701 $project_required_f = ', project_required';
702 $project_required_v = ', ' . (int)$project_required;
704 $project_required_f = '';
705 $project_required_v = '';
708 $task_required = $fields['task_required'];
709 if ($task_required !== null) {
710 $task_required_f = ', task_required';
711 $task_required_v = ', ' . (int)$task_required;
713 $task_required_f = '';
714 $task_required_v = '';
717 $record_type = $fields['record_type'];
718 if ($record_type !== null) {
719 $record_type_f = ', record_type';
720 $record_type_v = ', ' . (int)$record_type;
726 $bcc_email = $fields['bcc_email'];
727 if ($bcc_email !== null) {
728 $bcc_email_f = ', bcc_email';
729 $bcc_email_v = ', ' . $mdb2->quote($bcc_email);
735 $plugins = $fields['plugins'];
736 if ($plugins !== null) {
737 $plugins_f = ', plugins';
738 $plugins_v = ', ' . $mdb2->quote($plugins);
744 $lock_spec = $fields['lock_spec'];
745 if ($lock_spec !== null) {
746 $lockspec_f = ', lock_spec';
747 $lockspec_v = ', ' . $mdb2->quote($lock_spec);
753 $workday_minutes = $fields['workday_minutes'];
754 if ($workday_minutes !== null) {
755 $workday_minutes_f = ', workday_minutes';
756 $workday_minutes_v = ', ' . (int)$workday_minutes;
758 $workday_minutes_f = '';
759 $workday_minutes_v = '';
762 $config = $fields['config'];
763 if ($config !== null) {
764 $config_f = ', config';
765 $config_v = ', ' . $mdb2->quote($config);
771 $sql = "insert into tt_teams ($columns $time_format_f $week_start_f $tracking_mode_f $project_required_f $task_required_f $record_type_f $bcc_email_f $plugins_f $lockspec_f $workday_minutes_f $config_f)
772 values($values $time_format_v $week_start_v $tracking_mode_v $project_required_v $task_required_v $record_type_v $bcc_email_v $plugins_v $lockspec_v $workday_minutes_v $config_v)";
773 $affected = $mdb2->exec($sql);
775 if (!is_a($affected, 'PEAR_Error')) {
776 $team_id = $mdb2->lastInsertID('tt_teams', 'id');
783 // The update function updates team information.
784 static function update($team_id, $fields)
786 $mdb2 = getConnection();
787 $name_part = 'name = '.$mdb2->quote($fields['name']);
790 $decimal_mark_part = '';
791 $date_format_part = '';
792 $time_format_part = '';
793 $week_start_part = '';
794 $tracking_mode_part = '';
795 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
796 $record_type_part = '';
797 $bcc_email_part = '';
800 $lock_spec_part = '';
801 $workday_minutes_part = '';
803 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
804 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
805 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
806 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
807 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
808 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
809 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
810 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
811 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
812 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
813 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
814 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
815 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
817 $sql = "update tt_teams set $name_part $currency_part $lang_part $decimal_mark_part
818 $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
819 $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part where id = $team_id";
820 $affected = $mdb2->exec($sql);
821 if (is_a($affected, 'PEAR_Error')) return false;
826 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
827 static function getInactiveTeams() {
828 $inactive_teams = array();
829 $mdb2 = getConnection();
831 // Get all team ids for teams created or modified more than 6 months ago.
832 // $ts = date('Y-m-d', strtotime('-1 year'));
833 $ts = date('Y-m-d', strtotime('-6 month'));
834 $sql = "select id from tt_teams where timestamp < '$ts' order by id";
835 $res = $mdb2->query($sql);
838 if (!is_a($res, 'PEAR_Error')) {
839 while ($val = $res->fetchRow()) {
840 $team_id = $val['id'];
841 if (ttTeamHelper::isTeamActive($team_id) == false) {
843 $inactive_teams[] = $team_id;
844 // Limit the array size for perfomance by allowing this operation on small chunks only.
845 if ($count >= 100) break;
848 return $inactive_teams;
853 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
854 static function isTeamActive($team_id) {
857 $mdb2 = getConnection();
858 $sql = "select id from tt_users where team_id = $team_id";
859 $res = $mdb2->query($sql);
860 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
861 while ($val = $res->fetchRow()) {
862 $users[] = $val['id'];
864 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
866 return false; // No users in team.
869 $ts = date('Y-m-d', strtotime('-2 years'));
870 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
871 $res = $mdb2->query($sql);
872 if (!is_a($res, 'PEAR_Error')) {
873 if ($val = $res->fetchRow()) {
874 $count = $val['cnt'];
879 return false; // No time entries for the last 2 years.
882 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
883 $count_last_year = 0;
884 $ts = date('Y-m-d', strtotime('-1 year'));
885 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
886 $res = $mdb2->query($sql);
887 if (!is_a($res, 'PEAR_Error')) {
888 if ($val = $res->fetchRow()) {
889 $count_last_year = $val['cnt'];
891 if ($count_last_year == 0)
892 return false; // No time entries for the last year and only a few entries before that.
898 // The delete function permanently deletes all data for a team.
899 static function delete($team_id) {
900 $mdb2 = getConnection();
903 $sql = "select id from tt_users where team_id = $team_id";
904 $res = $mdb2->query($sql);
905 if (is_a($res, 'PEAR_Error')) return false;
906 while ($val = $res->fetchRow()) {
907 $user_id = $val['id'];
908 if (!ttUserHelper::delete($user_id)) return false;
912 if (!ttTeamHelper::deleteTasks($team_id)) return false;
914 // Delete client to project binds.
915 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
916 $affected = $mdb2->exec($sql);
917 if (is_a($affected, 'PEAR_Error')) return false;
920 $sql = "delete from tt_projects where team_id = $team_id";
921 $affected = $mdb2->exec($sql);
922 if (is_a($affected, 'PEAR_Error')) return false;
925 $sql = "delete from tt_clients where team_id = $team_id";
926 $affected = $mdb2->exec($sql);
927 if (is_a($affected, 'PEAR_Error')) return false;
930 $sql = "delete from tt_invoices where team_id = $team_id";
931 $affected = $mdb2->exec($sql);
932 if (is_a($affected, 'PEAR_Error')) return false;
934 // Delete custom fields.
935 if (!ttTeamHelper::deleteCustomFields($team_id)) return false;
938 $sql = "delete from tt_teams where id = $team_id";
939 $affected = $mdb2->exec($sql);
940 if (is_a($affected, 'PEAR_Error')) return false;
945 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
946 static function markTasksDeleted($team_id) {
947 $mdb2 = getConnection();
948 $sql = "select id from tt_tasks where team_id = $team_id";
949 $res = $mdb2->query($sql);
950 if (is_a($res, 'PEAR_Error')) return false;
952 while ($val = $res->fetchRow()) {
954 // Delete task binds.
955 $task_id = $val['id'];
956 $sql = "delete from tt_project_task_binds where task_id = $task_id";
957 $affected = $mdb2->exec($sql);
958 if (is_a($affected, 'PEAR_Error')) return false;
960 // Mark task as deleted.
961 $sql = "update tt_tasks set status = NULL where id = $task_id";
962 $affected = $mdb2->exec($sql);
963 if (is_a($affected, 'PEAR_Error')) return false;
969 // The deleteTasks deletes all tasks and task binds for an inactive team.
970 static function deleteTasks($team_id) {
971 $mdb2 = getConnection();
972 $sql = "select id from tt_tasks where team_id = $team_id";
973 $res = $mdb2->query($sql);
974 if (is_a($res, 'PEAR_Error')) return false;
976 while ($val = $res->fetchRow()) {
978 // Delete task binds.
979 $task_id = $val['id'];
980 $sql = "delete from tt_project_task_binds where task_id = $task_id";
981 $affected = $mdb2->exec($sql);
982 if (is_a($affected, 'PEAR_Error')) return false;
985 $sql = "delete from tt_tasks where id = $task_id";
986 $affected = $mdb2->exec($sql);
987 if (is_a($affected, 'PEAR_Error')) return false;
993 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
994 static function deleteCustomFields($team_id) {
995 $mdb2 = getConnection();
996 $sql = "select id from tt_custom_fields where team_id = $team_id";
997 $res = $mdb2->query($sql);
998 if (is_a($res, 'PEAR_Error')) return false;
1000 while ($val = $res->fetchRow()) {
1001 $field_id = $val['id'];
1003 // Clean up tt_custom_field_log.
1004 $sql = "delete from tt_custom_field_log where field_id = $field_id";
1005 $affected = $mdb2->exec($sql);
1006 if (is_a($affected, 'PEAR_Error')) return false;
1008 // Clean up tt_custom_field_options.
1009 $sql = "delete from tt_custom_field_options where field_id = $field_id";
1010 $affected = $mdb2->exec($sql);
1011 if (is_a($affected, 'PEAR_Error')) return false;
1013 // Delete custom field.
1014 $sql = "delete from tt_custom_fields where id = $field_id";
1015 $affected = $mdb2->exec($sql);
1016 if (is_a($affected, 'PEAR_Error')) return false;
1022 // enablePlugin either enables or disables a specific plugin for team.
1023 static function enablePlugin($plugin, $enable = true)
1026 if (!$user->canManageTeam())
1029 $plugin_array = explode(',', $user->plugins);
1030 if ($enable && !in_array($plugin, $plugin_array))
1031 $plugin_array[] = $plugin; // Add plugin to array.
1033 if (!$enable && in_array($plugin, $plugin_array)) {
1034 $key = array_search($plugin, $plugin_array);
1036 unset($plugin_array[$key]); // Remove plugin from array.
1039 $plugins = implode(',', $plugin_array);
1040 if ($plugins != $user->plugins) {
1041 if (!ttTeamHelper::update($user->team_id, array('name' => $user->team,'plugins' => $plugins)))
1043 $user->plugins = $plugins;