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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttConfigHelper');
30 import('ttGroupHelper');
31 import('ttBehalfUser');
34 import('form.ActionForm');
37 var $login = null; // User login.
38 var $name = null; // User name.
39 var $id = null; // User id.
40 var $org_id = null; // Organization id.
41 var $group_id = null; // Group id.
42 var $role_id = null; // Role id.
43 var $role_name = null; // Role name.
44 var $rank = null; // User role rank.
45 var $client_id = null; // Client id for client user role.
46 var $quota_percent = 100.0; // Time quota percent for quotas plugin.
47 var $behalf_id = null; // User id, on behalf of whom we are working.
48 var $behalf_group_id = null; // Group id, on behalf of which we are working.
49 var $behalf_name = null; // User name, on behalf of whom we are working.
50 var $group_name = null; // Group name.
51 var $behalf_group_name = null;// Group name, on behalf of which we are working.
52 var $email = null; // User email.
53 var $lang = null; // Language.
54 var $decimal_mark = null; // Decimal separator.
55 var $date_format = null; // Date format.
56 var $time_format = null; // Time format.
57 var $week_start = 0; // Week start day.
58 var $show_holidays = 0; // Whether to show holidays in calendar.
59 var $tracking_mode = 0; // Tracking mode.
60 var $project_required = 0; // Whether project selection is required on time entires.
61 var $task_required = 0; // Whether task selection is required on time entires.
62 var $record_type = 0; // Record type (duration vs start and finish, or both).
63 var $punch_mode = 0; // Whether punch mode is enabled for user.
64 var $allow_overlap = 0; // Whether to allow overlapping time entries.
65 var $future_entries = 0; // Whether to allow creating future entries.
66 var $bcc_email = null; // Bcc email.
67 var $allow_ip = null; // Specification from where user is allowed access.
68 var $password_complexity = null; // Password complexity example.
69 var $currency = null; // Currency.
70 var $plugins = null; // Comma-separated list of enabled plugins.
72 // Refactoring ongoing. Towards using helper instead of config string?
73 var $config = null; // Comma-separated list of miscellaneous config options.
74 var $configHelper = null; // An instance of ttConfigHelper class.
76 var $custom_logo = 0; // Whether to use a custom logo for group.
77 var $lock_spec = null; // Cron specification for record locking.
78 var $holidays = null; // Holidays specification.
79 var $workday_minutes = 480; // Number of work minutes in a regular day.
80 var $rights = array(); // An array of user rights such as 'track_own_time', etc.
81 var $is_client = false; // Whether user is a client as determined by missing 'track_own_time' right.
83 var $behalfUser = null; // A ttBehalfUser instance with on behalf user attributes.
84 var $behalfGroup = null; // A ttGroup instance with on behalf group attributes.
87 function __construct($login, $id = null) {
88 if (!$login && !$id) {
89 // nothing to initialize
93 $mdb2 = getConnection();
95 $sql = "SELECT u.id, u.login, u.name, u.group_id, u.role_id, r.rank, r.name as role_name, r.rights, u.client_id,".
96 " u.quota_percent, u.email, g.org_id, g.name as group_name, g.currency, g.lang, g.decimal_mark, g.date_format,".
97 " g.time_format, g.week_start, g.tracking_mode, g.project_required, g.task_required, g.record_type,".
98 " g.bcc_email, g.allow_ip, g.password_complexity, g.plugins, g.config, g.lock_spec, g.holidays, g.workday_minutes, g.custom_logo".
99 " FROM tt_users u LEFT JOIN tt_groups g ON (u.group_id = g.id) LEFT JOIN tt_roles r on (r.id = u.role_id) WHERE ";
101 $sql .= "u.id = $id";
103 $sql .= "u.login = ".$mdb2->quote($login);
104 $sql .= " AND u.status = 1";
106 $res = $mdb2->query($sql);
107 if (is_a($res, 'PEAR_Error')) {
111 $val = $res->fetchRow();
112 if ($val['id'] > 0) {
113 $this->login = $val['login'];
114 $this->name = $val['name'];
115 $this->id = $val['id'];
116 $this->org_id = $val['org_id'];
117 $this->group_id = $val['group_id'];
118 $this->role_id = $val['role_id'];
119 $this->role_name = $val['role_name'];
120 $this->rights = explode(',', $val['rights']);
121 $this->rank = $val['rank'];
122 $this->client_id = $val['client_id'];
123 $this->is_client = $this->client_id && !in_array('track_own_time', $this->rights);
124 if ($val['quota_percent']) $this->quota_percent = $val['quota_percent'];
125 $this->email = $val['email'];
126 $this->lang = $val['lang'];
127 $this->decimal_mark = $val['decimal_mark'];
128 $this->date_format = $val['date_format'];
129 $this->time_format = $val['time_format'];
130 $this->week_start = $val['week_start'];
131 $this->tracking_mode = $val['tracking_mode'];
132 $this->project_required = $val['project_required'];
133 $this->task_required = $val['task_required'];
134 $this->record_type = $val['record_type'];
135 $this->bcc_email = $val['bcc_email'];
136 $this->allow_ip = $val['allow_ip'];
137 $this->password_complexity = $val['password_complexity'];
138 $this->group_name = $val['group_name'];
139 $this->currency = $val['currency'];
140 $this->plugins = $val['plugins'];
141 $this->lock_spec = $val['lock_spec'];
142 $this->holidays = $val['holidays'];
143 $this->workday_minutes = $val['workday_minutes'];
144 $this->custom_logo = $val['custom_logo'];
146 // TODO: refactor this.
147 $this->config = $val['config'];
148 $this->configHelper = new ttConfigHelper($val['config']);
150 // Set user config options.
151 $this->show_holidays = $this->configHelper->getDefinedValue('show_holidays');
152 $this->punch_mode = $this->configHelper->getDefinedValue('punch_mode');
153 $this->allow_overlap = $this->configHelper->getDefinedValue('allow_overlap');
154 $this->future_entries = $this->configHelper->getDefinedValue('future_entries');
156 // Set "on behalf" id and name (user).
157 if (isset($_SESSION['behalf_id'])) {
158 $this->behalf_id = $_SESSION['behalf_id'];
159 $this->behalf_name = $_SESSION['behalf_name'];
161 $this->behalfUser = new ttBehalfUser($this->behalf_id, $this->org_id);
163 // Set "on behalf" id and name (group).
164 if (isset($_SESSION['behalf_group_id'])) {
165 $this->behalf_group_id = $_SESSION['behalf_group_id'];
166 $this->behalf_group_name = $_SESSION['behalf_group_name'];
168 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
173 // getUser returns user id on behalf of whom the current user is operating.
175 return ($this->behalfUser ? $this->behalfUser->id : $this->id);
178 // getName returns user name on behalf of whom the current user is operating.
180 return ($this->behalfUser ? $this->behalfUser->name : $this->name);
183 // getQuotaPercent returns quota percent for active user.
184 function getQuotaPercent() {
185 return ($this->behalfUser ? $this->behalfUser->quota_percent : $this->quota_percent);
188 // The getGroup returns group id on behalf of which the current user is operating.
189 function getGroup() {
190 return ($this->behalfGroup ? $this->behalfGroup->id : $this->group_id);
193 // getDecimalMark returns decimal mark for active group.
194 function getDecimalMark() {
195 return ($this->behalfGroup ? $this->behalfGroup->decimal_mark : $this->decimal_mark);
198 // getDateFormat returns date format for active group.
199 function getDateFormat() {
200 return ($this->behalfGroup ? $this->behalfGroup->date_format : $this->date_format);
203 // getTimeFormat returns time format for active group.
204 function getTimeFormat() {
205 return ($this->behalfGroup ? $this->behalfGroup->time_format : $this->time_format);
208 // getWeekStart returns week start day for active group.
209 function getWeekStart() {
210 return ($this->behalfGroup ? $this->behalfGroup->week_start : $this->week_start);
213 // getTrackingMode returns tracking mode for active group.
214 function getTrackingMode() {
215 return ($this->behalfGroup ? $this->behalfGroup->tracking_mode : $this->tracking_mode);
218 // getRecordType returns record type for active group.
219 function getRecordType() {
220 return ($this->behalfGroup ? $this->behalfGroup->record_type : $this->record_type);
223 // getCurrency returns currency string for active group.
224 function getCurrency() {
225 return ($this->behalfGroup ? $this->behalfGroup->currency : $this->currency);
228 // getPlugins returns plugins string for active group.
229 function getPlugins() {
230 return ($this->behalfGroup ? $this->behalfGroup->plugins : $this->plugins);
233 // getLockSpec returns lock specification for active group.
234 function getLockSpec() {
235 return ($this->behalfGroup ? $this->behalfGroup->lock_spec : $this->lock_spec);
238 // getHolidays returns holidays specification for active group.
239 function getHolidays() {
240 return ($this->behalfGroup ? $this->behalfGroup->holidays : $this->holidays);
243 // getWorkdayMinutes returns workday_minutes for active group.
244 function getWorkdayMinutes() {
245 return ($this->behalfGroup ? $this->behalfGroup->workday_minutes : $this->workday_minutes);
248 // getConfig returns config string for active group.
249 function getConfig() {
250 return ($this->behalfGroup ? $this->behalfGroup->configHelper->getConfig() : $this->configHelper->getConfig());
253 // getConfigOption returns true if an option is defined for group.
254 // This helps us keeping a set of user attributes smaller.
255 // We determine whether the option is set only on pages that need to know.
256 // For example: confirm_save is used only on time and expense edit pages.
257 function getConfigOption($name) {
258 $config = new ttConfigHelper($this->getConfig());
259 return $config->getDefinedValue($name);
262 // getConfigInt retruns an integer value defined in a group, or false.
263 function getConfigInt($name, $defaultVal) {
264 $config = new ttConfigHelper($this->getConfig());
265 return $config->getIntValue($name, $defaultVal);
268 // can - determines whether user has a right to do something.
269 function can($do_something) {
270 return in_array($do_something, $this->rights);
273 // isClient - determines whether current user is a client.
274 function isClient() {
275 return $this->is_client;
278 // isPluginEnabled checks whether a plugin is enabled for user.
279 function isPluginEnabled($plugin)
281 return in_array($plugin, explode(',', $this->getPlugins()));
284 // isOptionEnabled checks whether a config option is enabled for user.
285 function isOptionEnabled($option)
287 return $this->behalfGroup ? $this->behalfGroup->configHelper->getDefinedValue($option) : $this->configHelper->getDefinedValue($option);
290 // setOption sets an option inside of ttConfigHelper instance.
291 // Note that it does not write to the database.
292 function setOption($option, $enable = true)
294 return $this->behalfGroup ? $this->behalfGroup->configHelper->setDefinedValue($option, $enable) : $this->configHelper->setDefinedValue($option, $enable);
297 // getAssignedProjects - returns an array of assigned projects.
298 function getAssignedProjects($includeFiles = false)
301 $mdb2 = getConnection();
303 $user_id = $this->getUser();
304 $group_id = $this->getGroup();
305 $org_id = $this->org_id;
308 $filePart = ', if(Sub1.entity_id is null, 0, 1) as has_files';
309 $fileJoin = " left join (select distinct entity_id from tt_files".
310 " where entity_type = 'project' and group_id = $group_id and org_id = $org_id and status = 1) Sub1".
311 " on (p.id = Sub1.entity_id)";
314 // Do a query with inner join to get assigned projects.
315 $sql = "select p.id, p.name, p.description, p.tasks, upb.rate $filePart from tt_projects p $fileJoin".
316 " inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)".
317 " where p.group_id = $group_id and p.org_id = $org_id and p.status = 1 order by p.name";
318 $res = $mdb2->query($sql);
319 if (!is_a($res, 'PEAR_Error')) {
320 while ($val = $res->fetchRow()) {
327 // getAssignedTasks - returns an array of assigned tasks.
328 function getAssignedTasks()
330 // Start with projects;
331 $projects = $this->getAssignedProjects();
332 if (!$projects) return false;
334 // Build an array of task ids.
336 foreach($projects as $project) {
337 $one_project_tasks = $project['tasks'] ? explode(',', $project['tasks']) : array();
338 $task_ids = array_unique(array_merge($task_ids, $one_project_tasks));
340 if (!$task_ids) return false;
342 // Get task descriptions.
344 $mdb2 = getConnection();
345 $tasks = implode(',', $task_ids); // This is a comma-separated list of task ids.
347 $group_id = $this->getGroup();
348 $org_id = $this->org_id;
350 $sql = "select id, name, description from tt_tasks".
351 " where group_id = $group_id and org_id = $org_id and status = 1 and id in ($tasks) order by name";
352 $res = $mdb2->query($sql);
353 if (!is_a($res, 'PEAR_Error')) {
354 while ($val = $res->fetchRow()) {
361 // getAssignedClients - returns an array of clients assigned to own projects.
362 function getAssignedClients()
364 // Start with projects;
365 $projects = $this->getAssignedProjects();
366 if (!$projects) return false;
367 $assigned_project_ids = array();
368 foreach($projects as $project) {
369 $assigned_project_ids[] = $project['id'];
372 $mdb2 = getConnection();
374 $group_id = $this->getGroup();
375 $org_id = $this->org_id;
377 // Get active clients for group.
379 $sql = "select id, name, address, projects from tt_clients where group_id = $group_id and org_id = $org_id and status = 1";
380 $res = $mdb2->query($sql);
381 if (!is_a($res, 'PEAR_Error')) {
382 while ($val = $res->fetchRow()) {
383 $client_project_ids = $val['projects'] ? explode(',', $val['projects']) : array();
384 if (array_intersect($assigned_project_ids, $client_project_ids))
385 $clients[] = $val; // Add client if one of user projects is a client project, too.
391 // isDateLocked checks whether a specifc date is locked for modifications.
392 function isDateLocked($date)
394 if (!$this->isPluginEnabled('lk'))
395 return false; // Locking feature is disabled.
397 if (!$this->getLockSpec())
398 return false; // There is no lock specification.
400 if (!$this->behalf_id && $this->can('override_own_date_lock'))
401 return false; // User is working as self and can override own date lock.
403 if ($this->behalf_id && $this->can('override_date_lock'))
404 return false; // User is working on behalf of someone else and can override date lock.
406 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.php');
407 require_once(LIBRARY_DIR.'/tdcron/class.tdcron.entry.php');
409 // Calculate the last occurrence of a lock.
410 $last = tdCron::getLastOccurrence($this->getLockSpec(), time());
411 $lockdate = new DateAndTime(DB_DATEFORMAT, strftime('%Y-%m-%d', $last));
412 if ($date->before($lockdate))
418 // canOverridePunchMode checks whether a user can override punch mode in a situation.
419 function canOverridePunchMode()
421 if (!$this->behalf_id && !$this->can('override_own_punch_mode'))
422 return false; // User is working as self and cannot override for self.
424 if ($this->behalf_id && !$this->can('override_punch_mode'))
425 return false; // User is working on behalf of someone else and cannot override.
430 // getUsers obtains users in a group, as specififed by options.
431 function getUsers($options) {
432 $mdb2 = getConnection();
434 $group_id = $this->getGroup();
435 $org_id = $this->org_id;
437 $skipClients = !isset($options['include_clients']);
438 $includeSelf = isset($options['include_self']);
440 $select_part = 'select u.id, u.group_id, u.name';
441 if (isset($options['include_login'])) {
442 $select_part .= ', u.login';
443 // Piggy-back on include_login to see if we must also include quota_percent.
444 $include_quota = $this->isPluginEnabled('mq');
445 if ($include_quota) {
446 $decimal_mark = $this->getDecimalMark();
447 $replaceDecimalMark = ('.' != $decimal_mark);
448 $select_part .= ', u.quota_percent';
451 if (!isset($options['include_clients'])) $select_part .= ', r.rights';
452 if (isset($options['include_role'])) $select_part .= ', r.name as role_name, r.rank';
454 $from_part = ' from tt_users u';
457 if (isset($options['max_rank']) || $skipClients || isset($options['include_role']))
458 $left_joins .= ' left join tt_roles r on (u.role_id = r.id)';
460 $where_part = " where u.org_id = $org_id and u.group_id = $group_id";
461 if (isset($options['status']))
462 $where_part .= ' and u.status = '.(int)$options['status'];
464 $where_part .= ' and u.status is not null';
466 $where_part .= " and (u.id = $this->id || r.rank <= ".(int)$options['max_rank'].')';
468 if (isset($options['max_rank'])) $where_part .= ' and r.rank <= '.(int)$options['max_rank'];
471 $order_part = " order by upper(u.name)";
473 $sql = $select_part.$from_part.$left_joins.$where_part.$order_part;
474 $res = $mdb2->query($sql);
475 $user_list = array();
476 if (is_a($res, 'PEAR_Error'))
479 while ($val = $res->fetchRow()) {
481 $isClient = in_array('track_own_time', explode(',', $val['rights'])) ? 0 : 1; // Clients do not have track_own_time right.
483 continue; // Skip adding clients.
485 if ($include_quota) {
486 $quota = $val['quota_percent'];
487 if (ttEndsWith($quota, '.00'))
488 $quota = substr($quota, 0, strlen($quota)-3); // Trim trailing ".00";
489 elseif ($replaceDecimalMark)
490 $quota = str_replace('.', $decimal_mark, $quota);
491 $val['quota_percent'] = $quota.'%';
496 if (isset($options['self_first'])) {
497 // Put own entry at the front.
498 $cnt = count($user_list);
499 for($i = 0; $i < $cnt; $i++) {
500 if ($user_list[$i]['id'] == $this->id) {
501 $self = $user_list[$i]; // Found self.
502 array_unshift($user_list, $self); // Put own entry at the front.
503 array_splice($user_list, $i+1, 1); // Remove duplicate.
510 // getGroupsForDropdown obtains an array of groups to populate "Group" dropdown.
512 // - User home group.
513 // - The entire stack of groups all the way down to current on behalf group.
514 // - All immediate children of the current on behalf group.
515 // This allows user to navigate easily to home group, anything in between, and 1 level below.
516 function getGroupsForDropdown() {
517 $mdb2 = getConnection();
519 // Start with subgroups.
521 $group_id = $this->getGroup();
522 $sql = "select id, name from tt_groups where org_id = $this->org_id and parent_id = $group_id and status = 1";
523 $res = $mdb2->query($sql);
524 if (!is_a($res, 'PEAR_Error')) {
525 while ($val = $res->fetchRow()) {
530 // Add current on behalf group to the beginning of array.
531 $selected_group_id = ($this->behalf_group_id ? $this->behalf_group_id : $this->group_id);
532 $selected_group_name = ($this->behalf_group_id ? $this->behalf_group_name : $this->group_name);
533 array_unshift($groups, array('id'=>$selected_group_id,'name'=>$selected_group_name));
535 // Iterate all the way to the home group, starting with selected ("on behalf") group.
536 $current_group_id = $selected_group_id;
537 while ($current_group_id != $this->group_id) {
538 $sql = "select parent_id from tt_groups where org_id = $this->org_id and id = $current_group_id and status = 1";
539 $res = $mdb2->query($sql);
540 if (is_a($res, 'PEAR_Error')) return false;
542 $val = $res->fetchRow();
543 $parent_id = $val['parent_id'];
545 // Get parent group name.
546 $sql = "select name from tt_groups where org_id = $this->org_id and id = $parent_id and status = 1";
547 $res = $mdb2->query($sql);
548 if (is_a($res, 'PEAR_Error')) return false;
549 $val = $res->fetchRow();
550 if (!$val) return false;
551 array_unshift($groups, array('id'=>$parent_id,'name'=>$val['name']));
552 $current_group_id = $parent_id;
560 // getSubgroups obtains a list of immediate subgroups.
561 function getSubgroups($group_id = null) {
562 $mdb2 = getConnection();
564 if (!$group_id) $group_id = $this->getGroup();
566 $sql = "select id, name, description from tt_groups where org_id = $this->org_id".
567 " and parent_id = $group_id and status is not null order by upper(name)";
568 $res = $mdb2->query($sql);
569 if (!is_a($res, 'PEAR_Error')) {
570 while ($val = $res->fetchRow()) {
577 // getUserDetails function is used to manage users in group and returns user details.
578 // At the moment, the function is used for user edits and deletes.
579 function getUserDetails($user_id) {
580 if (!$this->can('manage_users')) return false;
582 $mdb2 = getConnection();
583 $group_id = $this->getGroup();
584 $org_id = $this->org_id;
586 // Determine max rank. If we are searching in on behalf group
587 // then rank restriction does not apply.
588 $max_rank = $this->behalfGroup ? MAX_RANK : $this->rank;
590 $sql = "select u.id, u.name, u.login, u.role_id, u.client_id, u.status, u.rate, u.quota_percent, u.email from tt_users u".
591 " left join tt_roles r on (u.role_id = r.id)".
592 " where u.id = $user_id and u.group_id = $group_id and u.org_id = $org_id and u.status is not null".
593 " and (r.rank < $max_rank or (r.rank = $max_rank and u.id = $this->id))"; // Users with lesser roles or self.
594 $res = $mdb2->query($sql);
595 if (!is_a($res, 'PEAR_Error')) {
596 $val = $res->fetchRow();
602 // checkBehalfId checks whether behalf_id is appropriate.
603 // On behalf user must be active and have lower rank if the user is from home group,
605 // - subgroup must ve valid;
606 // - user should be a member of it.
607 function checkBehalfId() {
608 if (!$this->behalfGroup) {
609 // Checking user from home group.
610 $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1);
611 $users = $this->getUsers($options);
612 foreach($users as $one_user) {
613 if ($one_user['id'] == $this->behalf_id)
617 // Checking user from a subgroup.
618 $group_id = $this->behalfGroup->id;
619 if (!$this->isSubgroupValid($group_id))
622 // So far, so good. Check user now.
623 $options = array('status'=>ACTIVE,'max_rank'=>MAX_RANK);
624 $users = $this->getUsers($options);
625 foreach($users as $one_user) {
626 if ($one_user['id'] == $this->behalf_id)
633 // adjustBehalfId attempts to adjust behalf_id and behalf_name to a first found
636 // Needed for situations when user does not have do_own_something right.
637 // Example: has view_charts but does not have view_own_charts.
638 // In this case we still allow access to charts, but set behalf_id to someone else.
639 // Another example: working in a subgroup on behalf of someone else.
640 function adjustBehalfId() {
641 $rank = $this->getMaxRankForGroup($this->getGroup());
643 // Adjust to first found user in group.
644 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
645 $users = $this->getUsers($options);
646 foreach($users as $one_user) {
647 // Fake loop to access first element.
648 $this->behalf_id = $one_user['id'];
649 $this->behalf_name = $one_user['name'];
650 $_SESSION['behalf_id'] = $this->behalf_id;
651 $_SESSION['behalf_name'] = $this->behalf_name;
657 // updateGroup updates group information with new data.
658 function updateGroup($fields) {
659 $mdb2 = getConnection();
661 $group_id = $fields['group_id'];
662 if ($group_id && !$this->isGroupValid($group_id)) return false;
663 if (!$group_id) $group_id = $this->getGroup();
665 if (isset($fields['name'])) $name_part = ', name = '.$mdb2->quote($fields['name']);
666 if (isset($fields['description'])) $description_part = ', description = '.$mdb2->quote($fields['description']);
667 if (isset($fields['currency'])) $currency_part = ', currency = '.$mdb2->quote($fields['currency']);
668 if (isset($fields['lang'])) $lang_part = ', lang = '.$mdb2->quote($fields['lang']);
669 if (isset($fields['decimal_mark'])) $decimal_mark_part = ', decimal_mark = '.$mdb2->quote($fields['decimal_mark']);
670 if (isset($fields['date_format'])) $date_format_part = ', date_format = '.$mdb2->quote($fields['date_format']);
671 if (isset($fields['time_format'])) $time_format_part = ', time_format = '.$mdb2->quote($fields['time_format']);
672 if (isset($fields['week_start'])) $week_start_part = ', week_start = '.(int) $fields['week_start'];
673 if (isset($fields['tracking_mode'])) {
674 $tracking_mode_part = ', tracking_mode = '.(int) $fields['tracking_mode'];
675 $project_required_part = ' , project_required = '.(int) $fields['project_required'];
676 $task_required_part = ' , task_required = '.(int) $fields['task_required'];
678 if (isset($fields['record_type'])) $record_type_part = ', record_type = '.(int) $fields['record_type'];
679 if (isset($fields['bcc_email'])) $bcc_email_part = ', bcc_email = '.$mdb2->quote($fields['bcc_email']);
680 if (isset($fields['allow_ip'])) $allow_ip_part = ', allow_ip = '.$mdb2->quote($fields['allow_ip']);
681 if (isset($fields['plugins'])) $plugins_part = ', plugins = '.$mdb2->quote($fields['plugins']);
682 if (isset($fields['config'])) $config_part = ', config = '.$mdb2->quote($fields['config']);
683 if (isset($fields['lock_spec'])) $lock_spec_part = ', lock_spec = '.$mdb2->quote($fields['lock_spec']);
684 if (isset($fields['holidays'])) $holidays_part = ', holidays = '.$mdb2->quote($fields['holidays']);
685 if (isset($fields['workday_minutes'])) $workday_minutes_part = ', workday_minutes = '.$mdb2->quote($fields['workday_minutes']);
686 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
688 $parts = trim($name_part.$description_part.$currency_part.$lang_part.$decimal_mark_part.$date_format_part.
689 $time_format_part.$week_start_part.$tracking_mode_part.$task_required_part.$project_required_part.$record_type_part.
690 $bcc_email_part.$allow_ip_part.$plugins_part.$config_part.$lock_spec_part.$holidays_part.$workday_minutes_part.$modified_part, ',');
692 $sql = "update tt_groups set $parts where id = $group_id and org_id = $this->org_id";
693 $affected = $mdb2->exec($sql);
694 if (is_a($affected, 'PEAR_Error')) return false;
699 // markUserDeleted marks a user in group as deleted.
700 function markUserDeleted($user_id) {
701 if (!$this->can('manage_users') || $this->id == $user_id)
704 // Make sure we operate on a legit user.
705 $user_details = $this->getUserDetails($user_id);
706 if (!$user_details) return false;
708 $mdb2 = getConnection();
709 $group_id = $this->getGroup();
710 $org_id = $this->org_id;
712 // Mark user to project binds as deleted.
713 $sql = "update tt_user_project_binds set status = NULL where user_id = $user_id".
714 " and group_id = $group_id and org_id = $org_id";
715 $affected = $mdb2->exec($sql);
716 if (is_a($affected, 'PEAR_Error'))
719 // Mark user favorite reports as deleted.
720 $sql = "update tt_fav_reports set status = NULL where user_id = $user_id".
721 " and group_id = $group_id and org_id = $org_id";
722 $affected = $mdb2->exec($sql);
723 if (is_a($affected, 'PEAR_Error'))
726 // Mark user as deleted.
727 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($this->id);
728 $sql = "update tt_users set status = null $modified_part where id = $user_id".
729 " and group_id = $group_id and org_id = $org_id";
730 $affected = $mdb2->exec($sql);
731 if (is_a($affected, 'PEAR_Error'))
737 // enablePlugin either enables or disables a specific plugin for group.
738 function enablePlugin($plugin, $enable = true)
740 if (!$this->can('manage_advanced_settings'))
741 return false; // Note: enablePlugin is currently only used on week_view.php.
742 // So, it's not really a plugin we are enabling, but rather week view display options.
743 // Therefore, a check for manage_advanced_settings, not manage_features.
745 $plugin_array = explode(',', $this->plugins);
746 if ($enable && !in_array($plugin, $plugin_array))
747 $plugin_array[] = $plugin; // Add plugin to array.
749 if (!$enable && in_array($plugin, $plugin_array)) {
750 $key = array_search($plugin, $plugin_array);
752 unset($plugin_array[$key]); // Remove plugin from array.
755 $plugins = implode(',', $plugin_array);
756 if ($plugins != $this->plugins) {
757 if (!$this->updateGroup(array('plugins' => $plugins)))
759 $this->plugins = $plugins;
765 // isUserValid determines if a user is valid for on behalf work.
766 function isUserValid($user_id) {
767 if ($user_id == $this->id)
769 return ($this->getUserDetails($user_id) != null);
772 // isGroupValid determines if a group is valid for user.
773 function isGroupValid($group_id) {
774 if ($group_id == $this->group_id)
777 return $this->isSubgroupValid($group_id);
780 // isSubgroupValid determines if a subgroup is valid for user.
781 // A subgroup is valid if:
782 // - user can manage_subgroups;
783 // - subgroup is either a direct child of user group, or "on the path"
784 // to it (grand-child, etc.).
785 function isSubgroupValid($subgroup_id) {
786 if (!$this->can('manage_subgroups')) return false; // User cannot manage subgroups.
788 $current_group_id = $subgroup_id;
789 while ($parent_group_id = ttGroupHelper::getParentGroup($current_group_id)) {
790 if ($parent_group_id == $this->group_id) {
791 return true; // Found it.
793 $current_group_id = $parent_group_id;
798 // getMaxRankForGroup determines effective user rank for a user in a given group.
799 // For home group it is the existing user rank (as per role) minus 1.
800 // For subgroups, if user can "manage_subgroups", it is MAX_RANK.
801 function getMaxRankForGroup($group_id) {
803 $max_rank = 0; // Start safely.
804 if ($this->group_id == $group_id) {
805 $max_rank = $this->rank - 1;
809 if ($this->isSubgroupValid($group_id))
810 $max_rank = MAX_RANK;
815 // getUserPartForHeader constructs a string for user to display on pages header.
816 // It changes with "on behalf" attributes for both user and group.
817 function getUserPartForHeader() {
819 if (!$this->id) return null;
821 $user_part = htmlspecialchars($this->name);
822 $user_part .= ' - '.htmlspecialchars($this->role_name);
823 if ($this->behalf_id) {
824 $user_part .= ' <span class="onBehalf">'.$i18n->get('label.on_behalf').' '.htmlspecialchars($this->behalf_name).'</span>';
826 if ($this->behalf_group_id) {
827 $user_part .= ', <span class="onBehalf">'.htmlspecialchars($this->behalf_group_name).'</span>';
829 if ($this->group_name) // Note: we did not require group names in the past.
830 $user_part .= ', '.$this->group_name;
835 // setOnBehalfGroup sets on behalf group for the user in both the object and the session.
836 function setOnBehalfGroup($group_id) {
838 // Unset things first.
839 $this->behalf_group_id = null;
840 $this->behalf_group_name = null;
841 $this->behalf_id = null;
842 $this->behalf_name = null;
843 unset($this->behalfGroup);
844 unset($_SESSION['behalf_group_id']);
845 unset($_SESSION['behalf_group_name']);
846 unset($_SESSION['behalf_id']);
847 unset($_SESSION['behalf_name']);
849 // Destroy report bean if it was set in session.
850 $form = new Form('dummyForm');
851 $bean = new ActionForm('reportBean', $form, $request);
852 if ($bean->isSaved()) {
853 $bean->destroyBean();
856 // Do not do anything if we don't have rights.
857 if (!$this->can('manage_subgroups')) return;
859 // No need to set if group is our home group.
860 if ($group_id == $this->group_id) return;
862 // No need to set if subgroup is not valid.
863 if (!$this->isSubgroupValid($group_id)) return;
865 // We are good to set on behalf group.
866 $onBehalfGroupName = ttGroupHelper::getGroupName($group_id);
867 $_SESSION['behalf_group_id'] = $group_id;
868 $_SESSION['behalf_group_name'] = $onBehalfGroupName;
869 $this->behalf_group_id = $group_id;
870 $this->behalf_group_name = $onBehalfGroupName;
872 $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id);
874 // Adjust on behalf user to first found user in subgroup.
875 $this->adjustBehalfId();
879 // setOnBehalfUser sets on behalf user both the object and the session.
880 function setOnBehalfUser($user_id) {
882 // Unset things first.
883 $this->behalf_id = null;
884 $this->behalf_name = null;
885 unset($this->behalfUser);
886 unset($_SESSION['behalf_id']);
887 unset($_SESSION['behalf_name']);
889 // No need to set if user is us.
890 if ($user_id == $this->id) return;
892 // No need to set if user id is not valid.
893 if (!$this->isUserValid($user_id)) return;
895 // We are good to set on behalf user.
896 $onBehalfUserName = ttUserHelper::getUserName($user_id);
897 $_SESSION['behalf_id'] = $user_id;
898 $_SESSION['behalf_name'] = $onBehalfUserName;
899 $this->behalf_id = $user_id;
900 $this->behalf_name = $onBehalfUserName;
902 $this->behalfUser = new ttBehalfUser($this->behalf_id, $this->org_id);
906 // The exists() function determines if an active user exists in context of a page.
907 // If we are working as self, true.
908 // If we are working in a subgroup with active users, true.
909 // If we are working in a subgroup without active users, false.
911 if (!$this->behalfGroup)
912 return true; // Working as self.
913 else if ($this->behalfGroup->active_users)
914 return true; // Subgroup has users.