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');
50 // Initialize and store date in session.
51 $cl_date = $request->getParameter('date', @$_SESSION['date']);
53 $now = new DateAndTime(DB_DATEFORMAT);
54 $cl_date = $now->toString(DB_DATEFORMAT);
56 $_SESSION['date'] = $cl_date;
58 // Initialize chart interval.
59 $cl_interval = $_SESSION['chart_interval'];
61 $sc = new ttSysConfig($user->id);
62 $cl_interval = $sc->getValue(SYSC_CHART_INTERVAL);
64 if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH;
65 $_SESSION['chart_interval'] = $cl_interval;
67 // Initialize chart type.
68 $cl_type = $_SESSION['chart_type'];
70 $sc = new ttSysConfig($user->id);
71 $cl_type = $sc->getValue(SYSC_CHART_TYPE);
73 if (MODE_TIME == $user->tracking_mode) {
74 if ($user->isPluginEnabled('cl'))
75 $cl_type = CHART_CLIENTS;
77 if ($cl_type == CHART_CLIENTS) {
78 if (!$user->isPluginEnabled('cl'))
79 $cl_type = CHART_PROJECTS;
80 } elseif ($cl_type == CHART_TASKS) {
81 if (MODE_PROJECTS_AND_TASKS != $user->tracking_mode)
82 $cl_type = CHART_PROJECTS;
85 if (!$cl_type) $cl_type = CHART_PROJECTS;
86 $_SESSION['chart_type'] = $cl_type;
88 // Who do we draw charts for?
89 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
91 if ($request->isPost()) {
92 // If chart interval changed - save it.
93 $cl_interval = $request->getParameter('interval');
95 // Save in the session
96 $_SESSION['chart_interval'] = $cl_interval;
98 $sc = new ttSysConfig($user->id);
99 $sc->setValue(SYSC_CHART_INTERVAL, $cl_interval);
101 // If chart type changed - save it.
102 $cl_type = $request->getParameter('type');
104 // Save in the session
105 $_SESSION['chart_type'] = $cl_type;
107 $sc = new ttSysConfig($user->id);
108 $sc->setValue(SYSC_CHART_TYPE, $cl_type);
110 // If user has changed - set behalf_id accordingly in the session.
111 if ($request->getParameter('onBehalfUser')) {
112 if($user->can('view_charts')) {
113 unset($_SESSION['behalf_id']);
114 unset($_SESSION['behalf_name']);
116 if($on_behalf_id != $user->id) {
117 $_SESSION['behalf_id'] = $on_behalf_id;
118 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
120 header('Location: charts.php');
126 // Elements of chartForm.
127 $chart_form = new Form('chartForm');
129 // User dropdown. Changes the user "on behalf" of whom we are working.
130 if ($user->can('view_charts')) {
131 if ($user->can('view_own_charts'))
132 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
134 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
135 $user_list = $user->getUsers($options);
136 if (count($user_list) >= 1) {
137 $chart_form->addInput(array('type'=>'combobox',
138 'onchange'=>'this.form.submit();',
139 'name'=>'onBehalfUser',
140 'value'=>$on_behalf_id,
142 'datakeys'=>array('id','name'),
144 $smarty->assign('on_behalf_control', 1);
148 // Chart interval options.
149 $intervals = array();
150 $intervals[INTERVAL_THIS_DAY] = $i18n->get('dropdown.selected_day');
151 $intervals[INTERVAL_THIS_WEEK] = $i18n->get('dropdown.selected_week');
152 $intervals[INTERVAL_THIS_MONTH] = $i18n->get('dropdown.selected_month');
153 $intervals[INTERVAL_THIS_YEAR] = $i18n->get('dropdown.selected_year');
154 $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time');
156 // Chart interval dropdown.
157 $chart_form->addInput(array('type' => 'combobox',
158 'onchange' => 'if(this.form) this.form.submit();',
159 'name' => 'interval',
160 'value' => $cl_interval,
164 // Chart type options.
165 $chart_selector = (MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl'));
166 if ($chart_selector) {
168 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
169 $types[CHART_PROJECTS] = $i18n->get('dropdown.projects');
170 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
171 $types[CHART_TASKS] = $i18n->get('dropdown.tasks');
172 if ($user->isPluginEnabled('cl'))
173 $types[CHART_CLIENTS] = $i18n->get('dropdown.clients');
175 // Add chart type dropdown.
176 $chart_form->addInput(array('type' => 'combobox',
177 'onchange' => 'if(this.form) this.form.submit();',
185 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
187 // Get data for our chart.
188 $totals = ttChartHelper::getTotals($on_behalf_id, $cl_type, $cl_date, $cl_interval);
189 $smarty->assign('totals', $totals);
191 // Prepare chart for drawing.
193 * We use libchart.php library to draw chart images. It can draw chart labels, too (embed in the image).
194 * But quality of such auto-scaled text is not good. Therefore, we only use libchart to draw a pie-chart picture with
195 * auto-calculated percentage markers around it. We print labels (to the side of the picture) ourselves,
196 * using the same colors libchart is using. For labels printout, the $totals array (which is used for picture points)
197 * is also passed to charts.tpl Smarty template.
199 * To make all of the above possible with only one database call to obtain $totals we have to print the chart image
200 * to a file here (see code below). Once the image is available as a .png file, the charts.tpl can render it.
202 * PieChartEx class is a little extension to libchart-provided PieChart class. It allows us to print the chart
203 * without title, logo, and labels.
205 $chart = new PieChartEx(300, 300);
206 $data_set = new XYDataSet();
207 foreach($totals as $total) {
208 $data_set->addPoint(new Point( $total['name'], $total['time']));
210 $chart->setDataSet($data_set);
212 // Prepare a file name.
213 $img_dir = TEMPLATE_DIR.'_c/'; // Directory.
214 $file_name = uniqid('chart_').'.png'; // Short file name. Unique ID here is to avoid problems with browser caching.
215 $img_ref = 'WEB-INF/templates_c/'.$file_name; // Image reference for html.
216 $file_name = $img_dir.$file_name; // Full file name.
218 // Clean up the file system from older images.
219 $img_files = glob($img_dir.'chart_*.png');
220 foreach($img_files as $file) {
221 // If the create time of file is older than 1 minute, delete it.
222 if (filemtime($file) < (time() - 60)) {
227 // Write chart image to file system.
228 $chart->renderEx(array('fileName'=>$file_name,'hideLogo'=>true,'hideTitle'=>true,'hideLabel'=>true));
229 // At this point libchart usage is complete and we have chart image on disk.
231 $smarty->assign('img_file_name', $img_ref);
232 $smarty->assign('chart_selector', $chart_selector);
233 $smarty->assign('forms', array($chart_form->getName() => $chart_form->toArray()));
234 $smarty->assign('title', $i18n->get('title.charts'));
235 $smarty->assign('content_page_name', 'charts.tpl');
236 $smarty->display('index.tpl');