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 if ($fields['lock_interval'] !== null) {
606 $locktime_f = ', locktime';
607 $locktime_v = ", " . (int)$fields['lock_interval'];
613 $lang = $fields['lang'];
619 $decimal_mark = $fields['decimal_mark'];
620 if ($decimal_mark !== null) {
621 $decimal_mark_f = ', decimal_mark';
622 $decimal_mark_v = ', ' . $mdb2->quote($decimal_mark);
624 $decimal_mark_f = '';
625 $decimal_mark_v = '';
628 $date_format = $fields['date_format'];
629 if ($date_format !== null) {
630 $date_format_f = ', date_format';
631 $date_format_v = ', ' . $mdb2->quote($date_format);
632 } elseif (defined('DATE_FORMAT_DEFAULT')) {
633 $date_format_f = ', date_format';
634 $date_format_v = ', ' . $mdb2->quote(DATE_FORMAT_DEFAULT);
640 $time_format = $fields['time_format'];
641 if ($time_format !== null) {
642 $time_format_f = ', time_format';
643 $time_format_v = ', ' . $mdb2->quote($time_format);
644 } elseif (defined('TIME_FORMAT_DEFAULT')) {
645 $time_format_f = ', time_format';
646 $time_format_v = ', ' . $mdb2->quote(TIME_FORMAT_DEFAULT);
652 $week_start = $fields['week_start'];
653 if ($week_start !== null) {
654 $week_start_f = ', week_start';
655 $week_start_v = ', ' . (int)$week_start;
656 } elseif (defined('WEEK_START_DEFAULT')) {
657 $week_start_f = ', week_start';
658 $week_start_v = ', ' . (int)WEEK_START_DEFAULT;
664 $plugins = $fields['plugins'];
665 if ($plugins !== null) {
666 $plugins_f = ', plugins';
667 $plugins_v = ', ' . $mdb2->quote($plugins);
673 $tracking_mode = $fields['tracking_mode'];
674 if ($tracking_mode !== null) {
675 $tracking_mode_f = ', tracking_mode';
676 $tracking_mode_v = ', ' . (int)$tracking_mode;
678 $tracking_mode_f = '';
679 $tracking_mode_v = '';
682 $record_type = $fields['record_type'];
683 if ($record_type !== null) {
684 $record_type_f = ', record_type';
685 $record_type_v = ', ' . (int)$record_type;
691 $sql = "insert into tt_teams (name, address, currency $locktime_f, lang $decimal_mark_f $date_format_f $time_format_f $week_start_f $plugins_f $tracking_mode_f $record_type_f)
693 $mdb2->quote(trim($fields['name'])).
694 ", ".$mdb2->quote(trim($fields['address'])).
695 ", ".$mdb2->quote(trim($fields['currency']))." $locktime_v, ".$mdb2->quote($lang).
696 "$decimal_mark_v $date_format_v $time_format_v $week_start_v $plugins_v $tracking_mode_v $record_type_v)";
697 $affected = $mdb2->exec($sql);
699 if (!is_a($affected, 'PEAR_Error')) {
700 $team_id = $mdb2->lastInsertID('tt_teams', 'id');
707 // The update function updates team information.
708 static function update($team_id, $fields)
710 // We'll require team name to be always set.
711 if (!isset($fields['name'])) return false;
713 $mdb2 = getConnection();
714 $name_part = 'name = '.$mdb2->quote($fields['name']);
719 $decimal_mark_part = '';
720 $date_format_part = '';
721 $time_format_part = '';
722 $week_start_part = '';
723 $tracking_mode_part = '';
724 $record_type_part = '';
726 $lock_spec_part = '';
728 if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
729 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
730 if (isset($fields['locktime'])) $locktime_part = ', locktime = '.intval($fields['locktime']);
731 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
732 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
733 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
734 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
735 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.intval($fields['week_start']);
736 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.intval($fields['tracking_mode']);
737 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
738 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
739 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
741 $sql = "update tt_teams set $name_part $addr_part $currency_part $locktime_part $lang_part $decimal_mark_part
742 $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part
743 $plugins_part $lock_spec_part where id = $team_id";
744 $affected = $mdb2->exec($sql);
746 if (is_a($affected, 'PEAR_Error')) {
753 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
754 static function getInactiveTeams() {
755 $inactive_teams = array();
756 $mdb2 = getConnection();
758 // Get all team ids for teams created or modified more than 1 year ago.
759 $ts = date('Y-m-d', strtotime('-1 year'));
760 $sql = "select id from tt_teams where timestamp < '$ts' order by id";
761 $res = $mdb2->query($sql);
764 if (!is_a($res, 'PEAR_Error')) {
765 while ($val = $res->fetchRow()) {
766 $team_id = $val['id'];
767 if (ttTeamHelper::isTeamActive($team_id) == false) {
769 $inactive_teams[] = $team_id;
770 // Limit the array size for perfomance by allowing this operation on small chunks only.
771 if ($count >= 25) break;
774 return $inactive_teams;
779 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
780 static function isTeamActive($team_id) {
783 $mdb2 = getConnection();
784 $sql = "select id from tt_users where team_id = $team_id";
785 $res = $mdb2->query($sql);
786 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
787 while ($val = $res->fetchRow()) {
788 $users[] = $val['id'];
790 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
792 return false; // No users in team.
795 $ts = date('Y-m-d', strtotime('-2 years'));
796 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
797 $res = $mdb2->query($sql);
798 if (!is_a($res, 'PEAR_Error')) {
799 if ($val = $res->fetchRow()) {
800 $count = $val['cnt'];
805 return false; // No time entries for the last 2 years.
808 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
809 $count_last_year = 0;
810 $ts = date('Y-m-d', strtotime('-1 year'));
811 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
812 $res = $mdb2->query($sql);
813 if (!is_a($res, 'PEAR_Error')) {
814 if ($val = $res->fetchRow()) {
815 $count_last_year = $val['cnt'];
817 if ($count_last_year == 0)
818 return false; // No time entries for the last year and only a few entries before that...
824 // The delete function permanently deletes all data for a team.
825 static function delete($team_id) {
826 $mdb2 = getConnection();
829 $sql = "select id from tt_users where team_id = $team_id";
830 $res = $mdb2->query($sql);
831 if (is_a($res, 'PEAR_Error')) return false;
832 while ($val = $res->fetchRow()) {
833 $user_id = $val['id'];
834 if (!ttUserHelper::delete($user_id))
839 if (!ttTeamHelper::deleteTasks($team_id))
842 // Delete client to project binds.
843 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
844 $affected = $mdb2->exec($sql);
845 if (is_a($affected, 'PEAR_Error'))
849 $sql = "delete from tt_projects where team_id = $team_id";
850 $affected = $mdb2->exec($sql);
851 if (is_a($affected, 'PEAR_Error'))
855 $sql = "delete from tt_clients where team_id = $team_id";
856 $affected = $mdb2->exec($sql);
857 if (is_a($affected, 'PEAR_Error'))
861 $sql = "delete from tt_invoices where team_id = $team_id";
862 $affected = $mdb2->exec($sql);
863 if (is_a($affected, 'PEAR_Error'))
866 // Delete custom fields.
867 if (!ttTeamHelper::deleteCustomFields($team_id))
871 $sql = "delete from tt_teams where id = $team_id";
872 $affected = $mdb2->exec($sql);
873 if (is_a($affected, 'PEAR_Error'))
879 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
880 static function markTasksDeleted($team_id) {
881 $mdb2 = getConnection();
882 $sql = "select id from tt_tasks where team_id = $team_id";
883 $res = $mdb2->query($sql);
884 if (is_a($res, 'PEAR_Error')) return false;
885 while ($val = $res->fetchRow()) {
887 // Delete task binds.
888 $task_id = $val['id'];
889 $sql = "delete from tt_project_task_binds where task_id = $task_id";
890 $affected = $mdb2->exec($sql);
891 if (is_a($affected, 'PEAR_Error'))
894 // Mark task as deleted.
895 $sql = "update tt_tasks set status = NULL where id = $task_id";
896 $affected = $mdb2->exec($sql);
897 if (is_a($affected, 'PEAR_Error'))
904 // The deleteTasks deletes all tasks and task binds for an inactive team.
905 static function deleteTasks($team_id) {
906 $mdb2 = getConnection();
907 $sql = "select id from tt_tasks where team_id = $team_id";
908 $res = $mdb2->query($sql);
909 if (is_a($res, 'PEAR_Error')) return false;
910 while ($val = $res->fetchRow()) {
912 // Delete task binds.
913 $task_id = $val['id'];
914 $sql = "delete from tt_project_task_binds where task_id = $task_id";
915 $affected = $mdb2->exec($sql);
916 if (is_a($affected, 'PEAR_Error'))
920 $sql = "delete from tt_tasks where id = $task_id";
921 $affected = $mdb2->exec($sql);
922 if (is_a($affected, 'PEAR_Error'))
929 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
930 static function deleteCustomFields($team_id) {
931 $mdb2 = getConnection();
932 $sql = "select id from tt_custom_fields where team_id = $team_id";
933 $res = $mdb2->query($sql);
934 if (is_a($res, 'PEAR_Error')) return false;
935 while ($val = $res->fetchRow()) {
936 $field_id = $val['id'];
938 // Clean up tt_custom_field_log.
939 $sql = "delete from tt_custom_field_log where field_id = $field_id";
940 $affected = $mdb2->exec($sql);
941 if (is_a($affected, 'PEAR_Error'))
944 // Clean up tt_custom_field_options.
945 $sql = "delete from tt_custom_field_options where field_id = $field_id";
946 $affected = $mdb2->exec($sql);
947 if (is_a($affected, 'PEAR_Error'))
950 // Delete custom field.
951 $sql = "delete from tt_custom_fields where id = $field_id";
952 $affected = $mdb2->exec($sql);
953 if (is_a($affected, 'PEAR_Error'))