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