From 9af5722a81d1999243ac3a3d51d3cf3c3256d86a Mon Sep 17 00:00:00 2001 From: Nik Okuntseff Date: Mon, 3 Dec 2018 15:09:57 +0000 Subject: [PATCH] Adjusted charts.php for subgroups. --- WEB-INF/lib/ttChartHelper.class.php | 35 ++++++++ WEB-INF/lib/ttUser.class.php | 11 ++- WEB-INF/templates/charts.tpl | 4 +- WEB-INF/templates/footer.tpl | 2 +- WEB-INF/templates/header.tpl | 8 +- charts.php | 127 ++++++++++++---------------- 6 files changed, 100 insertions(+), 87 deletions(-) diff --git a/WEB-INF/lib/ttChartHelper.class.php b/WEB-INF/lib/ttChartHelper.class.php index 75a38d07..7a4d2584 100644 --- a/WEB-INF/lib/ttChartHelper.class.php +++ b/WEB-INF/lib/ttChartHelper.class.php @@ -132,4 +132,39 @@ class ttChartHelper { return $result; } + + // adjustType - adjust chart type to something that is available for a group. + static function adjustType($requested_type) { + global $user; + $tracking_mode = $user->getTrackingMode(); + $client_option = $user->isPluginEnabled('cl'); + + // We have 3 possible options for chart tyep: projects, tasks, or clients. + // Deal with each one individually. + + if ($requested_type == CHART_PROJECTS) { + if ($tracking_mode == MODE_PROJECTS || $tracking_mode == MODE_PROJECTS_AND_TASKS) + return CHART_PROJECTS; + else if ($client_option) + return CHART_CLIENTS; + } + + if ($requested_type == CHART_TASKS) { + if ($tracking_mode == MODE_PROJECTS_AND_TASKS) + return CHART_TASKS; + if ($tracking_mode == MODE_PROJECTS) + return CHART_PROJECTS; + else if ($client_option) + return CHART_CLIENTS; + } + + if ($requested_type == CHART_CLIENTS) { + if ($client_option) + return CHART_CLIENTS; + else if ($tracking_mode == MODE_PROJECTS || ($tracking_mode == MODE_PROJECTS_AND_TASKS)) + return CHART_PROJECTS; + } + + return CHART_PROJECTS; + } } diff --git a/WEB-INF/lib/ttUser.class.php b/WEB-INF/lib/ttUser.class.php index 587ca73c..5e359cd2 100644 --- a/WEB-INF/lib/ttUser.class.php +++ b/WEB-INF/lib/ttUser.class.php @@ -523,7 +523,7 @@ class ttUser { // - subgroup must ve valid; // - user should be a member of it. function checkBehalfId() { - if (!$this->behalf_group_id) { + if (!$this->behalfGroup) { // Checking user from home group. $options = array('status'=>ACTIVE,'max_rank'=>$this->rank-1); $users = $this->getUsers($options); @@ -533,7 +533,7 @@ class ttUser { } } else { // Checking user from a subgroup. - $group_id = $this->behalf_group_id; + $group_id = $this->behalfGroup->id; if (!$this->isSubgroupValid($group_id)) return false; @@ -556,11 +556,10 @@ class ttUser { // In this case we still allow access to charts, but set behalf_id to someone else. // Another example: working in a subgroup on behalf of someone else. function adjustBehalfId() { - $group_id = $this->behalf_group_id ? $this->behalf_group_id : $this->group_id; - $rank = $this->getMaxRankForGroup($group_id); + $rank = $this->getMaxRankForGroup($this->getGroup()); // Adjust to first found user in group. - $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank); + $options = array('status'=>ACTIVE,'max_rank'=>$rank); $users = $this->getUsers($options); foreach($users as $one_user) { // Fake loop to access first element. @@ -786,7 +785,7 @@ class ttUser { $this->behalfGroup = new ttGroup($this->behalf_group_id, $this->org_id); - // Adjust on behalf user. + // Adjust on behalf user to first found user in subgroup. $this->adjustBehalfId(); return; } diff --git a/WEB-INF/templates/charts.tpl b/WEB-INF/templates/charts.tpl index 5e173e10..b7df0e85 100644 --- a/WEB-INF/templates/charts.tpl +++ b/WEB-INF/templates/charts.tpl @@ -2,8 +2,8 @@ {$forms.chartForm.open} -{if $on_behalf_control} - +{if $user_dropdown} + {if $chart_selector} {/if} diff --git a/WEB-INF/templates/footer.tpl b/WEB-INF/templates/footer.tpl index dbbcb63f..aacf7a64 100644 --- a/WEB-INF/templates/footer.tpl +++ b/WEB-INF/templates/footer.tpl @@ -12,7 +12,7 @@
{$i18n.label.user}: {$forms.chartForm.onBehalfUser.control}{$i18n.label.user}: {$forms.chartForm.user.control}{$i18n.form.charts.chart}: {$forms.chartForm.type.control}
-
 Anuko Time Tracker 1.18.29.4572 | Copyright © Anuko | +  Anuko Time Tracker 1.18.29.4573 | Copyright © Anuko | {$i18n.footer.credits} | {$i18n.footer.license} | {$i18n.footer.improve} diff --git a/WEB-INF/templates/header.tpl b/WEB-INF/templates/header.tpl index de25c066..57e842ee 100644 --- a/WEB-INF/templates/header.tpl +++ b/WEB-INF/templates/header.tpl @@ -120,14 +120,14 @@ {if $user->isPluginEnabled('iv') && ($user->can('view_own_invoices') || $user->can('manage_invoices'))} · {$i18n.title.invoices} {/if} - {if ($user->isPluginEnabled('ch') && ($user->can('view_own_charts') || $user->can('view_charts'))) && ($smarty.const.MODE_PROJECTS == $user->tracking_mode - || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl'))} + {if ($user->isPluginEnabled('ch') && ($user->can('view_own_charts') || $user->can('view_charts'))) && ($smarty.const.MODE_PROJECTS == $user->getTrackingMode() + || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() || $user->isPluginEnabled('cl'))} · {$i18n.menu.charts} {/if} - {if ($user->can('view_own_projects') || $user->can('manage_projects')) && ($smarty.const.MODE_PROJECTS == $user->tracking_mode || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode)} + {if ($user->can('view_own_projects') || $user->can('manage_projects')) && ($smarty.const.MODE_PROJECTS == $user->getTrackingMode() || $smarty.const.MODE_PROJECTS_AND_TASKS == $user->getTrackingMode())} · {$i18n.menu.projects} {/if} - {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->tracking_mode && ($user->can('view_own_tasks') || $user->can('manage_tasks')))} + {if ($smarty.const.MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && ($user->can('view_own_tasks') || $user->can('manage_tasks')))} · {$i18n.menu.tasks} {/if} {if $user->can('view_users') || $user->can('manage_users')} diff --git a/charts.php b/charts.php index 3923d9a0..adaa8ae5 100644 --- a/charts.php +++ b/charts.php @@ -54,6 +54,28 @@ if (!$user->behalf_id && !$user->can('view_own_charts') && !$user->adjustBehalfI header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to view on behalf. exit(); } +if ($request->isPost() && $request->getParameter('user')) { + if (!$user->isUserValid($request->getParameter('user'))) { + header('Location: access_denied.php'); // Wrong user id on post. + exit(); + } +} +// End of access checks. + +// Determine user for which we display this page. +$userChanged = $request->getParameter('user_changed'); +if ($request->isPost() && $userChanged) { + $user_id = $request->getParameter('user'); + $user->setOnBehalfUser($user_id); +} else { + $user_id = $user->getUser(); + // Handle a situation for no users in on behalf group. + if ($user->behalfGroup && $user_id == $user->id) + $user_id = null; +} + +$uc = new ttUserConfig(); +$tracking_mode = $user->getTrackingMode(); // Initialize and store date in session. $cl_date = $request->getParameter('date', @$_SESSION['date']); @@ -63,73 +85,29 @@ if(!$cl_date) { } $_SESSION['date'] = $cl_date; -// Initialize chart interval. -$cl_interval = $_SESSION['chart_interval']; -if (!$cl_interval) { - $uc = new ttUserConfig(); - $cl_interval = $uc->getValue(SYSC_CHART_INTERVAL); -} -if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH; -$_SESSION['chart_interval'] = $cl_interval; - -// Initialize chart type. -$cl_type = $_SESSION['chart_type']; -if (!$cl_type) { - $uc = new ttUserConfig(); - $cl_type = $uc->getValue(SYSC_CHART_TYPE); -} -if (MODE_TIME == $user->getTrackingMode()) { - if ($user->isPluginEnabled('cl')) - $cl_type = CHART_CLIENTS; -} else { - if ($cl_type == CHART_CLIENTS) { - if (!$user->isPluginEnabled('cl')) - $cl_type = CHART_PROJECTS; - } elseif ($cl_type == CHART_TASKS) { - if (MODE_PROJECTS_AND_TASKS != $user->getTrackingMode()) - $cl_type = CHART_PROJECTS; - } -} -if (!$cl_type) $cl_type = CHART_PROJECTS; -$_SESSION['chart_type'] = $cl_type; - -// Who do we draw charts for? -$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id)); - if ($request->isPost()) { - // If chart interval changed - save it. $cl_interval = $request->getParameter('interval'); - if ($cl_interval) { - // Save in the session - $_SESSION['chart_interval'] = $cl_interval; - // and permanently. - $uc = new ttUserConfig(); - $uc->setValue(SYSC_CHART_INTERVAL, $cl_interval); - } - // If chart type changed - save it. + if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH; + $_SESSION['chart_interval'] = $cl_interval; + $uc->setValue(SYSC_CHART_INTERVAL, $cl_interval); + $cl_type = $request->getParameter('type'); - if ($cl_type) { - // Save in the session - $_SESSION['chart_type'] = $cl_type; - // and permanently. - $uc = new ttUserConfig(); - $uc->setValue(SYSC_CHART_TYPE, $cl_type); - } - // If user has changed - set behalf_id accordingly in the session. - if ($request->getParameter('onBehalfUser')) { - if($user->can('view_charts')) { - unset($_SESSION['behalf_id']); - unset($_SESSION['behalf_name']); - - if($on_behalf_id != $user->id) { - $_SESSION['behalf_id'] = $on_behalf_id; - $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id); - } - header('Location: charts.php'); - exit(); - } - } -} // isPost + if (!$cl_type) $cl_type = ttChartHelper::adjustType($cl_type); + $_SESSION['chart_type'] = $cl_type; + $uc->setValue(SYSC_CHART_TYPE, $cl_type); +} else { + // Initialize chart interval. + $cl_interval = $_SESSION['chart_interval']; + if (!$cl_interval) $cl_interval = $uc->getValue(SYSC_CHART_INTERVAL); + if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH; + $_SESSION['chart_interval'] = $cl_interval; + + // Initialize chart type. + $cl_type = $_SESSION['chart_type']; + if (!$cl_type) $cl_type = $uc->getValue(SYSC_CHART_TYPE); + $cl_type = ttChartHelper::adjustType($cl_type); + $_SESSION['chart_type'] = $cl_type; +} // Elements of chartForm. $chart_form = new Form('chartForm'); @@ -143,13 +121,14 @@ if ($user->can('view_charts')) { $user_list = $user->getUsers($options); if (count($user_list) >= 1) { $chart_form->addInput(array('type'=>'combobox', - 'onchange'=>'this.form.submit();', - 'name'=>'onBehalfUser', - 'value'=>$on_behalf_id, + 'onchange'=>'this.form.user_changed.value=1;this.form.submit();', + 'name'=>'user', + 'value'=>$user_id, 'data'=>$user_list, 'datakeys'=>array('id','name'), )); - $smarty->assign('on_behalf_control', 1); + $chart_form->addInput(array('type'=>'hidden','name'=>'user_changed')); + $smarty->assign('user_dropdown', 1); } } @@ -163,26 +142,26 @@ $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time'); // Chart interval dropdown. $chart_form->addInput(array('type' => 'combobox', - 'onchange' => 'if(this.form) this.form.submit();', + 'onchange' => 'this.form.submit();', 'name' => 'interval', 'value' => $cl_interval, 'data' => $intervals )); // Chart type options. -$chart_selector = (MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl')); +$chart_selector = (MODE_PROJECTS_AND_TASKS == $tracking_mode || $user->isPluginEnabled('cl')); if ($chart_selector) { $types = array(); - if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) + if (MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode) $types[CHART_PROJECTS] = $i18n->get('dropdown.projects'); - if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) + if (MODE_PROJECTS_AND_TASKS == $tracking_mode) $types[CHART_TASKS] = $i18n->get('dropdown.tasks'); if ($user->isPluginEnabled('cl')) $types[CHART_CLIENTS] = $i18n->get('dropdown.clients'); // Add chart type dropdown. $chart_form->addInput(array('type' => 'combobox', - 'onchange' => 'if(this.form) this.form.submit();', + 'onchange' => 'this.form.submit();', 'name' => 'type', 'value' => $cl_type, 'data' => $types @@ -193,7 +172,7 @@ if ($chart_selector) { $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar // Get data for our chart. -$totals = ttChartHelper::getTotals($on_behalf_id, $cl_type, $cl_date, $cl_interval); +$totals = ttChartHelper::getTotals($user_id, $cl_type, $cl_date, $cl_interval); $smarty->assign('totals', $totals); // Prepare chart for drawing. -- 2.20.1