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