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