Refactored and renamed ttSysConfig class.
[timetracker.git] / charts.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // Note: This script uses Lichart PHP library and requires GD 2.0.1 or later.
30
31 require_once('initialize.php');
32 import('form.Form');
33 import('DateAndTime');
34 import('ttChartHelper');
35 import('ttUserConfig');
36 import('PieChartEx');
37 import('ttUserHelper');
38 import('ttTeamHelper');
39
40 // Access checks.
41 if (!(ttAccessAllowed('view_own_charts') || ttAccessAllowed('view_charts'))) {
42   header('Location: access_denied.php');
43   exit();
44 }
45 if (!$user->isPluginEnabled('ch')) {
46   header('Location: feature_disabled.php');
47   exit();
48 }
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.
51   exit();
52 }
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.
55   exit();
56 }
57
58 // Initialize and store date in session.
59 $cl_date = $request->getParameter('date', @$_SESSION['date']);
60 if(!$cl_date) {
61   $now = new DateAndTime(DB_DATEFORMAT);
62   $cl_date = $now->toString(DB_DATEFORMAT);
63 }
64 $_SESSION['date'] = $cl_date;
65
66 // Initialize chart interval.
67 $cl_interval = $_SESSION['chart_interval'];
68 if (!$cl_interval) {
69   $uc = new ttUserConfig();
70   $cl_interval = $uc->getValue(SYSC_CHART_INTERVAL);
71 }
72 if (!$cl_interval) $cl_interval = INTERVAL_THIS_MONTH;
73 $_SESSION['chart_interval'] = $cl_interval;
74
75 // Initialize chart type.
76 $cl_type = $_SESSION['chart_type'];
77 if (!$cl_type) {
78   $uc = new ttUserConfig();
79   $cl_type = $uc->getValue(SYSC_CHART_TYPE);
80 }
81 if (MODE_TIME == $user->getTrackingMode()) {
82   if ($user->isPluginEnabled('cl'))
83     $cl_type = CHART_CLIENTS;
84 } else {
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->getTrackingMode())
90       $cl_type = CHART_PROJECTS;
91   }
92 }
93 if (!$cl_type) $cl_type = CHART_PROJECTS;
94 $_SESSION['chart_type'] = $cl_type;
95
96 // Who do we draw charts for?
97 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
98
99 if ($request->isPost()) {
100   // If chart interval changed - save it.
101   $cl_interval = $request->getParameter('interval');
102   if ($cl_interval) {
103     // Save in the session
104     $_SESSION['chart_interval'] = $cl_interval;
105     // and permanently.
106     $uc = new ttUserConfig();
107     $uc->setValue(SYSC_CHART_INTERVAL, $cl_interval);
108   }
109   // If chart type changed - save it.
110   $cl_type = $request->getParameter('type');
111   if ($cl_type) {
112     // Save in the session
113     $_SESSION['chart_type'] = $cl_type;
114     // and permanently.
115     $uc = new ttUserConfig();
116     $uc->setValue(SYSC_CHART_TYPE, $cl_type);
117   }
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']);
123
124       if($on_behalf_id != $user->id) {
125         $_SESSION['behalf_id'] = $on_behalf_id;
126         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
127       }
128       header('Location: charts.php');
129       exit();
130     }
131   }
132 } // isPost
133
134 // Elements of chartForm.
135 $chart_form = new Form('chartForm');
136
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);
141   else
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,
149       'data'=>$user_list,
150       'datakeys'=>array('id','name'),
151     ));
152     $smarty->assign('on_behalf_control', 1);
153   }
154 }
155
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');
163
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,
169   'data' => $intervals
170 ));
171
172 // Chart type options.
173 $chart_selector = (MODE_PROJECTS_AND_TASKS == $user->tracking_mode || $user->isPluginEnabled('cl'));
174 if ($chart_selector) {
175   $types = array();
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');
182
183   // Add chart type dropdown.
184   $chart_form->addInput(array('type' => 'combobox',
185     'onchange' => 'if(this.form) this.form.submit();',
186     'name' => 'type',
187     'value' => $cl_type,
188     'data' => $types
189   ));
190 }
191
192 // Calendar.
193 $chart_form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
194
195 // Get data for our chart.
196 $totals = ttChartHelper::getTotals($on_behalf_id, $cl_type, $cl_date, $cl_interval);
197 $smarty->assign('totals', $totals);
198
199 // Prepare chart for drawing.
200 /*
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.
206  *
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.
209  *
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.
212  */
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']));
217 }
218 $chart->setDataSet($data_set); 
219
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. 
225
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)) {
231     unlink($file);
232   }
233 }
234
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.
238
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');