9abef9df89803823dc563a5e5e2181126c748369
[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   // insert - inserts an entry into tt_roles table.
32   static function insert($fields)
33   {
34     $mdb2 = getConnection();
35
36     $team_id = (int) $fields['team_id'];
37     $name = $fields['name'];
38     $rank = (int) $fields['rank'];
39     $rights = $fields['rights'];
40     $status = $fields['status'];
41
42     $sql = "insert into tt_roles (team_id, name, rank, rights, status)
43       values ($team_id, ".$mdb2->quote($name).", $rank, ".$mdb2->quote($rights).", ".$mdb2->quote($status).")";
44     $affected = $mdb2->exec($sql);
45     if (is_a($affected, 'PEAR_Error'))
46       return false;
47
48     return true;
49   }
50
51   // rolesExist - checks whether roles for team already exist.
52   static function rolesExist()
53   {
54     $mdb2 = getConnection();
55     global $user;
56
57     $sql = "select count(*) as count from tt_roles where team_id = $user->team_id";
58     $res = $mdb2->query($sql);
59     if (!is_a($res, 'PEAR_Error')) {
60       $val = $res->fetchRow();
61       if ($val['count'] > 0)
62         return true; // Roles for team exist.
63     }
64     return false;
65   }
66
67   // createDefaultRoles - creates a set of predefined roles for the team to use.
68   static function createDefaultRoles()
69   {
70     $mdb2 = getConnection();
71     global $i18n;
72     global $user;
73
74     // Active roles.
75     $name = $mdb2->quote($i18n->getKey('role.user.label'));
76     $description = $mdb2->quote($i18n->getKey('role.user.description'));
77     $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');
78     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 4, $rights, 1)";
79     $affected = $mdb2->exec($sql);
80     if (is_a($affected, 'PEAR_Error'))
81       return false;
82
83     $name = $mdb2->quote($i18n->getKey('role.client.label'));
84     $description = $mdb2->quote($i18n->getKey('role.client.description'));
85     $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');  // TODO: adjust rights.
86     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 16, $rights, 1)";
87     $affected = $mdb2->exec($sql);
88     if (is_a($affected, 'PEAR_Error'))
89       return false;
90
91     $name = $mdb2->quote($i18n->getKey('role.comanager.label'));
92     $description = $mdb2->quote($i18n->getKey('role.comanager.description'));
93     $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');  // TODO: adjust rights.
94     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 68, $rights, 1)";
95     $affected = $mdb2->exec($sql);
96     if (is_a($affected, 'PEAR_Error'))
97       return false;
98
99     $name = $mdb2->quote($i18n->getKey('role.manager.label'));
100     $description = $mdb2->quote($i18n->getKey('role.manager.description'));
101     $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users');  // TODO: adjust rights.
102     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 324, $rights, 1)";
103     $affected = $mdb2->exec($sql);
104     if (is_a($affected, 'PEAR_Error'))
105       return false;
106
107     // Inactive roles.
108     $name = $mdb2->quote($i18n->getKey('role.supervisor.label'));
109     $description = $mdb2->quote($i18n->getKey('role.supervisor.description'));
110     $rights = $mdb2->quote('data_entry,view_own_data,manage_own_settings,view_users'); // TODO: adjust rights.
111     $sql = "insert into tt_roles (team_id, name, description, rank, rights, status) values($user->team_id, $name, $description, 12, $rights, 0)";
112     $affected = $mdb2->exec($sql);
113     if (is_a($affected, 'PEAR_Error'))
114       return false;
115
116     return true;
117   }
118 }