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 groups.
36 // The getUsersForClient obtains all active and inactive users in a group that are relevant to a client.
37 static function getUsersForClient() {
39 $mdb2 = getConnection();
41 $sql = "select u.id, u.name from tt_user_project_binds upb".
42 " inner join tt_client_project_binds cpb on (upb.project_id = cpb.project_id and cpb.client_id = $user->client_id)".
43 " inner join tt_users u on (u.id = upb.user_id)".
44 " where (u.status = 1 or u.status = 0)".
46 " order by upper(u.name)";
47 $res = $mdb2->query($sql);
49 if (is_a($res, 'PEAR_Error'))
51 while ($val = $res->fetchRow()) {
57 // The getActiveUsers obtains all active users in a given group.
58 static function getActiveUsers($options = null) {
61 $mdb2 = getConnection();
63 if (isset($options['getAllFields']))
64 $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)";
66 $sql = "select id, name from tt_users where group_id = $user->group_id and status = 1 order by upper(name)";
67 $res = $mdb2->query($sql);
69 if (is_a($res, 'PEAR_Error'))
71 while ($val = $res->fetchRow()) {
72 // Localize top manager role name, as it is not localized in db.
73 if ($val['rank'] == 512)
74 $val['role_name'] = $i18n->get('role.top_manager.label');
78 if (isset($options['putSelfFirst'])) {
79 // Put own entry at the front.
80 $cnt = count($user_list);
81 for($i = 0; $i < $cnt; $i++) {
82 if ($user_list[$i]['id'] == $user->id) {
83 $self = $user_list[$i]; // Found self.
84 array_unshift($user_list, $self); // Put own entry at the front.
85 array_splice($user_list, $i+1, 1); // Remove duplicate.
92 // The swapRolesWith swaps existing user role with that of another user.
93 static function swapRolesWith($user_id) {
95 $mdb2 = getConnection();
97 // Obtain role id for the user we are swapping ourselves with.
98 $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";
99 $res = $mdb2->query($sql);
100 if (is_a($res, 'PEAR_Error'))
102 $val = $res->fetchRow();
103 if (!$val['id'] || !$val['role_id'])
106 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
109 $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
110 $affected = $mdb2->exec($sql);
111 if (is_a($affected, 'PEAR_Error')) return false;
114 $role_id = $val['role_id'];
115 $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
116 $affected = $mdb2->exec($sql);
117 if (is_a($affected, 'PEAR_Error')) return false;
122 // The getUsersForSwap obtains all users a current user can swap roles with.
123 static function getUsersForSwap() {
125 $mdb2 = getConnection();
127 $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)";
128 $res = $mdb2->query($sql);
129 $user_list = array();
130 if (is_a($res, 'PEAR_Error'))
132 while ($val = $res->fetchRow()) {
133 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
135 continue; // Skip adding clients.
142 // The getUsers obtains all active and inactive (but not deleted) users in a group.
143 static function getUsers() {
145 $mdb2 = getConnection();
146 $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
147 $res = $mdb2->query($sql);
148 $user_list = array();
149 if (is_a($res, 'PEAR_Error'))
151 while ($val = $res->fetchRow()) {
157 // The getInactiveUsers obtains all inactive users in a group.
158 static function getInactiveUsers($group_id, $all_fields = false) {
159 $mdb2 = getConnection();
162 $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)";
164 $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
165 $res = $mdb2->query($sql);
167 if (!is_a($res, 'PEAR_Error')) {
168 while ($val = $res->fetchRow()) {
176 // getActiveProjects - returns an array of active projects for a group.
177 static function getActiveProjects($group_id)
180 $mdb2 = getConnection();
182 $sql = "select id, name, description, tasks from tt_projects
183 where group_id = $group_id and status = 1 order by upper(name)";
184 $res = $mdb2->query($sql);
186 if (!is_a($res, 'PEAR_Error')) {
187 while ($val = $res->fetchRow()) {
194 // getInactiveProjects - returns an array of inactive projects for a group.
195 static function getInactiveProjects($group_id)
198 $mdb2 = getConnection();
200 $sql = "select id, name, description, tasks from tt_projects
201 where group_id = $group_id and status = 0 order by upper(name)";
202 $res = $mdb2->query($sql);
204 if (!is_a($res, 'PEAR_Error')) {
205 while ($val = $res->fetchRow()) {
212 // The getAllProjects obtains all projects in a group.
213 static function getAllProjects($group_id, $all_fields = false) {
214 $mdb2 = getConnection();
217 $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
219 $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
220 $res = $mdb2->query($sql);
222 if (!is_a($res, 'PEAR_Error')) {
223 while ($val = $res->fetchRow()) {
231 // getActiveTasks - returns an array of active tasks for a group.
232 static function getActiveTasks($group_id)
235 $mdb2 = getConnection();
237 $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
238 $res = $mdb2->query($sql);
240 if (!is_a($res, 'PEAR_Error')) {
241 while ($val = $res->fetchRow()) {
248 // getInactiveTasks - returns an array of inactive tasks for a group.
249 static function getInactiveTasks($group_id)
252 $mdb2 = getConnection();
254 $sql = "select id, name, description from tt_tasks
255 where group_id = $group_id and status = 0 order by upper(name)";
256 $res = $mdb2->query($sql);
258 if (!is_a($res, 'PEAR_Error')) {
259 while ($val = $res->fetchRow()) {
266 // The getAllTasks obtains all tasks in a group.
267 static function getAllTasks($group_id, $all_fields = false) {
268 $mdb2 = getConnection();
271 $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
273 $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
274 $res = $mdb2->query($sql);
276 if (!is_a($res, 'PEAR_Error')) {
277 while ($val = $res->fetchRow()) {
285 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
286 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
287 static function getActiveRolesForUser()
291 $mdb2 = getConnection();
293 $group_id = $user->getActiveGroup();
294 $org_id = $user->org_id;
296 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $user->rank and status = 1 order by rank";
297 $res = $mdb2->query($sql);
299 if (!is_a($res, 'PEAR_Error')) {
300 while ($val = $res->fetchRow()) {
301 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
302 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
303 continue; // Skip adding a client role.
310 // getActiveRoles - returns an array of active roles for a group.
311 static function getActiveRoles($group_id)
314 $mdb2 = getConnection();
316 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
317 $res = $mdb2->query($sql);
319 if (!is_a($res, 'PEAR_Error')) {
320 while ($val = $res->fetchRow()) {
321 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
328 // getInactiveRoles - returns an array of inactive roles for a group.
329 static function getInactiveRoles($group_id)
332 $mdb2 = getConnection();
334 $sql = "select id, name, rank, description from tt_roles
335 where group_id = $group_id and status = 0 order by rank";
336 $res = $mdb2->query($sql);
338 if (!is_a($res, 'PEAR_Error')) {
339 while ($val = $res->fetchRow()) {
346 // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
347 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
348 static function getInactiveRolesForUser()
352 $mdb2 = getConnection();
354 $group_id = $user->getActiveGroup();
355 $org_id = $user->org_id;
357 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $user->rank and status = 0 order by rank";
358 $res = $mdb2->query($sql);
360 if (!is_a($res, 'PEAR_Error')) {
361 while ($val = $res->fetchRow()) {
362 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
363 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
364 continue; // Skip adding a client role.
371 // The getActiveClients returns an array of active clients for a group.
372 static function getActiveClients($group_id, $all_fields = false)
375 $mdb2 = getConnection();
378 $sql = "select * from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
380 $sql = "select id, name from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
382 $res = $mdb2->query($sql);
384 if (!is_a($res, 'PEAR_Error')) {
385 while ($val = $res->fetchRow()) {
392 // The getInactiveClients returns an array of inactive clients for a group.
393 static function getInactiveClients($group_id, $all_fields = false)
396 $mdb2 = getConnection();
399 $sql = "select * from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
401 $sql = "select id, name from tt_clients where group_id = $group_id and status = 0 order by upper(name)";
403 $res = $mdb2->query($sql);
405 if (!is_a($res, 'PEAR_Error')) {
406 while ($val = $res->fetchRow()) {
413 // The getAllClients obtains all clients in a group.
414 static function getAllClients($group_id, $all_fields = false) {
415 $mdb2 = getConnection();
418 $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
420 $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
422 $res = $mdb2->query($sql);
424 if (!is_a($res, 'PEAR_Error')) {
425 while ($val = $res->fetchRow()) {
433 // The getActiveInvoices returns an array of active invoices for a group.
434 static function getActiveInvoices($localizeDates = true)
437 $addPaidStatus = $user->isPluginEnabled('ps');
440 $mdb2 = getConnection();
442 if ($user->isClient())
443 $client_part = " and i.client_id = $user->client_id";
445 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
446 left join tt_clients c on (c.id = i.client_id)
447 where i.status = 1 and i.group_id = $user->group_id $client_part order by i.name";
448 $res = $mdb2->query($sql);
450 if (!is_a($res, 'PEAR_Error')) {
451 $dt = new DateAndTime(DB_DATEFORMAT);
452 while ($val = $res->fetchRow()) {
453 if ($localizeDates) {
454 $dt->parseVal($val['date']);
455 $val['date'] = $dt->toString($user->date_format);
458 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
465 // The getAllInvoices returns an array of all invoices for a group.
466 static function getAllInvoices()
471 $mdb2 = getConnection();
473 $sql = "select * from tt_invoices where group_id = $user->group_id";
474 $res = $mdb2->query($sql);
476 if (!is_a($res, 'PEAR_Error')) {
477 $dt = new DateAndTime(DB_DATEFORMAT);
478 while ($val = $res->fetchRow()) {
485 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
486 static function getRecentInvoices($group_id, $client_id)
491 $mdb2 = getConnection();
493 $sql = "select i.id, i.name from tt_invoices i
494 left join tt_clients c on (c.id = i.client_id)
495 where i.group_id = $group_id and i.status = 1 and c.id = $client_id
496 order by i.id desc limit 3";
497 $res = $mdb2->query($sql);
499 if (!is_a($res, 'PEAR_Error')) {
500 $dt = new DateAndTime(DB_DATEFORMAT);
501 while ($val = $res->fetchRow()) {
508 // getUserToProjectBinds - obtains all user to project binds for a group.
509 static function getUserToProjectBinds($group_id) {
510 $mdb2 = getConnection();
513 $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";
514 $res = $mdb2->query($sql);
516 if (!is_a($res, 'PEAR_Error')) {
517 while ($val = $res->fetchRow()) {
525 // The getAllCustomFields obtains all custom fields in a group.
526 static function getAllCustomFields($group_id) {
527 $mdb2 = getConnection();
529 $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
531 $res = $mdb2->query($sql);
533 if (!is_a($res, 'PEAR_Error')) {
534 while ($val = $res->fetchRow()) {
542 // The getAllCustomFieldOptions obtains all custom field options in a group.
543 static function getAllCustomFieldOptions($group_id) {
544 $mdb2 = getConnection();
546 $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";
548 $res = $mdb2->query($sql);
550 if (!is_a($res, 'PEAR_Error')) {
551 while ($val = $res->fetchRow()) {
559 // The getCustomFieldLog obtains all custom field log entries for a group.
560 static function getCustomFieldLog($group_id) {
561 $mdb2 = getConnection();
563 $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";
565 $res = $mdb2->query($sql);
567 if (!is_a($res, 'PEAR_Error')) {
568 while ($val = $res->fetchRow()) {
576 // getFavReports - obtains all favorite reports for all users in a group.
577 static function getFavReports($group_id) {
578 $mdb2 = getConnection();
581 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
582 $res = $mdb2->query($sql);
584 if (!is_a($res, 'PEAR_Error')) {
585 while ($val = $res->fetchRow()) {
593 // getExpenseItems - obtains all expense items for all users in a group.
594 static function getExpenseItems($group_id) {
595 $mdb2 = getConnection();
598 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
599 $res = $mdb2->query($sql);
601 if (!is_a($res, 'PEAR_Error')) {
602 while ($val = $res->fetchRow()) {
610 // getPredefinedExpenses - obtains predefined expenses for a group.
611 static function getPredefinedExpenses($group_id) {
613 $replaceDecimalMark = ('.' != $user->decimal_mark);
615 $mdb2 = getConnection();
618 $sql = "select id, name, cost from tt_predefined_expenses where group_id = $group_id";
619 $res = $mdb2->query($sql);
621 if (!is_a($res, 'PEAR_Error')) {
622 while ($val = $res->fetchRow()) {
623 if ($replaceDecimalMark)
624 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
632 // getNotifications - obtains notification descriptions for a group.
633 static function getNotifications($group_id) {
634 $mdb2 = getConnection();
637 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
638 left join tt_fav_reports fr on (fr.id = c.report_id)
639 where c.group_id = $group_id and c.status = 1 and fr.status = 1";
640 $res = $mdb2->query($sql);
642 if (!is_a($res, 'PEAR_Error')) {
643 while ($val = $res->fetchRow()) {
651 // getMonthlyQuotas - obtains monthly quotas for a group.
652 static function getMonthlyQuotas($group_id) {
653 $mdb2 = getConnection();
656 $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
657 $res = $mdb2->query($sql);
659 if (!is_a($res, 'PEAR_Error')) {
660 while ($val = $res->fetchRow()) {
668 // The delete function permanently deletes all data for a group.
669 static function delete($group_id) {
670 $mdb2 = getConnection();
673 $sql = "select id from tt_users where group_id = $group_id";
674 $res = $mdb2->query($sql);
675 if (is_a($res, 'PEAR_Error')) return false;
676 while ($val = $res->fetchRow()) {
677 $user_id = $val['id'];
678 if (!ttUserHelper::delete($user_id)) return false;
682 if (!ttTeamHelper::deleteTasks($group_id)) return false;
684 // Delete client to project binds.
685 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
686 $affected = $mdb2->exec($sql);
687 if (is_a($affected, 'PEAR_Error')) return false;
690 $sql = "delete from tt_projects where group_id = $group_id";
691 $affected = $mdb2->exec($sql);
692 if (is_a($affected, 'PEAR_Error')) return false;
695 $sql = "delete from tt_clients where group_id = $group_id";
696 $affected = $mdb2->exec($sql);
697 if (is_a($affected, 'PEAR_Error')) return false;
700 $sql = "delete from tt_invoices where group_id = $group_id";
701 $affected = $mdb2->exec($sql);
702 if (is_a($affected, 'PEAR_Error')) return false;
704 // Delete custom fields.
705 if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
708 $sql = "delete from tt_roles where group_id = $group_id";
709 $affected = $mdb2->exec($sql);
710 if (is_a($affected, 'PEAR_Error')) return false;
712 // Delete cron entries.
713 $sql = "delete from tt_cron where group_id = $group_id";
714 $affected = $mdb2->exec($sql);
715 if (is_a($affected, 'PEAR_Error')) return false;
717 // Delete predefined expenses.
718 $sql = "delete from tt_predefined_expenses where group_id = $group_id";
719 $affected = $mdb2->exec($sql);
720 if (is_a($affected, 'PEAR_Error')) return false;
722 // Delete monthly quotas.
723 $sql = "delete from tt_monthly_quotas where group_id = $group_id";
724 $affected = $mdb2->exec($sql);
725 if (is_a($affected, 'PEAR_Error')) return false;
728 $sql = "delete from tt_groups where id = $group_id";
729 $affected = $mdb2->exec($sql);
730 if (is_a($affected, 'PEAR_Error')) return false;
735 // The deleteTasks deletes all tasks and task binds for an inactive group.
736 static function deleteTasks($group_id) {
737 $mdb2 = getConnection();
738 $sql = "select id from tt_tasks where group_id = $group_id";
739 $res = $mdb2->query($sql);
740 if (is_a($res, 'PEAR_Error')) return false;
742 while ($val = $res->fetchRow()) {
744 // Delete task binds.
745 $task_id = $val['id'];
746 $sql = "delete from tt_project_task_binds where task_id = $task_id";
747 $affected = $mdb2->exec($sql);
748 if (is_a($affected, 'PEAR_Error')) return false;
751 $sql = "delete from tt_tasks where id = $task_id";
752 $affected = $mdb2->exec($sql);
753 if (is_a($affected, 'PEAR_Error')) return false;
759 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
760 static function deleteCustomFields($group_id) {
761 $mdb2 = getConnection();
762 $sql = "select id from tt_custom_fields where group_id = $group_id";
763 $res = $mdb2->query($sql);
764 if (is_a($res, 'PEAR_Error')) return false;
766 while ($val = $res->fetchRow()) {
767 $field_id = $val['id'];
769 // Clean up tt_custom_field_log.
770 $sql = "delete from tt_custom_field_log where field_id = $field_id";
771 $affected = $mdb2->exec($sql);
772 if (is_a($affected, 'PEAR_Error')) return false;
774 // Clean up tt_custom_field_options.
775 $sql = "delete from tt_custom_field_options where field_id = $field_id";
776 $affected = $mdb2->exec($sql);
777 if (is_a($affected, 'PEAR_Error')) return false;
779 // Delete custom field.
780 $sql = "delete from tt_custom_fields where id = $field_id";
781 $affected = $mdb2->exec($sql);
782 if (is_a($affected, 'PEAR_Error')) return false;