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 // delete - marks the role as deleted.
54 static function delete($role_id) {
57 $mdb2 = getConnection();
59 // Mark the task as deleted.
60 $sql = "update tt_roles set status = NULL where id = $role_id and team_id = $user->team_id";
61 $affected = $mdb2->exec($sql);
62 return (!is_a($affected, 'PEAR_Error'));
65 // insert - inserts an entry into tt_roles table.
66 static function insert($fields)
68 $mdb2 = getConnection();
70 $team_id = (int) $fields['team_id'];
71 $name = $fields['name'];
72 $rank = (int) $fields['rank'];
73 $rights = $fields['rights'];
74 $status = $fields['status'];
76 $sql = "insert into tt_roles (team_id, name, rank, rights, status)
77 values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
78 $affected = $mdb2->exec($sql);
79 if (is_a($affected, 'PEAR_Error'))
85 // rolesExist - checks whether roles for team already exist.
86 static function rolesExist()
88 $mdb2 = getConnection();
91 $sql = "select count(*) as count from tt_roles where team_id = $user->team_id";
92 $res = $mdb2->query($sql);
93 if (!is_a($res, 'PEAR_Error')) {
94 $val = $res->fetchRow();
95 if ($val['count'] > 0)
96 return true; // Roles for team exist.
101 // createDefaultRoles - creates a set of predefined roles for the team to use.
102 static function createDefaultRoles()
104 $mdb2 = getConnection();
108 $rights_client = 'view_own_data,manage_own_settings';
109 $rights_user = 'data_entry,view_own_data,manage_own_settings,view_users';
110 $rights_supervisor = $rights_user.',on_behalf_data_entry,view_data,override_punch_mode,swap_roles,approve_timesheets';
111 $rights_comanager = $rights_supervisor.',manage_users,manage_projects,manage_tasks,manage_custom_fields,manage_clients,manage_invoices';
112 $rights_manager = $rights_comanager.'manage_features,manage_basic_settings,manage_advanced_settings,manage_roles,export_data,manage_subgroups';
115 $name = $mdb2->quote($i18n->getKey('role.user.label'));
116 $description = $mdb2->quote($i18n->getKey('role.user.description'));
117 $rights = $mdb2->quote($rights_user);
118 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
119 $affected = $mdb2->exec($sql);
120 if (is_a($affected, 'PEAR_Error'))
123 $name = $mdb2->quote($i18n->getKey('role.client.label'));
124 $description = $mdb2->quote($i18n->getKey('role.client.description'));
125 $rights = $mdb2->quote($rights_client);
126 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
127 $affected = $mdb2->exec($sql);
128 if (is_a($affected, 'PEAR_Error'))
131 $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
132 $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
133 $rights = $mdb2->quote($rights_comanager);
134 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
135 $affected = $mdb2->exec($sql);
136 if (is_a($affected, 'PEAR_Error'))
139 $name = $mdb2->quote($i18n->getKey('role.manager.label'));
140 $description = $mdb2->quote($i18n->getKey('role.manager.description'));
141 $rights = $mdb2->quote($rights_manager);
142 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
143 $affected = $mdb2->exec($sql);
144 if (is_a($affected, 'PEAR_Error'))
148 $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
149 $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
150 $rights = $mdb2->quote($rights_supervisor);
151 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
152 $affected = $mdb2->exec($sql);
153 if (is_a($affected, 'PEAR_Error'))