Minor improvements in Italian translation.
[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   var $rights_array = array();  // An array of user rights, planned replacement of array mask.
63
64   // Constructor.
65   function __construct($login, $id = null) {
66     if (!$login && !$id) {
67       // nothing to initialize
68       return;
69     }
70
71     $mdb2 = getConnection();
72
73     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.client_id, u.email, t.name as team_name, 
74       t.currency, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start,
75       t.tracking_mode, t.project_required, t.task_required, t.record_type,
76       t.bcc_email, t.plugins, t.config, t.lock_spec, t.workday_minutes, t.custom_logo
77       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) WHERE ";
78     if ($id)
79       $sql .= "u.id = $id";
80     else
81       $sql .= "u.login = ".$mdb2->quote($login);
82     $sql .= " AND u.status = 1";
83
84     $res = $mdb2->query($sql);
85     if (is_a($res, 'PEAR_Error')) {
86       return;
87     }
88
89     $val = $res->fetchRow();
90     if ($val['id'] > 0) {
91       $this->login = $val['login'];
92       $this->name = $val['name'];
93       $this->id = $val['id'];
94       $this->team_id = $val['team_id'];
95       $this->role = $val['role'];
96       $this->client_id = $val['client_id'];
97       $this->email = $val['email'];
98       $this->lang = $val['lang'];
99       $this->decimal_mark = $val['decimal_mark'];
100       $this->date_format = $val['date_format'];
101       $this->time_format = $val['time_format'];
102       $this->week_start = $val['week_start'];
103       $this->tracking_mode = $val['tracking_mode'];
104       $this->project_required = $val['project_required'];
105       $this->task_required = $val['task_required'];
106       $this->record_type = $val['record_type'];
107       $this->bcc_email = $val['bcc_email'];
108       $this->team = $val['team_name'];
109       $this->currency = $val['currency'];
110       $this->plugins = $val['plugins'];
111       $this->lock_spec = $val['lock_spec'];
112       $this->workday_minutes = $val['workday_minutes'];
113       $this->custom_logo = $val['custom_logo'];
114
115       $this->config = $val['config'];
116       $config_array = explode(',', $this->config);
117
118       // Set user config options.
119       $this->show_holidays = in_array('show_holidays', $config_array);
120       $this->punch_in_mode = in_array('punch_in_mode', $config_array);
121       $this->allow_overlap = in_array('allow_overlap', $config_array);
122       $this->future_entries = in_array('future_entries', $config_array);
123       $this->uncompleted_indicators = in_array('uncompleted_indicators', $config_array);
124
125       // Set "on behalf" id and name.
126       if (isset($_SESSION['behalf_id'])) {
127           $this->behalf_id = $_SESSION['behalf_id'];
128           $this->behalf_name = $_SESSION['behalf_name'];
129       }
130
131       // Set user rights.
132       if ($this->role == ROLE_USER) {
133         $this->rights = right_data_entry|right_view_charts|right_view_reports;
134         // TODO: get customized rights from the database instead.
135         $this->rights_array[] = "data_entry";       // Right to enter time and expense records into Time Tracker.
136         $this->rights_array[] = "view_own_reports"; // Right to view own reports (for a specific user).
137         $this->rights_array[] = "view_own_charts";  // Right to view own charts (for a specific user).
138       } elseif ($this->role == ROLE_CLIENT) {
139         $this->rights = right_view_reports|right_view_invoices; // TODO: how about right_view_charts, too?
140         $this->rights_array[] = "view_client_reports";  // Right to view reports for a specific client.
141         $this->rights_array[] = "view_client_charts";   // Right to view charts for a specific client.
142         $this->rights_array[] = "view_client_invoices"; // Right to view invoices for a specific client.
143       } elseif ($this->role == ROLE_COMANAGER) {
144         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
145       } elseif ($this->role == ROLE_MANAGER) {
146         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
147       } elseif ($this->role == ROLE_SITE_ADMIN) {
148         $this->rights = right_administer_site;
149       }
150
151 /*
152 // TODO: redesign of user rights and roles is currently ongoing.
153 // As we run our of bits for sure at some point, rights should be strings instead,
154 // for example: "data_entry".
155 // Also, we need rights editor page and team-customized roles.
156 // Move this stuff from here to ttUser class.
157 //
158 // User access rights - bits that collectively define an access mask to the system (a role).
159 // We'll have some bits here (1,2, etc...) reserved for future use.
160 define('right_data_entry', 4);     // Right to enter work hours and expenses.
161 define('right_view_charts', 8);    // Right to view charts.
162 define('right_view_reports', 16);  // Right to view reports.
163 define('right_view_invoices', 32); // Right to view invoices.
164 define('right_manage_team', 64);   // Right to manage team. Note that this is not full access to team.
165 define('right_assign_roles', 128); // Right to assign user roles.
166 define('right_export_team', 256);  // Right to export team data to a file.
167 define('right_administer_site', 1024); // Admin account right to manage the application as a whole.
168
169 // User roles.
170 define('ROLE_USER', 4);          // Regular user.
171 define('ROLE_CLIENT', 16);       // Client (to view reports and invoices).
172 define('ROLE_COMANAGER', 68);    // Team co-manager. Can do many things but not as much as team manager.
173 define('ROLE_MANAGER', 324);     // Team manager. Can do everything for a team.
174 define('ROLE_SITE_ADMIN', 1024); // Site administrator.
175 */
176
177       // Adjust punch_in_mode for managers as they are allowed to overwrite start and end times.
178       if ($this->canManageTeam()) $this->punch_in_mode = 0;
179     }
180   }
181
182   // The getActiveUser returns user id on behalf of whom current user is operating.
183   function getActiveUser() {
184     return ($this->behalf_id ? $this->behalf_id : $this->id);
185   }
186
187   // isAdmin - determines whether current user is admin (has right_administer_site).
188   function isAdmin() {
189     return (right_administer_site & $this->role);
190   }
191
192   // isManager - determines whether current user is team manager.
193   function isManager() {
194     return (ROLE_MANAGER == $this->role);
195   }
196
197   // isCoManager - determines whether current user is team comanager.
198   function isCoManager() {
199     return (ROLE_COMANAGER == $this->role);
200   }
201
202   // isClient - determines whether current user is a client.
203   function isClient() {
204     return (ROLE_CLIENT == $this->role);
205   }
206
207   // canManageTeam - determines whether current user is manager or co-manager.
208   function canManageTeam() {
209     return (right_manage_team & $this->role);
210   }
211
212   // isPluginEnabled checks whether a plugin is enabled for user.
213   function isPluginEnabled($plugin)
214   {
215     return in_array($plugin, explode(',', $this->plugins));
216   }
217
218   // getAssignedProjects - returns an array of assigned projects.
219   function getAssignedProjects()
220   {
221     $result = array();
222     $mdb2 = getConnection();
223
224     // Do a query with inner join to get assigned projects.
225     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
226       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
227       where p.team_id = $this->team_id and p.status = 1 order by p.name";
228     $res = $mdb2->query($sql);
229     if (!is_a($res, 'PEAR_Error')) {
230       while ($val = $res->fetchRow()) {
231         $result[] = $val;
232       }
233     }
234     return $result;
235   }
236
237   // isDateLocked checks whether a specifc date is locked for modifications.
238   function isDateLocked($date)
239   {
240     if ($this->isPluginEnabled('lk') && $this->lock_spec) {
241       // Override for managers.
242       if ($this->canManageTeam()) return false;
243
244       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
245       require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
246
247       // Calculate the last occurrence of a lock.
248       $last = tdCron::getLastOccurrence($this->lock_spec, time());
249       $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
250       if ($date->before($lockdate)) {
251         return true;
252       }
253     }
254     return false;
255   }
256 }