Added capability to add custom roles - work in progress.
[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 getRoleByRank looks up a role by its rank.
72   static function getRoleByRank($rank) {
73     global $user;
74     $mdb2 = getConnection();
75
76     $rank = (int) $rank; // Cast to int just in case for better security.
77
78     $sql = "select id from tt_roles where team_id = $user->team_id and rank = $rank and (status = 1 or status = 0)";
79     $res = $mdb2->query($sql);
80
81     if (!is_a($res, 'PEAR_Error')) {
82       $val = $res->fetchRow();
83       if ($val['id'])
84         return $val;
85     }
86     return false;
87   }
88
89   // update function updates a role in the database.
90   static function update($fields) {
91     global $user;
92     $mdb2 = getConnection();
93
94     $id = (int)$fields['id'];
95     if (isset($fields['name'])) $name_part = 'name = '.$mdb2->quote($fields['name']);
96     if (isset($fields['description'])) $descr_part = ', description = '.$mdb2->quote($fields['description']);
97     if (isset($fields['status'])) $status_part = ', status = '.(int)$fields['status'];
98     if (isset($fields['rights'])) $rights_part = ', rights = '.$mdb2->quote($fields['rights']);
99     $parts = trim($name_part.$descr_part.$status_part.$rights_part, ',');
100     $sql = "update tt_roles set $parts where id = $id and team_id = $user->team_id";
101     $affected = $mdb2->exec($sql);
102     return (!is_a($affected, 'PEAR_Error'));
103   }
104
105   // delete - marks the role as deleted.
106   static function delete($role_id) {
107     global $user;
108
109     $mdb2 = getConnection();
110
111     // Mark the task as deleted.
112     $sql = "update tt_roles set status = NULL where id = $role_id and team_id = $user->team_id";
113     $affected = $mdb2->exec($sql);
114     return (!is_a($affected, 'PEAR_Error'));
115   }
116
117   // insert - inserts an entry into tt_roles table.
118   static function insert($fields)
119   {
120     $mdb2 = getConnection();
121
122     $team_id = (int) $fields['team_id'];
123     $name = $fields['name'];
124     $rank = (int) $fields['rank'];
125     $description = $fields['description'];
126     $rights = $fields['rights'];
127     $status = $fields['status'];
128
129     $sql = "insert into tt_roles (team_id, name, rank, description, rights, status)
130       values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($description).", ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
131     $affected = $mdb2->exec($sql);
132     if (is_a($affected, 'PEAR_Error'))
133       return false;
134
135     return true;
136   }
137
138   // rolesExist - checks whether roles for team already exist.
139   static function rolesExist()
140   {
141     $mdb2 = getConnection();
142     global $user;
143
144     $sql = "select count(*) as count from tt_roles where team_id = $user->team_id";
145     $res = $mdb2->query($sql);
146     if (!is_a($res, 'PEAR_Error')) {
147       $val = $res->fetchRow();
148       if ($val['count'] > 0)
149         return true; // Roles for team exist.
150     }
151     return false;
152   }
153
154   // createDefaultRoles - creates a set of predefined roles for the team to use.
155   static function createDefaultRoles()
156   {
157     $mdb2 = getConnection();
158     global $i18n;
159     global $user;
160
161     $rights_client = 'view_own_data,manage_own_settings';
162     $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
163     $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
164     $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
165     $rights_manager = $rights_comanager.'manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
166
167     // Active roles.
168     $name = $mdb2->quote($i18n->getKey('role.user.label'));
169     $description = $mdb2->quote($i18n->getKey('role.user.description'));
170     $rights = $mdb2->quote($rights_user);
171     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
172     $affected = $mdb2->exec($sql);
173     if (is_a($affected, 'PEAR_Error'))
174       return false;
175
176     $name = $mdb2->quote($i18n->getKey('role.client.label'));
177     $description = $mdb2->quote($i18n->getKey('role.client.description'));
178     $rights = $mdb2->quote($rights_client);
179     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
180     $affected = $mdb2->exec($sql);
181     if (is_a($affected, 'PEAR_Error'))
182       return false;
183
184     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
185     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
186     $rights = $mdb2->quote($rights_comanager);
187     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
188     $affected = $mdb2->exec($sql);
189     if (is_a($affected, 'PEAR_Error'))
190       return false;
191
192     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
193     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
194     $rights = $mdb2->quote($rights_manager);
195     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
196     $affected = $mdb2->exec($sql);
197     if (is_a($affected, 'PEAR_Error'))
198       return false;
199
200     // Inactive roles.
201     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
202     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
203     $rights = $mdb2->quote($rights_supervisor);
204     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
205     $affected = $mdb2->exec($sql);
206     if (is_a($affected, 'PEAR_Error'))
207       return false;
208
209     return true;
210   }
211 }