- $sql = "select id, name, lang, timestamp from tt_teams where status = 1 order by id desc";
- $res = $mdb2->query($sql);
- $result = array();
- if (!is_a($res, 'PEAR_Error')) {
- while ($val = $res->fetchRow()) {
- $val['date'] = substr($val['timestamp'], 0, 10); // Strip the time.
- $result[] = $val;
- }
- return $result;
- }
- return false;
- }
-
- // The markDeleted function marks the team and everything in it as deleted.
- static function markDeleted($team_id) {
-
- // Iterate through team users and mark them as deleted.
- $users = ttTeamHelper::getAllUsers($team_id);
- foreach ($users as $one_user) {
- if (!ttUserHelper::markDeleted($one_user['id'])) return false;
- }
-
- // Mark tasks deleted.
- if (!ttTeamHelper::markTasksDeleted($team_id)) return false;
-
- $mdb2 = getConnection();
-
- // Mark projects deleted.
- $sql = "update tt_projects set status = NULL where team_id = $team_id";
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- // Mark clients deleted.
- $sql = "update tt_clients set status = NULL where team_id = $team_id";
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- // Mark custom fields deleted.
- $sql = "update tt_custom_fields set status = NULL where team_id = $team_id";
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- // Mark team deleted.
- $sql = "update tt_teams set status = NULL where id = $team_id";
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- return true;
- }
-
- // The getTeamDetails function returns team details.
- static function getTeamDetails($team_id) {
- $result = array();
- $mdb2 = getConnection();
-
- $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
- from tt_teams t
- inner join tt_users u on (u.team_id = t.id)
- inner join tt_roles r on (r.id = u.role_id and r.rank = 512)
- where t.id = $team_id";
-
- $res = $mdb2->query($sql);
- if (!is_a($res, 'PEAR_Error')) {
- $val = $res->fetchRow();
- return $val;
- }
-
- return false;
- }
-
- // The insert function creates a new team.
- static function insert($fields) {
-
- $mdb2 = getConnection();
-
- // Start with team name and currency.
- $columns = 'name, currency';
- $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
-
- if ($fields['decimal_mark']) {
- $columns .= ', decimal_mark';
- $values .= ', '.$mdb2->quote($fields['decimal_mark']);
- }
-
- $lang = $fields['lang'];
- if (!$lang) {
- global $i18n;
- $lang = $i18n->lang;
- }
- $columns .= ', lang';
- $values .= ', '.$mdb2->quote($lang);
-
- if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
- $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
- $columns .= ', date_format';
- $values .= ', '.$mdb2->quote($date_format);
- }
-
- if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
- $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
- $columns .= ', time_format';
- $values .= ', '.$mdb2->quote($time_format);
- }
-
- if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
- $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
- $columns .= ', week_start';
- $values .= ', '.(int)$week_start;
- }
-
- if ($fields['tracking_mode']) {
- $columns .= ', tracking_mode';
- $values .= ', '.(int)$fields['tracking_mode'];
- }
-
- if ($fields['project_required']) {
- $columns .= ', project_required';
- $values .= ', '.(int)$fields['project_required'];
- }
-
- if ($fields['task_required']) {
- $columns .= ', task_required';
- $values .= ', '.(int)$fields['task_required'];
- }
-
- if ($fields['record_type']) {
- $columns .= ', record_type';
- $values .= ', '.(int)$fields['record_type'];
- }
-
- if ($fields['bcc_email']) {
- $columns .= ', bcc_email';
- $values .= ', '.$mdb2->quote($fields['bcc_email']);
- }
-
- if ($fields['plugins']) {
- $columns .= ', plugins';
- $values .= ', '.$mdb2->quote($fields['plugins']);
- }
-
- if ($fields['lock_spec']) {
- $columns .= ', lock_spec';
- $values .= ', '.$mdb2->quote($fields['lock_spec']);
- }
-
- if ($fields['workday_minutes']) {
- $columns .= ', workday_minutes';
- $values .= ', '.(int)$fields['workday_minutes'];
- }
-
- if ($fields['config']) {
- $columns .= ', config';
- $values .= ', '.$mdb2->quote($fields['config']);
- }
-
- $sql = "insert into tt_teams ($columns) values($values)";
- $affected = $mdb2->exec($sql);
-
- if (!is_a($affected, 'PEAR_Error')) {
- $team_id = $mdb2->lastInsertID('tt_teams', 'id');
- return $team_id;
- }
-
- return false;
- }
-
- // The update function updates team information.
- static function update($team_id, $fields)
- {
- $mdb2 = getConnection();
- $name_part = 'name = '.$mdb2->quote($fields['name']);
- $currency_part = '';
- $lang_part = '';
- $decimal_mark_part = '';
- $date_format_part = '';
- $time_format_part = '';
- $week_start_part = '';
- $tracking_mode_part = '';
- $task_required_part = ' , task_required = '.(int) $fields['task_required'];
- $record_type_part = '';
- $bcc_email_part = '';
- $plugins_part = '';
- $config_part = '';
- $lock_spec_part = '';
- $workday_minutes_part = '';
-
- if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
- if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
- if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
- if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
- if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
- if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
- if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
- if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
- if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
- if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
- if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
- if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
- if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
-
- $sql = "update tt_teams set $name_part $currency_part $lang_part $decimal_mark_part
- $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
- $bcc_email_part $plugins_part $config_part $lock_spec_part $workday_minutes_part where id = $team_id";
- $affected = $mdb2->exec($sql);
- if (is_a($affected, 'PEAR_Error')) return false;
-
- return true;
- }
-
- // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
- static function getInactiveTeams() {
- $inactive_teams = array();
- $mdb2 = getConnection();
-
- // Get all team ids for teams created or modified more than 6 months ago.
- // $ts = date('Y-m-d', strtotime('-1 year'));
- $ts = date('Y-m-d', strtotime('-6 month'));
- $sql = "select id from tt_teams where timestamp < '$ts' order by id";