f468f5522d3b94b26786eb284c79c37d61ec1a00
[timetracker.git] / WEB-INF / lib / ttUser.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 class ttUser {
30   var $login = null;            // User login.
31   var $name = null;             // User name.
32   var $id = null;               // User id.
33   var $team_id = null;          // Team id.
34   var $role = null;             // User role (user, client, comanager, manager, admin). TODO: remove when new roles are done.
35   var $role_id = null;          // Role id.
36   var $rank = null;             // User role rank.
37   var $client_id = null;        // Client id for client user role.
38   var $behalf_id = null;        // User id, on behalf of whom we are working.
39   var $behalf_name = null;      // User name, on behalf of whom we are working.
40   var $email = null;            // User email.
41   var $lang = null;             // Language.
42   var $decimal_mark = null;     // Decimal separator.
43   var $date_format = null;      // Date format.
44   var $time_format = null;      // Time format.
45   var $week_start = 0;          // Week start day.
46   var $show_holidays = 0;       // Whether to show holidays in calendar.
47   var $tracking_mode = 0;       // Tracking mode.
48   var $project_required = 0;    // Whether project selection is required on time entires.
49   var $task_required = 0;       // Whether task selection is required on time entires.
50   var $record_type = 0;         // Record type (duration vs start and finish, or both).
51   var $punch_mode = 0;          // Whether punch mode is enabled for user.
52   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
53   var $future_entries = 0;      // Whether to allow creating future entries.
54   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
55   var $bcc_email = null;        // Bcc email.
56   var $currency = null;         // Currency.
57   var $plugins = null;          // Comma-separated list of enabled plugins.
58   var $config = null;           // Comma-separated list of miscellaneous config options.
59   var $team = null;             // Team name.
60   var $custom_logo = 0;         // Whether to use a custom logo for team.
61   var $lock_spec = null;        // Cron specification for record locking.
62   var $workday_minutes = 480;   // Number of work minutes in a regular day.
63   var $rights = array();        // An array of user rights such as 'track_own_time', etc.
64   var $is_client = false;       // Whether user is a client as determined by missing 'track_own_time' right.
65
66   // Constructor.
67   function __construct($login, $id = null) {
68     if (!$login && !$id) {
69       // nothing to initialize
70       return;
71     }
72
73     $mdb2 = getConnection();
74
75     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.role_id, r.rank, r.rights, u.client_id, u.email, t.name as team_name,
76       t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
77       t.tracking_mode, t.project_required, t.task_required, t.record_type,
78       t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
79       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
80     if ($id)
81       $sql .= "u.id = $id";
82     else
83       $sql .= "u.login = ".$mdb2->quote($login);
84     $sql .= " AND u.status = 1";
85
86     $res = $mdb2->query($sql);
87     if (is_a($res, 'PEAR_Error')) {
88       return;
89     }
90
91     $val = $res->fetchRow();
92     if ($val['id'] > 0) {
93       $this->login = $val['login'];
94       $this->name = $val['name'];
95       $this->id = $val['id'];
96       $this->team_id = $val['team_id'];
97       $this->role = $val['role'];
98       $this->role_id = $val['role_id'];
99       $this->rights = explode(',', $val['rights']);
100       $this->is_client = !in_array('track_own_time', $this->rights);
101       $this->rank = $val['rank'];
102       // Downgrade rank to legacy role, if it is still in use.
103       if ($this->role > 0 && $this->rank > $this->role)
104         $this->rank = $this->role; // TODO: remove after roles revamp.
105       // Upgrade rank from legacy role, for user who does not yet have a role_id.
106       if (!$this->rank && !$this->role_id && $this->role > 0)
107         $this->rank = $this->role; // TODO: remove after roles revamp.
108       $this->client_id = $val['client_id'];
109       $this->email = $val['email'];
110       $this->lang = $val['lang'];
111       $this->decimal_mark = $val['decimal_mark'];
112       $this->date_format = $val['date_format'];
113       $this->time_format = $val['time_format'];
114       $this->week_start = $val['week_start'];
115       $this->tracking_mode = $val['tracking_mode'];
116       $this->project_required = $val['project_required'];
117       $this->task_required = $val['task_required'];
118       $this->record_type = $val['record_type'];
119       $this->bcc_email = $val['bcc_email'];
120       $this->team = $val['team_name'];
121       $this->currency = $val['currency'];
122       $this->plugins = $val['plugins'];
123       $this->lock_spec = $val['lock_spec'];
124       $this->workday_minutes = $val['workday_minutes'];
125       $this->custom_logo = $val['custom_logo'];
126
127       $this->config = $val['config'];
128       $config_array = explode(',', $this->config);
129
130       // Set user config options.
131       $this->show_holidays = in_array('show_holidays', $config_array);
132       $this->punch_mode = in_array('punch_mode', $config_array);
133       $this->allow_overlap = in_array('allow_overlap', $config_array);
134       $this->future_entries = in_array('future_entries', $config_array);
135       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
136
137       // Set "on behalf" id and name.
138       if (isset($_SESSION['behalf_id'])) {
139           $this->behalf_id = $_SESSION['behalf_id'];
140           $this->behalf_name = $_SESSION['behalf_name'];
141       }
142     }
143   }
144
145   // The getActiveUser returns user id on behalf of whom the current user is operating.
146   function getActiveUser() {
147     return ($this->behalf_id ? $this->behalf_id : $this->id);
148   }
149
150   // can - determines whether user has a right to do something.
151   function can($do_something) {
152     return in_array($do_something, $this->rights);
153   }
154
155   // isAdmin - determines whether current user is admin (has right_administer_site).
156   function isAdmin() {
157     return $this->can('administer_site');
158   }
159
160   // isManager - determines whether current user is team manager.
161   function isManager() {
162     return (ROLE_MANAGER == $this->role);
163   }
164
165   // isCoManager - determines whether current user is team comanager.
166   function isCoManager() {
167     return (ROLE_COMANAGER == $this->role);
168   }
169
170   // isClient - determines whether current user is a client.
171   function isClient() {
172     return $this->is_client;
173   }
174
175   // canManageTeam - determines whether current user is manager or co-manager.
176   function canManageTeam() {
177     return (right_manage_team & $this->role);
178   }
179
180   // isPluginEnabled checks whether a plugin is enabled for user.
181   function isPluginEnabled($plugin)
182   {
183     return in_array($plugin, explode(',', $this->plugins));
184   }
185
186   // getAssignedProjects - returns an array of assigned projects.
187   function getAssignedProjects()
188   {
189     $result = array();
190     $mdb2 = getConnection();
191
192     // Do a query with inner join to get assigned projects.
193     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
194       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
195       where p.team_id = $this->team_id and p.status = 1 order by p.name";
196     $res = $mdb2->query($sql);
197     if (!is_a($res, 'PEAR_Error')) {
198       while ($val = $res->fetchRow()) {
199         $result[] = $val;
200       }
201     }
202     return $result;
203   }
204
205   // isDateLocked checks whether a specifc date is locked for modifications.
206   function isDateLocked($date)
207   {
208     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
209
210       // Override.
211       if ($this->can('override_date_lock')) return false;
212
213       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
214       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
215
216       // Calculate the last occurrence of a lock.
217       $last = tdCron::getLastOccurrence($this->lock_spec, time());
218       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
219       if ($date->before($lockdate)) {
220         return true;
221       }
222     }
223     return false;
224   }
225
226   // migrateLegacyRole makes changes to user database record and assigns a user to
227   // one of pre-defined roles, which are created if necessary.
228   // No changes to $this instance are done.
229   function migrateLegacyRole() {
230     // Do nothing if we already have a role_id.
231     if ($this->role_id) return false;
232
233     // Create default roles if necessary.
234     import ('ttRoleHelper');
235     if (!ttRoleHelper::rolesExist()) ttRoleHelper::createDefaultRoles(); // TODO: refactor or remove after roles revamp.
236
237     // Obtain new role id based on legacy role.
238     $role_id = ttRoleHelper::getRoleByRank($this->role);
239     if (!$role_id) return false; // Role not found, nothing to do.
240
241     $mdb2 = getConnection();
242     $sql = "update tt_users set role_id = $role_id where id = $this->id and team_id = $this->team_id";
243     $affected = $mdb2->exec($sql);
244     if (is_a($affected, 'PEAR_Error'))
245       return false;
246
247     return true;
248   }
249 }