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');
31 import('ttInvoiceHelper');
33 // Class ttTeamHelper - contains helper functions that operate with teams.
36 // The getUserCount function returns number of people in team.
37 static function getUserCount($group_id) {
38 $mdb2 = getConnection();
40 $sql = "select count(id) as cnt from tt_users where group_id = $group_id and status = 1";
41 $res = $mdb2->query($sql);
43 if (!is_a($res, 'PEAR_Error')) {
44 $val = $res->fetchRow();
50 // The getUsersForClient obtains all active and inactive users in a team that are relevant to a client.
51 static function getUsersForClient() {
53 $mdb2 = getConnection();
55 $sql = "select u.id, u.name from tt_user_project_binds upb
56 inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)
57 inner join tt_users u on (u.id = upb.user_id)
58 where (u.status = 1 or u.status = 0)
60 order by upper(u.name)";
61 $res = $mdb2->query($sql);
63 if (is_a($res, 'PEAR_Error'))
65 while ($val = $res->fetchRow()) {
71 // The getActiveUsers obtains all active users in a given team.
72 static function getActiveUsers($options = null) {
75 $mdb2 = getConnection();
77 if (isset($options['getAllFields']))
78 $sql = "select u.*, r.name as role_name, r.rank from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $user->group_id and u.status = 1 order by upper(u.name)";
80 $sql = "select id, name from tt_users where group_id = $user->group_id and status = 1 order by upper(name)";
81 $res = $mdb2->query($sql);
83 if (is_a($res, 'PEAR_Error'))
85 while ($val = $res->fetchRow()) {
86 // Localize top manager role name, as it is not localized in db.
87 if ($val['rank'] == 512)
88 $val['role_name'] = $i18n->get('role.top_manager.label');
92 if (isset($options['putSelfFirst'])) {
93 // Put own entry at the front.
94 $cnt = count($user_list);
95 for($i = 0; $i < $cnt; $i++) {
96 if ($user_list[$i]['id'] == $user->id) {
97 $self = $user_list[$i]; // Found self.
98 array_unshift($user_list, $self); // Put own entry at the front.
99 array_splice($user_list, $i+1, 1); // Remove duplicate.
106 // The swapRolesWith swaps existing user role with that of another user.
107 static function swapRolesWith($user_id) {
109 $mdb2 = getConnection();
111 // Obtain role id for the user we are swapping ourselves with.
112 $sql = "select u.id, u.role_id from tt_users u left join tt_roles r on (u.role_id = r.id) where u.id = $user_id and u.group_id = $user->group_id and u.status = 1 and r.rank < $user->rank";
113 $res = $mdb2->query($sql);
114 if (is_a($res, 'PEAR_Error'))
116 $val = $res->fetchRow();
117 if (!$val['id'] || !$val['role_id'])
120 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
123 $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
124 $affected = $mdb2->exec($sql);
125 if (is_a($affected, 'PEAR_Error')) return false;
128 $role_id = $val['role_id'];
129 $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
130 $affected = $mdb2->exec($sql);
131 if (is_a($affected, 'PEAR_Error')) return false;
136 // The getUsersForSwap obtains all users a current user can swap roles with.
137 static function getUsersForSwap() {
139 $mdb2 = getConnection();
141 $sql = "select u.id, u.name, r.rank, r.rights from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $user->group_id and u.status = 1 and r.rank < $user->rank order by upper(u.name)";
142 $res = $mdb2->query($sql);
143 $user_list = array();
144 if (is_a($res, 'PEAR_Error'))
146 while ($val = $res->fetchRow()) {
147 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
149 continue; // Skip adding clients.
156 // The getUsers obtains all active and inactive (but not deleted) users in a given team.
157 static function getUsers() {
159 $mdb2 = getConnection();
160 $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
161 $res = $mdb2->query($sql);
162 $user_list = array();
163 if (is_a($res, 'PEAR_Error'))
165 while ($val = $res->fetchRow()) {
171 // The getInactiveUsers obtains all inactive users in a given team.
172 static function getInactiveUsers($group_id, $all_fields = false) {
173 $mdb2 = getConnection();
176 $sql = "select u.*, r.name as role_name from tt_users u left join tt_roles r on (u.role_id = r.id) where u.group_id = $group_id and u.status = 0 order by upper(u.name)";
178 $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
179 $res = $mdb2->query($sql);
181 if (!is_a($res, 'PEAR_Error')) {
182 while ($val = $res->fetchRow()) {
190 // The getAllUsers obtains all users in a given team.
191 static function getAllUsers($group_id, $all_fields = false) {
192 $mdb2 = getConnection();
194 $sql = "select * from tt_users where group_id = $group_id order by upper(name)";
196 $sql = "select id, name from tt_users where group_id = $group_id order by upper(name)";
197 $res = $mdb2->query($sql);
199 if (!is_a($res, 'PEAR_Error')) {
200 while ($val = $res->fetchRow()) {
208 // getActiveProjects - returns an array of active projects for team.
209 static function getActiveProjects($group_id)
212 $mdb2 = getConnection();
214 $sql = "select id, name, description, tasks from tt_projects
215 where group_id = $group_id and status = 1 order by upper(name)";
216 $res = $mdb2->query($sql);
218 if (!is_a($res, 'PEAR_Error')) {
219 while ($val = $res->fetchRow()) {
226 // getInactiveProjects - returns an array of inactive projects for team.
227 static function getInactiveProjects($group_id)
230 $mdb2 = getConnection();
232 $sql = "select id, name, description, tasks from tt_projects
233 where group_id = $group_id and status = 0 order by upper(name)";
234 $res = $mdb2->query($sql);
236 if (!is_a($res, 'PEAR_Error')) {
237 while ($val = $res->fetchRow()) {
244 // The getAllProjects obtains all projects in a given team.
245 static function getAllProjects($group_id, $all_fields = false) {
246 $mdb2 = getConnection();
249 $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
251 $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
252 $res = $mdb2->query($sql);
254 if (!is_a($res, 'PEAR_Error')) {
255 while ($val = $res->fetchRow()) {
263 // getActiveTasks - returns an array of active tasks for team.
264 static function getActiveTasks($group_id)
267 $mdb2 = getConnection();
269 $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
270 $res = $mdb2->query($sql);
272 if (!is_a($res, 'PEAR_Error')) {
273 while ($val = $res->fetchRow()) {
280 // getInactiveTasks - returns an array of inactive tasks for team.
281 static function getInactiveTasks($group_id)
284 $mdb2 = getConnection();
286 $sql = "select id, name, description from tt_tasks
287 where group_id = $group_id and status = 0 order by upper(name)";
288 $res = $mdb2->query($sql);
290 if (!is_a($res, 'PEAR_Error')) {
291 while ($val = $res->fetchRow()) {
298 // The getAllTasks obtains all tasks in a given team.
299 static function getAllTasks($group_id, $all_fields = false) {
300 $mdb2 = getConnection();
303 $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
305 $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
306 $res = $mdb2->query($sql);
308 if (!is_a($res, 'PEAR_Error')) {
309 while ($val = $res->fetchRow()) {
317 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
318 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
319 static function getActiveRolesForUser()
323 $mdb2 = getConnection();
325 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $user->group_id and rank < $user->rank and status = 1 order by rank";
326 $res = $mdb2->query($sql);
328 if (!is_a($res, 'PEAR_Error')) {
329 while ($val = $res->fetchRow()) {
330 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
331 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
332 continue; // Skip adding a client role.
339 // getActiveRoles - returns an array of active roles for team.
340 static function getActiveRoles($group_id)
343 $mdb2 = getConnection();
345 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
346 $res = $mdb2->query($sql);
348 if (!is_a($res, 'PEAR_Error')) {
349 while ($val = $res->fetchRow()) {
350 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
357 // getInactiveRoles - returns an array of inactive roles for team.
358 static function getInactiveRoles($group_id)
361 $mdb2 = getConnection();
363 $sql = "select id, name, rank, description from tt_roles
364 where group_id = $group_id and status = 0 order by rank";
365 $res = $mdb2->query($sql);
367 if (!is_a($res, 'PEAR_Error')) {
368 while ($val = $res->fetchRow()) {
375 // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
376 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
377 static function getInactiveRolesForUser()
381 $mdb2 = getConnection();
383 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $user->group_id and rank < $user->rank and status = 0 order by rank";
384 $res = $mdb2->query($sql);
386 if (!is_a($res, 'PEAR_Error')) {
387 while ($val = $res->fetchRow()) {
388 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
389 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
390 continue; // Skip adding a client role.
397 // The getActiveClients returns an array of active clients for team.
398 static function getActiveClients($group_id, $all_fields = false)
401 $mdb2 = getConnection();
404 $sql = "select * from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
406 $sql = "select id, name from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
408 $res = $mdb2->query($sql);
410 if (!is_a($res, 'PEAR_Error')) {
411 while ($val = $res->fetchRow()) {
418 // The getInactiveClients returns an array of inactive clients for team.
419 static function getInactiveClients($group_id, $all_fields = false)
422 $mdb2 = getConnection();
425 $sql = "select * from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
427 $sql = "select id, name from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
429 $res = $mdb2->query($sql);
431 if (!is_a($res, 'PEAR_Error')) {
432 while ($val = $res->fetchRow()) {
439 // The getAllClients obtains all clients in a given team.
440 static function getAllClients($group_id, $all_fields = false) {
441 $mdb2 = getConnection();
444 $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
446 $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
448 $res = $mdb2->query($sql);
450 if (!is_a($res, 'PEAR_Error')) {
451 while ($val = $res->fetchRow()) {
459 // The getActiveInvoices returns an array of active invoices for team.
460 static function getActiveInvoices($localizeDates = true)
463 $addPaidStatus = $user->isPluginEnabled('ps');
466 $mdb2 = getConnection();
468 if ($user->isClient())
469 $client_part = " and i.client_id = $user->client_id";
471 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
472 left join tt_clients c on (c.id = i.client_id)
473 where i.status = 1 and i.group_id = $user->group_id $client_part order by i.name";
474 $res = $mdb2->query($sql);
476 if (!is_a($res, 'PEAR_Error')) {
477 $dt = new DateAndTime(DB_DATEFORMAT);
478 while ($val = $res->fetchRow()) {
479 if ($localizeDates) {
480 $dt->parseVal($val['date']);
481 $val['date'] = $dt->toString($user->date_format);
484 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
491 // The getAllInvoices returns an array of all invoices for team.
492 static function getAllInvoices()
497 $mdb2 = getConnection();
499 $sql = "select * from tt_invoices where group_id = $user->group_id";
500 $res = $mdb2->query($sql);
502 if (!is_a($res, 'PEAR_Error')) {
503 $dt = new DateAndTime(DB_DATEFORMAT);
504 while ($val = $res->fetchRow()) {
511 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
512 static function getRecentInvoices($group_id, $client_id)
517 $mdb2 = getConnection();
519 $sql = "select i.id, i.name from tt_invoices i
520 left join tt_clients c on (c.id = i.client_id)
521 where i.group_id = $group_id and i.status = 1 and c.id = $client_id
522 order by i.id desc limit 3";
523 $res = $mdb2->query($sql);
525 if (!is_a($res, 'PEAR_Error')) {
526 $dt = new DateAndTime(DB_DATEFORMAT);
527 while ($val = $res->fetchRow()) {
534 // getUserToProjectBinds - obtains all user to project binds for a team.
535 static function getUserToProjectBinds($group_id) {
536 $mdb2 = getConnection();
539 $sql = "select * from tt_user_project_binds where user_id in (select id from tt_users where group_id = $group_id) order by user_id, status, project_id";
540 $res = $mdb2->query($sql);
542 if (!is_a($res, 'PEAR_Error')) {
543 while ($val = $res->fetchRow()) {
551 // The getAllCustomFields obtains all custom fields in a given team.
552 static function getAllCustomFields($group_id) {
553 $mdb2 = getConnection();
555 $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
557 $res = $mdb2->query($sql);
559 if (!is_a($res, 'PEAR_Error')) {
560 while ($val = $res->fetchRow()) {
568 // The getAllCustomFieldOptions obtains all custom field options in a given team.
569 static function getAllCustomFieldOptions($group_id) {
570 $mdb2 = getConnection();
572 $sql = "select * from tt_custom_field_options where field_id in (select id from tt_custom_fields where group_id = $group_id) order by id";
574 $res = $mdb2->query($sql);
576 if (!is_a($res, 'PEAR_Error')) {
577 while ($val = $res->fetchRow()) {
585 // The getCustomFieldLog obtains all custom field log entries for a given team.
586 static function getCustomFieldLog($group_id) {
587 $mdb2 = getConnection();
589 $sql = "select * from tt_custom_field_log where field_id in (select id from tt_custom_fields where group_id = $group_id) order by id";
591 $res = $mdb2->query($sql);
593 if (!is_a($res, 'PEAR_Error')) {
594 while ($val = $res->fetchRow()) {
602 // getFavReports - obtains all favorite reports for all users in team.
603 static function getFavReports($group_id) {
604 $mdb2 = getConnection();
607 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
608 $res = $mdb2->query($sql);
610 if (!is_a($res, 'PEAR_Error')) {
611 while ($val = $res->fetchRow()) {
619 // getExpenseItems - obtains all expense items for all users in team.
620 static function getExpenseItems($group_id) {
621 $mdb2 = getConnection();
624 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
625 $res = $mdb2->query($sql);
627 if (!is_a($res, 'PEAR_Error')) {
628 while ($val = $res->fetchRow()) {
636 // getPredefinedExpenses - obtains predefined expenses for team.
637 static function getPredefinedExpenses($group_id) {
639 $replaceDecimalMark = ('.' != $user->decimal_mark);
641 $mdb2 = getConnection();
644 $sql = "select id, name, cost from tt_predefined_expenses where group_id = $group_id";
645 $res = $mdb2->query($sql);
647 if (!is_a($res, 'PEAR_Error')) {
648 while ($val = $res->fetchRow()) {
649 if ($replaceDecimalMark)
650 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
658 // getNotifications - obtains notification descriptions for team.
659 static function getNotifications($group_id) {
660 $mdb2 = getConnection();
663 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
664 left join tt_fav_reports fr on (fr.id = c.report_id)
665 where c.group_id = $group_id and c.status = 1 and fr.status = 1";
666 $res = $mdb2->query($sql);
668 if (!is_a($res, 'PEAR_Error')) {
669 while ($val = $res->fetchRow()) {
677 // getMonthlyQuotas - obtains monthly quotas for team.
678 static function getMonthlyQuotas($group_id) {
679 $mdb2 = getConnection();
682 $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
683 $res = $mdb2->query($sql);
685 if (!is_a($res, 'PEAR_Error')) {
686 while ($val = $res->fetchRow()) {
694 // The markDeleted function marks the team and everything in it as deleted.
695 static function markDeleted($group_id) {
697 // Iterate through team users and mark them as deleted.
698 $users = ttTeamHelper::getAllUsers($group_id);
699 foreach ($users as $one_user) {
700 if (!ttUserHelper::markDeleted($one_user['id'])) return false;
703 // Mark tasks deleted.
704 if (!ttTeamHelper::markTasksDeleted($group_id)) return false;
706 $mdb2 = getConnection();
708 // Mark roles deleted.
709 $sql = "update tt_roles set status = NULL where group_id = $group_id";
710 $affected = $mdb2->exec($sql);
711 if (is_a($affected, 'PEAR_Error')) return false;
713 // Mark projects deleted.
714 $sql = "update tt_projects set status = NULL where group_id = $group_id";
715 $affected = $mdb2->exec($sql);
716 if (is_a($affected, 'PEAR_Error')) return false;
718 // Mark clients deleted.
719 $sql = "update tt_clients set status = NULL where group_id = $group_id";
720 $affected = $mdb2->exec($sql);
721 if (is_a($affected, 'PEAR_Error')) return false;
723 // Mark custom fields deleted.
724 $sql = "update tt_custom_fields set status = NULL where group_id = $group_id";
725 $affected = $mdb2->exec($sql);
726 if (is_a($affected, 'PEAR_Error')) return false;
728 // Mark group deleted.
729 $sql = "update tt_groups set status = NULL where id = $group_id";
730 $affected = $mdb2->exec($sql);
731 if (is_a($affected, 'PEAR_Error')) return false;
736 // The getTeamDetails function returns team details.
737 static function getTeamDetails($group_id) {
739 $mdb2 = getConnection();
741 $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
743 inner join tt_users u on (u.group_id = t.id)
744 inner join tt_roles r on (r.id = u.role_id and r.rank = 512)
745 where t.id = $group_id";
747 $res = $mdb2->query($sql);
748 if (!is_a($res, 'PEAR_Error')) {
749 $val = $res->fetchRow();
756 // The insert function creates a new team.
757 static function insert($fields) {
760 $mdb2 = getConnection();
762 // Start with team name and currency.
763 $columns = 'name, currency';
764 $values = $mdb2->quote(trim($fields['name'])).', '.$mdb2->quote(trim($fields['currency']));
766 if ($fields['decimal_mark']) {
767 $columns .= ', decimal_mark';
768 $values .= ', '.$mdb2->quote($fields['decimal_mark']);
771 $lang = $fields['lang'];
776 $columns .= ', lang';
777 $values .= ', '.$mdb2->quote($lang);
779 if ($fields['date_format'] || defined('DATE_FORMAT_DEFAULT')) {
780 $date_format = $fields['date_format'] ? $fields['date_format'] : DATE_FORMAT_DEFAULT;
781 $columns .= ', date_format';
782 $values .= ', '.$mdb2->quote($date_format);
785 if ($fields['time_format'] || defined('TIME_FORMAT_DEFAULT')) {
786 $time_format = $fields['time_format'] ? $fields['time_format'] : TIME_FORMAT_DEFAULT;
787 $columns .= ', time_format';
788 $values .= ', '.$mdb2->quote($time_format);
791 if ($fields['week_start'] || defined('WEEK_START_DEFAULT')) {
792 $week_start = $fields['week_start'] ? $fields['week_start'] : WEEK_START_DEFAULT;
793 $columns .= ', week_start';
794 $values .= ', '.(int)$week_start;
797 if ($fields['tracking_mode']) {
798 $columns .= ', tracking_mode';
799 $values .= ', '.(int)$fields['tracking_mode'];
802 if ($fields['project_required']) {
803 $columns .= ', project_required';
804 $values .= ', '.(int)$fields['project_required'];
807 if ($fields['task_required']) {
808 $columns .= ', task_required';
809 $values .= ', '.(int)$fields['task_required'];
812 if ($fields['record_type']) {
813 $columns .= ', record_type';
814 $values .= ', '.(int)$fields['record_type'];
817 if ($fields['bcc_email']) {
818 $columns .= ', bcc_email';
819 $values .= ', '.$mdb2->quote($fields['bcc_email']);
822 if ($fields['plugins']) {
823 $columns .= ', plugins';
824 $values .= ', '.$mdb2->quote($fields['plugins']);
827 if ($fields['lock_spec']) {
828 $columns .= ', lock_spec';
829 $values .= ', '.$mdb2->quote($fields['lock_spec']);
832 if ($fields['workday_minutes']) {
833 $columns .= ', workday_minutes';
834 $values .= ', '.(int)$fields['workday_minutes'];
837 if ($fields['config']) {
838 $columns .= ', config';
839 $values .= ', '.$mdb2->quote($fields['config']);
842 $columns .= ', created, created_ip, created_by';
843 $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id);
845 $sql = "insert into tt_groups ($columns) values($values)";
846 $affected = $mdb2->exec($sql);
848 if (!is_a($affected, 'PEAR_Error')) {
849 $group_id = $mdb2->lastInsertID('tt_groups', 'id');
856 // The update function updates team information.
857 static function update($group_id, $fields)
860 $mdb2 = getConnection();
861 $name_part = 'name = '.$mdb2->quote($fields['name']);
864 $decimal_mark_part = '';
865 $date_format_part = '';
866 $time_format_part = '';
867 $week_start_part = '';
868 $tracking_mode_part = '';
869 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
870 $record_type_part = '';
871 $bcc_email_part = '';
875 $lock_spec_part = '';
876 $workday_minutes_part = '';
878 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
879 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
880 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
881 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
882 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
883 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
884 if (isset($fields['tracking_mode'])) $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
885 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
886 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
887 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
888 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
889 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
890 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
891 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
892 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
894 $sql = "update tt_groups set $name_part $currency_part $lang_part $decimal_mark_part
895 $date_format_part $time_format_part $week_start_part $tracking_mode_part $task_required_part $record_type_part
896 $bcc_email_part $allow_ip_part $plugins_part $config_part $lock_spec_part $workday_minutes_part $modified_part where id = $group_id";
897 $affected = $mdb2->exec($sql);
898 if (is_a($affected, 'PEAR_Error')) return false;
903 // The getInactiveTeams is a maintenance function that returns an array of inactive team ids (max 100).
904 static function getInactiveTeams() {
905 $inactive_teams = array();
906 $mdb2 = getConnection();
908 // Get all team ids for teams created or modified more than 8 months ago.
909 // $ts = date('Y-m-d', strtotime('-1 year'));
910 $ts = $mdb2->quote(date('Y-m-d', strtotime('-8 month')));
911 $sql = "select id from tt_groups where created < $ts and (modified is null or modified < $ts) order by id";
912 $res = $mdb2->query($sql);
915 if (!is_a($res, 'PEAR_Error')) {
916 while ($val = $res->fetchRow()) {
917 $group_id = $val['id'];
918 if (ttTeamHelper::isTeamActive($group_id) == false) {
920 $inactive_teams[] = $group_id;
921 // Limit the array size for perfomance by allowing this operation on small chunks only.
922 if ($count >= 100) break;
925 return $inactive_teams;
930 // The isTeamActive determines if a team is using Time Tracker or abandoned it.
931 static function isTeamActive($group_id) {
934 $mdb2 = getConnection();
935 $sql = "select id from tt_users where group_id = $group_id";
936 $res = $mdb2->query($sql);
937 if (is_a($res, 'PEAR_Error')) die($res->getMessage());
938 while ($val = $res->fetchRow()) {
939 $users[] = $val['id'];
941 $user_list = implode(',', $users); // This is a comma-separated list of user ids.
943 return false; // No users in team.
946 $ts = date('Y-m-d', strtotime('-2 years'));
947 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
948 $res = $mdb2->query($sql);
949 if (!is_a($res, 'PEAR_Error')) {
950 if ($val = $res->fetchRow()) {
951 $count = $val['cnt'];
956 return false; // No time entries for the last 2 years.
959 // We will consider a team inactive if it has 5 or less time entries made more than 1 year ago.
960 $count_last_year = 0;
961 $ts = date('Y-m-d', strtotime('-1 year'));
962 $sql = "select count(*) as cnt from tt_log where user_id in ($user_list) and created > '$ts'";
963 $res = $mdb2->query($sql);
964 if (!is_a($res, 'PEAR_Error')) {
965 if ($val = $res->fetchRow()) {
966 $count_last_year = $val['cnt'];
968 if ($count_last_year == 0)
969 return false; // No time entries for the last year and only a few entries before that.
975 // The delete function permanently deletes all data for a team.
976 static function delete($group_id) {
977 $mdb2 = getConnection();
980 $sql = "select id from tt_users where group_id = $group_id";
981 $res = $mdb2->query($sql);
982 if (is_a($res, 'PEAR_Error')) return false;
983 while ($val = $res->fetchRow()) {
984 $user_id = $val['id'];
985 if (!ttUserHelper::delete($user_id)) return false;
989 if (!ttTeamHelper::deleteTasks($group_id)) return false;
991 // Delete client to project binds.
992 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
993 $affected = $mdb2->exec($sql);
994 if (is_a($affected, 'PEAR_Error')) return false;
997 $sql = "delete from tt_projects where group_id = $group_id";
998 $affected = $mdb2->exec($sql);
999 if (is_a($affected, 'PEAR_Error')) return false;
1002 $sql = "delete from tt_clients where group_id = $group_id";
1003 $affected = $mdb2->exec($sql);
1004 if (is_a($affected, 'PEAR_Error')) return false;
1007 $sql = "delete from tt_invoices where group_id = $group_id";
1008 $affected = $mdb2->exec($sql);
1009 if (is_a($affected, 'PEAR_Error')) return false;
1011 // Delete custom fields.
1012 if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
1015 $sql = "delete from tt_roles where group_id = $group_id";
1016 $affected = $mdb2->exec($sql);
1017 if (is_a($affected, 'PEAR_Error')) return false;
1020 $sql = "delete from tt_groups where id = $group_id";
1021 $affected = $mdb2->exec($sql);
1022 if (is_a($affected, 'PEAR_Error')) return false;
1027 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a team.
1028 static function markTasksDeleted($group_id) {
1029 $mdb2 = getConnection();
1030 $sql = "select id from tt_tasks where group_id = $group_id";
1031 $res = $mdb2->query($sql);
1032 if (is_a($res, 'PEAR_Error')) return false;
1034 while ($val = $res->fetchRow()) {
1036 // Delete task binds.
1037 $task_id = $val['id'];
1038 $sql = "delete from tt_project_task_binds where task_id = $task_id";
1039 $affected = $mdb2->exec($sql);
1040 if (is_a($affected, 'PEAR_Error')) return false;
1042 // Mark task as deleted.
1043 $sql = "update tt_tasks set status = NULL where id = $task_id";
1044 $affected = $mdb2->exec($sql);
1045 if (is_a($affected, 'PEAR_Error')) return false;
1051 // The deleteTasks deletes all tasks and task binds for an inactive team.
1052 static function deleteTasks($group_id) {
1053 $mdb2 = getConnection();
1054 $sql = "select id from tt_tasks where group_id = $group_id";
1055 $res = $mdb2->query($sql);
1056 if (is_a($res, 'PEAR_Error')) return false;
1058 while ($val = $res->fetchRow()) {
1060 // Delete task binds.
1061 $task_id = $val['id'];
1062 $sql = "delete from tt_project_task_binds where task_id = $task_id";
1063 $affected = $mdb2->exec($sql);
1064 if (is_a($affected, 'PEAR_Error')) return false;
1067 $sql = "delete from tt_tasks where id = $task_id";
1068 $affected = $mdb2->exec($sql);
1069 if (is_a($affected, 'PEAR_Error')) return false;
1075 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive team.
1076 static function deleteCustomFields($group_id) {
1077 $mdb2 = getConnection();
1078 $sql = "select id from tt_custom_fields where group_id = $group_id";
1079 $res = $mdb2->query($sql);
1080 if (is_a($res, 'PEAR_Error')) return false;
1082 while ($val = $res->fetchRow()) {
1083 $field_id = $val['id'];
1085 // Clean up tt_custom_field_log.
1086 $sql = "delete from tt_custom_field_log where field_id = $field_id";
1087 $affected = $mdb2->exec($sql);
1088 if (is_a($affected, 'PEAR_Error')) return false;
1090 // Clean up tt_custom_field_options.
1091 $sql = "delete from tt_custom_field_options where field_id = $field_id";
1092 $affected = $mdb2->exec($sql);
1093 if (is_a($affected, 'PEAR_Error')) return false;
1095 // Delete custom field.
1096 $sql = "delete from tt_custom_fields where id = $field_id";
1097 $affected = $mdb2->exec($sql);
1098 if (is_a($affected, 'PEAR_Error')) return false;
1104 // enablePlugin either enables or disables a specific plugin for team.
1105 static function enablePlugin($plugin, $enable = true)
1108 if (!$user->can('manage_features'))
1111 $plugin_array = explode(',', $user->plugins);
1112 if ($enable && !in_array($plugin, $plugin_array))
1113 $plugin_array[] = $plugin; // Add plugin to array.
1115 if (!$enable && in_array($plugin, $plugin_array)) {
1116 $key = array_search($plugin, $plugin_array);
1118 unset($plugin_array[$key]); // Remove plugin from array.
1121 $plugins = implode(',', $plugin_array);
1122 if ($plugins != $user->plugins) {
1123 if (!ttTeamHelper::update($user->group_id, array('name' => $user->team,'plugins' => $plugins)))
1125 $user->plugins = $plugins;