697d541fc78b44efffaeaeb92ac1013dcdfdacd4
[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).
35   var $client_id = null;        // Client id for client user role.
36   var $behalf_id = null;        // User id, on behalf of whom we are working.
37   var $behalf_name = null;      // User name, on behalf of whom we are working.
38   var $email = null;            // User email.
39   var $lang = null;             // Language.
40   var $decimal_mark = null;     // Decimal separator.
41   var $date_format = null;      // Date format.
42   var $time_format = null;      // Time format.
43   var $week_start = 0;          // Week start day.
44   var $show_holidays = 0;       // Whether to show holidays in calendar.
45   var $tracking_mode = 0;       // Tracking mode.
46   var $project_required = 0;    // Whether project selection is required on time entires.
47   var $task_required = 0;       // Whether task selection is required on time entires.
48   var $record_type = 0;         // Record type (duration vs start and finish, or both).
49   var $punch_in_mode = 0;       // Whether punch in mode is enabled for user.
50   var $allow_overlap = 0;       // Whether to allow overlapping time entries.
51   var $future_entries = 0;      // Whether to allow creating future entries.
52   var $uncompleted_indicators = 0; // Uncompleted time entry indicators (show nowhere or on users page).
53   var $bcc_email = null;        // Bcc email.
54   var $currency = null;         // Currency.
55   var $plugins = null;          // Comma-separated list of enabled plugins.
56   var $config = null;           // Comma-separated list of miscellaneous config options.
57   var $team = null;             // Team name.
58   var $custom_logo = 0;         // Whether to use a custom logo for team.
59   var $lock_spec = null;        // Cron specification for record locking.
60   var $workday_minutes = 480;   // Number of work minutes in a regular day.
61   var $rights = 0;              // A mask of user rights.
62
63   // Constructor.
64   function __construct($login, $id = null) {
65     if (!$login && !$id) {
66       // nothing to initialize
67       return;
68     }
69
70     $mdb2 = getConnection();
71
72     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.client_id, u.email, t.name as team_name, 
73       t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
74       t.tracking_mode, t.project_required, t.task_required, t.record_type,
75       t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
76       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) WHERE ";
77     if ($id)
78       $sql .= "u.id = $id";
79     else
80       $sql .= "u.login = ".$mdb2->quote($login);
81     $sql .= " AND u.status = 1";
82
83     $res = $mdb2->query($sql);
84     if (is_a($res, 'PEAR_Error')) {
85       return;
86     }
87
88     $val = $res->fetchRow();
89     if ($val['id'] > 0) {
90       $this->login = $val['login'];
91       $this->name = $val['name'];
92       $this->id = $val['id'];
93       $this->team_id = $val['team_id'];
94       $this->role = $val['role'];
95       $this->client_id = $val['client_id'];
96       $this->email = $val['email'];
97       $this->lang = $val['lang'];
98       $this->decimal_mark = $val['decimal_mark'];
99       $this->date_format = $val['date_format'];
100       $this->time_format = $val['time_format'];
101       $this->week_start = $val['week_start'];
102       $this->tracking_mode = $val['tracking_mode'];
103       $this->project_required = $val['project_required'];
104       $this->task_required = $val['task_required'];
105       $this->record_type = $val['record_type'];
106       $this->bcc_email = $val['bcc_email'];
107       $this->team = $val['team_name'];
108       $this->currency = $val['currency'];
109       $this->plugins = $val['plugins'];
110       $this->lock_spec = $val['lock_spec'];
111       $this->workday_minutes = $val['workday_minutes'];
112       $this->custom_logo = $val['custom_logo'];
113
114       $this->config = $val['config'];
115       $config_array = explode(',', $this->config);
116
117       // Set user config options.
118       $this->show_holidays = in_array('show_holidays', $config_array);
119       $this->punch_in_mode = in_array('punch_in_mode', $config_array);
120       $this->allow_overlap = in_array('allow_overlap', $config_array);
121       $this->future_entries = in_array('future_entries', $config_array);
122       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
123
124       // Set "on behalf" id and name.
125       if (isset($_SESSION['behalf_id'])) {
126           $this->behalf_id = $_SESSION['behalf_id'];
127           $this->behalf_name = $_SESSION['behalf_name'];
128       }
129
130       // Set user rights.
131       if ($this->role == ROLE_USER) {
132         $this->rights = right_data_entry|right_view_charts|right_view_reports;
133       } elseif ($this->role == ROLE_CLIENT) {
134         $this->rights = right_view_reports|right_view_invoices; // TODO: how about right_view_charts, too?
135       } elseif ($this->role == ROLE_COMANAGER) {
136         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
137       } elseif ($this->role == ROLE_MANAGER) {
138         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
139       } elseif ($this->role == ROLE_SITE_ADMIN) {
140         $this->rights = right_administer_site;
141       }
142
143       // Adjust punch_in_mode for managers as they are allowed to overwrite start and end times.
144       if ($this->canManageTeam()) $this->punch_in_mode = 0;
145     }
146   }
147
148   // The getActiveUser returns user id on behalf of whom current user is operating.
149   function getActiveUser() {
150     return ($this->behalf_id ? $this->behalf_id : $this->id);
151   }
152
153   // isAdmin - determines whether current user is admin (has right_administer_site).
154   function isAdmin() {
155     return (right_administer_site & $this->role);
156   }
157
158   // isManager - determines whether current user is team manager.
159   function isManager() {
160     return (ROLE_MANAGER == $this->role);
161   }
162
163   // isCoManager - determines whether current user is team comanager.
164   function isCoManager() {
165     return (ROLE_COMANAGER == $this->role);
166   }
167
168   // isClient - determines whether current user is a client.
169   function isClient() {
170     return (ROLE_CLIENT == $this->role);
171   }
172
173   // canManageTeam - determines whether current user is manager or co-manager.
174   function canManageTeam() {
175     return (right_manage_team & $this->role);
176   }
177
178   // isPluginEnabled checks whether a plugin is enabled for user.
179   function isPluginEnabled($plugin)
180   {
181     return in_array($plugin, explode(',', $this->plugins));
182   }
183
184   // getAssignedProjects - returns an array of assigned projects.
185   function getAssignedProjects()
186   {
187     $result = array();
188     $mdb2 = getConnection();
189
190     // Do a query with inner join to get assigned projects.
191     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
192       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
193       where p.team_id = $this->team_id and p.status = 1 order by p.name";
194     $res = $mdb2->query($sql);
195     if (!is_a($res, 'PEAR_Error')) {
196       while ($val = $res->fetchRow()) {
197         $result[] = $val;
198       }
199     }
200     return $result;
201   }
202
203   // isDateLocked checks whether a specifc date is locked for modifications.
204   function isDateLocked($date)
205   {
206     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
207       // Override for managers.
208       if ($this->canManageTeam()) return false;
209
210       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
211       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
212
213       // Calculate the last occurrence of a lock.
214       $last = tdCron::getLastOccurrence($this->lock_spec, time());
215       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
216       if ($date->before($lockdate)) {
217         return true;
218       }
219     }
220     return false;
221   }
222 }