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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 // The ttRoleHelper is a class to help with custom group roles.
32 // get - gets details of a role identified by its id.
33 static function get($id)
37 $mdb2 = getConnection();
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);
43 if (!is_a($res, 'PEAR_Error')) {
44 $val = $res->fetchRow();
45 if ($val['id'] != '') {
53 // The getRoleByName looks up a role by name.
54 static function getRoleByName($role_name) {
56 $mdb2 = getConnection();
59 $sql = "select id from tt_roless where team_id = $user->team_id and name = ".
60 $mdb2->quote($role_name)." and (status = 1 or status = 0)";
61 $res = $mdb2->query($sql);
63 if (!is_a($res, 'PEAR_Error')) {
64 $val = $res->fetchRow();
71 // update function updates a role in the database.
72 static function update($fields)
75 $mdb2 = getConnection();
77 $id = (int)$fields['id'];
78 $name = $fields['name'];
79 $description = $fields['description'];
80 $status = (int)$fields['status'];
81 // TODO: add rights later when we have them.
83 $sql = "update tt_roles set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).
84 ", status = $status where id = $id and team_id = $user->team_id";
85 $affected = $mdb2->exec($sql);
86 return (!is_a($affected, 'PEAR_Error'));
89 // delete - marks the role as deleted.
90 static function delete($role_id) {
93 $mdb2 = getConnection();
95 // Mark the task as deleted.
96 $sql = "update tt_roles set status = NULL where id = $role_id and team_id = $user->team_id";
97 $affected = $mdb2->exec($sql);
98 return (!is_a($affected, 'PEAR_Error'));
101 // insert - inserts an entry into tt_roles table.
102 static function insert($fields)
104 $mdb2 = getConnection();
106 $team_id = (int) $fields['team_id'];
107 $name = $fields['name'];
108 $rank = (int) $fields['rank'];
109 $rights = $fields['rights'];
110 $status = $fields['status'];
112 $sql = "insert into tt_roles (team_id, name, rank, rights, status)
113 values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
114 $affected = $mdb2->exec($sql);
115 if (is_a($affected, 'PEAR_Error'))
121 // rolesExist - checks whether roles for team already exist.
122 static function rolesExist()
124 $mdb2 = getConnection();
127 $sql = "select count(*) as count from tt_roles where team_id = $user->team_id";
128 $res = $mdb2->query($sql);
129 if (!is_a($res, 'PEAR_Error')) {
130 $val = $res->fetchRow();
131 if ($val['count'] > 0)
132 return true; // Roles for team exist.
137 // createDefaultRoles - creates a set of predefined roles for the team to use.
138 static function createDefaultRoles()
140 $mdb2 = getConnection();
144 $rights_client = 'view_own_data,manage_own_settings';
145 $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
146 $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
147 $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
148 $rights_manager = $rights_comanager.'manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
151 $name = $mdb2->quote($i18n->getKey('role.user.label'));
152 $description = $mdb2->quote($i18n->getKey('role.user.description'));
153 $rights = $mdb2->quote($rights_user);
154 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
155 $affected = $mdb2->exec($sql);
156 if (is_a($affected, 'PEAR_Error'))
159 $name = $mdb2->quote($i18n->getKey('role.client.label'));
160 $description = $mdb2->quote($i18n->getKey('role.client.description'));
161 $rights = $mdb2->quote($rights_client);
162 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
163 $affected = $mdb2->exec($sql);
164 if (is_a($affected, 'PEAR_Error'))
167 $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
168 $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
169 $rights = $mdb2->quote($rights_comanager);
170 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
171 $affected = $mdb2->exec($sql);
172 if (is_a($affected, 'PEAR_Error'))
175 $name = $mdb2->quote($i18n->getKey('role.manager.label'));
176 $description = $mdb2->quote($i18n->getKey('role.manager.description'));
177 $rights = $mdb2->quote($rights_manager);
178 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
179 $affected = $mdb2->exec($sql);
180 if (is_a($affected, 'PEAR_Error'))
184 $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
185 $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
186 $rights = $mdb2->quote($rights_supervisor);
187 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
188 $affected = $mdb2->exec($sql);
189 if (is_a($affected, 'PEAR_Error'))