vikappointments
/
site
/
helpers
/
libraries
/
statistics
/
widgets
/
packages
/
status
Last commit date
count.php
1 month ago
index.html
1 month ago
count.php
204 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 | * Widget used to draw a pie chart containing the total count of status codes. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | class VAPStatisticsWidgetPackagesStatusCount extends VAPStatisticsWidget |
| 20 | { |
| 21 | /** |
| 22 | * Checks whether the specified group is supported by the widget. |
| 23 | * |
| 24 | * @param string $group The group to check. |
| 25 | * |
| 26 | * @return boolean True if supported, false otherwise. |
| 27 | */ |
| 28 | public function isSupported($group) |
| 29 | { |
| 30 | return empty($group) || $group == '*' || $group == 'dashboard' || $group == 'packages'; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Checks whether the specified user is capable to access this widget. |
| 35 | * |
| 36 | * @param JUser $user The user instance. |
| 37 | * |
| 38 | * @return boolean True if capable, false otherwise. |
| 39 | */ |
| 40 | public function checkPermissions($user) |
| 41 | { |
| 42 | return $user->authorise('core.access.analytics.packages', 'com_vikappointments'); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Override this method to return a configuration form of the widget. |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | public function getForm() |
| 51 | { |
| 52 | return array( |
| 53 | /** |
| 54 | * The initial date to take when a new session starts. |
| 55 | * |
| 56 | * @var select |
| 57 | */ |
| 58 | 'range' => array( |
| 59 | 'type' => 'select', |
| 60 | 'label' => JText::translate('VAP_CHART_INITIAL_RANGE_FIELD'), |
| 61 | 'help' => JText::translate('VAP_CHART_INITIAL_RANGE_FIELD_HELP'), |
| 62 | 'default' => '-6 months', |
| 63 | 'options' => array( |
| 64 | 'all' => JText::translate('VAPMANAGECONFIGCRON4'), |
| 65 | '-1 week' => JText::plural('VAP_LAST_N_WEEKS', 1), |
| 66 | '-2 weeks' => JText::plural('VAP_LAST_N_WEEKS', 2), |
| 67 | '-1 month' => JText::plural('VAP_LAST_N_MONTHS', 1), |
| 68 | '-3 months' => JText::plural('VAP_LAST_N_MONTHS', 3), |
| 69 | '-6 months' => JText::plural('VAP_LAST_N_MONTHS', 6), |
| 70 | '-9 months' => JText::plural('VAP_LAST_N_MONTHS', 9), |
| 71 | '-12 months' => JText::plural('VAP_LAST_N_MONTHS', 12), |
| 72 | ), |
| 73 | ), |
| 74 | |
| 75 | /** |
| 76 | * The initial date of the range. |
| 77 | * |
| 78 | * The parameter is VOLATILE because, every time the session |
| 79 | * ends, we need to restore the field to an empty value, just |
| 80 | * to obtain the current date. |
| 81 | * |
| 82 | * @var date |
| 83 | */ |
| 84 | 'datefrom' => array( |
| 85 | 'type' => 'date', |
| 86 | 'label' => JText::translate('VAPEXPORTRES3'), |
| 87 | 'volatile' => true, |
| 88 | ), |
| 89 | |
| 90 | /** |
| 91 | * The ending date of the range. |
| 92 | * |
| 93 | * The parameter is VOLATILE because, every time the session |
| 94 | * ends, we need to restore the field to an empty value, just |
| 95 | * to obtain the current date. |
| 96 | * |
| 97 | * @var date |
| 98 | */ |
| 99 | 'dateto' => array( |
| 100 | 'type' => 'date', |
| 101 | 'label' => JText::translate('VAPEXPORTRES4'), |
| 102 | 'volatile' => true, |
| 103 | ), |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Checks whether the widget is able to export the fetched data. |
| 109 | * |
| 110 | * @return array A list of supported exportable functions. |
| 111 | */ |
| 112 | public function isExportable() |
| 113 | { |
| 114 | return ['print']; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Loads the dataset(s) that will be recovered asynchronously |
| 119 | * for being displayed within the widget. |
| 120 | * |
| 121 | * It is possible to return an array of records to be passed |
| 122 | * to a chart or directly the HTML to replace. |
| 123 | * |
| 124 | * @return mixed |
| 125 | */ |
| 126 | public function getData() |
| 127 | { |
| 128 | $filters = array(); |
| 129 | $filters['datefrom'] = $this->getOption('datefrom'); |
| 130 | $filters['dateto'] = $this->getOption('dateto'); |
| 131 | $filters['range'] = $this->getOption('range', '-6 months'); |
| 132 | |
| 133 | $tz = JFactory::getUser()->getTimezone(); |
| 134 | |
| 135 | // use default range in case both the specified dates are empty |
| 136 | if (VAPDateHelper::isNull($filters['datefrom']) && VAPDateHelper::isNull($filters['dateto'])) |
| 137 | { |
| 138 | if ($filters['range'] != 'all') |
| 139 | { |
| 140 | // create current date by using the timezone of the logged-in user |
| 141 | $filters['dateto'] = JFactory::getDate('now', $tz); |
| 142 | // set ending date to current one, 1 second before tomorrow |
| 143 | $filters['dateto']->modify('23:59:59'); |
| 144 | |
| 145 | if (strpos($filters['range'], 'months') !== false) |
| 146 | { |
| 147 | // set ending date to the end of the month |
| 148 | $filters['dateto']->modify($filters['dateto']->format('Y-m-t')); |
| 149 | } |
| 150 | |
| 151 | // set starting date to current one at midnight |
| 152 | $filters['datefrom'] = clone $filters['dateto']; |
| 153 | // go to next day to properly calculate the starting date |
| 154 | $filters['datefrom']->modify('tomorrow 00:00:00'); |
| 155 | // subtract given range |
| 156 | $filters['datefrom']->modify($filters['range']); |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | if (!VAPDateHelper::isNull($filters['datefrom'])) |
| 162 | { |
| 163 | // convert specified dates to SQL format |
| 164 | $filters['datefrom'] = JFactory::getDate(VAPDateHelper::getDate($filters['datefrom'], 0, 0, 0), $tz); |
| 165 | } |
| 166 | |
| 167 | if (!VAPDateHelper::isNull($filters['dateto'])) |
| 168 | { |
| 169 | // convert specified dates to SQL format |
| 170 | $filters['dateto'] = JFactory::getDate(VAPDateHelper::getDate($filters['dateto'], 23, 59, 59), $tz); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // import packages helper |
| 175 | VAPLoader::import('libraries.statistics.helpers.packages'); |
| 176 | // fetch statuses count |
| 177 | $data = VAPStatisticsHelperPackages::getStatusesCount($filters['datefrom'], $filters['dateto']); |
| 178 | |
| 179 | return $data; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Create adapter for export method. |
| 184 | * This widget only supports "print" export function. |
| 185 | * |
| 186 | * @param mixed $rule The requested export type. |
| 187 | * |
| 188 | * @return void |
| 189 | */ |
| 190 | public function export($rule = null) |
| 191 | { |
| 192 | // auto-print the document |
| 193 | JHtml::fetch('vaphtml.sitescripts.winprint', 256); |
| 194 | |
| 195 | // auto-fetch the widget data |
| 196 | $data = array( |
| 197 | 'data' => $this->getData(), |
| 198 | ); |
| 199 | |
| 200 | // display widget with fetched data |
| 201 | echo $this->display($data); |
| 202 | } |
| 203 | } |
| 204 |