PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / layouts / statistics / widgets / services / employees / count.php
vikappointments / admin / layouts / statistics / widgets / services / employees Last commit date
chart.php 6 days ago count.php 6 days ago index.html 6 days ago
count.php
293 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Layout variables
16 * -----------------
17 * @var VAPStatisticsWidget $widget The instance of the widget to be displayed.
18 * @var mixed $data The chart data to immediately display.
19 * @var boolean $legend True to display the legend, false otherwise.
20 */
21 extract($displayData);
22
23 // include chart JS dependencies
24 JHtml::fetch('vaphtml.assets.chartjs');
25
26 // get list of preset colors
27 $colors = JHtml::fetch('vaphtml.color.preset', $list = true, $group = false);
28
29 JText::script('VAP_N_RESERVATIONS');
30 JText::script('VAP_N_RESERVATIONS_1');
31
32 ?>
33
34 <style>
35
36 .canvas-align-center {
37 height: calc(100% - 20px) !important;
38 }
39
40 </style>
41
42 <div class="canvas-align-center services-employees-count" id="services-employees-count-<?php echo $widget->getID(); ?>">
43 <canvas></canvas>
44 </div>
45
46 <div class="widget-floating-box">
47
48 <span class="badge badge-info pull-left range"></span>
49
50 <span class="badge badge-info pull-left datefrom" style="margin-right: 4px;"></span>
51 <span class="badge badge-important pull-left dateto"></span>
52
53 </div>
54
55 <script>
56
57 /**
58 * Defines a pool of charts, if undefined.
59 *
60 * @var object
61 */
62 if (typeof SERVICES_EMPLOYEES_COUNT === 'undefined') {
63 var SERVICES_EMPLOYEES_COUNT = {};
64 }
65
66 // init callbacks pool in case the caller didn't declare it
67 if (typeof WIDGET_CALLBACKS === 'undefined') {
68 var WIDGET_CALLBACKS = {};
69 }
70
71 (function($) {
72 'use strict';
73
74 // get default system preset of colors
75 const colorsPreset = <?php echo json_encode($colors); ?>;
76
77 /**
78 * Register callback to be executed after
79 * completing the update request.
80 *
81 * @param mixed widget The widget selector.
82 * @param mixed data The AJAX response.
83 * @param object config The widget configuration.
84 *
85 * @return void
86 */
87 WIDGET_CALLBACKS[<?php echo $widget->getID(); ?>] = (widget, data, config) => {
88 // get widget ID
89 var id = $(widget).attr('id');
90
91 // start from first position of the preset
92 let colorIndex = 0;
93
94 // retrieve selected service name
95 var serviceName = $('select[name="<?php echo $widget->getName() . '_' . $widget->getID(); ?>_service"]')
96 .find('option[value="' + config.service + '"]')
97 .text().trim();
98
99 // prepare chart data
100 var chartData = {
101 // dataset options
102 datasets: [{
103 // dataset values
104 data: [],
105 // dataset color
106 backgroundColor: [],
107 hoverBorderColor: [],
108 }],
109 // dataset labels
110 labels: [],
111 };
112
113 $.each(data, (k, employee) => {
114 var label = employee.name;
115
116 // get progressive color
117 employee.color = colorsPreset[colorIndex++ % colorsPreset.length];
118
119 <?php
120 if (isset($data))
121 {
122 ?>
123 // display total if we are printing the document
124 var sublabel = employee.total;
125
126 if (config.valuetype == 'total') {
127 // format value as currency
128 sublabel = VAPCurrency.getInstance().format(employee.total);
129 }
130
131 label += ' (' + sublabel + ')';
132 <?php
133 }
134 ?>
135
136 // push appointments total
137 chartData.datasets[0].data.push(employee.total);
138 // hide highlight on hover
139 chartData.datasets[0].hoverBorderColor.push('#0000');
140 // push label
141 chartData.labels.push(label);
142 // fetch background color
143 chartData.datasets[0].backgroundColor.push('#' + employee.color);
144 });
145
146 // prepare chart configuration
147 var options = {
148 // hide legend
149 legend: {
150 // draw legend only if explicitly requested
151 display: <?php echo !empty($legend) ? 'true' : 'false'; ?>,
152 },
153 // tooltip handling
154 tooltips: {
155 // tooltip callbacks are used to customize default texts
156 callbacks: {
157 // include service name within the tooltip title
158 title: (items, data) => {
159 if (serviceName) {
160 return serviceName;
161 }
162 },
163 // format the tooltip text displayed when hovering a point
164 label: (tooltipItem, data) => {
165 // keep default label
166 var label = data.labels[tooltipItem.index] || '';
167
168 <?php
169 if (!isset($data))
170 {
171 ?>
172 // display formatted appointments only if we are NOT printing the document
173 if (label) {
174 label += ': ';
175
176 var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
177
178 // format as currency in case of earning
179 if (config.valuetype == 'total') {
180 label += VAPCurrency.getInstance().format(value);
181 } else {
182 var count = parseInt(value);
183 var langk = 'VAP_N_RESERVATIONS';
184
185 // format label by fetching singular/plural form
186 if (count == 1) {
187 label += Joomla.JText._(langk + '_1');
188 } else {
189 label += Joomla.JText._(langk).replace(/%d/, count);
190 }
191 }
192 }
193 <?php
194 }
195 ?>
196
197 return ' ' + label;
198 },
199 // change label colors because, by default, the legend background is blank
200 labelColor: (tooltipItem, chart) => {
201 // get tooltip item meta data
202 var meta = chart.data.datasets[tooltipItem.datasetIndex];
203
204 return {
205 // use white border
206 borderColor: 'rgb(0,0,0)',
207 // use same item background color
208 backgroundColor: meta.backgroundColor[tooltipItem.index],
209 };
210 },
211 },
212 },
213 animation: {
214 // unset duration in case we are exporting the chart
215 duration: <?php echo isset($data) ? 0 : 1000; ?>,
216 },
217 // the percentage of the chart that is cut out of the middle
218 cutoutPercentage: 70,
219 };
220
221 // init chart from scratch if NULL
222 if (!SERVICES_EMPLOYEES_COUNT.hasOwnProperty(id)) {
223 // get 2D canvas for DOUGHNUT chart
224 var canvas = $(widget).find('canvas')[0];
225 var ctx = canvas.getContext('2d');
226
227 // init chart from scratch if undefined
228 SERVICES_EMPLOYEES_COUNT[id] = new Chart(ctx, {
229 type: 'doughnut',
230 data: chartData,
231 options: options,
232 });
233 } else {
234 // update chart data
235 SERVICES_EMPLOYEES_COUNT[id].data = chartData;
236
237 // update chart options
238 SERVICES_EMPLOYEES_COUNT[id].options = options;
239
240 // refresh chart
241 SERVICES_EMPLOYEES_COUNT[id].update();
242 }
243
244 // update badges
245 if (config.datefrom || config.dateto) {
246 // at least a date was selected, show "from" and "to"
247 $(widget).find('.badge.datefrom').text(config.datefrom ? config.datefrom : '--');
248 $(widget).find('.badge.dateto').text(config.dateto ? config.dateto : '--');
249
250 // hide default range
251 $(widget).find('.badge.range').text('');
252 } else {
253 // hide both empty dates
254 $(widget).find('.badge.datefrom').text('');
255 $(widget).find('.badge.dateto').text('');
256
257 // retrieve selected range text
258 var range = $('select[name="<?php echo $widget->getName() . '_' . $widget->getID(); ?>_range"]')
259 .find('option[value="' + config.range + '"]')
260 .text();
261
262 // show range
263 $(widget).find('.badge.range').text(range);
264 }
265 }
266
267 <?php
268
269 ////////////////////////
270 ///// EXPORT UTILS /////
271 ////////////////////////
272
273 if (isset($data))
274 {
275 ?>
276 $(function() {
277 // auto-invoke callback on page loading completion
278 WIDGET_CALLBACKS[<?php echo $widget->getID(); ?>](
279 // exported assume, there should be only one widget displayed
280 $('#services-employees-count-<?php echo $widget->getID(); ?>'),
281 // JSON-encode the passed chart data
282 <?php echo json_encode($data); ?>,
283 // JSON-encode the widget configuration
284 <?php echo json_encode($widget->getOptions()); ?>
285 );
286 });
287 <?php
288 }
289 ?>
290 })(jQuery);
291
292 </script>
293