Refactoring in import.
[timetracker.git] / WEB-INF / lib / ttRoleHelper.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 // The ttRoleHelper is a class to help with custom group roles.
30 class ttRoleHelper {
31
32   // get - gets details of a role identified by its id.
33   static function get($id)
34   {
35     global $user;
36
37     $mdb2 = getConnection();
38
39     $sql = "select id, name, description, rank, rights, status from tt_roles
40       where id = $id and group_id = ".$user->getGroup()." and (status = 0 or status = 1)";
41     $res = $mdb2->query($sql);
42
43     if (!is_a($res, 'PEAR_Error')) {
44       $val = $res->fetchRow();
45           if ($val['id'] != '') {
46         return $val;
47       } else
48         return false;
49     }
50     return false;
51   }
52
53   // The getRoleByName looks up a role by name.
54   static function getRoleByName($role_name) {
55
56     $mdb2 = getConnection();
57     global $user;
58
59     $sql = "select id from tt_roles where group_id = ".$user->getGroup().
60       " and name = ".$mdb2->quote($role_name)." and (status = 1 or status = 0)";
61     $res = $mdb2->query($sql);
62
63     if (!is_a($res, 'PEAR_Error')) {
64       $val = $res->fetchRow();
65       if ($val['id'])
66         return $val;
67     }
68     return false;
69   }
70
71   // The getTopManagerRoleID obtains an ID for top manager role.
72   static function getTopManagerRoleID() {
73     $mdb2 = getConnection();
74
75     $sql = "select id from tt_roles where group_id = 0 and rank = 512";
76     $res = $mdb2->query($sql);
77
78     if (!is_a($res, 'PEAR_Error')) {
79       $val = $res->fetchRow();
80       if ($val['id'])
81         return $val['id'];
82     }
83     return false;
84   }
85
86   // isClientRole determines if the role is a "client" role.
87   // This simply means the role has no "track_own_time" right.
88   static function isClientRole($role_id) {
89     global $user;
90     $mdb2 = getConnection();
91
92     $sql = "select rights from tt_roles where group_id = ".$user->getGroup()." and id = $role_id";
93     $res = $mdb2->query($sql);
94
95     if (!is_a($res, 'PEAR_Error')) {
96       $val = $res->fetchRow();
97       if ($val['rights']) {
98         return !in_array('track_own_time', explode(',', $val['rights']));
99       }
100     }
101     return false;
102   }
103
104   // getRoleByRank looks up a role by its rank.
105   static function getRoleByRank($rank, $group_id) {
106     global $user;
107     $mdb2 = getConnection();
108
109     $rank = (int) $rank; // Cast to int just in case for better security.
110
111     $sql = "select id from tt_roles where group_id = $group_id and rank = $rank and (status = 1 or status = 0)";
112     $res = $mdb2->query($sql);
113
114     if (!is_a($res, 'PEAR_Error')) {
115       $val = $res->fetchRow();
116       if ($val['id'])
117         return $val['id'];
118     }
119     return false;
120   }
121
122   // update function updates a role in the database.
123   static function update($fields) {
124     global $user;
125     $mdb2 = getConnection();
126
127     $group_id = $user->getGroup();
128     $org_id = $user->org_id;
129
130     $id = (int)$fields['id'];
131     if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
132     if (isset($fields['rank'])) $rank_part = ', rank = '.(int)$fields['rank'];
133     if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
134     if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
135     if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
136     $parts = trim($name_part.$rank_part.$descr_part.$status_part.$rights_part, ',');
137     $sql = "update tt_roles set $parts where id = $id and group_id = $group_id and org_id = $org_id";
138     $affected = $mdb2->exec($sql);
139     return (!is_a($affected, 'PEAR_Error'));
140   }
141
142   // delete - marks the role as deleted.
143   static function delete($role_id) {
144     global $user;
145
146     $mdb2 = getConnection();
147     $group_id = $user->getGroup();
148     $org_id = $user->org_id;
149
150     // Mark the task as deleted.
151     $sql = "update tt_roles set status = NULL where id = $role_id and group_id = $group_id and org_id = $org_id";
152     $affected = $mdb2->exec($sql);
153     return (!is_a($affected, 'PEAR_Error'));
154   }
155
156   // insert - inserts an entry into tt_roles table.
157   static function insert($fields)
158   {
159     global $user;
160     $mdb2 = getConnection();
161
162     $group_id = $user->getGroup();
163     $org_id = $user->org_id;
164     $name = $fields['name'];
165     $rank = (int) $fields['rank'];
166     $description = $fields['description'];
167     $rights = $fields['rights'];
168     $status = $fields['status'];
169
170     $sql = "insert into tt_roles (group_id, org_id, name, rank, description, rights, status)
171       values ($group_id, $org_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
172     $affected = $mdb2->exec($sql);
173     if (is_a($affected, 'PEAR_Error'))
174       return false;
175
176     $sql = "SELECT LAST_INSERT_ID() AS last_id";
177     $res = $mdb2->query($sql);
178     $val = $res->fetchRow();
179     $last_id = $val['last_id'];
180     return $last_id;
181   }
182
183   // createPredefinedRoles - creates a set of predefined roles for a group to use.
184   static function createPredefinedRoles($group_id, $lang)
185   {
186     // We need localized role names and a new I18n object to obtain them.
187     import('I18n');
188     $i18n = new I18n();
189     $i18n->load($lang);
190
191     $mdb2 = getConnection();
192
193     $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
194     $rights_user = 'track_own_time,track_own_expenses,view_own_reports,view_own_charts,view_own_projects,view_own_tasks,manage_own_settings,view_users';
195     $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,view_charts,view_own_clients,override_punch_mode,override_date_lock,override_own_date_lock,swap_roles,approve_timesheets';
196     $rights_comanager = $rights_supervisor.',manage_own_account,manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices,override_allow_ip,manage_basic_settings,view_all_reports';
197     $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
198
199     // Active roles.
200     $name = $mdb2->quote($i18n->get('role.user.label'));
201     $description = $mdb2->quote($i18n->get('role.user.description'));
202     $rights = $mdb2->quote($rights_user);
203     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 4, $rights, 1)";
204     $affected = $mdb2->exec($sql);
205     if (is_a($affected, 'PEAR_Error'))
206       return false;
207
208     $name = $mdb2->quote($i18n->get('role.client.label'));
209     $description = $mdb2->quote($i18n->get('role.client.description'));
210     $rights = $mdb2->quote($rights_client);
211     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 16, $rights, 1)";
212     $affected = $mdb2->exec($sql);
213     if (is_a($affected, 'PEAR_Error'))
214       return false;
215
216     $name = $mdb2->quote($i18n->get('role.comanager.label'));
217     $description = $mdb2->quote($i18n->get('role.comanager.description'));
218     $rights = $mdb2->quote($rights_comanager);
219     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 68, $rights, 1)";
220     $affected = $mdb2->exec($sql);
221     if (is_a($affected, 'PEAR_Error'))
222       return false;
223
224     $name = $mdb2->quote($i18n->get('role.manager.label'));
225     $description = $mdb2->quote($i18n->get('role.manager.description'));
226     $rights = $mdb2->quote($rights_manager);
227     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 324, $rights, 1)";
228     $affected = $mdb2->exec($sql);
229     if (is_a($affected, 'PEAR_Error'))
230       return false;
231
232     // Inactive roles.
233     $name = $mdb2->quote($i18n->get('role.supervisor.label'));
234     $description = $mdb2->quote($i18n->get('role.supervisor.description'));
235     $rights = $mdb2->quote($rights_supervisor);
236     $sql = "insert into tt_roles (group_id, org_id, name, description, rank, rights, status) values($group_id, $group_id, $name, $description, 12, $rights, 0)";
237     $affected = $mdb2->exec($sql);
238     if (is_a($affected, 'PEAR_Error'))
239       return false;
240
241     return true;
242   }
243
244   // createPredefinedRoles_1_17_44 - used in dbinstall.php during database schema update.
245   static function createPredefinedRoles_1_17_44($group_id, $lang)
246   {
247     // We need localized role names and a new I18n object to obtain them.
248     import('I18n');
249     $i18n = new I18n();
250     $i18n->load($lang);
251
252     $mdb2 = getConnection();
253
254     $rights_client = 'view_own_reports,view_own_charts,view_own_invoices,manage_own_settings';
255     $rights_user = 'track_own_time,track_own_expenses,view_own_reports,view_own_charts,view_own_projects,view_own_tasks,manage_own_settings,view_users';
256     $rights_supervisor = $rights_user.',track_time,track_expenses,view_reports,view_charts,view_own_clients,override_punch_mode,override_date_lock,override_own_date_lock,swap_roles,approve_timesheets';
257     $rights_comanager = $rights_supervisor.',manage_own_account,manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices,override_allow_ip,manage_basic_settings,view_all_reports';
258     $rights_manager = $rights_comanager.',manage_features,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
259
260     // Active roles.
261     $name = $mdb2->quote($i18n->get('role.user.label'));
262     $description = $mdb2->quote($i18n->get('role.user.description'));
263     $rights = $mdb2->quote($rights_user);
264     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 4, $rights, 1)";
265     $affected = $mdb2->exec($sql);
266     if (is_a($affected, 'PEAR_Error'))
267       return false;
268
269     $name = $mdb2->quote($i18n->get('role.client.label'));
270     $description = $mdb2->quote($i18n->get('role.client.description'));
271     $rights = $mdb2->quote($rights_client);
272     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 16, $rights, 1)";
273     $affected = $mdb2->exec($sql);
274     if (is_a($affected, 'PEAR_Error'))
275       return false;
276
277     $name = $mdb2->quote($i18n->get('role.comanager.label'));
278     $description = $mdb2->quote($i18n->get('role.comanager.description'));
279     $rights = $mdb2->quote($rights_comanager);
280     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 68, $rights, 1)";
281     $affected = $mdb2->exec($sql);
282     if (is_a($affected, 'PEAR_Error'))
283       return false;
284
285     $name = $mdb2->quote($i18n->get('role.manager.label'));
286     $description = $mdb2->quote($i18n->get('role.manager.description'));
287     $rights = $mdb2->quote($rights_manager);
288     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 324, $rights, 1)";
289     $affected = $mdb2->exec($sql);
290     if (is_a($affected, 'PEAR_Error'))
291       return false;
292
293     // Inactive roles.
294     $name = $mdb2->quote($i18n->get('role.supervisor.label'));
295     $description = $mdb2->quote($i18n->get('role.supervisor.description'));
296     $rights = $mdb2->quote($rights_supervisor);
297     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($group_id, $name, $description, 12, $rights, 0)";
298     $affected = $mdb2->exec($sql);
299     if (is_a($affected, 'PEAR_Error'))
300       return false;
301
302     return true;
303   }
304
305   // getRoleByRank_1_17_44 is used in dbinstall.php and looks up a role by its rank.
306   static function getRoleByRank_1_17_44($rank, $group_id) {
307     global $user;
308     $mdb2 = getConnection();
309
310     $rank = (int) $rank; // Cast to int just in case for better security.
311
312     $sql = "select id from tt_roles where team_id = $group_id and rank = $rank and (status = 1 or status = 0)";
313     $res = $mdb2->query($sql);
314
315     if (!is_a($res, 'PEAR_Error')) {
316       $val = $res->fetchRow();
317       if ($val['id'])
318         return $val['id'];
319     }
320     return false;
321   }
322
323   // copyRolesToGroup copies roles from current on behalf group to another.
324   static function copyRolesToGroup($group_id) {
325     global $user;
326     $mdb2 = getConnection();
327
328     $org_id = $user->org_id;
329     $columns = '(group_id, org_id, name, description, rank, rights, status)';
330     $roles = ttGroupHelper::getRoles(); // Roles in current on behalf group.
331
332     foreach ($roles as $role) {
333       $values = "values($group_id, $org_id".
334         ', '.$mdb2->quote($role['name']).
335         ', '.$mdb2->quote($role['description']).
336         ', '.(int)$role['rank'].
337         ', '.$mdb2->quote($role['rights']).
338         ', '.$mdb2->quote($role['status']).
339         ')';
340       $sql = "insert into tt_roles $columns $values";
341       $affected = $mdb2->exec($sql);
342       if (is_a($affected, 'PEAR_Error'))
343         return false;
344     }
345     return true;
346   }
347 }