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();
109 $name = $mdb2->quote($i18n->getKey('role.user.label'));
110 $description = $mdb2->quote($i18n->getKey('role.user.description'));
111 $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');
112 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
113 $affected = $mdb2->exec($sql);
114 if (is_a($affected, 'PEAR_Error'))
117 $name = $mdb2->quote($i18n->getKey('role.client.label'));
118 $description = $mdb2->quote($i18n->getKey('role.client.description'));
119 $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users'); // TODO: adjust rights.
120 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
121 $affected = $mdb2->exec($sql);
122 if (is_a($affected, 'PEAR_Error'))
125 $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
126 $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
127 $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users'); // TODO: adjust rights.
128 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
129 $affected = $mdb2->exec($sql);
130 if (is_a($affected, 'PEAR_Error'))
133 $name = $mdb2->quote($i18n->getKey('role.manager.label'));
134 $description = $mdb2->quote($i18n->getKey('role.manager.description'));
135 $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users'); // TODO: adjust rights.
136 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
137 $affected = $mdb2->exec($sql);
138 if (is_a($affected, 'PEAR_Error'))
142 $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
143 $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
144 $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users'); // TODO: adjust rights.
145 $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
146 $affected = $mdb2->exec($sql);
147 if (is_a($affected, 'PEAR_Error'))