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.
 
 101   // The getUsers obtains all active and inactive (but not deleted) users in a given team.
 
 102   static function getUsers() {
 
 104     $mdb2 = getConnection();
 
 106     $sql = "select id, name from tt_users where team_id = $user->team_id and (status = 1 or status = 0) order by name";
 
 107     $res = $mdb2->query($sql);
 
 108     $user_list = array();
 
 109     if (is_a($res, 'PEAR_Error'))
 
 111     while ($val = $res->fetchRow()) {
 
 118   // The getInactiveUsers obtains all inactive users in a given team.
 
 119   static function getInactiveUsers($team_id, $all_fields = false) {
 
 120     $mdb2 = getConnection();
 
 123       $sql = "select * from tt_users where team_id = $team_id and status = 0 order by name";
 
 125       $sql = "select id, name from tt_users where team_id = $team_id and status = 0 order by name";
 
 126     $res = $mdb2->query($sql);
 
 128     if (!is_a($res, 'PEAR_Error')) {
 
 129       while ($val = $res->fetchRow()) {
 
 137   // The getAllUsers obtains all users in a given team.
 
 138   static function getAllUsers($team_id, $all_fields = false) {
 
 139     $mdb2 = getConnection();
 
 142       $sql = "select * from tt_users where team_id = $team_id order by name";
 
 144       $sql = "select id, name from tt_users where team_id = $team_id order by name";
 
 145     $res = $mdb2->query($sql);
 
 147     if (!is_a($res, 'PEAR_Error')) {
 
 148       while ($val = $res->fetchRow()) {
 
 156   // getActiveProjects - returns an array of active projects for team.
 
 157   static function getActiveProjects($team_id)
 
 160     $mdb2 = getConnection();
 
 162     $sql = "select id, name, description, tasks from tt_projects
 
 163       where team_id = $team_id and status = 1 order by name";
 
 164     $res = $mdb2->query($sql);
 
 166     if (!is_a($res, 'PEAR_Error')) {
 
 167       while ($val = $res->fetchRow()) {
 
 174   // getInactiveProjects - returns an array of inactive projects for team.
 
 175   static function getInactiveProjects($team_id)
 
 178     $mdb2 = getConnection();
 
 180     $sql = "select id, name, description, tasks from tt_projects
 
 181       where team_id = $team_id and status = 0 order by name";
 
 182     $res = $mdb2->query($sql);
 
 184     if (!is_a($res, 'PEAR_Error')) {
 
 185       while ($val = $res->fetchRow()) {
 
 192   // The getAllProjects obtains all projects in a given team.
 
 193   static function getAllProjects($team_id, $all_fields = false) {
 
 194     $mdb2 = getConnection();
 
 197       $sql = "select * from tt_projects where team_id = $team_id order by status, name";
 
 199       $sql = "select id, name from tt_projects where team_id = $team_id order by status, name";
 
 200     $res = $mdb2->query($sql);
 
 202     if (!is_a($res, 'PEAR_Error')) {
 
 203       while ($val = $res->fetchRow()) {
 
 211   // getActiveTasks - returns an array of active tasks for team.
 
 212   static function getActiveTasks($team_id)
 
 215     $mdb2 = getConnection();
 
 217     $sql = "select id, name, description from tt_tasks where team_id = $team_id and status = 1 order by name";
 
 218     $res = $mdb2->query($sql);
 
 220     if (!is_a($res, 'PEAR_Error')) {
 
 221       while ($val = $res->fetchRow()) {
 
 228   // getInactiveTasks - returns an array of inactive tasks for team.
 
 229   static function getInactiveTasks($team_id)
 
 232     $mdb2 = getConnection();
 
 234     $sql = "select id, name, description from tt_tasks
 
 235       where team_id = $team_id and status = 0 order by name";
 
 236     $res = $mdb2->query($sql);
 
 238     if (!is_a($res, 'PEAR_Error')) {
 
 239       while ($val = $res->fetchRow()) {
 
 246   // The getAllTasks obtains all tasks in a given team.
 
 247   static function getAllTasks($team_id, $all_fields = false) {
 
 248     $mdb2 = getConnection();
 
 251       $sql = "select * from tt_tasks where team_id = $team_id order by status, name";
 
 253       $sql = "select id, name from tt_tasks where team_id = $team_id order by status, name";
 
 254     $res = $mdb2->query($sql);
 
 256     if (!is_a($res, 'PEAR_Error')) {
 
 257       while ($val = $res->fetchRow()) {
 
 265   // The getActiveClients returns an array of active clients for team.
 
 266   static function getActiveClients($team_id, $all_fields = false)
 
 269     $mdb2 = getConnection();
 
 272       $sql = "select * from tt_clients where team_id = $team_id and status = 1 order by name";
 
 274       $sql = "select id, name from tt_clients where team_id = $team_id and status = 1 order by name";
 
 276     $res = $mdb2->query($sql);
 
 278     if (!is_a($res, 'PEAR_Error')) {
 
 279       while ($val = $res->fetchRow()) {
 
 286   // The getInactiveClients returns an array of inactive clients for team.
 
 287   static function getInactiveClients($team_id, $all_fields = false)
 
 290     $mdb2 = getConnection();
 
 293       $sql = "select * from tt_clients where team_id = $team_id and status = 0 order by name";
 
 295       $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";
 
 316     $res = $mdb2->query($sql);
 
 318     if (!is_a($res, 'PEAR_Error')) {
 
 319       while ($val = $res->fetchRow()) {
 
 327   // The getActiveInvoices returns an array of active invoices for team.
 
 328   static function getActiveInvoices($localizeDates = true)
 
 333     $mdb2 = getConnection();
 
 335     if (ROLE_CLIENT == $user->role && $user->client_id)
 
 336       $client_part = " and i.client_id = $user->client_id";
 
 338     $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
 
 339       left join tt_clients c on (c.id = i.client_id)
 
 340       where i.status = 1 and i.team_id = $user->team_id $client_part order by i.name";
 
 341     $res = $mdb2->query($sql);
 
 343     if (!is_a($res, 'PEAR_Error')) {
 
 344       $dt = new DateAndTime(DB_DATEFORMAT);
 
 345       while ($val = $res->fetchRow()) {
 
 346         if ($localizeDates) {
 
 347           $dt->parseVal($val['date']);
 
 348           $val['date'] = $dt->toString($user->date_format);
 
 356   // The getAllInvoices returns an array of all invoices for team.
 
 357   static function getAllInvoices()
 
 362     $mdb2 = getConnection();
 
 364     $sql = "select * from tt_invoices where team_id = $user->team_id";
 
 365     $res = $mdb2->query($sql);
 
 367     if (!is_a($res, 'PEAR_Error')) {
 
 368       $dt = new DateAndTime(DB_DATEFORMAT);
 
 369       while ($val = $res->fetchRow()) {
 
 376   // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
 
 377   static function getRecentInvoices($team_id, $client_id)
 
 382     $mdb2 = getConnection();
 
 384     $sql = "select i.id, i.name from tt_invoices i
 
 385       left join tt_clients c on (c.id = i.client_id)
 
 386       where i.team_id = $team_id and i.status = 1 and c.id = $client_id
 
 387       order by i.id desc limit 3";
 
 388     $res = $mdb2->query($sql);
 
 390     if (!is_a($res, 'PEAR_Error')) {
 
 391       $dt = new DateAndTime(DB_DATEFORMAT);
 
 392       while ($val = $res->fetchRow()) {
 
 399   // getUserToProjectBinds - obtains all user to project binds for a team.
 
 400   static function getUserToProjectBinds($team_id) {
 
 401     $mdb2 = getConnection();
 
 404     $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";
 
 405     $res = $mdb2->query($sql);
 
 407     if (!is_a($res, 'PEAR_Error')) {
 
 408       while ($val = $res->fetchRow()) {
 
 416   // The getAllCustomFields obtains all custom fields in a given team.
 
 417   static function getAllCustomFields($team_id) {
 
 418     $mdb2 = getConnection();
 
 420     $sql = "select * from tt_custom_fields where team_id = $team_id order by status";
 
 422     $res = $mdb2->query($sql);
 
 424     if (!is_a($res, 'PEAR_Error')) {
 
 425       while ($val = $res->fetchRow()) {
 
 433   // The getAllCustomFieldOptions obtains all custom field options in a given team.
 
 434   static function getAllCustomFieldOptions($team_id) {
 
 435     $mdb2 = getConnection();
 
 437     $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";
 
 439     $res = $mdb2->query($sql);
 
 441     if (!is_a($res, 'PEAR_Error')) {
 
 442       while ($val = $res->fetchRow()) {
 
 450   // The getCustomFieldLog obtains all custom field log entries for a given team.
 
 451   static function getCustomFieldLog($team_id) {
 
 452     $mdb2 = getConnection();
 
 454     $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";
 
 456     $res = $mdb2->query($sql);
 
 458     if (!is_a($res, 'PEAR_Error')) {
 
 459       while ($val = $res->fetchRow()) {
 
 467   // getFavReports - obtains all favorite reports for all users in team.
 
 468   static function getFavReports($team_id) {
 
 469     $mdb2 = getConnection();
 
 472     $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where team_id = $team_id)";
 
 473     $res = $mdb2->query($sql);
 
 475     if (!is_a($res, 'PEAR_Error')) {
 
 476       while ($val = $res->fetchRow()) {
 
 484   // getExpenseItems - obtains all expense items for all users in team.
 
 485   static function getExpenseItems($team_id) {
 
 486     $mdb2 = getConnection();
 
 489     $sql = "select * from tt_expense_items where user_id in (select id from tt_users where team_id = $team_id)";
 
 490     $res = $mdb2->query($sql);
 
 492     if (!is_a($res, 'PEAR_Error')) {
 
 493       while ($val = $res->fetchRow()) {
 
 501   // getNotifications - obtains notification descriptions for team.
 
 502   static function getNotifications($team_id) {
 
 503     $mdb2 = getConnection();
 
 506     $sql = "select c.id, c.cron_spec, c.email, fr.name from tt_cron c
 
 507       left join tt_fav_reports fr on (fr.id = c.report_id)
 
 508       where c.team_id = $team_id and c.status is not null";
 
 509     $res = $mdb2->query($sql);
 
 511     if (!is_a($res, 'PEAR_Error')) {
 
 512       while ($val = $res->fetchRow()) {
 
 520   // The getTeams function returns an array of all active teams on the server.
 
 521   static function getTeams() {
 
 523     $mdb2 = getConnection();
 
 525     $sql =  "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
 
 526     $res = $mdb2->query($sql);
 
 528     if (!is_a($res, 'PEAR_Error')) {
 
 529       while ($val = $res->fetchRow()) {
 
 530         $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
 
 538   // The markDeleted function marks the team and everything in it as deleted.
 
 539   static function markDeleted($team_id) {
 
 541     // Iterate through team users and mark them as deleted.
 
 542     $users = ttTeamHelper::getAllUsers($team_id);
 
 543     foreach ($users as $one_user) {
 
 544       if (!ttUserHelper::markDeleted($one_user['id'])) return false;
 
 547     // Mark tasks deleted.
 
 548     if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
 
 550     $mdb2 = getConnection();
 
 552     // Mark projects deleted.
 
 553     $sql = "update tt_projects set status = NULL where team_id = $team_id";
 
 554     $affected = $mdb2->exec($sql);
 
 555     if (is_a($affected, 'PEAR_Error')) return false;
 
 557     // Mark clients deleted.
 
 558     $sql = "update tt_clients set status = NULL where team_id = $team_id";
 
 559     $affected = $mdb2->exec($sql);
 
 560     if (is_a($affected, 'PEAR_Error')) return false;
 
 562     // Mark custom fields deleted.
 
 563     $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
 
 564     $affected = $mdb2->exec($sql);
 
 565     if (is_a($affected, 'PEAR_Error')) return false;
 
 567     // Mark team deleted.
 
 568     $sql = "update tt_teams set status = NULL where id = $team_id";
 
 569     $affected = $mdb2->exec($sql);
 
 570     if (is_a($affected, 'PEAR_Error')) return false;
 
 575   // The getTeamDetails function returns team details.
 
 576   static function getTeamDetails($team_id) {
 
 578     $mdb2 = getConnection();
 
 580     $role_manager = ROLE_MANAGER;
 
 581     $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
 
 583       inner join tt_users u on (u.team_id = t.id and u.role = $role_manager)
 
 584       where t.id = $team_id";
 
 586     $res = $mdb2->query($sql);
 
 587     if (!is_a($res, 'PEAR_Error')) {
 
 588       $val = $res->fetchRow();
 
 595   // The insert function creates a new team.
 
 596   static function insert($fields) {
 
 598     $mdb2 = getConnection();
 
 600     $lock_spec = $fields['lock_spec'];
 
 601     if ($lock_spec !== null) {
 
 602       $lockspec_f = ', lock_spec';
 
 603       $lockspec_v = ', ' . $mdb2->quote($lock_spec);
 
 609     $lang = $fields['lang'];
 
 615     $decimal_mark = $fields['decimal_mark'];
 
 616     if ($decimal_mark !== null) {
 
 617       $decimal_mark_f = ', decimal_mark';
 
 618       $decimal_mark_v = ', ' . $mdb2->quote($decimal_mark);
 
 620       $decimal_mark_f = '';
 
 621       $decimal_mark_v = '';
 
 624     $date_format = $fields['date_format'];
 
 625     if ($date_format !== null) {
 
 626       $date_format_f = ', date_format';
 
 627       $date_format_v = ', ' . $mdb2->quote($date_format);
 
 628     } elseif (defined('DATE_FORMAT_DEFAULT')) {
 
 629       $date_format_f = ', date_format';
 
 630       $date_format_v = ', ' . $mdb2->quote(DATE_FORMAT_DEFAULT);
 
 636     $time_format = $fields['time_format'];
 
 637     if ($time_format !== null) {
 
 638       $time_format_f = ', time_format';
 
 639       $time_format_v = ', ' . $mdb2->quote($time_format);
 
 640     } elseif (defined('TIME_FORMAT_DEFAULT')) {
 
 641       $time_format_f = ', time_format';
 
 642       $time_format_v = ', ' . $mdb2->quote(TIME_FORMAT_DEFAULT);
 
 648     $week_start = $fields['week_start'];
 
 649     if ($week_start !== null) {
 
 650       $week_start_f = ', week_start';
 
 651       $week_start_v = ', ' . (int)$week_start;
 
 652     } elseif (defined('WEEK_START_DEFAULT')) {
 
 653       $week_start_f = ', week_start';
 
 654       $week_start_v = ', ' . (int)WEEK_START_DEFAULT;
 
 660     $plugins = $fields['plugins'];
 
 661     if ($plugins !== null) {
 
 662       $plugins_f = ', plugins';
 
 663       $plugins_v = ', ' . $mdb2->quote($plugins);
 
 669     $tracking_mode = $fields['tracking_mode'];
 
 670     if ($tracking_mode !== null) {
 
 671       $tracking_mode_f = ', tracking_mode';
 
 672       $tracking_mode_v = ', ' . (int)$tracking_mode;
 
 674       $tracking_mode_f = '';
 
 675       $tracking_mode_v = '';
 
 678     $record_type = $fields['record_type'];
 
 679     if ($record_type !== null) {
 
 680       $record_type_f = ', record_type';
 
 681       $record_type_v = ', ' . (int)$record_type;
 
 687     $sql = "insert into tt_teams (name, address, currency $lockspec_f, lang $decimal_mark_f $date_format_f $time_format_f $week_start_f $plugins_f $tracking_mode_f $record_type_f)
 
 688       values(".$mdb2->quote(trim($fields['name'])).
 
 689       ", ".$mdb2->quote(trim($fields['address'])).
 
 690       ", ".$mdb2->quote(trim($fields['currency']))." $lockspec_v, ".$mdb2->quote($lang).
 
 691       "$decimal_mark_v $date_format_v $time_format_v $week_start_v $plugins_v $tracking_mode_v $record_type_v)";
 
 692     $affected = $mdb2->exec($sql);
 
 694     if (!is_a($affected, 'PEAR_Error')) {
 
 695       $team_id = $mdb2->lastInsertID('tt_teams', 'id');
 
 702   // The update function updates team information.
 
 703   static function update($team_id, $fields)
 
 705     // We'll require team name to be always set.
 
 706     if (!isset($fields['name'])) return false;
 
 708     $mdb2 = getConnection();
 
 709     $name_part = 'name = '.$mdb2->quote($fields['name']);
 
 713     $decimal_mark_part = '';
 
 714     $date_format_part = '';
 
 715     $time_format_part = '';
 
 716     $week_start_part = '';
 
 717     $tracking_mode_part = '';
 
 718     $record_type_part = '';
 
 720     $lock_spec_part = '';
 
 722     if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
 
 723     if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
 
 724     if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
 
 725     if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
 
 726     if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
 
 727     if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
 
 728     if (isset($fields['week_start'])) $week_start_part = ', week_start = '.intval($fields['week_start']);
 
 729     if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.intval($fields['tracking_mode']);
 
 730     if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
 
 731     if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
 
 732     if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
 
 734     $sql = "update tt_teams set $name_part $addr_part $currency_part $lang_part $decimal_mark_part
 
 735       $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part
 
 736       $plugins_part $lock_spec_part where id = $team_id";
 
 737     $affected = $mdb2->exec($sql);
 
 738     if (is_a($affected, 'PEAR_Error')) return false;
 
 743   // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 25).
 
 744   static function getInactiveTeams() {
 
 745     $inactive_teams = array();
 
 746     $mdb2 = getConnection();
 
 748     // Get all team ids for teams created or modified more than 1 year ago.
 
 749     $ts = date('Y-m-d', strtotime('-1 year'));
 
 750     $sql =  "select id from tt_teams where timestamp < '$ts' order by id";
 
 751     $res = $mdb2->query($sql);
 
 754     if (!is_a($res, 'PEAR_Error')) {
 
 755       while ($val = $res->fetchRow()) {
 
 756         $team_id = $val['id'];
 
 757         if (ttTeamHelper::isTeamActive($team_id) == false) {
 
 759           $inactive_teams[] = $team_id;
 
 760           // Limit the array size for perfomance by allowing this operation on small chunks only.
 
 761           if ($count >= 100) break;
 
 764       return $inactive_teams;
 
 769   // The isTeamActive determines if a team is using Time Tracker or abandoned it.
 
 770   static function isTeamActive($team_id) {
 
 773     $mdb2 = getConnection();
 
 774     $sql = "select id from tt_users where team_id = $team_id";
 
 775     $res = $mdb2->query($sql);
 
 776     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
 
 777     while ($val = $res->fetchRow()) {
 
 778       $users[] = $val['id'];
 
 780     $user_list = implode(',', $users); // This is a comma-separated list of user ids.
 
 782       return false; // No users in team.
 
 785     $ts = date('Y-m-d', strtotime('-2 years'));
 
 786     $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
 
 787     $res = $mdb2->query($sql);
 
 788     if (!is_a($res, 'PEAR_Error')) {
 
 789       if ($val = $res->fetchRow()) {
 
 790         $count = $val['cnt'];
 
 795       return false;  // No time entries for the last 2 years.
 
 798       // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
 
 799       $count_last_year = 0;
 
 800       $ts = date('Y-m-d', strtotime('-1 year'));
 
 801       $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";
 
 802       $res = $mdb2->query($sql);
 
 803       if (!is_a($res, 'PEAR_Error')) {
 
 804         if ($val = $res->fetchRow()) {
 
 805           $count_last_year = $val['cnt'];
 
 807         if ($count_last_year == 0)
 
 808           return false;  // No time entries for the last year and only a few entries before that.
 
 814   // The delete function permanently deletes all data for a team.
 
 815   static function delete($team_id) {
 
 816     $mdb2 = getConnection();
 
 819     $sql = "select id from tt_users where team_id = $team_id";
 
 820     $res = $mdb2->query($sql);
 
 821     if (is_a($res, 'PEAR_Error')) return false;
 
 822     while ($val = $res->fetchRow()) {
 
 823       $user_id = $val['id'];
 
 824       if (!ttUserHelper::delete($user_id)) return false;
 
 828     if (!ttTeamHelper::deleteTasks($team_id)) return false;
 
 830     // Delete client to project binds.
 
 831     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
 
 832     $affected = $mdb2->exec($sql);
 
 833     if (is_a($affected, 'PEAR_Error')) return false;
 
 836     $sql = "delete from tt_projects where team_id = $team_id";
 
 837     $affected = $mdb2->exec($sql);
 
 838     if (is_a($affected, 'PEAR_Error')) return false;
 
 841     $sql = "delete from tt_clients where team_id = $team_id";
 
 842     $affected = $mdb2->exec($sql);
 
 843     if (is_a($affected, 'PEAR_Error')) return false;
 
 846     $sql = "delete from tt_invoices where team_id = $team_id";
 
 847     $affected = $mdb2->exec($sql);
 
 848     if (is_a($affected, 'PEAR_Error')) return false;
 
 850     // Delete custom fields.
 
 851     if (!ttTeamHelper::deleteCustomFields($team_id)) return false;
 
 854     $sql = "delete from tt_teams where id = $team_id";
 
 855     $affected = $mdb2->exec($sql);
 
 856     if (is_a($affected, 'PEAR_Error')) return false;
 
 861   // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
 
 862   static function markTasksDeleted($team_id) {
 
 863     $mdb2 = getConnection();
 
 864     $sql = "select id from tt_tasks where team_id = $team_id";
 
 865     $res = $mdb2->query($sql);
 
 866     if (is_a($res, 'PEAR_Error')) return false;
 
 868     while ($val = $res->fetchRow()) {
 
 870       // Delete task binds.
 
 871       $task_id = $val['id'];
 
 872       $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 873       $affected = $mdb2->exec($sql);
 
 874       if (is_a($affected, 'PEAR_Error')) return false;
 
 876       // Mark task as deleted.
 
 877       $sql = "update tt_tasks set status = NULL where id = $task_id";
 
 878       $affected = $mdb2->exec($sql);
 
 879       if (is_a($affected, 'PEAR_Error')) return false;
 
 885   // The deleteTasks deletes all tasks and task binds for an inactive team.
 
 886   static function deleteTasks($team_id) {
 
 887     $mdb2 = getConnection();
 
 888     $sql = "select id from tt_tasks where team_id = $team_id";
 
 889     $res = $mdb2->query($sql);
 
 890     if (is_a($res, 'PEAR_Error')) return false;
 
 892     while ($val = $res->fetchRow()) {
 
 894       // Delete task binds.
 
 895       $task_id = $val['id'];
 
 896       $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 897       $affected = $mdb2->exec($sql);
 
 898       if (is_a($affected, 'PEAR_Error')) return false;
 
 901       $sql = "delete from tt_tasks where id = $task_id";
 
 902       $affected = $mdb2->exec($sql);
 
 903       if (is_a($affected, 'PEAR_Error')) return false;
 
 909   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
 
 910   static function deleteCustomFields($team_id) {
 
 911     $mdb2 = getConnection();
 
 912     $sql = "select id from tt_custom_fields where team_id = $team_id";
 
 913     $res = $mdb2->query($sql);
 
 914     if (is_a($res, 'PEAR_Error')) return false;
 
 916     while ($val = $res->fetchRow()) {
 
 917       $field_id = $val['id'];
 
 919       // Clean up tt_custom_field_log.
 
 920       $sql = "delete from tt_custom_field_log where field_id = $field_id";
 
 921       $affected = $mdb2->exec($sql);
 
 922       if (is_a($affected, 'PEAR_Error')) return false;
 
 924       // Clean up tt_custom_field_options.
 
 925       $sql = "delete from tt_custom_field_options where field_id = $field_id";
 
 926       $affected = $mdb2->exec($sql);
 
 927       if (is_a($affected, 'PEAR_Error')) return false;
 
 929       // Delete custom field.
 
 930       $sql = "delete from tt_custom_fields where id = $field_id";
 
 931       $affected = $mdb2->exec($sql);
 
 932       if (is_a($affected, 'PEAR_Error')) return false;