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 = '';
 
 727     if (isset($fields['address'])) $addr_part = ', address = '.$mdb2->quote($fields['address']);
 
 728     if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
 
 729     if (isset($fields['locktime'])) $locktime_part = ', locktime = '.intval($fields['locktime']);
 
 730     if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
 
 731     if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
 
 732     if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
 
 733     if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
 
 734     if (isset($fields['week_start'])) $week_start_part = ', week_start = '.intval($fields['week_start']);
 
 735     if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.intval($fields['tracking_mode']);
 
 736     if (isset($fields['record_type'])) $record_type_part = ', record_type = '.intval($fields['record_type']);
 
 737     if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
 
 739     $sql = "update tt_teams set $name_part $addr_part $currency_part $locktime_part $lang_part $decimal_mark_part
 
 740       $date_format_part $time_format_part $week_start_part $tracking_mode_part $record_type_part $plugins_part where id = $team_id";
 
 741     $affected = $mdb2->exec($sql);
 
 743     if (is_a($affected, 'PEAR_Error')) {
 
 750   // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
 
 751   static function getInactiveTeams() {
 
 752     $inactive_teams = array();
 
 753     $mdb2 = getConnection();
 
 755     // Get all team ids for teams created or modified more than 1 year ago.
 
 756     $ts = date('Y-m-d', strtotime('-1 year'));
 
 757     $sql =  "select id from tt_teams where timestamp < '$ts' order by id";
 
 758     $res = $mdb2->query($sql);
 
 761     if (!is_a($res, 'PEAR_Error')) {
 
 762       while ($val = $res->fetchRow()) {
 
 763         $team_id = $val['id'];
 
 764         if (ttTeamHelper::isTeamActive($team_id) == false) {
 
 766           $inactive_teams[] = $team_id;
 
 767           // Limit the array size for perfomance by allowing this operation on small chunks only.
 
 768           if ($count >= 25) break;
 
 771       return $inactive_teams;
 
 776   // The isTeamActive determines if a team is using Time Tracker or abandoned it.
 
 777   static function isTeamActive($team_id) {
 
 780         $mdb2 = getConnection();
 
 781     $sql = "select id from tt_users where team_id = $team_id";
 
 782     $res = $mdb2->query($sql);
 
 783     if (is_a($res, 'PEAR_Error')) die($res->getMessage());
 
 784     while ($val = $res->fetchRow()) {
 
 785       $users[] = $val['id'];
 
 787     $user_list = implode(',', $users); // This is a comma-separated list of user ids.
 
 789       return false; // No users in team.
 
 792         $ts = date('Y-m-d', strtotime('-2 years')); 
 
 793     $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";  
 
 794     $res = $mdb2->query($sql);
 
 795     if (!is_a($res, 'PEAR_Error')) {
 
 796       if ($val = $res->fetchRow()) {
 
 797         $count = $val['cnt'];
 
 802       return false;  // No time entries for the last 2 years.
 
 805       // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
 
 806       $count_last_year = 0;
 
 807       $ts = date('Y-m-d', strtotime('-1 year')); 
 
 808       $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and timestamp > '$ts'";  
 
 809       $res = $mdb2->query($sql);
 
 810       if (!is_a($res, 'PEAR_Error')) {
 
 811         if ($val = $res->fetchRow()) {
 
 812           $count_last_year = $val['cnt'];
 
 814         if ($count_last_year == 0)
 
 815           return false;  // No time entries for the last year and only a few entries before that...
 
 821   // The delete function permanently deletes all data for a team.
 
 822   static function delete($team_id) {
 
 823         $mdb2 = getConnection();
 
 826     $sql = "select id from tt_users where team_id = $team_id";
 
 827     $res = $mdb2->query($sql);
 
 828     if (is_a($res, 'PEAR_Error')) return false;
 
 829     while ($val = $res->fetchRow()) {
 
 830       $user_id = $val['id'];
 
 831       if (!ttUserHelper::delete($user_id))
 
 836     if (!ttTeamHelper::deleteTasks($team_id))
 
 839     // Delete client to project binds.
 
 840     $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where team_id = $team_id)";
 
 841     $affected = $mdb2->exec($sql);
 
 842     if (is_a($affected, 'PEAR_Error'))
 
 846         $sql = "delete from tt_projects where team_id = $team_id";
 
 847         $affected = $mdb2->exec($sql);
 
 848         if (is_a($affected, 'PEAR_Error'))
 
 852         $sql = "delete 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_invoices where team_id = $team_id";
 
 859         $affected = $mdb2->exec($sql);
 
 860         if (is_a($affected, 'PEAR_Error'))
 
 863         // Delete custom fields.
 
 864         if (!ttTeamHelper::deleteCustomFields($team_id))
 
 868     $sql = "delete from tt_teams where id = $team_id";
 
 869     $affected = $mdb2->exec($sql);
 
 870     if (is_a($affected, 'PEAR_Error'))
 
 876   // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
 
 877   static function markTasksDeleted($team_id) {
 
 878         $mdb2 = getConnection();
 
 879         $sql = "select id from tt_tasks where team_id = $team_id";
 
 880         $res = $mdb2->query($sql);
 
 881         if (is_a($res, 'PEAR_Error')) return false;
 
 882         while ($val = $res->fetchRow()) {
 
 884           // Delete task binds.
 
 885           $task_id = $val['id'];
 
 886           $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 887           $affected = $mdb2->exec($sql);
 
 888           if (is_a($affected, 'PEAR_Error'))
 
 891           // Mark task as deleted.
 
 892           $sql = "update tt_tasks set status = NULL where id = $task_id";
 
 893           $affected = $mdb2->exec($sql);
 
 894           if (is_a($affected, 'PEAR_Error'))
 
 901   // The deleteTasks deletes all tasks and task binds for an inactive team.
 
 902   static function deleteTasks($team_id) {
 
 903         $mdb2 = getConnection();
 
 904         $sql = "select id from tt_tasks where team_id = $team_id";
 
 905         $res = $mdb2->query($sql);
 
 906         if (is_a($res, 'PEAR_Error')) return false;
 
 907         while ($val = $res->fetchRow()) {
 
 909           // Delete task binds.
 
 910           $task_id = $val['id'];
 
 911           $sql = "delete from tt_project_task_binds where task_id = $task_id";
 
 912           $affected = $mdb2->exec($sql);
 
 913           if (is_a($affected, 'PEAR_Error'))
 
 917           $sql = "delete from tt_tasks where id = $task_id";
 
 918           $affected = $mdb2->exec($sql);
 
 919           if (is_a($affected, 'PEAR_Error'))
 
 926   // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
 
 927   static function deleteCustomFields($team_id) {
 
 928         $mdb2 = getConnection();
 
 929         $sql = "select id from tt_custom_fields where team_id = $team_id";
 
 930         $res = $mdb2->query($sql);
 
 931         if (is_a($res, 'PEAR_Error')) return false;
 
 932         while ($val = $res->fetchRow()) {
 
 933           $field_id = $val['id'];
 
 935           // Clean up tt_custom_field_log.
 
 936           $sql = "delete from tt_custom_field_log where field_id = $field_id";
 
 937           $affected = $mdb2->exec($sql);
 
 938           if (is_a($affected, 'PEAR_Error'))
 
 941           // Clean up tt_custom_field_options.
 
 942           $sql = "delete from tt_custom_field_options where field_id = $field_id";
 
 943           $affected = $mdb2->exec($sql);
 
 944           if (is_a($affected, 'PEAR_Error'))
 
 947           // Delete custom field.
 
 948           $sql = "delete from tt_custom_fields where id = $field_id";
 
 949           $affected = $mdb2->exec($sql);
 
 950           if (is_a($affected, 'PEAR_Error'))