Introduced ttAdmin class for admin tasks.
[timetracker.git] / WEB-INF / lib / ttAdmin.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // ttAdmin class is used to perform admin tasks.
30 class ttAdmin {
31
32   // Constructor.
33   function __construct() {
34   }
35
36   // getSubgroups rerurns an array of subgroups for a group.
37   function getSubgroups($group_id) {
38     return array(); // TODO: not yet implemented.
39   }
40
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);
46     $users = array();
47     if (!is_a($res, 'PEAR_Error')) {
48       while ($val = $res->fetchRow()) {
49         $users[] = $val;
50       }
51     }
52     return $users;
53   }
54
55   // markUserDeleted marks a user and all things associated with user as deleted.
56   function markUserDeleted($user_id) {
57     $mdb2 = getConnection();
58
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'))
63       return false;
64
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'))
69       return false;
70
71     // Mark user as deleted.
72     global $user;
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'))
77       return false;
78
79     return true;
80   }
81
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;
88
89     while ($val = $res->fetchRow()) {
90       // Delete task binds.
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;
95
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;
100     }
101     return true;
102   }
103
104   // markGroupDeleted marks the group and everything in it as deleted.
105   function markGroupDeleted($group_id) {
106
107     // Keep the logic simple by returning false on first error.
108
109     // Obtain subgroups and call self recursively on them.
110     $subgroups = $this->getSubgroups();
111     foreach($subgroups as $subgroup) {
112       if (!$this->markGroupDeleted($subgroup['id']))
113         return false;
114     }
115
116     // Now that we are done with subgroups, handle this group.
117     $users = $this->getUsers($group_id);
118
119     // Iterate through team users and mark them as deleted.
120     foreach ($users as $one_user) {
121       if (!$this->markUserDeleted($one_user['id']))
122           return false;
123     }
124
125     // Mark tasks deleted.
126     if (!$this->markTasksDeleted($group_id)) return false;
127
128     $mdb2 = getConnection();
129
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;
134
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;
139
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;
144
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;
149
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;
154
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;
159
160     // Note: we don't mark tt_log or tt_expense_items deleted here.
161     // Reasoning is:
162     //
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.
166     //
167     // 2) DB maintenance script (Clean up DB from inactive teams) should
168     // get rid of these items permanently eventually.
169
170     // Mark group deleted.
171     global $user;
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;
176
177     return true;
178   }
179 }