Finished refactoring ttAdmin class.
[timetracker.git] / WEB-INF / lib / ttGroupHelper.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('ttRoleHelper');
30
31 // Class ttGroupHelper - contains helper functions that operate with groups.
32 // This is a planned replacement for ttTeamHelper as we move forward with subgroups.
33 class ttGroupHelper {
34
35   // The getGroupName function returns group name.
36   static function getGroupName($group_id) {
37     $mdb2 = getConnection();
38
39     $sql = "select name from tt_groups where id = $group_id and (status = 1 or status = 0)";
40     $res = $mdb2->query($sql);
41
42     if (!is_a($res, 'PEAR_Error')) {
43       $val = $res->fetchRow();
44       return $val['name'];
45     }
46     return false;
47   }
48
49   // The getParentGroup determines a parent group for a given group.
50   static function getParentGroup($group_id) {
51     global $user;
52
53     $mdb2 = getConnection();
54
55     $sql = "select parent_id from tt_groups where id = $group_id and org_id = $user->org_id and status = 1";
56     $res = $mdb2->query($sql);
57
58     if (!is_a($res, 'PEAR_Error')) {
59       $val = $res->fetchRow();
60       return $val['parent_id'];
61     }
62     return false;
63   }
64
65   // The getSubgroupByName obtain an immediate subgroup by name if one exists.
66   static function getSubgroupByName($name) {
67     global $user;
68
69     $mdb2 = getConnection();
70     $parent_id = $user->getGroup();
71     $org_id = $user->org_id;
72
73     $sql = "select id from tt_groups where parent_id = $parent_id and org_id = $org_id".
74       " and name = ".$mdb2->quote($name)." and status is not null";
75     $res = $mdb2->query($sql);
76     if (!is_a($res, 'PEAR_Error')) {
77       $val = $res->fetchRow();
78       if ($val && $val['id'])
79         return $val;
80     }
81     return false;
82   }
83
84   // The insertSubgroup inserts a new subgroup in database.
85   static function insertSubgroup($fields) {
86     global $user;
87
88     $mdb2 = getConnection();
89     $parent_id = $user->getGroup();
90     $org_id = $user->org_id;
91     $name = $fields['name'];
92     $description = $fields['description'];
93
94     // We need to inherit other attributes from the parent group.
95     $attrs = ttGroupHelper::getGroupAttrs($parent_id);
96
97     $columns = '(parent_id, org_id, name, description, currency, decimal_mark, lang, date_format, time_format'.
98       ', week_start, tracking_mode, project_required, task_required, record_type, bcc_email'.
99       ', allow_ip, password_complexity, plugins, lock_spec'.
100       ', workday_minutes, config, created, created_ip, created_by)';
101
102     $values = " values ($parent_id, $org_id";
103     $values .= ', '.$mdb2->quote($name);
104     $values .= ', '.$mdb2->quote($description);
105     $values .= ', '.$mdb2->quote($attrs['currency']);
106     $values .= ', '.$mdb2->quote($attrs['decimal_mark']);
107     $values .= ', '.$mdb2->quote($attrs['lang']);
108     $values .= ', '.$mdb2->quote($attrs['date_format']);
109     $values .= ', '.$mdb2->quote($attrs['time_format']);
110     $values .= ', '.(int)$attrs['week_start'];
111     $values .= ', '.(int)$attrs['tracking_mode'];
112     $values .= ', '.(int)$attrs['project_required'];
113     $values .= ', '.(int)$attrs['task_required'];
114     $values .= ', '.(int)$attrs['record_type'];
115     $values .= ', '.$mdb2->quote($attrs['bcc_email']);
116     $values .= ', '.$mdb2->quote($attrs['allow_ip']);
117     $values .= ', '.$mdb2->quote($attrs['password_complexity']);
118     $values .= ', '.$mdb2->quote($attrs['plugins']);
119     $values .= ', '.$mdb2->quote($attrs['lock_spec']);
120     $values .= ', '.(int)$attrs['workday_minutes'];
121     $values .= ', '.$mdb2->quote($attrs['config']);
122     $values .= ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$user->id;
123     $values .= ')';
124
125     $sql = 'insert into tt_groups '.$columns.$values;
126     $affected = $mdb2->exec($sql);
127     if (is_a($affected, 'PEAR_Error')) return false;
128
129     $subgroup_id = $mdb2->lastInsertID('tt_groups', 'id');
130
131     // Copy roles from the parent group to child group.
132     if (!ttRoleHelper::copyRolesToGroup($subgroup_id))
133       return false;
134     
135     return $subgroup_id;
136   }
137
138   // markGroupDeleted marks a group and everything in it as deleted.
139   // This function is called in context of a logged on user (global $user object).
140   // It uses current user attributes for access checks and in sql queries.
141   // Compare this with admin:
142   //   admin can delete any group.
143   //   user can delete only relevant groups and only if allowed.
144   static function markGroupDeleted($group_id) {
145     global $user;
146
147     $mdb2 = getConnection();
148     $org_id = $user->org_id;
149
150     // Security check.
151     if (!$user->isGroupValid($group_id))
152       return false;
153
154     // Keep the logic simple by returning false on first error.
155
156     // Obtain subgroups and call self recursively on them.
157     $subgroups = $user->getSubgroups($group_id);
158     foreach($subgroups as $subgroup) {
159       if (!ttGroupHelper::markGroupDeleted($subgroup['id']))
160         return false;
161     }
162
163     // Now do actual work with all entities.
164
165     // Some things cannot be marked deleted as we don't have the status field for them.
166     // Just delete such things (until we have a better way to deal with them).
167     $tables_to_delete_from = array(
168       'tt_config',
169       'tt_predefined_expenses',
170       'tt_client_project_binds',
171       'tt_project_task_binds'
172     );
173     foreach($tables_to_delete_from as $table) {
174       if (!ttGroupHelper::deleteGroupEntriesFromTable($group_id, $table))
175         return false;
176     }
177
178     // Now mark status deleted where we can.
179     // Note: we don't mark tt_log, tt_custom_field_lod, or tt_expense_items deleted here.
180     // Reasoning is:
181     //
182     // 1) Users may mark some of them deleted during their work.
183     // If we mark all of them deleted here, we can't recover nicely
184     // as we'll lose track of what was accidentally deleted by user.
185     //
186     // 2) DB maintenance script (Clean up DB from inactive groups) should
187     // get rid of these items permanently eventually.
188     $tables_to_mark_deleted_in = array(
189       'tt_cron',
190       'tt_fav_reports',
191       // 'tt_expense_items',
192       // 'tt_custom_field_log',
193       'tt_custom_field_options',
194       'tt_custom_fields',
195       // 'tt_log',
196       'tt_invoices',
197       'tt_user_project_binds',
198       'tt_users',
199       'tt_clients',
200       'tt_projects',
201       'tt_tasks',
202       'tt_roles'
203     );
204     foreach($tables_to_mark_deleted_in as $table) {
205       if (!ttGroupHelper::markGroupDeletedInTable($group_id, $table))
206         return false;
207     }
208
209     // Mark group deleted.
210     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
211     $sql = "update tt_groups set status = null $modified_part where id = $group_id and org_id = $org_id";
212     $affected = $mdb2->exec($sql);
213     if (is_a($affected, 'PEAR_Error')) return false;
214
215     return true;
216   }
217
218   // markGroupDeletedInTable is a generic helper function for markGroupDeleted.
219   // It updates ONE table by setting status to NULL for all records belonging to a group.
220   static function markGroupDeletedInTable($group_id, $table_name) {
221     global $user;
222     $mdb2 = getConnection();
223
224     // Add modified info to sql for some tables, depending on table name.
225     if ($table_name == 'tt_users') {
226       $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
227     }
228
229     $org_id = $user->org_id; // The only security measure we use here for match.
230     $sql = "update $table_name set status = null $modified_part where group_id = $group_id and org_id = $org_id";
231     $affected = $mdb2->exec($sql);
232     return (!is_a($affected, 'PEAR_Error'));
233   }
234
235   // deleteGroupEntriesFromTable is a generic helper function for markGroupDeleted.
236   // It deletes entries in ONE table belonging to a given group.
237   static function deleteGroupEntriesFromTable($group_id, $table_name) {
238     global $user;
239     $mdb2 = getConnection();
240
241     $org_id = $user->org_id; // The only security measure we use here for match.
242     $sql = "delete from $table_name where group_id = $group_id and org_id = $org_id";
243     $affected = $mdb2->exec($sql);
244     return (!is_a($affected, 'PEAR_Error'));
245   }
246
247   // getGroupAttrs obtains all group attributes.
248   static function getGroupAttrs($group_id) {
249     global $user;
250     $mdb2 = getConnection();
251
252     $sql =  "select * from tt_groups".
253             " where status = 1 and id = $group_id and org_id = $user->org_id";
254     $res = $mdb2->query($sql);
255     if (!is_a($res, 'PEAR_Error')) {
256       $val = $res->fetchRow();
257     }
258     return $val;
259   }
260
261   // getRoles obtains all active and inactive roles in current group.
262   static function getRoles() {
263     global $user;
264     $mdb2 = getConnection();
265
266     $group_id = $user->getGroup();
267     $org_id = $user->org_id;
268     $sql =  "select * from tt_roles".
269       " where group_id = $group_id and org_id = $org_id and status is not null";
270     $res = $mdb2->query($sql);
271     if (is_a($res, 'PEAR_Error')) return false;
272     while ($val = $res->fetchRow()) {
273       $roles[] = $val;
274     }
275     return $roles;
276   }
277 }