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');
32 // Class ttTeamHelper - contains helper functions that operate with teams.
35 // The getUserCount function returns number of people in team.
36 static function getUserCount($team_id) {
37 $mdb2 = getConnection();
39 $sql = "select count(id) as cnt from tt_users where team_id = $team_id and status = 1";
40 $res = $mdb2->query($sql);
42 if (!is_a($res, 'PEAR_Error')) {
43 $val = $res->fetchRow();
49 // The getUsersForClient obtains all active and inactive users in a team that are relevant to a client.
50 static function getUsersForClient() {
52 $mdb2 = getConnection();
54 $sql = "select u.id, u.name from tt_user_project_binds upb
55 inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)
56 inner join tt_users u on (u.id = upb.user_id)
57 where (u.status = 1 or u.status = 0)
60 $res = $mdb2->query($sql);
62 if (is_a($res, 'PEAR_Error'))
64 while ($val = $res->fetchRow()) {
70 // The getActiveUsers obtains all active users in a given team.
71 static function getActiveUsers($options = null) {
73 $mdb2 = getConnection();
75 if (isset($options['getAllFields']))
76 $sql = "select * from tt_users where team_id = $user->team_id and status = 1 order by name";
78 $sql = "select id, name from tt_users where team_id = $user->team_id and status = 1 order by name";
79 $res = $mdb2->query($sql);
81 if (is_a($res, 'PEAR_Error'))
83 while ($val = $res->fetchRow()) {
87 if (isset($options['putSelfFirst'])) {
88 // Put own entry at the front.
89 $cnt = count($user_list);
90 for($i = 0; $i < $cnt; $i++) {
91 if ($user_list[$i]['id'] == $user->id) {
92 $self = $user_list[$i]; // Found self.
93 array_unshift($user_list, $self); // Put own entry at the front.
94 array_splice($user_list, $i+1, 1); // Remove duplicate.
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 name";
108 $res = $mdb2->query($sql);
109 $user_list = array();
110 if (is_a($res, 'PEAR_Error'))
112 while ($val = $res->fetchRow()) {
120 // The getInactiveUsers obtains all inactive users in a given team.
121 static function getInactiveUsers($team_id, $all_fields = false) {
122 $mdb2 = getConnection();
125 $sql = "select * from tt_users where team_id = $team_id and status = 0 order by name";
127 $sql = "select id, name from tt_users where team_id = $team_id and status = 0 order by name";
128 $res = $mdb2->query($sql);
130 if (!is_a($res, 'PEAR_Error')) {
131 while ($val = $res->fetchRow()) {
139 // The getAllUsers obtains all users in a given team.
140 static function getAllUsers($team_id, $all_fields = false) {
141 $mdb2 = getConnection();
144 $sql = "select * from tt_users where team_id = $team_id order by name";
146 $sql = "select id, name from tt_users where team_id = $team_id order by name";
147 $res = $mdb2->query($sql);
149 if (!is_a($res, 'PEAR_Error')) {
150 while ($val = $res->fetchRow()) {
158 // getActiveProjects - returns an array of active projects for team.
159 static function getActiveProjects($team_id)
162 $mdb2 = getConnection();
164 $sql = "select id, name, description, tasks from tt_projects
165 where team_id = $team_id and status = 1 order by name";
166 $res = $mdb2->query($sql);
168 if (!is_a($res, 'PEAR_Error')) {
169 while ($val = $res->fetchRow()) {
176 // getInactiveProjects - returns an array of inactive projects for team.
177 static function getInactiveProjects($team_id)
180 $mdb2 = getConnection();
182 $sql = "select id, name, description, tasks from tt_projects
183 where team_id = $team_id and status = 0 order by name";
184 $res = $mdb2->query($sql);
186 if (!is_a($res, 'PEAR_Error')) {
187 while ($val = $res->fetchRow()) {
194 // The getAllProjects obtains all projects in a given team.
195 static function getAllProjects($team_id, $all_fields = false) {
196 $mdb2 = getConnection();
199 $sql = "select * from tt_projects where team_id = $team_id order by status, name";
201 $sql = "select id, name from tt_projects where team_id = $team_id order by status, name";
202 $res = $mdb2->query($sql);
204 if (!is_a($res, 'PEAR_Error')) {
205 while ($val = $res->fetchRow()) {
213 // getActiveTasks - returns an array of active tasks for team.
214 static function getActiveTasks($team_id)
217 $mdb2 = getConnection();
219 $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by name";
220 $res = $mdb2->query($sql);
222 if (!is_a($res, 'PEAR_Error')) {
223 while ($val = $res->fetchRow()) {
230 // getInactiveTasks - returns an array of inactive tasks for team.
231 static function getInactiveTasks($team_id)
234 $mdb2 = getConnection();
236 $sql = "select id, name, description from tt_tasks
237 where team_id = $team_id and status = 0 order by name";
238 $res = $mdb2->query($sql);
240 if (!is_a($res, 'PEAR_Error')) {
241 while ($val = $res->fetchRow()) {
248 // The getAllTasks obtains all tasks in a given team.
249 static function getAllTasks($team_id, $all_fields = false) {
250 $mdb2 = getConnection();
253 $sql = "select * from tt_tasks where team_id = $team_id order by status, name";
255 $sql = "select id, name from tt_tasks where team_id = $team_id order by status, name";
256 $res = $mdb2->query($sql);
258 if (!is_a($res, 'PEAR_Error')) {
259 while ($val = $res->fetchRow()) {
267 // The getActiveClients returns an array of active clients for team.
268 static function getActiveClients($team_id, $all_fields = false)
271 $mdb2 = getConnection();
274 $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by name";
276 $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by 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 name";
296 $sql = "select id, name from tt_clients where team_id = $team_id and status = 0 order by name";
297 $res = $mdb2->query($sql);
299 if (!is_a($res, 'PEAR_Error')) {
300 while ($val = $res->fetchRow()) {
307 // The getAllClients obtains all clients in a given team.
308 static function getAllClients($team_id, $all_fields = false) {
309 $mdb2 = getConnection();
312 $sql = "select * from tt_clients where team_id = $team_id order by status, name";
314 $sql = "select id, name from tt_clients where team_id = $team_id order by status, name";
315 $res = $mdb2->query($sql);
317 if (!is_a($res, 'PEAR_Error')) {
318 while ($val = $res->fetchRow()) {
326 // The getActiveInvoices returns an array of active invoices for team.
327 static function getActiveInvoices($localizeDates = true)
332 $mdb2 = getConnection();
334 if (ROLE_CLIENT == $user->role && $user->client_id)
335 $client_part = " and i.client_id = $user->client_id";
337 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
338 left join tt_clients c on (c.id = i.client_id)
339 where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
340 $res = $mdb2->query($sql);
342 if (!is_a($res, 'PEAR_Error')) {
343 $dt = new DateAndTime(DB_DATEFORMAT);
344 while ($val = $res->fetchRow()) {
345 if ($localizeDates) {
346 $dt->parseVal($val['date']);
347 $val['date'] = $dt->toString($user->date_format);
355 // The getAllInvoices returns an array of all invoices for team.
356 static function getAllInvoices()
361 $mdb2 = getConnection();
363 $sql = "select * from tt_invoices where team_id = $user->team_id";
364 $res = $mdb2->query($sql);
366 if (!is_a($res, 'PEAR_Error')) {
367 $dt = new DateAndTime(DB_DATEFORMAT);
368 while ($val = $res->fetchRow()) {
375 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
376 static function getRecentInvoices($team_id, $client_id)
381 $mdb2 = getConnection();
383 $sql = "select i.id, i.name from tt_invoices i
384 left join tt_clients c on (c.id = i.client_id)
385 where i.team_id = $team_id and i.status = 1 and c.id = $client_id
386 order by i.id desc limit 3";
387 $res = $mdb2->query($sql);
389 if (!is_a($res, 'PEAR_Error')) {
390 $dt = new DateAndTime(DB_DATEFORMAT);
391 while ($val = $res->fetchRow()) {
398 // getUserToProjectBinds - obtains all user to project binds for a team.
399 static function getUserToProjectBinds($team_id) {
400 $mdb2 = getConnection();
403 $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";
404 $res = $mdb2->query($sql);
406 if (!is_a($res, 'PEAR_Error')) {
407 while ($val = $res->fetchRow()) {
415 // The getAllCustomFields obtains all custom fields in a given team.
416 static function getAllCustomFields($team_id) {
417 $mdb2 = getConnection();
419 $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
421 $res = $mdb2->query($sql);
423 if (!is_a($res, 'PEAR_Error')) {
424 while ($val = $res->fetchRow()) {
432 // The getAllCustomFieldOptions obtains all custom field options in a given team.
433 static function getAllCustomFieldOptions($team_id) {
434 $mdb2 = getConnection();
436 $sql = "select * from tt_custom_field_options where field_id in (select id from tt_custom_fields where team_id = $team_id) order by id";
438 $res = $mdb2->query($sql);
440 if (!is_a($res, 'PEAR_Error')) {
441 while ($val = $res->fetchRow()) {
449 // The getCustomFieldLog obtains all custom field log entries for a given team.
450 static function getCustomFieldLog($team_id) {
451 $mdb2 = getConnection();
453 $sql = "select * from tt_custom_field_log where field_id in (select id from tt_custom_fields where team_id = $team_id) order by id";
455 $res = $mdb2->query($sql);
457 if (!is_a($res, 'PEAR_Error')) {
458 while ($val = $res->fetchRow()) {
466 // getFavReports - obtains all favorite reports for all users in team.
467 static function getFavReports($team_id) {
468 $mdb2 = getConnection();
471 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
472 $res = $mdb2->query($sql);
474 if (!is_a($res, 'PEAR_Error')) {
475 while ($val = $res->fetchRow()) {
483 // getExpenseItems - obtains all expense items for all users in team.
484 static function getExpenseItems($team_id) {
485 $mdb2 = getConnection();
488 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
489 $res = $mdb2->query($sql);
491 if (!is_a($res, 'PEAR_Error')) {
492 while ($val = $res->fetchRow()) {
500 // getNotifications - obtains notification descriptions for team.
501 static function getNotifications($team_id) {
502 $mdb2 = getConnection();
505 $sql = "select c.id, c.cron_spec, c.email, fr.name from tt_cron c
506 left join tt_fav_reports fr on (fr.id = c.report_id)
507 where c.team_id = $team_id and c.status is not null";
508 $res = $mdb2->query($sql);
510 if (!is_a($res, 'PEAR_Error')) {
511 while ($val = $res->fetchRow()) {
519 // The getTeams function returns an array of all active teams on the server.
520 static function getTeams() {
522 $mdb2 = getConnection();
524 $sql = "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
525 $res = $mdb2->query($sql);
527 if (!is_a($res, 'PEAR_Error')) {
528 while ($val = $res->fetchRow()) {
529 $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
537 // The markDeleted function marks the team and everything in it as deleted.
538 static function markDeleted($team_id) {
540 // Iterate through team users and mark them as deleted.
541 $users = ttTeamHelper::getAllUsers($team_id);
542 foreach ($users as $one_user) {
543 if (!ttUserHelper::markDeleted($one_user['id']))
547 // Mark tasks deleted.
548 if (!ttTeamHelper::markTasksDeleted($team_id))
551 $mdb2 = getConnection();
553 // Mark projects deleted.
554 $sql = "update tt_projects set status = NULL where team_id = $team_id";
555 $affected = $mdb2->exec($sql);
556 if (is_a($affected, 'PEAR_Error'))
559 // Mark clients deleted.
560 $sql = "update tt_clients set status = NULL where team_id = $team_id";
561 $affected = $mdb2->exec($sql);
562 if (is_a($affected, 'PEAR_Error'))
565 // Mark custom fields deleted.
566 $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
567 $affected = $mdb2->exec($sql);
568 if (is_a($affected, 'PEAR_Error'))
571 // Mark team deleted.
572 $sql = "update tt_teams set status = NULL where id = $team_id";
573 $affected = $mdb2->exec($sql);
574 if (is_a($affected, 'PEAR_Error'))
580 // The getTeamDetails function returns team details.
581 static function getTeamDetails($team_id) {
583 $mdb2 = getConnection();
585 $role_manager = ROLE_MANAGER;
586 $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
588 inner join tt_users u on (u.team_id = t.id and u.role = $role_manager)
589 where t.id = $team_id";
591 $res = $mdb2->query($sql);
592 if (!is_a($res, 'PEAR_Error')) {
593 $val = $res->fetchRow();
600 // The insert function creates a new team.
601 static function insert($fields) {
603 $mdb2 = getConnection();
605 $lock_spec = $fields['lock_spec'];
606 if ($lock_spec !== null) {
607 $lockspec_f = ', lock_spec';
608 $lockspec_v = ', ' . $mdb2->quote($lock_spec);
614 if ($fields['lock_interval'] !== null) {
615 $locktime_f = ', locktime';
616 $locktime_v = ", " . (int)$fields['lock_interval'];
622 $lang = $fields['lang'];
628 $decimal_mark = $fields['decimal_mark'];
629 if ($decimal_mark !== null) {
630 $decimal_mark_f = ', decimal_mark';
631 $decimal_mark_v = ', ' . $mdb2->quote($decimal_mark);
633 $decimal_mark_f = '';
634 $decimal_mark_v = '';
637 $date_format = $fields['date_format'];
638 if ($date_format !== null) {
639 $date_format_f = ', date_format';
640 $date_format_v = ', ' . $mdb2->quote($date_format);
641 } elseif (defined('DATE_FORMAT_DEFAULT')) {
642 $date_format_f = ', date_format';
643 $date_format_v = ', ' . $mdb2->quote(DATE_FORMAT_DEFAULT);
649 $time_format = $fields['time_format'];
650 if ($time_format !== null) {
651 $time_format_f = ', time_format';
652 $time_format_v = ', ' . $mdb2->quote($time_format);
653 } elseif (defined('TIME_FORMAT_DEFAULT')) {
654 $time_format_f = ', time_format';
655 $time_format_v = ', ' . $mdb2->quote(TIME_FORMAT_DEFAULT);
661 $week_start = $fields['week_start'];
662 if ($week_start !== null) {
663 $week_start_f = ', week_start';
664 $week_start_v = ', ' . (int)$week_start;
665 } elseif (defined('WEEK_START_DEFAULT')) {
666 $week_start_f = ', week_start';
667 $week_start_v = ', ' . (int)WEEK_START_DEFAULT;
673 $plugins = $fields['plugins'];
674 if ($plugins !== null) {
675 $plugins_f = ', plugins';
676 $plugins_v = ', ' . $mdb2->quote($plugins);
682 $tracking_mode = $fields['tracking_mode'];
683 if ($tracking_mode !== null) {
684 $tracking_mode_f = ', tracking_mode';
685 $tracking_mode_v = ', ' . (int)$tracking_mode;
687 $tracking_mode_f = '';
688 $tracking_mode_v = '';
691 $record_type = $fields['record_type'];
692 if ($record_type !== null) {
693 $record_type_f = ', record_type';
694 $record_type_v = ', ' . (int)$record_type;
700 $sql = "insert into tt_teams (name, address, currency $lockspec_f $locktime_f, lang $decimal_mark_f $date_format_f $time_format_f $week_start_f $plugins_f $tracking_mode_f $record_type_f)
702 $mdb2->quote(trim($fields['name'])).
703 ", ".$mdb2->quote(trim($fields['address'])).
704 ", ".$mdb2->quote(trim($fields['currency']))." $lockspec_v $locktime_v, ".$mdb2->quote($lang).
705 "$decimal_mark_v $date_format_v $time_format_v $week_start_v $plugins_v $tracking_mode_v $record_type_v)";
706 $affected = $mdb2->exec($sql);
708 if (!is_a($affected, 'PEAR_Error')) {
709 $team_id = $mdb2->lastInsertID('tt_teams', 'id');
716 // The update function updates team information.
717 static function update($team_id, $fields)
719 // We'll require team name to be always set.
720 if (!isset($fields['name'])) return false;
722 $mdb2 = getConnection();
723 $name_part = 'name = '.$mdb2->quote($fields['name']);
728 $decimal_mark_part = '';
729 $date_format_part = '';
730 $time_format_part = '';
731 $week_start_part = '';
732 $tracking_mode_part = '';
733 $record_type_part = '';
735 $lock_spec_part = '';
737 if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
738 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
739 if (isset($fields['locktime'])) $locktime_part = ', locktime = '.intval($fields['locktime']);
740 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
741 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
742 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
743 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
744 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.intval($fields['week_start']);
745 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.intval($fields['tracking_mode']);
746 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
747 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
748 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
750 $sql = "update tt_teams set $name_part $addr_part $currency_part $locktime_part $lang_part $decimal_mark_part
751 $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part
752 $plugins_part $lock_spec_part where id = $team_id";
753 $affected = $mdb2->exec($sql);
755 if (is_a($affected, 'PEAR_Error')) {
762 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
763 static function getInactiveTeams() {
764 $inactive_teams = array();
765 $mdb2 = getConnection();
767 // Get all team ids for teams created or modified more than 1 year ago.
768 $ts = date('Y-m-d', strtotime('-1 year'));
769 $sql = "select id from tt_teams where timestamp < '$ts' order by id";
770 $res = $mdb2->query($sql);
773 if (!is_a($res, 'PEAR_Error')) {
774 while ($val = $res->fetchRow()) {
775 $team_id = $val['id'];
776 if (ttTeamHelper::isTeamActive($team_id) == false) {
778 $inactive_teams[] = $team_id;
779 // Limit the array size for perfomance by allowing this operation on small chunks only.
780 if ($count >= 25) break;
783 return $inactive_teams;
788 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
789 static function isTeamActive($team_id) {
792 $mdb2 = getConnection();
793 $sql = "select id from tt_users where team_id = $team_id";
794 $res = $mdb2->query($sql);
795 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
796 while ($val = $res->fetchRow()) {
797 $users[] = $val['id'];
799 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
801 return false; // No users in team.
804 $ts = date('Y-m-d', strtotime('-2 years'));
805 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
806 $res = $mdb2->query($sql);
807 if (!is_a($res, 'PEAR_Error')) {
808 if ($val = $res->fetchRow()) {
809 $count = $val['cnt'];
814 return false; // No time entries for the last 2 years.
817 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
818 $count_last_year = 0;
819 $ts = date('Y-m-d', strtotime('-1 year'));
820 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
821 $res = $mdb2->query($sql);
822 if (!is_a($res, 'PEAR_Error')) {
823 if ($val = $res->fetchRow()) {
824 $count_last_year = $val['cnt'];
826 if ($count_last_year == 0)
827 return false; // No time entries for the last year and only a few entries before that...
833 // The delete function permanently deletes all data for a team.
834 static function delete($team_id) {
835 $mdb2 = getConnection();
838 $sql = "select id from tt_users where team_id = $team_id";
839 $res = $mdb2->query($sql);
840 if (is_a($res, 'PEAR_Error')) return false;
841 while ($val = $res->fetchRow()) {
842 $user_id = $val['id'];
843 if (!ttUserHelper::delete($user_id))
848 if (!ttTeamHelper::deleteTasks($team_id))
851 // Delete client to project binds.
852 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
853 $affected = $mdb2->exec($sql);
854 if (is_a($affected, 'PEAR_Error'))
858 $sql = "delete from tt_projects where team_id = $team_id";
859 $affected = $mdb2->exec($sql);
860 if (is_a($affected, 'PEAR_Error'))
864 $sql = "delete from tt_clients where team_id = $team_id";
865 $affected = $mdb2->exec($sql);
866 if (is_a($affected, 'PEAR_Error'))
870 $sql = "delete from tt_invoices where team_id = $team_id";
871 $affected = $mdb2->exec($sql);
872 if (is_a($affected, 'PEAR_Error'))
875 // Delete custom fields.
876 if (!ttTeamHelper::deleteCustomFields($team_id))
880 $sql = "delete from tt_teams where id = $team_id";
881 $affected = $mdb2->exec($sql);
882 if (is_a($affected, 'PEAR_Error'))
888 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
889 static function markTasksDeleted($team_id) {
890 $mdb2 = getConnection();
891 $sql = "select id from tt_tasks where team_id = $team_id";
892 $res = $mdb2->query($sql);
893 if (is_a($res, 'PEAR_Error')) return false;
894 while ($val = $res->fetchRow()) {
896 // Delete task binds.
897 $task_id = $val['id'];
898 $sql = "delete from tt_project_task_binds where task_id = $task_id";
899 $affected = $mdb2->exec($sql);
900 if (is_a($affected, 'PEAR_Error'))
903 // Mark task as deleted.
904 $sql = "update tt_tasks set status = NULL where id = $task_id";
905 $affected = $mdb2->exec($sql);
906 if (is_a($affected, 'PEAR_Error'))
913 // The deleteTasks deletes all tasks and task binds for an inactive team.
914 static function deleteTasks($team_id) {
915 $mdb2 = getConnection();
916 $sql = "select id from tt_tasks where team_id = $team_id";
917 $res = $mdb2->query($sql);
918 if (is_a($res, 'PEAR_Error')) return false;
919 while ($val = $res->fetchRow()) {
921 // Delete task binds.
922 $task_id = $val['id'];
923 $sql = "delete from tt_project_task_binds where task_id = $task_id";
924 $affected = $mdb2->exec($sql);
925 if (is_a($affected, 'PEAR_Error'))
929 $sql = "delete from tt_tasks where id = $task_id";
930 $affected = $mdb2->exec($sql);
931 if (is_a($affected, 'PEAR_Error'))
938 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
939 static function deleteCustomFields($team_id) {
940 $mdb2 = getConnection();
941 $sql = "select id from tt_custom_fields where team_id = $team_id";
942 $res = $mdb2->query($sql);
943 if (is_a($res, 'PEAR_Error')) return false;
944 while ($val = $res->fetchRow()) {
945 $field_id = $val['id'];
947 // Clean up tt_custom_field_log.
948 $sql = "delete from tt_custom_field_log where field_id = $field_id";
949 $affected = $mdb2->exec($sql);
950 if (is_a($affected, 'PEAR_Error'))
953 // Clean up tt_custom_field_options.
954 $sql = "delete from tt_custom_field_options where field_id = $field_id";
955 $affected = $mdb2->exec($sql);
956 if (is_a($affected, 'PEAR_Error'))
959 // Delete custom field.
960 $sql = "delete from tt_custom_fields where id = $field_id";
961 $affected = $mdb2->exec($sql);
962 if (is_a($affected, 'PEAR_Error'))