count.php
270 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 | JText::script('VAP_N_ORDERS'); |
| 27 | JText::script('VAP_N_ORDERS_1'); |
| 28 | |
| 29 | ?> |
| 30 | |
| 31 | <style> |
| 32 | |
| 33 | .canvas-align-center { |
| 34 | height: calc(100% - 20px) !important; |
| 35 | } |
| 36 | |
| 37 | </style> |
| 38 | |
| 39 | <div class="canvas-align-center packages-items-count" id="packages-items-count-<?php echo $widget->getID(); ?>"> |
| 40 | <canvas></canvas> |
| 41 | </div> |
| 42 | |
| 43 | <div class="widget-floating-box"> |
| 44 | |
| 45 | <span class="badge badge-info pull-left range"></span> |
| 46 | |
| 47 | <span class="badge badge-info pull-left datefrom" style="margin-right: 4px;"></span> |
| 48 | <span class="badge badge-important pull-left dateto"></span> |
| 49 | |
| 50 | </div> |
| 51 | |
| 52 | <script> |
| 53 | |
| 54 | /** |
| 55 | * Defines a pool of charts, if undefined. |
| 56 | * |
| 57 | * @var object |
| 58 | */ |
| 59 | if (typeof PACKAGES_ITEMS_COUNT === 'undefined') { |
| 60 | var PACKAGES_ITEMS_COUNT = {}; |
| 61 | } |
| 62 | |
| 63 | // init callbacks pool in case the caller didn't declare it |
| 64 | if (typeof WIDGET_CALLBACKS === 'undefined') { |
| 65 | var WIDGET_CALLBACKS = {}; |
| 66 | } |
| 67 | |
| 68 | (function($) { |
| 69 | 'use strict'; |
| 70 | |
| 71 | /** |
| 72 | * Register callback to be executed after |
| 73 | * completing the update request. |
| 74 | * |
| 75 | * @param mixed widget The widget selector. |
| 76 | * @param mixed data The AJAX response. |
| 77 | * @param object config The widget configuration. |
| 78 | * |
| 79 | * @return void |
| 80 | */ |
| 81 | WIDGET_CALLBACKS[<?php echo $widget->getID(); ?>] = (widget, data, config) => { |
| 82 | // get widget ID |
| 83 | var id = $(widget).attr('id'); |
| 84 | |
| 85 | // prepare chart data |
| 86 | var chartData = { |
| 87 | // dataset options |
| 88 | datasets: [{ |
| 89 | // dataset values |
| 90 | data: [], |
| 91 | // dataset color |
| 92 | backgroundColor: [], |
| 93 | hoverBorderColor: [], |
| 94 | }], |
| 95 | // dataset labels |
| 96 | labels: [], |
| 97 | }; |
| 98 | |
| 99 | $.each(data, (k, pack) => { |
| 100 | var label = pack.name; |
| 101 | |
| 102 | <?php |
| 103 | if (isset($data)) |
| 104 | { |
| 105 | ?> |
| 106 | // display total if we are printing the document |
| 107 | var sublabel = pack[config.valuetype]; |
| 108 | |
| 109 | if (config.valuetype == 'total') { |
| 110 | // format value as currency |
| 111 | sublabel = VAPCurrency.getInstance().format(sublabel); |
| 112 | } |
| 113 | |
| 114 | label += ' (' + sublabel + ')'; |
| 115 | <?php |
| 116 | } |
| 117 | ?> |
| 118 | |
| 119 | // push appointments total |
| 120 | chartData.datasets[0].data.push(pack[config.valuetype]); |
| 121 | // hide highlight on hover |
| 122 | chartData.datasets[0].hoverBorderColor.push('#0000'); |
| 123 | // push label |
| 124 | chartData.labels.push(label); |
| 125 | // fetch background color |
| 126 | chartData.datasets[0].backgroundColor.push('#' + pack.color); |
| 127 | }); |
| 128 | |
| 129 | // prepare chart configuration |
| 130 | var options = { |
| 131 | // hide legend |
| 132 | legend: { |
| 133 | // draw legend only if explicitly requested |
| 134 | display: <?php echo !empty($legend) ? 'true' : 'false'; ?>, |
| 135 | }, |
| 136 | // tooltip handling |
| 137 | tooltips: { |
| 138 | // tooltip callbacks are used to customize default texts |
| 139 | callbacks: { |
| 140 | // format the tooltip text displayed when hovering a point |
| 141 | label: (tooltipItem, data) => { |
| 142 | // keep default label |
| 143 | var label = data.labels[tooltipItem.index] || ''; |
| 144 | |
| 145 | <?php |
| 146 | if (!isset($data)) |
| 147 | { |
| 148 | ?> |
| 149 | // display formatted appointments only if we are NOT printing the document |
| 150 | if (label) { |
| 151 | label += ': '; |
| 152 | |
| 153 | var value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index]; |
| 154 | |
| 155 | // format as currency in case of earning |
| 156 | if (config.valuetype == 'total') { |
| 157 | label += VAPCurrency.getInstance().format(value); |
| 158 | } else { |
| 159 | var count = parseInt(value); |
| 160 | var langk = 'VAP_N_ORDERS'; |
| 161 | |
| 162 | // format label by fetching singular/plural form |
| 163 | if (count == 1) { |
| 164 | label += Joomla.JText._(langk + '_1'); |
| 165 | } else { |
| 166 | label += Joomla.JText._(langk).replace(/%d/, count); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | <?php |
| 171 | } |
| 172 | ?> |
| 173 | |
| 174 | return ' ' + label; |
| 175 | }, |
| 176 | // change label colors because, by default, the legend background is blank |
| 177 | labelColor: (tooltipItem, chart) => { |
| 178 | // get tooltip item meta data |
| 179 | var meta = chart.data.datasets[tooltipItem.datasetIndex]; |
| 180 | |
| 181 | return { |
| 182 | // use white border |
| 183 | borderColor: 'rgb(0,0,0)', |
| 184 | // use same item background color |
| 185 | backgroundColor: meta.backgroundColor[tooltipItem.index], |
| 186 | }; |
| 187 | }, |
| 188 | }, |
| 189 | }, |
| 190 | animation: { |
| 191 | // unset duration in case we are exporting the chart |
| 192 | duration: <?php echo isset($data) ? 0 : 1000; ?>, |
| 193 | }, |
| 194 | // the percentage of the chart that is cut out of the middle |
| 195 | cutoutPercentage: 70, |
| 196 | }; |
| 197 | |
| 198 | // init chart from scratch if NULL |
| 199 | if (!PACKAGES_ITEMS_COUNT.hasOwnProperty(id)) { |
| 200 | // get 2D canvas for DOUGHNUT chart |
| 201 | var canvas = $(widget).find('canvas')[0]; |
| 202 | var ctx = canvas.getContext('2d'); |
| 203 | |
| 204 | // init chart from scratch if undefined |
| 205 | PACKAGES_ITEMS_COUNT[id] = new Chart(ctx, { |
| 206 | type: 'doughnut', |
| 207 | data: chartData, |
| 208 | options: options, |
| 209 | }); |
| 210 | } else { |
| 211 | // update chart data |
| 212 | PACKAGES_ITEMS_COUNT[id].data = chartData; |
| 213 | |
| 214 | // update chart options |
| 215 | PACKAGES_ITEMS_COUNT[id].options = options; |
| 216 | |
| 217 | // refresh chart |
| 218 | PACKAGES_ITEMS_COUNT[id].update(); |
| 219 | } |
| 220 | |
| 221 | // update badges |
| 222 | if (config.datefrom || config.dateto) { |
| 223 | // at least a date was selected, show "from" and "to" |
| 224 | $(widget).find('.badge.datefrom').text(config.datefrom ? config.datefrom : '--'); |
| 225 | $(widget).find('.badge.dateto').text(config.dateto ? config.dateto : '--'); |
| 226 | |
| 227 | // hide default range |
| 228 | $(widget).find('.badge.range').text(''); |
| 229 | } else { |
| 230 | // hide both empty dates |
| 231 | $(widget).find('.badge.datefrom').text(''); |
| 232 | $(widget).find('.badge.dateto').text(''); |
| 233 | |
| 234 | // retrieve selected range text |
| 235 | var range = $('select[name="<?php echo $widget->getName() . '_' . $widget->getID(); ?>_range"]') |
| 236 | .find('option[value="' + config.range + '"]') |
| 237 | .text(); |
| 238 | |
| 239 | // show range |
| 240 | $(widget).find('.badge.range').text(range); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | <?php |
| 245 | |
| 246 | //////////////////////// |
| 247 | ///// EXPORT UTILS ///// |
| 248 | //////////////////////// |
| 249 | |
| 250 | if (isset($data)) |
| 251 | { |
| 252 | ?> |
| 253 | $(function() { |
| 254 | // auto-invoke callback on page loading completion |
| 255 | WIDGET_CALLBACKS[<?php echo $widget->getID(); ?>]( |
| 256 | // exported assume, there should be only one widget displayed |
| 257 | $('#packages-items-count-<?php echo $widget->getID(); ?>'), |
| 258 | // JSON-encode the passed chart data |
| 259 | <?php echo json_encode($data); ?>, |
| 260 | // JSON-encode the widget configuration |
| 261 | <?php echo json_encode($widget->getOptions()); ?> |
| 262 | ); |
| 263 | }); |
| 264 | <?php |
| 265 | } |
| 266 | ?> |
| 267 | })(jQuery); |
| 268 | |
| 269 | </script> |
| 270 |