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')) {
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 $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
132 if (count($user_list) > 1) {
133 $chart_form->addInput(array('type'=>'combobox',
134 'onchange'=>'this.form.submit();',
135 'name'=>'onBehalfUser',
136 'value'=>$on_behalf_id,
138 'datakeys'=>array('id','name'),
140 $smarty->assign('on_behalf_control', 1);
144 // Chart interval options.
145 $intervals = array();
146 $intervals[INTERVAL_THIS_DAY] = $i18n->get('dropdown.selected_day');
147 $intervals[INTERVAL_THIS_WEEK] = $i18n->get('dropdown.selected_week');
148 $intervals[INTERVAL_THIS_MONTH] = $i18n->get('dropdown.selected_month');
149 $intervals[INTERVAL_THIS_YEAR] = $i18n->get('dropdown.selected_year');
150 $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time');
152 // Chart interval dropdown.
153 $chart_form->addInput(array('type' => 'combobox',
154 'onchange' => 'if(this.form) this.form.submit();',
155 'name' => 'interval',
156 'value' => $cl_interval,
160 // Chart type options.
161 $chart_selector = (MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl'));
162 if ($chart_selector) {
164 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
165 $types[CHART_PROJECTS] = $i18n->get('dropdown.projects');
166 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
167 $types[CHART_TASKS] = $i18n->get('dropdown.tasks');
168 if ($user->isPluginEnabled('cl'))
169 $types[CHART_CLIENTS] = $i18n->get('dropdown.clients');
171 // Add chart type dropdown.
172 $chart_form->addInput(array('type' => 'combobox',
173 'onchange' => 'if(this.form) this.form.submit();',
181 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
183 // Get data for our chart.
184 $totals = ttChartHelper::getTotals($on_behalf_id, $cl_type, $cl_date, $cl_interval);
185 $smarty->assign('totals', $totals);
187 // Prepare chart for drawing.
189 * We use libchart.php library to draw chart images. It can draw chart labels, too (embed in the image).
190 * But quality of such auto-scaled text is not good. Therefore, we only use libchart to draw a pie-chart picture with
191 * auto-calculated percentage markers around it. We print labels (to the side of the picture) ourselves,
192 * using the same colors libchart is using. For labels printout, the $totals array (which is used for picture points)
193 * is also passed to charts.tpl Smarty template.
195 * To make all of the above possible with only one database call to obtain $totals we have to print the chart image
196 * to a file here (see code below). Once the image is available as a .png file, the charts.tpl can render it.
198 * PieChartEx class is a little extension to libchart-provided PieChart class. It allows us to print the chart
199 * without title, logo, and labels.
201 $chart = new PieChartEx(300, 300);
202 $data_set = new XYDataSet();
203 foreach($totals as $total) {
204 $data_set->addPoint(new Point( $total['name'], $total['time']));
206 $chart->setDataSet($data_set);
208 // Prepare a file name.
209 $img_dir = TEMPLATE_DIR.'_c/'; // Directory.
210 $file_name = uniqid('chart_').'.png'; // Short file name. Unique ID here is to avoid problems with browser caching.
211 $img_ref = 'WEB-INF/templates_c/'.$file_name; // Image reference for html.
212 $file_name = $img_dir.$file_name; // Full file name.
214 // Clean up the file system from older images.
215 $img_files = glob($img_dir.'chart_*.png');
216 foreach($img_files as $file) {
217 // If the create time of file is older than 1 minute, delete it.
218 if (filemtime($file) < (time() - 60)) {
223 // Write chart image to file system.
224 $chart->renderEx(array('fileName'=>$file_name,'hideLogo'=>true,'hideTitle'=>true,'hideLabel'=>true));
225 // At this point libchart usage is complete and we have chart image on disk.
227 $smarty->assign('img_file_name', $img_ref);
228 $smarty->assign('chart_selector', $chart_selector);
229 $smarty->assign('forms', array($chart_form->getName() => $chart_form->toArray()));
230 $smarty->assign('title', $i18n->get('title.charts'));
231 $smarty->assign('content_page_name', 'charts.tpl');
232 $smarty->display('index.tpl');