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 // Note: This script uses Lichart PHP library and requires GD 2.0.1 or later.
31 require_once('initialize.php');
33 import('DateAndTime');
34 import('ttChartHelper');
35 import('ttSysConfig');
37 import('ttUserHelper');
38 import('ttTeamHelper');
41 if (!(ttAccessAllowed('view_own_charts') || ttAccessAllowed('view_charts'))) {
42 header('Location: access_denied.php');
45 if (!$user->isPluginEnabled('ch')) {
46 header('Location: feature_disabled.php');
49 if ($user->behalf_id && (!$user->can('view_charts') || !$user->checkBehalfId())) {
50 header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
53 if (!$user->behalf_id && !$user->can('view_own_charts') && !$user->adjustBehalfId()) {
54 header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to view on behalf.
58 // Initialize and store date in session.
59 $cl_date = $request->getParameter('date', @$_SESSION['date']);
61 $now = new DateAndTime(DB_DATEFORMAT);
62 $cl_date = $now->toString(DB_DATEFORMAT);
64 $_SESSION['date'] = $cl_date;
66 // Initialize chart interval.
67 $cl_interval = $_SESSION['chart_interval'];
69 $sc = new ttSysConfig($user->id);
70 $cl_interval = $sc->getValue(SYSC_CHART_INTERVAL);
72 if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH;
73 $_SESSION['chart_interval'] = $cl_interval;
75 // Initialize chart type.
76 $cl_type = $_SESSION['chart_type'];
78 $sc = new ttSysConfig($user->id);
79 $cl_type = $sc->getValue(SYSC_CHART_TYPE);
81 if (MODE_TIME == $user->tracking_mode) {
82 if ($user->isPluginEnabled('cl'))
83 $cl_type = CHART_CLIENTS;
85 if ($cl_type == CHART_CLIENTS) {
86 if (!$user->isPluginEnabled('cl'))
87 $cl_type = CHART_PROJECTS;
88 } elseif ($cl_type == CHART_TASKS) {
89 if (MODE_PROJECTS_AND_TASKS != $user->tracking_mode)
90 $cl_type = CHART_PROJECTS;
93 if (!$cl_type) $cl_type = CHART_PROJECTS;
94 $_SESSION['chart_type'] = $cl_type;
96 // Who do we draw charts for?
97 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
99 if ($request->isPost()) {
100 // If chart interval changed - save it.
101 $cl_interval = $request->getParameter('interval');
103 // Save in the session
104 $_SESSION['chart_interval'] = $cl_interval;
106 $sc = new ttSysConfig($user->id);
107 $sc->setValue(SYSC_CHART_INTERVAL, $cl_interval);
109 // If chart type changed - save it.
110 $cl_type = $request->getParameter('type');
112 // Save in the session
113 $_SESSION['chart_type'] = $cl_type;
115 $sc = new ttSysConfig($user->id);
116 $sc->setValue(SYSC_CHART_TYPE, $cl_type);
118 // If user has changed - set behalf_id accordingly in the session.
119 if ($request->getParameter('onBehalfUser')) {
120 if($user->can('view_charts')) {
121 unset($_SESSION['behalf_id']);
122 unset($_SESSION['behalf_name']);
124 if($on_behalf_id != $user->id) {
125 $_SESSION['behalf_id'] = $on_behalf_id;
126 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
128 header('Location: charts.php');
134 // Elements of chartForm.
135 $chart_form = new Form('chartForm');
137 // User dropdown. Changes the user "on behalf" of whom we are working.
138 if ($user->can('view_charts')) {
139 if ($user->can('view_own_charts'))
140 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
142 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
143 $user_list = $user->getUsers($options);
144 if (count($user_list) >= 1) {
145 $chart_form->addInput(array('type'=>'combobox',
146 'onchange'=>'this.form.submit();',
147 'name'=>'onBehalfUser',
148 'value'=>$on_behalf_id,
150 'datakeys'=>array('id','name'),
152 $smarty->assign('on_behalf_control', 1);
156 // Chart interval options.
157 $intervals = array();
158 $intervals[INTERVAL_THIS_DAY] = $i18n->get('dropdown.selected_day');
159 $intervals[INTERVAL_THIS_WEEK] = $i18n->get('dropdown.selected_week');
160 $intervals[INTERVAL_THIS_MONTH] = $i18n->get('dropdown.selected_month');
161 $intervals[INTERVAL_THIS_YEAR] = $i18n->get('dropdown.selected_year');
162 $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time');
164 // Chart interval dropdown.
165 $chart_form->addInput(array('type' => 'combobox',
166 'onchange' => 'if(this.form) this.form.submit();',
167 'name' => 'interval',
168 'value' => $cl_interval,
172 // Chart type options.
173 $chart_selector = (MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl'));
174 if ($chart_selector) {
176 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
177 $types[CHART_PROJECTS] = $i18n->get('dropdown.projects');
178 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
179 $types[CHART_TASKS] = $i18n->get('dropdown.tasks');
180 if ($user->isPluginEnabled('cl'))
181 $types[CHART_CLIENTS] = $i18n->get('dropdown.clients');
183 // Add chart type dropdown.
184 $chart_form->addInput(array('type' => 'combobox',
185 'onchange' => 'if(this.form) this.form.submit();',
193 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
195 // Get data for our chart.
196 $totals = ttChartHelper::getTotals($on_behalf_id, $cl_type, $cl_date, $cl_interval);
197 $smarty->assign('totals', $totals);
199 // Prepare chart for drawing.
201 * We use libchart.php library to draw chart images. It can draw chart labels, too (embed in the image).
202 * But quality of such auto-scaled text is not good. Therefore, we only use libchart to draw a pie-chart picture with
203 * auto-calculated percentage markers around it. We print labels (to the side of the picture) ourselves,
204 * using the same colors libchart is using. For labels printout, the $totals array (which is used for picture points)
205 * is also passed to charts.tpl Smarty template.
207 * To make all of the above possible with only one database call to obtain $totals we have to print the chart image
208 * to a file here (see code below). Once the image is available as a .png file, the charts.tpl can render it.
210 * PieChartEx class is a little extension to libchart-provided PieChart class. It allows us to print the chart
211 * without title, logo, and labels.
213 $chart = new PieChartEx(300, 300);
214 $data_set = new XYDataSet();
215 foreach($totals as $total) {
216 $data_set->addPoint(new Point( $total['name'], $total['time']));
218 $chart->setDataSet($data_set);
220 // Prepare a file name.
221 $img_dir = TEMPLATE_DIR.'_c/'; // Directory.
222 $file_name = uniqid('chart_').'.png'; // Short file name. Unique ID here is to avoid problems with browser caching.
223 $img_ref = 'WEB-INF/templates_c/'.$file_name; // Image reference for html.
224 $file_name = $img_dir.$file_name; // Full file name.
226 // Clean up the file system from older images.
227 $img_files = glob($img_dir.'chart_*.png');
228 foreach($img_files as $file) {
229 // If the create time of file is older than 1 minute, delete it.
230 if (filemtime($file) < (time() - 60)) {
235 // Write chart image to file system.
236 $chart->renderEx(array('fileName'=>$file_name,'hideLogo'=>true,'hideTitle'=>true,'hideLabel'=>true));
237 // At this point libchart usage is complete and we have chart image on disk.
239 $smarty->assign('img_file_name', $img_ref);
240 $smarty->assign('chart_selector', $chart_selector);
241 $smarty->assign('forms', array($chart_form->getName() => $chart_form->toArray()));
242 $smarty->assign('title', $i18n->get('title.charts'));
243 $smarty->assign('content_page_name', 'charts.tpl');
244 $smarty->display('index.tpl');