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 $group_id = $user->getGroup();
64 $org_id = $user->org_id;
66 if (isset($options['getAllFields']))
67 $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 = $group_id and u.org_id = $org_id and u.status = 1 order by upper(u.name)";
69 $sql = "select id, name from tt_users where group_id = $group_id and org_id = $org_id and status = 1 order by upper(name)";
70 $res = $mdb2->query($sql);
72 if (is_a($res, 'PEAR_Error'))
74 while ($val = $res->fetchRow()) {
75 // Localize top manager role name, as it is not localized in db.
76 if ($val['rank'] == 512)
77 $val['role_name'] = $i18n->get('role.top_manager.label');
81 if (isset($options['putSelfFirst'])) {
82 // Put own entry at the front.
83 $cnt = count($user_list);
84 for($i = 0; $i < $cnt; $i++) {
85 if ($user_list[$i]['id'] == $user->id) {
86 $self = $user_list[$i]; // Found self.
87 array_unshift($user_list, $self); // Put own entry at the front.
88 array_splice($user_list, $i+1, 1); // Remove duplicate.
95 // The swapRolesWith swaps existing user role with that of another user.
96 static function swapRolesWith($user_id) {
98 $mdb2 = getConnection();
100 // Obtain role id for the user we are swapping ourselves with.
101 $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";
102 $res = $mdb2->query($sql);
103 if (is_a($res, 'PEAR_Error'))
105 $val = $res->fetchRow();
106 if (!$val['id'] || !$val['role_id'])
109 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
112 $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
113 $affected = $mdb2->exec($sql);
114 if (is_a($affected, 'PEAR_Error')) return false;
117 $role_id = $val['role_id'];
118 $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
119 $affected = $mdb2->exec($sql);
120 if (is_a($affected, 'PEAR_Error')) return false;
125 // The getUsersForSwap obtains all users a current user can swap roles with.
126 static function getUsersForSwap() {
128 $mdb2 = getConnection();
130 $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)";
131 $res = $mdb2->query($sql);
132 $user_list = array();
133 if (is_a($res, 'PEAR_Error'))
135 while ($val = $res->fetchRow()) {
136 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
138 continue; // Skip adding clients.
145 // The getUsers obtains all active and inactive (but not deleted) users in a group.
146 static function getUsers() {
148 $mdb2 = getConnection();
149 $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
150 $res = $mdb2->query($sql);
151 $user_list = array();
152 if (is_a($res, 'PEAR_Error'))
154 while ($val = $res->fetchRow()) {
160 // The getInactiveUsers obtains all inactive users in a group.
161 static function getInactiveUsers($group_id, $all_fields = false) {
162 $mdb2 = getConnection();
165 $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)";
167 $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
168 $res = $mdb2->query($sql);
170 if (!is_a($res, 'PEAR_Error')) {
171 while ($val = $res->fetchRow()) {
179 // getActiveProjects - returns an array of active projects for a group.
180 static function getActiveProjects($group_id)
183 $mdb2 = getConnection();
185 $sql = "select id, name, description, tasks from tt_projects
186 where group_id = $group_id and status = 1 order by upper(name)";
187 $res = $mdb2->query($sql);
189 if (!is_a($res, 'PEAR_Error')) {
190 while ($val = $res->fetchRow()) {
197 // getInactiveProjects - returns an array of inactive projects for a group.
198 static function getInactiveProjects($group_id)
201 $mdb2 = getConnection();
203 $sql = "select id, name, description, tasks from tt_projects
204 where group_id = $group_id and status = 0 order by upper(name)";
205 $res = $mdb2->query($sql);
207 if (!is_a($res, 'PEAR_Error')) {
208 while ($val = $res->fetchRow()) {
215 // The getAllProjects obtains all projects in a group.
216 static function getAllProjects($group_id, $all_fields = false) {
217 $mdb2 = getConnection();
220 $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
222 $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
223 $res = $mdb2->query($sql);
225 if (!is_a($res, 'PEAR_Error')) {
226 while ($val = $res->fetchRow()) {
234 // getActiveTasks - returns an array of active tasks for a group.
235 static function getActiveTasks($group_id)
238 $mdb2 = getConnection();
240 $sql = "select id, name, description from tt_tasks where group_id = $group_id and status = 1 order by upper(name)";
241 $res = $mdb2->query($sql);
243 if (!is_a($res, 'PEAR_Error')) {
244 while ($val = $res->fetchRow()) {
251 // getInactiveTasks - returns an array of inactive tasks for a group.
252 static function getInactiveTasks($group_id)
255 $mdb2 = getConnection();
257 $sql = "select id, name, description from tt_tasks
258 where group_id = $group_id and status = 0 order by upper(name)";
259 $res = $mdb2->query($sql);
261 if (!is_a($res, 'PEAR_Error')) {
262 while ($val = $res->fetchRow()) {
269 // The getAllTasks obtains all tasks in a group.
270 static function getAllTasks($group_id, $all_fields = false) {
271 $mdb2 = getConnection();
274 $sql = "select * from tt_tasks where group_id = $group_id order by status, upper(name)";
276 $sql = "select id, name from tt_tasks where group_id = $group_id order by status, upper(name)";
277 $res = $mdb2->query($sql);
279 if (!is_a($res, 'PEAR_Error')) {
280 while ($val = $res->fetchRow()) {
288 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
289 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
290 static function getActiveRolesForUser()
294 $mdb2 = getConnection();
296 $group_id = $user->getGroup();
297 $org_id = $user->org_id;
299 // Determine max rank. If we are working in on behalf group
300 // then rank restriction does not apply.
301 $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
303 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $max_rank and status = 1 order by rank";
304 $res = $mdb2->query($sql);
306 if (!is_a($res, 'PEAR_Error')) {
307 while ($val = $res->fetchRow()) {
308 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
309 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
310 continue; // Skip adding a client role.
317 // getActiveRoles - returns an array of active roles for a group.
318 static function getActiveRoles($group_id)
321 $mdb2 = getConnection();
323 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
324 $res = $mdb2->query($sql);
326 if (!is_a($res, 'PEAR_Error')) {
327 while ($val = $res->fetchRow()) {
328 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
335 // getInactiveRoles - returns an array of inactive roles for a group.
336 static function getInactiveRoles($group_id)
339 $mdb2 = getConnection();
341 $sql = "select id, name, rank, description from tt_roles
342 where group_id = $group_id and status = 0 order by rank";
343 $res = $mdb2->query($sql);
345 if (!is_a($res, 'PEAR_Error')) {
346 while ($val = $res->fetchRow()) {
353 // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
354 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
355 static function getInactiveRolesForUser()
359 $mdb2 = getConnection();
361 $group_id = $user->getGroup();
362 $org_id = $user->org_id;
364 // Determine max rank. If we are working in on behalf group
365 // then rank restriction does not apply.
366 $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
368 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and org_id = $org_id and rank < $max_rank and status = 0 order by rank";
369 $res = $mdb2->query($sql);
371 if (!is_a($res, 'PEAR_Error')) {
372 while ($val = $res->fetchRow()) {
373 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
374 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
375 continue; // Skip adding a client role.
382 // The getActiveClients returns an array of active clients for a group.
383 static function getActiveClients($group_id, $all_fields = false)
386 $mdb2 = getConnection();
389 $sql = "select * from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
391 $sql = "select id, name from tt_clients where group_id = $group_id and status = 1 order by upper(name)";
393 $res = $mdb2->query($sql);
395 if (!is_a($res, 'PEAR_Error')) {
396 while ($val = $res->fetchRow()) {
403 // The getAllClients obtains all clients in a group.
404 static function getAllClients($group_id, $all_fields = false) {
405 $mdb2 = getConnection();
408 $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
410 $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
412 $res = $mdb2->query($sql);
414 if (!is_a($res, 'PEAR_Error')) {
415 while ($val = $res->fetchRow()) {
423 // The getActiveInvoices returns an array of active invoices for a group.
424 static function getActiveInvoices($localizeDates = true)
427 $addPaidStatus = $user->isPluginEnabled('ps');
430 $mdb2 = getConnection();
432 if ($user->isClient())
433 $client_part = " and i.client_id = $user->client_id";
435 $sql = "select i.id, i.name, i.date, i.client_id, i.status, c.name as client_name from tt_invoices i
436 left join tt_clients c on (c.id = i.client_id)
437 where i.status = 1 and i.group_id = $user->group_id $client_part order by i.name";
438 $res = $mdb2->query($sql);
440 if (!is_a($res, 'PEAR_Error')) {
441 $dt = new DateAndTime(DB_DATEFORMAT);
442 while ($val = $res->fetchRow()) {
443 if ($localizeDates) {
444 $dt->parseVal($val['date']);
445 $val['date'] = $dt->toString($user->date_format);
448 $val['paid'] = ttInvoiceHelper::isPaid($val['id']);
455 // The getAllInvoices returns an array of all invoices for a group.
456 static function getAllInvoices()
461 $mdb2 = getConnection();
463 $sql = "select * from tt_invoices where group_id = $user->group_id";
464 $res = $mdb2->query($sql);
466 if (!is_a($res, 'PEAR_Error')) {
467 $dt = new DateAndTime(DB_DATEFORMAT);
468 while ($val = $res->fetchRow()) {
475 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
476 static function getRecentInvoices($group_id, $client_id)
481 $mdb2 = getConnection();
483 $sql = "select i.id, i.name from tt_invoices i
484 left join tt_clients c on (c.id = i.client_id)
485 where i.group_id = $group_id and i.status = 1 and c.id = $client_id
486 order by i.id desc limit 3";
487 $res = $mdb2->query($sql);
489 if (!is_a($res, 'PEAR_Error')) {
490 $dt = new DateAndTime(DB_DATEFORMAT);
491 while ($val = $res->fetchRow()) {
498 // getUserToProjectBinds - obtains all user to project binds for a group.
499 static function getUserToProjectBinds($group_id) {
500 $mdb2 = getConnection();
503 $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";
504 $res = $mdb2->query($sql);
506 if (!is_a($res, 'PEAR_Error')) {
507 while ($val = $res->fetchRow()) {
515 // The getAllCustomFields obtains all custom fields in a group.
516 static function getAllCustomFields($group_id) {
517 $mdb2 = getConnection();
519 $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
521 $res = $mdb2->query($sql);
523 if (!is_a($res, 'PEAR_Error')) {
524 while ($val = $res->fetchRow()) {
532 // The getAllCustomFieldOptions obtains all custom field options in a group.
533 static function getAllCustomFieldOptions($group_id) {
534 $mdb2 = getConnection();
536 $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";
538 $res = $mdb2->query($sql);
540 if (!is_a($res, 'PEAR_Error')) {
541 while ($val = $res->fetchRow()) {
549 // The getCustomFieldLog obtains all custom field log entries for a group.
550 static function getCustomFieldLog($group_id) {
551 $mdb2 = getConnection();
553 $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";
555 $res = $mdb2->query($sql);
557 if (!is_a($res, 'PEAR_Error')) {
558 while ($val = $res->fetchRow()) {
566 // getFavReports - obtains all favorite reports for all users in a group.
567 static function getFavReports($group_id) {
568 $mdb2 = getConnection();
571 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
572 $res = $mdb2->query($sql);
574 if (!is_a($res, 'PEAR_Error')) {
575 while ($val = $res->fetchRow()) {
583 // getExpenseItems - obtains all expense items for all users in a group.
584 static function getExpenseItems($group_id) {
585 $mdb2 = getConnection();
588 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
589 $res = $mdb2->query($sql);
591 if (!is_a($res, 'PEAR_Error')) {
592 while ($val = $res->fetchRow()) {
600 // getPredefinedExpenses - obtains predefined expenses for a group.
601 static function getPredefinedExpenses($group_id) {
603 $replaceDecimalMark = ('.' != $user->decimal_mark);
605 $mdb2 = getConnection();
608 $sql = "select id, name, cost from tt_predefined_expenses where group_id = $group_id";
609 $res = $mdb2->query($sql);
611 if (!is_a($res, 'PEAR_Error')) {
612 while ($val = $res->fetchRow()) {
613 if ($replaceDecimalMark)
614 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
622 // getNotifications - obtains notification descriptions for a group.
623 static function getNotifications($group_id) {
624 $mdb2 = getConnection();
627 $sql = "select c.id, c.cron_spec, c.email, c.report_condition, fr.name from tt_cron c
628 left join tt_fav_reports fr on (fr.id = c.report_id)
629 where c.group_id = $group_id and c.status = 1 and fr.status = 1";
630 $res = $mdb2->query($sql);
632 if (!is_a($res, 'PEAR_Error')) {
633 while ($val = $res->fetchRow()) {
641 // getMonthlyQuotas - obtains monthly quotas for a group.
642 static function getMonthlyQuotas($group_id) {
643 $mdb2 = getConnection();
646 $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
647 $res = $mdb2->query($sql);
649 if (!is_a($res, 'PEAR_Error')) {
650 while ($val = $res->fetchRow()) {
658 // The delete function permanently deletes all data for a group.
659 static function delete($group_id) {
660 $mdb2 = getConnection();
663 $sql = "select id from tt_users where group_id = $group_id";
664 $res = $mdb2->query($sql);
665 if (is_a($res, 'PEAR_Error')) return false;
666 while ($val = $res->fetchRow()) {
667 $user_id = $val['id'];
668 if (!ttUserHelper::delete($user_id)) return false;
672 if (!ttTeamHelper::deleteTasks($group_id)) return false;
674 // Delete client to project binds.
675 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
676 $affected = $mdb2->exec($sql);
677 if (is_a($affected, 'PEAR_Error')) return false;
680 $sql = "delete from tt_projects where group_id = $group_id";
681 $affected = $mdb2->exec($sql);
682 if (is_a($affected, 'PEAR_Error')) return false;
685 $sql = "delete 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_invoices where group_id = $group_id";
691 $affected = $mdb2->exec($sql);
692 if (is_a($affected, 'PEAR_Error')) return false;
694 // Delete custom fields.
695 if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
698 $sql = "delete from tt_roles where group_id = $group_id";
699 $affected = $mdb2->exec($sql);
700 if (is_a($affected, 'PEAR_Error')) return false;
702 // Delete cron entries.
703 $sql = "delete from tt_cron where group_id = $group_id";
704 $affected = $mdb2->exec($sql);
705 if (is_a($affected, 'PEAR_Error')) return false;
707 // Delete predefined expenses.
708 $sql = "delete from tt_predefined_expenses where group_id = $group_id";
709 $affected = $mdb2->exec($sql);
710 if (is_a($affected, 'PEAR_Error')) return false;
712 // Delete monthly quotas.
713 $sql = "delete from tt_monthly_quotas where group_id = $group_id";
714 $affected = $mdb2->exec($sql);
715 if (is_a($affected, 'PEAR_Error')) return false;
718 $sql = "delete from tt_groups where id = $group_id";
719 $affected = $mdb2->exec($sql);
720 if (is_a($affected, 'PEAR_Error')) return false;
725 // The deleteTasks deletes all tasks and task binds for an inactive group.
726 static function deleteTasks($group_id) {
727 $mdb2 = getConnection();
728 $sql = "select id from tt_tasks where group_id = $group_id";
729 $res = $mdb2->query($sql);
730 if (is_a($res, 'PEAR_Error')) return false;
732 while ($val = $res->fetchRow()) {
734 // Delete task binds.
735 $task_id = $val['id'];
736 $sql = "delete from tt_project_task_binds where task_id = $task_id";
737 $affected = $mdb2->exec($sql);
738 if (is_a($affected, 'PEAR_Error')) return false;
741 $sql = "delete from tt_tasks where id = $task_id";
742 $affected = $mdb2->exec($sql);
743 if (is_a($affected, 'PEAR_Error')) return false;
749 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
750 static function deleteCustomFields($group_id) {
751 $mdb2 = getConnection();
752 $sql = "select id from tt_custom_fields where group_id = $group_id";
753 $res = $mdb2->query($sql);
754 if (is_a($res, 'PEAR_Error')) return false;
756 while ($val = $res->fetchRow()) {
757 $field_id = $val['id'];
759 // Clean up tt_custom_field_log.
760 $sql = "delete from tt_custom_field_log where field_id = $field_id";
761 $affected = $mdb2->exec($sql);
762 if (is_a($affected, 'PEAR_Error')) return false;
764 // Clean up tt_custom_field_options.
765 $sql = "delete from tt_custom_field_options where field_id = $field_id";
766 $affected = $mdb2->exec($sql);
767 if (is_a($affected, 'PEAR_Error')) return false;
769 // Delete custom field.
770 $sql = "delete from tt_custom_fields where id = $field_id";
771 $affected = $mdb2->exec($sql);
772 if (is_a($affected, 'PEAR_Error')) return false;