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 / options / revenue / count.php
vikappointments / admin / layouts / statistics / widgets / options / revenue Last commit date
chart.php 6 days ago count.php 6 days ago index.html 6 days ago items.php 6 days ago table.php 6 days ago
count.php
282 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_ORDERS');
30 JText::script('VAP_N_ORDERS_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 options-revenue-count" id="options-revenue-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 OPTIONS_REVENUE_COUNT === 'undefined') {
63 var OPTIONS_REVENUE_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 // prepare chart data
95 var chartData = {
96 // dataset options
97 datasets: [{
98 // dataset values
99 data: [],
100 // dataset color
101 backgroundColor: [],
102 hoverBorderColor: [],
103 }],
104 // dataset labels
105 labels: [],
106 };
107
108 $.each(data, (optionName, count) => {
109 var label = optionName;
110
111 // get progressive color
112 let color = colorsPreset[colorIndex++ % colorsPreset.length];
113
114 <?php
115 if (isset($data))
116 {
117 ?>
118 // display total if we are printing the document
119 var sublabel = count;
120
121 if (config.valuetype == 'total') {
122 // format value as currency
123 sublabel = VAPCurrency.getInstance().format(count);
124 }
125
126 label += ' (' + sublabel + ')';
127 <?php
128 }
129 ?>
130
131 // push appointments total
132 chartData.datasets[0].data.push(count);
133 // hide highlight on hover
134 chartData.datasets[0].hoverBorderColor.push('#0000');
135 // push label
136 chartData.labels.push(label);
137 // fetch background color
138 chartData.datasets[0].backgroundColor.push('#' + color);
139 });
140
141 // prepare chart configuration
142 var options = {
143 // hide legend
144 legend: {
145 // draw legend only if explicitly requested
146 display: <?php echo !empty($legend) ? 'true' : 'false'; ?>,
147 },
148 // tooltip handling
149 tooltips: {
150 // tooltip callbacks are used to customize default texts
151 callbacks: {
152 // format the tooltip text displayed when hovering a point
153 label: (tooltipItem, data) => {
154 // keep default label
155 var label = data.labels[tooltipItem.index] || '';
156
157 <?php
158 if (!isset($data))
159 {
160 ?>
161 // display formatted appointments only if we are NOT printing the document
162 if (label) {
163 label += ': ';
164
165 var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
166
167 // format as currency in case of earning
168 if (config.valuetype == 'total') {
169 label += VAPCurrency.getInstance().format(value);
170 } else {
171 var count = parseInt(value);
172 var langk = 'VAP_N_ORDERS';
173
174 // format label by fetching singular/plural form
175 if (count == 1) {
176 label += Joomla.JText._(langk + '_1');
177 } else {
178 label += Joomla.JText._(langk).replace(/%d/, count);
179 }
180 }
181 }
182 <?php
183 }
184 ?>
185
186 return ' ' + label;
187 },
188 // change label colors because, by default, the legend background is blank
189 labelColor: (tooltipItem, chart) => {
190 // get tooltip item meta data
191 var meta = chart.data.datasets[tooltipItem.datasetIndex];
192
193 return {
194 // use white border
195 borderColor: 'rgb(0,0,0)',
196 // use same item background color
197 backgroundColor: meta.backgroundColor[tooltipItem.index],
198 };
199 },
200 },
201 },
202 animation: {
203 // unset duration in case we are exporting the chart
204 duration: <?php echo isset($data) ? 0 : 1000; ?>,
205 },
206 // the percentage of the chart that is cut out of the middle
207 cutoutPercentage: 70,
208 };
209
210 // init chart from scratch if NULL
211 if (!OPTIONS_REVENUE_COUNT.hasOwnProperty(id)) {
212 // get 2D canvas for DOUGHNUT chart
213 var canvas = $(widget).find('canvas')[0];
214 var ctx = canvas.getContext('2d');
215
216 // init chart from scratch if undefined
217 OPTIONS_REVENUE_COUNT[id] = new Chart(ctx, {
218 type: 'doughnut',
219 data: chartData,
220 options: options,
221 });
222 } else {
223 // update chart data
224 OPTIONS_REVENUE_COUNT[id].data = chartData;
225
226 // update chart options
227 OPTIONS_REVENUE_COUNT[id].options = options;
228
229 // refresh chart
230 OPTIONS_REVENUE_COUNT[id].update();
231 }
232
233 // update badges
234 if (config.datefrom || config.dateto) {
235 // at least a date was selected, show "from" and "to"
236 $(widget).find('.badge.datefrom').text(config.datefrom ? config.datefrom : '--');
237 $(widget).find('.badge.dateto').text(config.dateto ? config.dateto : '--');
238
239 // hide default range
240 $(widget).find('.badge.range').text('');
241 } else {
242 // hide both empty dates
243 $(widget).find('.badge.datefrom').text('');
244 $(widget).find('.badge.dateto').text('');
245
246 // retrieve selected range text
247 var range = $('select[name="<?php echo $widget->getName() . '_' . $widget->getID(); ?>_range"]')
248 .find('option[value="' + config.range + '"]')
249 .text();
250
251 // show range
252 $(widget).find('.badge.range').text(range);
253 }
254 }
255
256 <?php
257
258 ////////////////////////
259 ///// EXPORT UTILS /////
260 ////////////////////////
261
262 if (isset($data))
263 {
264 ?>
265 $(function() {
266 // auto-invoke callback on page loading completion
267 WIDGET_CALLBACKS[<?php echo $widget->getID(); ?>](
268 // exported assume, there should be only one widget displayed
269 $('#options-revenue-count-<?php echo $widget->getID(); ?>'),
270 // JSON-encode the passed chart data
271 <?php echo json_encode($data); ?>,
272 // JSON-encode the widget configuration
273 <?php echo json_encode($widget->getOptions()); ?>
274 );
275 });
276 <?php
277 }
278 ?>
279 })(jQuery);
280
281 </script>
282