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 // ttAdmin class is used to perform admin tasks.
33 function __construct() {
36 // getSubgroups rerurns an array of subgroups for a group.
37 function getSubgroups($group_id) {
38 return array(); // TODO: not yet implemented.
41 // getUsers obtains user ids in a group.
42 function getUsers($group_id) {
43 $mdb2 = getConnection();
44 $sql = "select id from tt_users where team_id = $group_id";
45 $res = $mdb2->query($sql);
47 if (!is_a($res, 'PEAR_Error')) {
48 while ($val = $res->fetchRow()) {
55 // markUserDeleted marks a user and all things associated with user as deleted.
56 function markUserDeleted($user_id) {
57 $mdb2 = getConnection();
59 // Mark user binds as deleted.
60 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
61 $affected = $mdb2->exec($sql);
62 if (is_a($affected, 'PEAR_Error'))
65 // Mark favorite reports as deleted.
66 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
67 $affected = $mdb2->exec($sql);
68 if (is_a($affected, 'PEAR_Error'))
71 // Mark user as deleted.
73 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
74 $sql = "update tt_users set status = NULL $modified_part where id = $user_id";
75 $affected = $mdb2->exec($sql);
76 if (is_a($affected, 'PEAR_Error'))
82 // The markTasksDeleted deletes task binds and marks the tasks as deleted for a group.
83 function markTasksDeleted($group_id) {
84 $mdb2 = getConnection();
85 $sql = "select id from tt_tasks where team_id = $group_id";
86 $res = $mdb2->query($sql);
87 if (is_a($res, 'PEAR_Error')) return false;
89 while ($val = $res->fetchRow()) {
91 $task_id = $val['id'];
92 $sql = "delete from tt_project_task_binds where task_id = $task_id";
93 $affected = $mdb2->exec($sql);
94 if (is_a($affected, 'PEAR_Error')) return false;
96 // Mark task as deleted.
97 $sql = "update tt_tasks set status = NULL where id = $task_id";
98 $affected = $mdb2->exec($sql);
99 if (is_a($affected, 'PEAR_Error')) return false;
104 // markGroupDeleted marks the group and everything in it as deleted.
105 function markGroupDeleted($group_id) {
107 // Keep the logic simple by returning false on first error.
109 // Obtain subgroups and call self recursively on them.
110 $subgroups = $this->getSubgroups();
111 foreach($subgroups as $subgroup) {
112 if (!$this->markGroupDeleted($subgroup['id']))
116 // Now that we are done with subgroups, handle this group.
117 $users = $this->getUsers($group_id);
119 // Iterate through team users and mark them as deleted.
120 foreach ($users as $one_user) {
121 if (!$this->markUserDeleted($one_user['id']))
125 // Mark tasks deleted.
126 if (!$this->markTasksDeleted($group_id)) return false;
128 $mdb2 = getConnection();
130 // Mark roles deleted.
131 $sql = "update tt_roles set status = NULL where team_id = $group_id";
132 $affected = $mdb2->exec($sql);
133 if (is_a($affected, 'PEAR_Error')) return false;
135 // Mark projects deleted.
136 $sql = "update tt_projects set status = NULL where team_id = $group_id";
137 $affected = $mdb2->exec($sql);
138 if (is_a($affected, 'PEAR_Error')) return false;
140 // Mark clients deleted.
141 $sql = "update tt_clients set status = NULL where team_id = $group_id";
142 $affected = $mdb2->exec($sql);
143 if (is_a($affected, 'PEAR_Error')) return false;
145 // Mark invoices deleted.
146 $sql = "update tt_invoices set status = NULL where team_id = $group_id";
147 $affected = $mdb2->exec($sql);
148 if (is_a($affected, 'PEAR_Error')) return false;
150 // Mark custom fields deleted.
151 $sql = "update tt_custom_fields set status = NULL where team_id = $group_id";
152 $affected = $mdb2->exec($sql);
153 if (is_a($affected, 'PEAR_Error')) return false;
155 // Mark notifications deleted.
156 $sql = "update tt_cron set status = NULL where team_id = $group_id";
157 $affected = $mdb2->exec($sql);
158 if (is_a($affected, 'PEAR_Error')) return false;
160 // Note: we don't mark tt_log or tt_expense_items deleted here.
163 // 1) Users may mark some of them deleted during their work.
164 // If we mark all of them deleted here, we can't recover nicely
165 // as we'll lose track of what was deleted by user.
167 // 2) DB maintenance script (Clean up DB from inactive teams) should
168 // get rid of these items permanently eventually.
170 // Mark group deleted.
172 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
173 $sql = "update tt_teams set status = NULL $modified_part where id = $group_id";
174 $affected = $mdb2->exec($sql);
175 if (is_a($affected, 'PEAR_Error')) return false;