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 / site / helpers / libraries / statistics / widgets / packages / rog.php
vikappointments / site / helpers / libraries / statistics / widgets / packages Last commit date
coupons 5 days ago items 5 days ago payments 5 days ago revenue 5 days ago status 5 days ago index.html 5 days ago overall.php 5 days ago rog.php 5 days ago
rog.php
230 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 fetch the Rate of Growth between 2 months.
16 *
17 * @since 1.7
18 */
19 class VAPStatisticsWidgetPackagesRog extends VAPStatisticsWidget
20 {
21 /**
22 * Internal flag used to check whether the current user
23 * owns enough permissions to access the financial data.
24 *
25 * Use true by default in case the widget is displayed
26 * without checking the permissions.
27 *
28 * @var boolean
29 */
30 protected $hasFinanceAccess = true;
31
32 /**
33 * Checks whether the specified group is supported by the widget.
34 *
35 * @param string $group The group to check.
36 *
37 * @return boolean True if supported, false otherwise.
38 */
39 public function isSupported($group)
40 {
41 return empty($group) || $group == '*' || $group == 'dashboard' || $group == 'packages';
42 }
43
44 /**
45 * Checks whether the specified user is capable to access this widget.
46 *
47 * @param JUser $user The user instance.
48 *
49 * @return boolean True if capable, false otherwise.
50 */
51 public function checkPermissions($user)
52 {
53 // detected finance visibility
54 $this->hasFinanceAccess = $user->authorise('core.access.analytics.finance', 'com_vikappointments');
55
56 return $user->authorise('core.access.analytics.packages', 'com_vikappointments');
57 }
58
59 /**
60 * Returns the widget description.
61 * By default, the description is a translatable string built
62 * in the following format: VAP_STATS_WIDGET_[NAME]_DESC.
63 *
64 * @return string
65 */
66 public function getDescription()
67 {
68 // replicate description used by "Finance - Rog" widget
69 return JText::translate('VAP_STATS_WIDGET_FINANCE_ROG_DESC');
70 }
71
72 /**
73 * Override this method to return a configuration form of the widget.
74 *
75 * @return array
76 */
77 public function getForm()
78 {
79 $form = array(
80 /**
81 * The column to compare (reservations count or total earning).
82 *
83 * @var select
84 */
85 'valuetype' => array(
86 'type' => 'select',
87 'label' => JText::translate('VAP_CHART_VALUE_TYPE_FIELD'),
88 'help' => JText::translate('VAP_CHART_VALUE_TYPE_FIELD_HELP'),
89 'default' => 'total',
90 'options' => array(
91 'total' => JText::translate('VAPREPORTSVALUETYPEOPT1'),
92 'count' => JText::translate('VAPREPORTSVALUETYPEOPT4'),
93 ),
94 ),
95
96 /**
97 * The first month to fetch.
98 *
99 * The parameter is VOLATILE because, every time the session
100 * ends, we need to restore the field to an empty value, just
101 * to obtain the current date.
102 *
103 * @var select
104 */
105 'month1' => array(
106 'type' => 'select',
107 'label' => JText::translate('VAPMANAGERESTRINTERVALMONTH') . ' #1',
108 'default' => JHtml::fetch('date', 'now', 'n'),
109 'volatile' => true,
110 'options' => JHtml::fetch('vikappointments.months'),
111 ),
112
113 /**
114 * The first year to which the selected month belongs.
115 *
116 * The parameter is VOLATILE because, every time the session
117 * ends, we need to restore the field to an empty value, just
118 * to obtain the current date.
119 *
120 * @var select
121 */
122 'year1' => array(
123 'type' => 'select',
124 'label' => JText::translate('VAPMANAGERESTRINTERVALYEAR') . ' #1',
125 'default' => JHtml::fetch('date', 'now', 'Y'),
126 'volatile' => true,
127 'options' => JHtml::fetch('vikappointments.years', -10, 0),
128 ),
129
130 /**
131 * The second month to fetch.
132 *
133 * The parameter is VOLATILE because, every time the session
134 * ends, we need to restore the field to an empty value, just
135 * to obtain the current date.
136 *
137 * @var select
138 */
139 'month2' => array(
140 'type' => 'select',
141 'label' => JText::translate('VAPMANAGERESTRINTERVALMONTH') . ' #2',
142 'default' => JHtml::fetch('date', '-1 month', 'n'),
143 'volatile' => true,
144 'options' => JHtml::fetch('vikappointments.months'),
145 ),
146
147 /**
148 * The first year to which the selected month belongs.
149 *
150 * The parameter is VOLATILE because, every time the session
151 * ends, we need to restore the field to an empty value, just
152 * to obtain the current date.
153 *
154 * @var select
155 */
156 'year2' => array(
157 'type' => 'select',
158 'label' => JText::translate('VAPMANAGERESTRINTERVALYEAR') . ' #2',
159 'default' => JHtml::fetch('date', '-1 month', 'Y'),
160 'volatile' => true,
161 'options' => JHtml::fetch('vikappointments.years', -10, 0),
162 ),
163
164 /**
165 * When enabled, the total earning of the month will be proportionally
166 * estimated depending on the money already earned and the remaining
167 * days (applies only for the current month).
168 *
169 * @var checkbox
170 */
171 'prop' => array(
172 'type' => 'checkbox',
173 'label' => JText::translate('VAP_STATS_WIDGET_FINANCE_ROG_PROP_FIELD'),
174 'help' => JText::translate('VAP_STATS_WIDGET_PACKAGES_ROG_PROP_FIELD_HELP'),
175 'default' => true,
176 ),
177 );
178
179 // check whether the user owns enough permissions to see financial data
180 if (!$this->hasFinanceAccess)
181 {
182 // change type to hidden to avoid its selection
183 $form['valuetype']['type'] = 'hidden';
184 // display by default the total number of packages
185 $form['valuetype']['default'] = 'count';
186 }
187
188 return $form;
189 }
190
191 /**
192 * Loads the dataset(s) that will be recovered asynchronously
193 * for being displayed within the widget.
194 *
195 * It is possible to return an array of records to be passed
196 * to a chart or directly the HTML to replace.
197 *
198 * @return mixed
199 */
200 public function getData()
201 {
202 // fetch selected ranges
203 $a = $this->getOption('year1') . '-' . $this->getOption('month1');
204 $b = $this->getOption('year2') . '-' . $this->getOption('month2');
205
206 if (!$this->hasFinanceAccess)
207 {
208 // always display the total number of packages in case
209 // the user cannot access financial data
210 $filters['valuetype'] = 'count';
211 }
212 else
213 {
214 // get value type from widget configuration
215 $filters['valuetype'] = $this->getOption('valuetype', 'total');
216 }
217
218 // import packages helper
219 VAPLoader::import('libraries.statistics.helpers.packages');
220 // fetch rog
221 $data = VAPStatisticsHelperPackages::getRog($a, $b, $filters['valuetype'], $this->getOption('prop'));
222
223 // replicate dates
224 $data['month1'] = $a;
225 $data['month2'] = $b;
226
227 return $data;
228 }
229 }
230