Fixed a typo in user right.
[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   // delete - marks the role as deleted.
54   static function delete($role_id) {
55     global $user;
56
57     $mdb2 = getConnection();
58
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'));
63   }
64
65   // insert - inserts an entry into tt_roles table.
66   static function insert($fields)
67   {
68     $mdb2 = getConnection();
69
70     $team_id = (int) $fields['team_id'];
71     $name = $fields['name'];
72     $rank = (int) $fields['rank'];
73     $rights = $fields['rights'];
74     $status = $fields['status'];
75
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'))
80       return false;
81
82     return true;
83   }
84
85   // rolesExist - checks whether roles for team already exist.
86   static function rolesExist()
87   {
88     $mdb2 = getConnection();
89     global $user;
90
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.
97     }
98     return false;
99   }
100
101   // createDefaultRoles - creates a set of predefined roles for the team to use.
102   static function createDefaultRoles()
103   {
104     $mdb2 = getConnection();
105     global $i18n;
106     global $user;
107
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';
113
114     // Active roles.
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'))
121       return false;
122
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'))
129       return false;
130
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'))
137       return false;
138
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'))
145       return false;
146
147     // Inactive roles.
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'))
154       return false;
155
156     return true;
157   }
158 }