Implemented locking feature as an optionally enabled plugin.
[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 $tracking_mode = 0;   // Tracking mode.
45   var $record_type = 0;     // Record type (duration vs start and finish, or both).
46   var $currency = null;     // Currency.
47   var $plugins = null;      // Comma-separated list of enabled plugins.
48   var $team = null;         // Team name.
49   var $custom_logo = 0;     // Whether to use a custom logo for team.
50   var $address = null;      // Address for invoices.
51   var $lock_interval = 0;   // Lock interval in days for time records.
52   var $rights = 0;          // A mask of user rights.
53
54   // Constructor.
55   function ttUser($login, $id = null) {
56     if (!$login && !$id) {
57       // nothing to initialize
58       return;
59     }
60
61     $mdb2 = getConnection();
62
63     $sql = "SELECT u.id, u.login, u.name, u.team_id, u.role, u.client_id, u.email, t.name as team_name, 
64       t.address, t.currency, t.locktime, t.lang, t.decimal_mark, t.date_format, t.time_format, t.week_start, t.tracking_mode, t.record_type, t.plugins, t.custom_logo
65       FROM tt_users u LEFT JOIN tt_teams t ON (u.team_id = t.id) WHERE ";
66     if ($id)
67       $sql .= "u.id = $id";
68     else
69       $sql .= "u.login = ".$mdb2->quote($login);
70     $sql .= " AND u.status = 1";
71
72     $res = $mdb2->query($sql);
73     if (is_a($res, 'PEAR_Error')) {
74       return;
75     }
76
77     $val = $res->fetchRow();
78     if ($val['id'] > 0) {
79       $this->login = $val['login'];
80       $this->name = $val['name'];
81       $this->id = $val['id'];
82       $this->team_id = $val['team_id'];
83       $this->role = $val['role'];
84       $this->client_id = $val['client_id'];
85       $this->email = $val['email'];
86       $this->lang = $val['lang'];
87       $this->decimal_mark = $val['decimal_mark'];
88       $this->date_format = $val['date_format'];
89       $this->time_format = $val['time_format'];
90       $this->week_start = $val['week_start'];
91       $this->tracking_mode = $val['tracking_mode'];
92       $this->record_type = $val['record_type'];
93       $this->team = $val['team_name'];
94       $this->address = $val['address'];
95       $this->currency = $val['currency'];
96       $this->plugins = $val['plugins'];
97       $this->custom_logo = $val['custom_logo'];
98       $this->lock_interval = $val['locktime'];
99
100       // Set "on behalf" id and name.
101       if (isset($_SESSION['behalf_id'])) {
102           $this->behalf_id = $_SESSION['behalf_id'];
103           $this->behalf_name = $_SESSION['behalf_name'];
104       }
105
106       // Set user rights.
107       if ($this->role == ROLE_USER) {
108         $this->rights = right_data_entry|right_view_charts|right_view_reports;
109       } elseif ($this->role == ROLE_CLIENT) {
110         $this->rights = right_view_reports|right_view_invoices; // TODO: how about right_view_charts, too?
111       } elseif ($this->role == ROLE_COMANAGER) {
112         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team;
113       } elseif ($this->role == ROLE_MANAGER) {
114         $this->rights = right_data_entry|right_view_charts|right_view_reports|right_view_invoices|right_manage_team|right_assign_roles|right_export_team;
115       } elseif ($this->role == ROLE_SITE_ADMIN) {
116         $this->rights = right_administer_site;
117       }
118     }
119   }
120
121   // The getActiveUser returns user id on behalf of whom current user is operating.
122   function getActiveUser() {
123     return ($this->behalf_id ? $this->behalf_id : $this->id);
124   }
125
126   // isAdmin - determines whether current user is admin (has right_administer_site).
127   function isAdmin() {
128     return (right_administer_site & $this->role);
129   }
130
131   // isManager - determines whether current user is team manager.
132   function isManager() {
133     return (ROLE_MANAGER == $this->role);
134   }
135
136   // isCoManager - determines whether current user is team comanager.
137   function isCoManager() {
138     return (ROLE_COMANAGER == $this->role);
139   }
140
141   // isClient - determines whether current user is a client.
142   function isClient() {
143     return (ROLE_CLIENT == $this->role);
144   }
145
146   // canManageTeam - determines whether current user is manager or co-manager.
147   function canManageTeam() {
148     return (right_manage_team & $this->role);
149   }
150
151   // isPluginEnabled checks whether a plugin is enabled for user.
152   function isPluginEnabled($plugin)
153   {
154     return in_array($plugin, explode(',', $this->plugins));
155   }
156
157   // getAssignedProjects - returns an array of assigned projects.
158   function getAssignedProjects()
159   {
160     $result = array();
161     $mdb2 = getConnection();
162
163     // Do a query with inner join to get assigned projects.
164     $sql = "select p.id, p.name, p.description, p.tasks, upb.rate from tt_projects p
165       inner join tt_user_project_binds upb on (upb.user_id = ".$this->getActiveUser()." and upb.project_id = p.id and upb.status = 1)
166       where p.team_id = $this->team_id and p.status = 1 order by p.name";
167     $res = $mdb2->query($sql);
168     if (!is_a($res, 'PEAR_Error')) {
169       while ($val = $res->fetchRow()) {
170         $result[] = $val;
171       }
172     }
173     return $result;
174   }
175
176   // isDateLocked checks whether a specifc date is locked for modifications.
177   function isDateLocked($date)
178   {
179     if ($this->isPluginEnabled('lk')) {
180       // Determine lock date. Entries earlier than lock date cannot be created or modified.
181       $lockdate = 0;
182       if ($this->lock_interval > 0) {
183         $lockdate = new DateAndTime();
184         $lockdate->decDay($this->lock_interval);
185       }
186       if($lockdate && $date->before($lockdate))
187         return true;
188     }
189     return false;
190   }
191 }