Put code to create default roles upon team creation.
[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 team_id = $user->team_id 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 team_id = $user->team_id and name = ".
60       $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 team_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   // The getRoleID looks up a role by its rank.
87   static function getRoleByRank($rank) {
88     global $user;
89     $mdb2 = getConnection();
90
91     $rank = (int) $rank; // Cast to int just in case for better security.
92
93     $sql = "select id from tt_roles where team_id = $user->team_id and rank = $rank and (status = 1 or status = 0)";
94     $res = $mdb2->query($sql);
95
96     if (!is_a($res, 'PEAR_Error')) {
97       $val = $res->fetchRow();
98       if ($val['id'])
99         return $val;
100     }
101     return false;
102   }
103
104   // update function updates a role in the database.
105   static function update($fields) {
106     global $user;
107     $mdb2 = getConnection();
108
109     $id = (int)$fields['id'];
110     if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
111     if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
112     if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
113     if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
114     $parts = trim($name_part.$descr_part.$status_part.$rights_part, ',');
115     $sql = "update tt_roles set $parts where id = $id and team_id = $user->team_id";
116     $affected = $mdb2->exec($sql);
117     return (!is_a($affected, 'PEAR_Error'));
118   }
119
120   // delete - marks the role as deleted.
121   static function delete($role_id) {
122     global $user;
123
124     $mdb2 = getConnection();
125
126     // Mark the task as deleted.
127     $sql = "update tt_roles set status = NULL where id = $role_id and team_id = $user->team_id";
128     $affected = $mdb2->exec($sql);
129     return (!is_a($affected, 'PEAR_Error'));
130   }
131
132   // insert - inserts an entry into tt_roles table.
133   static function insert($fields)
134   {
135     $mdb2 = getConnection();
136
137     $team_id = (int) $fields['team_id'];
138     $name = $fields['name'];
139     $rank = (int) $fields['rank'];
140     $description = $fields['description'];
141     $rights = $fields['rights'];
142     $status = $fields['status'];
143
144     $sql = "insert into tt_roles (team_id, name, rank, description, rights, status)
145       values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
146     $affected = $mdb2->exec($sql);
147     if (is_a($affected, 'PEAR_Error'))
148       return false;
149
150     return true;
151   }
152
153   // rolesExist - checks whether roles for team already exist.
154   static function rolesExist()
155   {
156     $mdb2 = getConnection();
157     global $user;
158
159     $sql = "select count(*) as count from tt_roles where team_id = $user->team_id";
160     $res = $mdb2->query($sql);
161     if (!is_a($res, 'PEAR_Error')) {
162       $val = $res->fetchRow();
163       if ($val['count'] > 0)
164         return true; // Roles for team exist.
165     }
166     return false;
167   }
168
169   // createPredefinedRoles - creates a set of predefined roles for the team to use.
170   static function createPredefinedRoles($team_id, $lang)
171   {
172     // We need localized role names and a new I18n object to obtain them.
173     import('I18n');
174     $i18n = new I18n();
175     $i18n->load($lang);
176
177     $mdb2 = getConnection();
178
179     $rights_client = 'view_own_data,manage_own_settings';
180     $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
181     $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
182     $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
183     $rights_manager = $rights_comanager.',manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
184
185     // Active roles.
186     $name = $mdb2->quote($i18n->getKey('role.user.label'));
187     $description = $mdb2->quote($i18n->getKey('role.user.description'));
188     $rights = $mdb2->quote($rights_user);
189     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 4, $rights, 1)";
190     $affected = $mdb2->exec($sql);
191     if (is_a($affected, 'PEAR_Error'))
192       return false;
193
194     $name = $mdb2->quote($i18n->getKey('role.client.label'));
195     $description = $mdb2->quote($i18n->getKey('role.client.description'));
196     $rights = $mdb2->quote($rights_client);
197     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 16, $rights, 1)";
198     $affected = $mdb2->exec($sql);
199     if (is_a($affected, 'PEAR_Error'))
200       return false;
201
202     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
203     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
204     $rights = $mdb2->quote($rights_comanager);
205     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 68, $rights, 1)";
206     $affected = $mdb2->exec($sql);
207     if (is_a($affected, 'PEAR_Error'))
208       return false;
209
210     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
211     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
212     $rights = $mdb2->quote($rights_manager);
213     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 324, $rights, 1)";
214     $affected = $mdb2->exec($sql);
215     if (is_a($affected, 'PEAR_Error'))
216       return false;
217
218     // Inactive roles.
219     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
220     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
221     $rights = $mdb2->quote($rights_supervisor);
222     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($team_id, $name, $description, 12, $rights, 0)";
223     $affected = $mdb2->exec($sql);
224     if (is_a($affected, 'PEAR_Error'))
225       return false;
226
227     return true;
228   }
229
230   // createDefaultRoles - creates a set of predefined roles for the team to use.
231   static function createDefaultRoles()
232   {
233     $mdb2 = getConnection();
234     global $i18n;
235     global $user;
236
237     $rights_client = 'view_own_data,manage_own_settings';
238     $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
239     $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
240     $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
241     $rights_manager = $rights_comanager.',manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
242
243     // Active roles.
244     $name = $mdb2->quote($i18n->getKey('role.user.label'));
245     $description = $mdb2->quote($i18n->getKey('role.user.description'));
246     $rights = $mdb2->quote($rights_user);
247     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
248     $affected = $mdb2->exec($sql);
249     if (is_a($affected, 'PEAR_Error'))
250       return false;
251
252     $name = $mdb2->quote($i18n->getKey('role.client.label'));
253     $description = $mdb2->quote($i18n->getKey('role.client.description'));
254     $rights = $mdb2->quote($rights_client);
255     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
256     $affected = $mdb2->exec($sql);
257     if (is_a($affected, 'PEAR_Error'))
258       return false;
259
260     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
261     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
262     $rights = $mdb2->quote($rights_comanager);
263     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
264     $affected = $mdb2->exec($sql);
265     if (is_a($affected, 'PEAR_Error'))
266       return false;
267
268     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
269     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
270     $rights = $mdb2->quote($rights_manager);
271     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
272     $affected = $mdb2->exec($sql);
273     if (is_a($affected, 'PEAR_Error'))
274       return false;
275
276     // Inactive roles.
277     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
278     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
279     $rights = $mdb2->quote($rights_supervisor);
280     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
281     $affected = $mdb2->exec($sql);
282     if (is_a($affected, 'PEAR_Error'))
283       return false;
284
285     return true;
286   }
287 }