Implemented delete group feature in group editor.
[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 import('ttUser');
30
31 // ttAdmin class is used to perform admin tasks.
32 class ttAdmin {
33
34   var $err = null; // Error object, passed to us as reference.
35                    // We use it to communicate errors to caller.
36
37   // Constructor.
38   function __construct(&$err = null) {
39     $this->err = $err;
40   }
41
42   // getSubgroups rerurns an array of subgroups for a group.
43   function getSubgroups($group_id) {
44     $mdb2 = getConnection();
45
46     $subgroups = array();
47     $sql =  "select id from tt_groups where parent_id = $group_id";
48     $res = $mdb2->query($sql);
49     if (!is_a($res, 'PEAR_Error')) {
50       while ($val = $res->fetchRow()) {
51         $subgroups[] = $val;
52       }
53     }
54     return $subgroups;
55   }
56
57   // getUsers obtains user ids in a group.
58   function getUsers($group_id) {
59     $mdb2 = getConnection();
60     $sql = "select id from tt_users where group_id = $group_id";
61     $res = $mdb2->query($sql);
62     $users = array();
63     if (!is_a($res, 'PEAR_Error')) {
64       while ($val = $res->fetchRow()) {
65         $users[] = $val;
66       }
67     }
68     return $users;
69   }
70
71   // markUserDeleted marks a user and all things associated with user as deleted.
72   function markUserDeleted($user_id) {
73     $mdb2 = getConnection();
74
75     // Mark user binds as deleted.
76     $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id";
77     $affected = $mdb2->exec($sql);
78     if (is_a($affected, 'PEAR_Error')) return false;
79
80     // Mark favorite reports as deleted.
81     $sql = "update tt_fav_reports set status = NULL where user_id = $user_id";
82     $affected = $mdb2->exec($sql);
83     if (is_a($affected, 'PEAR_Error')) return false;
84
85     // Mark user as deleted.
86     global $user;
87     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
88     $sql = "update tt_users set status = NULL $modified_part where id = $user_id";
89     $affected = $mdb2->exec($sql);
90     if (is_a($affected, 'PEAR_Error')) return false;
91
92     return true;
93   }
94
95   // The markTasksDeleted deletes task binds and marks the tasks as deleted for a group.
96   function markTasksDeleted($group_id) {
97     $mdb2 = getConnection();
98     $sql = "select id from tt_tasks where group_id = $group_id";
99     $res = $mdb2->query($sql);
100     if (is_a($res, 'PEAR_Error')) return false;
101
102     while ($val = $res->fetchRow()) {
103       // Delete task binds.
104       $task_id = $val['id'];
105       $sql = "delete from tt_project_task_binds where task_id = $task_id";
106       $affected = $mdb2->exec($sql);
107       if (is_a($affected, 'PEAR_Error')) return false;
108
109       // Mark task as deleted.
110       $sql = "update tt_tasks set status = NULL where id = $task_id";
111       $affected = $mdb2->exec($sql);
112       if (is_a($affected, 'PEAR_Error')) return false;
113     }
114
115     return true;
116   }
117
118   // markGroupDeleted marks a group and everything in it as deleted.
119   function markGroupDeleted($group_id) {
120
121     // Keep the logic simple by returning false on first error.
122
123     // Obtain subgroups and call self recursively on them.
124     $subgroups = $this->getSubgroups($group_id);
125     foreach($subgroups as $subgroup) {
126       if (!$this->markGroupDeleted($subgroup['id']))
127         return false;
128     }
129
130     // Now that we are done with subgroups, handle this group.
131     $users = $this->getUsers($group_id);
132
133     // Iterate through group users and mark them as deleted.
134     foreach ($users as $one_user) {
135       if (!$this->markUserDeleted($one_user['id']))
136           return false;
137     }
138
139     // Mark tasks deleted.
140     if (!$this->markTasksDeleted($group_id)) return false;
141
142     $mdb2 = getConnection();
143
144     // Mark roles deleted.
145     $sql = "update tt_roles set status = NULL where group_id = $group_id";
146     $affected = $mdb2->exec($sql);
147     if (is_a($affected, 'PEAR_Error')) return false;
148
149     // Mark projects deleted.
150     $sql = "update tt_projects set status = NULL where group_id = $group_id";
151     $affected = $mdb2->exec($sql);
152     if (is_a($affected, 'PEAR_Error')) return false;
153
154     // Mark clients deleted.
155     $sql = "update tt_clients set status = NULL where group_id = $group_id";
156     $affected = $mdb2->exec($sql);
157     if (is_a($affected, 'PEAR_Error')) return false;
158
159     // Mark invoices deleted.
160     $sql = "update tt_invoices set status = NULL where group_id = $group_id";
161     $affected = $mdb2->exec($sql);
162     if (is_a($affected, 'PEAR_Error')) return false;
163
164     // Mark custom fields deleted.
165     $sql = "update tt_custom_fields set status = NULL where group_id = $group_id";
166     $affected = $mdb2->exec($sql);
167     if (is_a($affected, 'PEAR_Error')) return false;
168
169     // Mark notifications deleted.
170     $sql = "update tt_cron set status = NULL where group_id = $group_id";
171     $affected = $mdb2->exec($sql);
172     if (is_a($affected, 'PEAR_Error')) return false;
173
174     // Note: we don't mark tt_log or tt_expense_items deleted here.
175     // Reasoning is:
176     //
177     // 1) Users may mark some of them deleted during their work.
178     // If we mark all of them deleted here, we can't recover nicely
179     // as we'll lose track of what was accidentally deleted by user.
180     //
181     // 2) DB maintenance script (Clean up DB from inactive groups) should
182     // get rid of these items permanently eventually.
183
184     // Mark group deleted.
185     global $user;
186     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
187     $sql = "update tt_groups set status = NULL $modified_part where id = $group_id";
188     $affected = $mdb2->exec($sql);
189     if (is_a($affected, 'PEAR_Error')) return false;
190
191     return true;
192   }
193
194   // validateGroupInfo validates group information entered by user.
195   function validateGroupInfo($fields) {
196     global $i18n;
197     global $auth;
198
199     $result = true;
200
201     if (!ttValidString($fields['group_name'], true)) {
202       $this->err->add($i18n->get('error.field'), $i18n->get('label.group_name'));
203       $result = false;
204     }
205     if (!ttValidString($fields['user_name'])) {
206       $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_name'));
207       $result = false;
208     }
209     if (!ttValidString($fields['new_login'])) {
210       $this->err->add($i18n->get('error.field'), $i18n->get('label.manager_login'));
211       $result = false;
212     }
213
214     // If we change login, it must be unique.
215     if ($fields['new_login'] != $fields['old_login']) {
216       if (ttUserHelper::getUserByLogin($fields['new_login'])) {
217         $this->err->add($i18n->get('error.user_exists'));
218         $result = false;
219       }
220     }
221
222     if (!$auth->isPasswordExternal() && ($fields['password1'] || $fields['password2'])) {
223       if (!ttValidString($fields['password1'])) {
224         $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
225         $result = false;
226       }
227       if (!ttValidString($fields['password2'])) {
228         $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
229         $result = false;
230       }
231       if ($fields['password1'] !== $fields['password2']) {
232         $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
233         $result = false;
234       }
235     }
236     if (!ttValidEmail($fields['email'], true)) {
237       $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
238       $result = false;
239     }
240
241     return $result;
242   }
243
244   // updateGroup validates user input and updates the group with new information.
245   function updateGroup($group_id, $fields) {
246     if (!$this->validateGroupInfo($fields)) return false; // Can't continue as user input is invalid.
247
248     global $user;
249     $mdb2 = getConnection();
250
251     // Update group name if it changed.
252     if ($fields['old_group_name'] != $fields['new_group_name']) {
253       $name_part = 'name = '.$mdb2->quote($fields['new_group_name']);
254       $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
255       $sql = 'update tt_groups set '.$name_part.$modified_part.' where id = '.$group_id;
256       $affected = $mdb2->exec($sql);
257       if (is_a($affected, 'PEAR_Error')) return false;
258     }
259
260     // Update group manager.
261     $user_id = $fields['user_id'];
262     $login_part = 'login = '.$mdb2->quote($fields['new_login']);
263     if ($fields['password1'])
264       $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
265     $name_part = ', name = '.$mdb2->quote($fields['user_name']);
266     $email_part = ', email = '.$mdb2->quote($fields['email']);
267     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
268     $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.'where id = '.$user_id;
269     $affected = $mdb2->exec($sql);
270     if (is_a($affected, 'PEAR_Error')) return false;
271
272     return true;
273   }
274
275   // validateUserInfo validates account information entered by user.
276   function validateUserInfo($fields) {
277     global $i18n;
278     global $user;
279     global $auth;
280
281     $result = true;
282
283     if (!ttValidString($fields['name'])) {
284       $this->err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
285       $result = false;
286     }
287     if (!ttValidString($fields['login'])) {
288       $this->err->add($i18n->get('error.field'), $i18n->get('label.login'));
289       $result = false;
290     }
291     // If we change login, it must be unique.
292     if ($fields['login'] != $user->login) {
293       if (ttUserHelper::getUserByLogin($fields['login'])) {
294         $this->err->add($i18n->get('error.user_exists'));
295         $result = false;
296       }
297     }
298     if (!$auth->isPasswordExternal() && ($fields['password1'] || $fields['password2'])) {
299       if (!ttValidString($fields['password1'])) {
300         $this->err->add($i18n->get('error.field'), $i18n->get('label.password'));
301         $result = false;
302       }
303       if (!ttValidString($fields['password2'])) {
304         $this->err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
305         $result = false;
306       }
307       if ($fields['password1'] !== $fields['password2']) {
308         $this->err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
309         $result = false;
310       }
311     }
312     if (!ttValidEmail($fields['email'], true)) {
313       $this->err->add($i18n->get('error.field'), $i18n->get('label.email'));
314       $result = false;
315     }
316
317     return $result;
318   }
319
320   // updateSelf validates user input and updates admin account with new information.
321   function updateSelf($fields) {
322     if (!$this->validateUserInfo($fields)) return false; // Can't continue as user input is invalid.
323
324     global $user;
325     global $i18n;
326     $mdb2 = getConnection();
327
328     // Update self.
329     $user_id = $user->id;
330     $login_part = 'login = '.$mdb2->quote($fields['login']);
331     if ($fields['password1'])
332       $password_part = ', password = md5('.$mdb2->quote($fields['password1']).')';
333     $name_part = ', name = '.$mdb2->quote($fields['name']);
334     $email_part = ', email = '.$mdb2->quote($fields['email']);
335     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
336     $sql = 'update tt_users set '.$login_part.$password_part.$name_part.$email_part.$modified_part.'where id = '.$user_id;
337     $affected = $mdb2->exec($sql);
338     if (is_a($affected, 'PEAR_Error')) {
339       $this->err->add($i18n->get('error.db'));
340       return false;
341     }
342
343     return true;
344   }
345
346   // getGroupDetails obtains group name and its top manager details.
347   function getGroupDetails($group_id) {
348     $result = array();
349     $mdb2 = getConnection();
350
351     $sql = "select g.name as group_name, u.id as manager_id, u.name as manager_name, u.login as manager_login, u.email as manager_email".
352       " from tt_groups g".
353       " inner join tt_users u on (u.group_id = g.id)".
354       " inner join tt_roles r on (r.id = u.role_id and r.rank = 512)".
355       " where g.id = $group_id";
356
357     $res = $mdb2->query($sql);
358     if (!is_a($res, 'PEAR_Error')) {
359       $val = $res->fetchRow();
360       return $val;
361     }
362
363     return false;
364   }
365 }