From: Nik Okuntseff Date: Tue, 27 Nov 2018 19:35:03 +0000 (+0000) Subject: Ongoing refactoring in time.php to support subgroups. X-Git-Tag: timetracker_1.19-1~537 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=52efa3cc9f8ad94f363f41cc2bca472b6492268d;p=timetracker.git Ongoing refactoring in time.php to support subgroups. --- diff --git a/WEB-INF/lib/ttGroup.class.php b/WEB-INF/lib/ttGroup.class.php index 1d33fe11..e232a0e1 100644 --- a/WEB-INF/lib/ttGroup.class.php +++ b/WEB-INF/lib/ttGroup.class.php @@ -88,7 +88,9 @@ class ttGroup { /* TODO: initialize other things here. $this->project_required = $val['project_required']; $this->task_required = $val['task_required']; + */ $this->record_type = $val['record_type']; + /* $this->bcc_email = $val['bcc_email']; $this->allow_ip = $val['allow_ip']; $this->password_complexity = $val['password_complexity']; diff --git a/WEB-INF/lib/ttUser.class.php b/WEB-INF/lib/ttUser.class.php index a6047a7f..968a1f10 100644 --- a/WEB-INF/lib/ttUser.class.php +++ b/WEB-INF/lib/ttUser.class.php @@ -181,6 +181,11 @@ class ttUser { return ($this->behalfGroup ? $this->behalfGroup->tracking_mode : $this->tracking_mode); } + // getRecordType returns record type for active group. + function getRecordType() { + return ($this->behalfGroup ? $this->behalfGroup->record_type : $this->record_type); + } + // getPlugins returns plugins string for active group. function getPlugins() { return ($this->behalfGroup ? $this->behalfGroup->plugins : $this->plugins); @@ -209,7 +214,7 @@ class ttUser { // isPluginEnabled checks whether a plugin is enabled for user. function isPluginEnabled($plugin) { - return in_array($plugin, explode(',', $this->plugins)); + return in_array($plugin, explode(',', $this->getPlugins())); } // getAssignedProjects - returns an array of assigned projects. @@ -662,6 +667,20 @@ class ttUser { return true; } + // isUserValid determines if a user is valid for on behalf work. + function isUserValid($user_id) { + if ($user_id == $this->id) + return true; + + $user_details = $this->getUser($user_id); // TODO: this will probably not work for higher ranks. + // In this case we'll need another function. + // Or adjust getUser for max rank. + if (!$user_details) + return false; + + return true; + } + // isGroupValid determines if a group is valid for user. function isGroupValid($group_id) { if ($group_id == $this->group_id) @@ -742,10 +761,10 @@ class ttUser { // Do not do anything if we don't have rights. if (!$this->can('manage_subgroups')) return; - // No need to set if the group is our home group. + // No need to set if group is our home group. if ($group_id == $this->group_id) return; - // No need to set if the subgroup is not valid. + // No need to set if subgroup is not valid. if (!$this->isSubgroupValid($group_id)) return; // We are good to set on behalf group. @@ -761,4 +780,28 @@ class ttUser { $this->adjustBehalfId(); return; } + + // setOnBehalfUser sets on behalf user both the object and the session. + function setOnBehalfUser($user_id) { + + // Unset things first. + $this->behalf_id = null; + $this->behalf_name = null; + unset($_SESSION['behalf_id']); + unset($_SESSION['behalf_name']); + + // No need to set if user is us. + if ($user_id == $this->id) return; + + // No need to set if user id is not valid. + if (!$this->isUserValid($user_id)) return; + + // We are good to set on behalf user. + $onBehalfUserName = ttUserHelper::getUserName($user_id); + $_SESSION['behalf_id'] = $user_id; + $_SESSION['behalf_name'] = $onBehalfUserName; + $this->behalf_id = $user_id; + $this->behalf_name = $onBehalfUserName; + return; + } } diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index 79ff7901..6e62d6e3 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
-
 Anuko Time Tracker 1.18.28.4528 | Copyright © Anuko | +  Anuko Time Tracker 1.18.28.4529 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/WEB-INF/templates/time.tpl b/WEB-INF/templates/time.tpl index 0e05f05b..bb862871 100644 --- a/WEB-INF/templates/time.tpl +++ b/WEB-INF/templates/time.tpl @@ -18,16 +18,16 @@
-{if $on_behalf_group_control} +{if $group_dropdown} - + {/if} -{if $on_behalf_control} +{if $user_dropdown} - + {/if} {if $user->isPluginEnabled('cl')} diff --git a/time.php b/time.php index ae17aa0a..33768d46 100644 --- a/time.php +++ b/time.php @@ -48,8 +48,35 @@ if (!$user->behalf_id && !$user->can('track_own_time') && !$user->adjustBehalfId header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf. exit(); } +if ($request->isPost()) { + $groupChanged = $request->getParameter('group_changed'); // Reused in multiple places below. + if ($groupChanged && !($user->can('manage_subgroups') && $user->isGroupValid($request->getParameter('group')))) { + header('Location: access_denied.php'); // Group changed, but no rght or wrong group id. + exit(); + } + $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below. + if ($userChanged && !($user->can('track_time') && $user->isUserValid($request->getParameter('user')))) { + header('Location: access_denied.php'); // Group changed, but no rght or wrong group id. + exit(); + } +} // End of access checks. +// Determine group for which we display this page. +if ($request->isPost() && $groupChanged) { + $group_id = $request->getParameter('group'); + $user->setOnBehalfGroup($group_id); +} else { + $group_id = $user->getGroup(); +} +// Determine user for which we display this page. +if ($request->isPost() && $userChanged) { + $user_id = $request->getParameter('user'); + $user->setOnBehalfUser($user_id); +} else { + $user_id = $user->getActiveUser(); +} + // Initialize and store date in session. $cl_date = $request->getParameter('date', @$_SESSION['date']); $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date); @@ -62,7 +89,7 @@ $_SESSION['date'] = $cl_date; // Use custom fields plugin if it is enabled. if ($user->isPluginEnabled('cf')) { require_once('plugins/CustomFields.class.php'); - $custom_fields = new CustomFields($user->group_id); + $custom_fields = new CustomFields($user->getGroup()); $smarty->assign('custom_fields', $custom_fields); } @@ -95,8 +122,8 @@ if ($user->isPluginEnabled('iv')) { if (isset($_SESSION['billable'])) $cl_billable = $_SESSION['billable']; } -$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id)); -$on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id)); +//$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id)); +//$on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id)); $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client'])); $_SESSION['client'] = $cl_client; $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project'])); @@ -106,45 +133,44 @@ $_SESSION['task'] = $cl_task; // Elements of timeRecordForm. $form = new Form('timeRecordForm'); - -if (isTrue(SUBGROUP_DEBUG)) { +// Group dropdown. if ($user->can('manage_subgroups')) { $groups = $user->getGroupsForDropdown(); if (count($groups) > 1) { $form->addInput(array('type'=>'combobox', - 'onchange'=>'this.form.submit();', - 'name'=>'onBehalfGroup', + 'onchange'=>'document.timeRecordForm.group_changed.value=1;document.timeRecordForm.submit();', + 'name'=>'group', 'style'=>'width: 250px;', - 'value'=>$on_behalf_group_id, + 'value'=>$group_id, 'data'=>$groups, 'datakeys'=>array('id','name'))); - $smarty->assign('on_behalf_group_control', 1); + $form->addInput(array('type'=>'hidden','name'=>'group_changed')); + $smarty->assign('group_dropdown', 1); } } -} // SUBGROUP_DEBUG - if ($user->can('track_time')) { - $rank = $user->getMaxRankForGroup($on_behalf_group_id); + $rank = $user->getMaxRankForGroup($group_id); if ($user->can('track_own_time')) - $options = array('group_id'=>$on_behalf_group_id,'status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true); + $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true); else - $options = array('group_id'=>$on_behalf_group_id,'status'=>ACTIVE,'max_rank'=>$rank); + $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank); $user_list = $user->getUsers($options); if (count($user_list) >= 1) { $form->addInput(array('type'=>'combobox', - 'onchange'=>'this.form.submit();', - 'name'=>'onBehalfUser', + 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();', + 'name'=>'user', 'style'=>'width: 250px;', - 'value'=>$on_behalf_id, + 'value'=>$user_id, 'data'=>$user_list, 'datakeys'=>array('id','name'))); - $smarty->assign('on_behalf_control', 1); + $form->addInput(array('type'=>'hidden','name'=>'user_changed')); + $smarty->assign('user_dropdown', 1); } } // Dropdown for clients in MODE_TIME. Use all active clients. -if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) { - $active_clients = ttTeamHelper::getActiveClients($user->group_id, true); +if (MODE_TIME == $user->getTrackingMode() && $user->isPluginEnabled('cl')) { + $active_clients = ttTeamHelper::getActiveClients($user->getGroup(), true); $form->addInput(array('type'=>'combobox', 'onchange'=>'fillProjectDropdown(this.value);', 'name'=>'client', @@ -156,7 +182,7 @@ if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) { // Note: in other modes the client list is filtered to relevant clients only. See below. } -if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) { +if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) { // Dropdown for projects assigned to user. $project_list = $user->getAssignedProjects(); $form->addInput(array('type'=>'combobox', @@ -170,7 +196,7 @@ if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->t // Dropdown for clients if the clients plugin is enabled. if ($user->isPluginEnabled('cl')) { - $active_clients = ttTeamHelper::getActiveClients($user->group_id, true); + $active_clients = ttTeamHelper::getActiveClients($user->getGroup(), true); // We need an array of assigned project ids to do some trimming. foreach($project_list as $project) $projects_assigned_to_user[] = $project['id']; @@ -197,8 +223,8 @@ if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->t } } -if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) { - $task_list = ttTeamHelper::getActiveTasks($user->group_id); +if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) { + $task_list = ttTeamHelper::getActiveTasks($user->getGroup()); $form->addInput(array('type'=>'combobox', 'name'=>'task', 'style'=>'width: 250px;', @@ -209,7 +235,7 @@ if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) { } // Add other controls. -if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) { +if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) { $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');")); $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');")); if ($user->punch_mode && !$user->canOverridePunchMode()) { @@ -218,7 +244,7 @@ if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type $form->getElement('finish')->setEnabled(false); } } -if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type)) +if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');")); if (!defined('NOTE_INPUT_HEIGHT')) define('NOTE_INPUT_HEIGHT', 40); @@ -367,62 +393,6 @@ if ($request->isPost()) { exit(); } } - elseif ($request->getParameter('onBehalfUser') || $request->getParameter('onBehalfGroup')) { - // User changed either on behalf user or group. - // TODO: Organize this code into a separate function. - - // We get here if one of the dropdowns changed. Handle these 2 situations differently. - // 1) User changed. Determine if user changed. Then do exactly as before. - // - // Group changed. Determine if group changed. - // Adjust group info. - // Adjust user info to first user in group (or self if we are in home group now). - // - // Determine if user was changed. - if ($request->getParameter('onBehalfUser')) { - $request_user_id = $request->getParameter('onBehalfUser'); - $session_user_id = $_SESSION['behalf_id']; - $user_changed = !(($session_user_id == null && ($user->id == $request_user_id)) - || ($session_user_id != null && ($request_user_id == $session_user_id))); - if ($user_changed && $user->can('track_time')) { - unset($_SESSION['behalf_id']); - unset($_SESSION['behalf_name']); - - if($request_user_id != $user->id) { - $_SESSION['behalf_id'] = $request_user_id; - $_SESSION['behalf_name'] = ttUserHelper::getUserName($request_user_id); - } - } - } - - if ($request->getParameter('onBehalfGroup')) { - // Determine if group was changed. - $request_group_id = $request->getParameter('onBehalfGroup'); - $session_group_id = $_SESSION['behalf_group_id']; - $group_changed = !(($session_group_id == null && ($user->group_id == $request_group_id)) - || ($session_group_id != null && ($request_group_id == $session_group_id))); - - if ($group_changed && $user->can('manage_subgroups')) { - unset($_SESSION['behalf_group_id']); - unset($_SESSION['behalf_group_name']); - if ($request_group_id == $user->group_id) - $user->behalf_group_id = null; - - if (($request_group_id != $user->group_id) && $user->isSubgroupValid($request_group_id)) { - $_SESSION['behalf_group_id'] = $request_group_id; - $_SESSION['behalf_group_name'] = ttGroupHelper::getGroupName($request_group_id); - $user->behalf_group_id = $request_group_id; - } - - unset($_SESSION['behalf_id']); - unset($_SESSION['behalf_name']); - if ($request_group_id != $user->group_id) - $user->adjustBehalfId(); - } - } - header('Location: time.php'); - exit(); - } } // isPost $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
{$i18n.label.group}:{$forms.timeRecordForm.onBehalfGroup.control}{$forms.timeRecordForm.group.control}
{$i18n.label.user}:{$forms.timeRecordForm.onBehalfUser.control}{$forms.timeRecordForm.user.control}