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') || !$user->isPluginEnabled('ch')) {
42 header('Location: access_denied.php');
46 // Initialize and store date in session.
47 $cl_date = $request->getParameter('date', @$_SESSION['date']);
49 $now = new DateAndTime(DB_DATEFORMAT);
50 $cl_date = $now->toString(DB_DATEFORMAT);
52 $_SESSION['date'] = $cl_date;
54 // Initialize chart interval.
55 $cl_interval = $_SESSION['chart_interval'];
57 $sc = new ttSysConfig($user->id);
58 $cl_interval = $sc->getValue(SYSC_CHART_INTERVAL);
60 if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH;
61 $_SESSION['chart_interval'] = $cl_interval;
63 // Initialize chart type.
64 $cl_type = $_SESSION['chart_type'];
66 $sc = new ttSysConfig($user->id);
67 $cl_type = $sc->getValue(SYSC_CHART_TYPE);
69 if (MODE_TIME == $user->tracking_mode) {
70 if ($user->isPluginEnabled('cl'))
71 $cl_type = CHART_CLIENTS;
73 if ($cl_type == CHART_CLIENTS) {
74 if (!$user->isPluginEnabled('cl'))
75 $cl_type = CHART_PROJECTS;
76 } elseif ($cl_type == CHART_TASKS) {
77 if (MODE_PROJECTS_AND_TASKS != $user->tracking_mode)
78 $cl_type = CHART_PROJECTS;
81 if (!$cl_type) $cl_type = CHART_PROJECTS;
82 $_SESSION['chart_type'] = $cl_type;
84 // Who do we draw charts for?
85 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
87 if ($request->isPost()) {
88 // If chart interval changed - save it.
89 $cl_interval = $request->getParameter('interval');
91 // Save in the session
92 $_SESSION['chart_interval'] = $cl_interval;
94 $sc = new ttSysConfig($user->id);
95 $sc->setValue(SYSC_CHART_INTERVAL, $cl_interval);
97 // If chart type changed - save it.
98 $cl_type = $request->getParameter('type');
100 // Save in the session
101 $_SESSION['chart_type'] = $cl_type;
103 $sc = new ttSysConfig($user->id);
104 $sc->setValue(SYSC_CHART_TYPE, $cl_type);
106 // If user has changed - set behalf_id accordingly in the session.
107 if ($request->getParameter('onBehalfUser')) {
108 if($user->canManageTeam()) {
109 unset($_SESSION['behalf_id']);
110 unset($_SESSION['behalf_name']);
112 if($on_behalf_id != $user->id) {
113 $_SESSION['behalf_id'] = $on_behalf_id;
114 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
116 header('Location: charts.php');
122 // Elements of chartForm.
123 $chart_form = new Form('chartForm');
125 // User dropdown. Changes the user "on behalf" of whom we are working.
126 if ($user->canManageTeam()) {
127 $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
128 if (count($user_list) > 1) {
129 $chart_form->addInput(array('type'=>'combobox',
130 'onchange'=>'this.form.submit();',
131 'name'=>'onBehalfUser',
132 'value'=>$on_behalf_id,
134 'datakeys'=>array('id','name'),
136 $smarty->assign('on_behalf_control', 1);
140 // Chart interval options.
141 $intervals = array();
142 $intervals[INTERVAL_THIS_DAY] = $i18n->get('dropdown.selected_day');
143 $intervals[INTERVAL_THIS_WEEK] = $i18n->get('dropdown.selected_week');
144 $intervals[INTERVAL_THIS_MONTH] = $i18n->get('dropdown.selected_month');
145 $intervals[INTERVAL_THIS_YEAR] = $i18n->get('dropdown.selected_year');
146 $intervals[INTERVAL_ALL_TIME] = $i18n->get('dropdown.all_time');
148 // Chart interval dropdown.
149 $chart_form->addInput(array('type' => 'combobox',
150 'onchange' => 'if(this.form) this.form.submit();',
151 'name' => 'interval',
152 'value' => $cl_interval,
156 // Chart type options.
157 $chart_selector = (MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl'));
158 if ($chart_selector) {
160 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
161 $types[CHART_PROJECTS] = $i18n->get('dropdown.projects');
162 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
163 $types[CHART_TASKS] = $i18n->get('dropdown.tasks');
164 if ($user->isPluginEnabled('cl'))
165 $types[CHART_CLIENTS] = $i18n->get('dropdown.clients');
167 // Add chart type dropdown.
168 $chart_form->addInput(array('type' => 'combobox',
169 'onchange' => 'if(this.form) this.form.submit();',
177 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
179 // Get data for our chart.
180 $totals = ttChartHelper::getTotals($on_behalf_id, $cl_type, $cl_date, $cl_interval);
181 $smarty->assign('totals', $totals);
183 // Prepare chart for drawing.
185 * We use libchart.php library to draw chart images. It can draw chart labels, too (embed in the image).
186 * But quality of such auto-scaled text is not good. Therefore, we only use libchart to draw a pie-chart picture with
187 * auto-calculated percentage markers around it. We print labels (to the side of the picture) ourselves,
188 * using the same colors libchart is using. For labels printout, the $totals array (which is used for picture points)
189 * is also passed to charts.tpl Smarty template.
191 * To make all of the above possible with only one database call to obtain $totals we have to print the chart image
192 * to a file here (see code below). Once the image is available as a .png file, the charts.tpl can render it.
194 * PieChartEx class is a little extension to libchart-provided PieChart class. It allows us to print the chart
195 * without title, logo, and labels.
197 $chart = new PieChartEx(300, 300);
198 $data_set = new XYDataSet();
199 foreach($totals as $total) {
200 $data_set->addPoint(new Point( $total['name'], $total['time']));
202 $chart->setDataSet($data_set);
204 // Prepare a file name.
205 $img_dir = TEMPLATE_DIR.'_c/'; // Directory.
206 $file_name = uniqid('chart_').'.png'; // Short file name. Unique ID here is to avoid problems with browser caching.
207 $img_ref = 'WEB-INF/templates_c/'.$file_name; // Image reference for html.
208 $file_name = $img_dir.$file_name; // Full file name.
210 // Clean up the file system from older images.
211 $img_files = glob($img_dir.'chart_*.png');
212 foreach($img_files as $file) {
213 // If the create time of file is older than 1 minute, delete it.
214 if (filemtime($file) < (time() - 60)) {
219 // Write chart image to file system.
220 $chart->renderEx(array('fileName'=>$file_name,'hideLogo'=>true,'hideTitle'=>true,'hideLabel'=>true));
221 // At this point libchart usage is complete and we have chart image on disk.
223 $smarty->assign('img_file_name', $img_ref);
224 $smarty->assign('chart_selector', $chart_selector);
225 $smarty->assign('forms', array($chart_form->getName() => $chart_form->toArray()));
226 $smarty->assign('title', $i18n->get('title.charts'));
227 $smarty->assign('content_page_name', 'charts.tpl');
228 $smarty->display('index.tpl');