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 swapRolesWith swaps existing user role with that of another user.
58 static function swapRolesWith($user_id) {
60 $mdb2 = getConnection();
62 // Obtain role id for the user we are swapping ourselves with.
63 $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";
64 $res = $mdb2->query($sql);
65 if (is_a($res, 'PEAR_Error'))
67 $val = $res->fetchRow();
68 if (!$val['id'] || !$val['role_id'])
71 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
74 $sql = "update tt_users set role_id = $user->role_id".$modified_part." where id = $user_id and group_id = $user->group_id";
75 $affected = $mdb2->exec($sql);
76 if (is_a($affected, 'PEAR_Error')) return false;
79 $role_id = $val['role_id'];
80 $sql = "update tt_users set role_id = $role_id".$modified_part." where id = $user->id and group_id = $user->group_id";
81 $affected = $mdb2->exec($sql);
82 if (is_a($affected, 'PEAR_Error')) return false;
87 // The getUsersForSwap obtains all users a current user can swap roles with.
88 static function getUsersForSwap() {
90 $mdb2 = getConnection();
92 $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)";
93 $res = $mdb2->query($sql);
95 if (is_a($res, 'PEAR_Error'))
97 while ($val = $res->fetchRow()) {
98 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
100 continue; // Skip adding clients.
107 // The getUsers obtains all active and inactive (but not deleted) users in a group.
108 static function getUsers() {
110 $mdb2 = getConnection();
111 $sql = "select id, name from tt_users where group_id = $user->group_id and (status = 1 or status = 0) order by upper(name)";
112 $res = $mdb2->query($sql);
113 $user_list = array();
114 if (is_a($res, 'PEAR_Error'))
116 while ($val = $res->fetchRow()) {
122 // The getInactiveUsers obtains all inactive users in a group.
123 static function getInactiveUsers($group_id, $all_fields = false) {
124 $mdb2 = getConnection();
127 $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)";
129 $sql = "select id, name from tt_users where group_id = $group_id and status = 0 order by upper(name)";
130 $res = $mdb2->query($sql);
132 if (!is_a($res, 'PEAR_Error')) {
133 while ($val = $res->fetchRow()) {
141 // The getAllProjects obtains all projects in a group.
142 static function getAllProjects($group_id, $all_fields = false) {
143 $mdb2 = getConnection();
146 $sql = "select * from tt_projects where group_id = $group_id order by status, upper(name)";
148 $sql = "select id, name from tt_projects where group_id = $group_id order by status, upper(name)";
149 $res = $mdb2->query($sql);
151 if (!is_a($res, 'PEAR_Error')) {
152 while ($val = $res->fetchRow()) {
160 // getActiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
161 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
162 static function getActiveRolesForUser()
166 $mdb2 = getConnection();
168 $group_id = $user->getGroup();
169 $org_id = $user->org_id;
171 // Determine max rank. If we are working in on behalf group
172 // then rank restriction does not apply.
173 $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
175 $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";
176 $res = $mdb2->query($sql);
178 if (!is_a($res, 'PEAR_Error')) {
179 while ($val = $res->fetchRow()) {
180 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
181 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
182 continue; // Skip adding a client role.
189 // getActiveRoles - returns an array of active roles for a group.
190 static function getActiveRoles($group_id)
193 $mdb2 = getConnection();
195 $sql = "select id, name, description, rank, rights from tt_roles where group_id = $group_id and status = 1 order by rank";
196 $res = $mdb2->query($sql);
198 if (!is_a($res, 'PEAR_Error')) {
199 while ($val = $res->fetchRow()) {
200 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
207 // getInactiveRoles - returns an array of inactive roles for a group.
208 static function getInactiveRoles($group_id)
211 $mdb2 = getConnection();
213 $sql = "select id, name, rank, description from tt_roles
214 where group_id = $group_id and status = 0 order by rank";
215 $res = $mdb2->query($sql);
217 if (!is_a($res, 'PEAR_Error')) {
218 while ($val = $res->fetchRow()) {
225 // getInactiveRolesForUser - returns an array of relevant active roles for user with rank less than self.
226 // "Relevant" means that client roles are filtered out if Client plugin is disabled.
227 static function getInactiveRolesForUser()
231 $mdb2 = getConnection();
233 $group_id = $user->getGroup();
234 $org_id = $user->org_id;
236 // Determine max rank. If we are working in on behalf group
237 // then rank restriction does not apply.
238 $max_rank = $user->behalfGroup ? MAX_RANK : $user->rank;
240 $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";
241 $res = $mdb2->query($sql);
243 if (!is_a($res, 'PEAR_Error')) {
244 while ($val = $res->fetchRow()) {
245 $val['is_client'] = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have data entry right.
246 if ($val['is_client'] && !$user->isPluginEnabled('cl'))
247 continue; // Skip adding a client role.
254 // The getAllClients obtains all clients in a group.
255 static function getAllClients($group_id, $all_fields = false) {
256 $mdb2 = getConnection();
259 $sql = "select * from tt_clients where group_id = $group_id order by status, upper(name)";
261 $sql = "select id, name from tt_clients where group_id = $group_id order by status, upper(name)";
263 $res = $mdb2->query($sql);
265 if (!is_a($res, 'PEAR_Error')) {
266 while ($val = $res->fetchRow()) {
274 // The getAllInvoices returns an array of all invoices for a group.
275 static function getAllInvoices()
280 $mdb2 = getConnection();
282 $sql = "select * from tt_invoices where group_id = $user->group_id";
283 $res = $mdb2->query($sql);
285 if (!is_a($res, 'PEAR_Error')) {
286 $dt = new DateAndTime(DB_DATEFORMAT);
287 while ($val = $res->fetchRow()) {
294 // The getRecentInvoices returns an array of recent invoices (max 3) for a client.
295 static function getRecentInvoices($group_id, $client_id)
300 $mdb2 = getConnection();
302 $sql = "select i.id, i.name from tt_invoices i
303 left join tt_clients c on (c.id = i.client_id)
304 where i.group_id = $group_id and i.status = 1 and c.id = $client_id
305 order by i.id desc limit 3";
306 $res = $mdb2->query($sql);
308 if (!is_a($res, 'PEAR_Error')) {
309 $dt = new DateAndTime(DB_DATEFORMAT);
310 while ($val = $res->fetchRow()) {
317 // getUserToProjectBinds - obtains all user to project binds for a group.
318 static function getUserToProjectBinds($group_id) {
319 $mdb2 = getConnection();
322 $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";
323 $res = $mdb2->query($sql);
325 if (!is_a($res, 'PEAR_Error')) {
326 while ($val = $res->fetchRow()) {
334 // The getAllCustomFields obtains all custom fields in a group.
335 static function getAllCustomFields($group_id) {
336 $mdb2 = getConnection();
338 $sql = "select * from tt_custom_fields where group_id = $group_id order by status";
340 $res = $mdb2->query($sql);
342 if (!is_a($res, 'PEAR_Error')) {
343 while ($val = $res->fetchRow()) {
351 // The getAllCustomFieldOptions obtains all custom field options in a group.
352 static function getAllCustomFieldOptions($group_id) {
353 $mdb2 = getConnection();
355 $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";
357 $res = $mdb2->query($sql);
359 if (!is_a($res, 'PEAR_Error')) {
360 while ($val = $res->fetchRow()) {
368 // The getCustomFieldLog obtains all custom field log entries for a group.
369 static function getCustomFieldLog($group_id) {
370 $mdb2 = getConnection();
372 $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";
374 $res = $mdb2->query($sql);
376 if (!is_a($res, 'PEAR_Error')) {
377 while ($val = $res->fetchRow()) {
385 // getFavReports - obtains all favorite reports for all users in a group.
386 static function getFavReports($group_id) {
387 $mdb2 = getConnection();
390 $sql = "select * from tt_fav_reports where user_id in (select id from tt_users where group_id = $group_id)";
391 $res = $mdb2->query($sql);
393 if (!is_a($res, 'PEAR_Error')) {
394 while ($val = $res->fetchRow()) {
402 // getExpenseItems - obtains all expense items for all users in a group.
403 static function getExpenseItems($group_id) {
404 $mdb2 = getConnection();
407 $sql = "select * from tt_expense_items where user_id in (select id from tt_users where group_id = $group_id)";
408 $res = $mdb2->query($sql);
410 if (!is_a($res, 'PEAR_Error')) {
411 while ($val = $res->fetchRow()) {
419 // getMonthlyQuotas - obtains monthly quotas for a group.
420 static function getMonthlyQuotas($group_id) {
421 $mdb2 = getConnection();
424 $sql = "select year, month, minutes from tt_monthly_quotas where group_id = $group_id";
425 $res = $mdb2->query($sql);
427 if (!is_a($res, 'PEAR_Error')) {
428 while ($val = $res->fetchRow()) {
436 // The delete function permanently deletes all data for a group.
437 static function delete($group_id) {
438 $mdb2 = getConnection();
441 $sql = "select id from tt_users where group_id = $group_id";
442 $res = $mdb2->query($sql);
443 if (is_a($res, 'PEAR_Error')) return false;
444 while ($val = $res->fetchRow()) {
445 $user_id = $val['id'];
446 if (!ttUserHelper::delete($user_id)) return false;
450 if (!ttTeamHelper::deleteTasks($group_id)) return false;
452 // Delete client to project binds.
453 $sql = "delete from tt_client_project_binds where client_id in (select id from tt_clients where group_id = $group_id)";
454 $affected = $mdb2->exec($sql);
455 if (is_a($affected, 'PEAR_Error')) return false;
458 $sql = "delete from tt_projects where group_id = $group_id";
459 $affected = $mdb2->exec($sql);
460 if (is_a($affected, 'PEAR_Error')) return false;
463 $sql = "delete from tt_clients where group_id = $group_id";
464 $affected = $mdb2->exec($sql);
465 if (is_a($affected, 'PEAR_Error')) return false;
468 $sql = "delete from tt_invoices where group_id = $group_id";
469 $affected = $mdb2->exec($sql);
470 if (is_a($affected, 'PEAR_Error')) return false;
472 // Delete custom fields.
473 if (!ttTeamHelper::deleteCustomFields($group_id)) return false;
476 $sql = "delete from tt_roles where group_id = $group_id";
477 $affected = $mdb2->exec($sql);
478 if (is_a($affected, 'PEAR_Error')) return false;
480 // Delete cron entries.
481 $sql = "delete from tt_cron where group_id = $group_id";
482 $affected = $mdb2->exec($sql);
483 if (is_a($affected, 'PEAR_Error')) return false;
485 // Delete predefined expenses.
486 $sql = "delete from tt_predefined_expenses where group_id = $group_id";
487 $affected = $mdb2->exec($sql);
488 if (is_a($affected, 'PEAR_Error')) return false;
490 // Delete monthly quotas.
491 $sql = "delete from tt_monthly_quotas where group_id = $group_id";
492 $affected = $mdb2->exec($sql);
493 if (is_a($affected, 'PEAR_Error')) return false;
496 $sql = "delete from tt_groups where id = $group_id";
497 $affected = $mdb2->exec($sql);
498 if (is_a($affected, 'PEAR_Error')) return false;
503 // The deleteTasks deletes all tasks and task binds for an inactive group.
504 static function deleteTasks($group_id) {
505 $mdb2 = getConnection();
506 $sql = "select id from tt_tasks where group_id = $group_id";
507 $res = $mdb2->query($sql);
508 if (is_a($res, 'PEAR_Error')) return false;
510 while ($val = $res->fetchRow()) {
512 // Delete task binds.
513 $task_id = $val['id'];
514 $sql = "delete from tt_project_task_binds where task_id = $task_id";
515 $affected = $mdb2->exec($sql);
516 if (is_a($affected, 'PEAR_Error')) return false;
519 $sql = "delete from tt_tasks where id = $task_id";
520 $affected = $mdb2->exec($sql);
521 if (is_a($affected, 'PEAR_Error')) return false;
527 // The deleteCustomFields cleans up tt_custom_field_log, tt_custom_field_options and tt_custom_fields tables for an inactive group.
528 static function deleteCustomFields($group_id) {
529 $mdb2 = getConnection();
530 $sql = "select id from tt_custom_fields where group_id = $group_id";
531 $res = $mdb2->query($sql);
532 if (is_a($res, 'PEAR_Error')) return false;
534 while ($val = $res->fetchRow()) {
535 $field_id = $val['id'];
537 // Clean up tt_custom_field_log.
538 $sql = "delete from tt_custom_field_log where field_id = $field_id";
539 $affected = $mdb2->exec($sql);
540 if (is_a($affected, 'PEAR_Error')) return false;
542 // Clean up tt_custom_field_options.
543 $sql = "delete from tt_custom_field_options where field_id = $field_id";
544 $affected = $mdb2->exec($sql);
545 if (is_a($affected, 'PEAR_Error')) return false;
547 // Delete custom field.
548 $sql = "delete from tt_custom_fields where id = $field_id";
549 $affected = $mdb2->exec($sql);
550 if (is_a($affected, 'PEAR_Error')) return false;