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