PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / html / sitescripts.php
vikappointments / site / helpers / libraries / html Last commit date
assets.php 1 month ago color.php 1 month ago countries.php 1 month ago index.html 1 month ago media.php 1 month ago site.php 1 month ago sitescripts.php 1 month ago status.php 1 month ago vikappointments.php 1 month ago
sitescripts.php
382 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 * VikAppointments HTML site scripts helper.
16 *
17 * @since 1.7
18 */
19 abstract class VAPHtmlSitescripts
20 {
21 /**
22 * Declares the statement that will be used to initialize a standard datepicker.
23 *
24 * @param string $selector The datepicker selector.
25 * @param array $options An array of options to be used while creating the
26 * jQuery datepicker.
27 *
28 * @return void
29 */
30 public static function calendar($selector, array $options = array())
31 {
32 $config = VAPFactory::getConfig();
33
34 // get date format
35 $date_format = $config->get('dateformat');
36
37 // attach regional jQuery datepicker first
38 VikAppointments::load_datepicker_regional();
39
40 // stringify options for JS usage
41 $json = json_encode($options);
42
43 $js =
44 <<<JS
45 jQuery(function($) {
46 // check if we have a mobile or a tablet
47 if (window.matchMedia && window.matchMedia("only screen and (max-width: 760px)").matches) {
48 // prevent keyboard easing, by blurring the field every time it gets focused
49 $('{$selector}')
50 .attr('autocomplete', 'off')
51 .attr('onfocus', 'this.blur()');
52 }
53
54 var format = "{$date_format}";
55 var separator = format[1];
56
57 // strip any separators from date format
58 format = format.replace(/[^a-z]/gi, '');
59
60 switch (format) {
61 case 'Ymd':
62 format = 'yy' + separator + 'mm' + separator + 'dd';
63 break;
64
65 case 'mdY':
66 format = 'mm' + separator + 'dd' + separator + 'yy';
67 break;
68
69 default:
70 format = 'dd' + separator + 'mm' + separator + 'yy';
71 }
72
73 let options = {$json};
74 // set date format
75 options.dateFormat = format;
76
77 $('{$selector}').datepicker(options);
78 });
79 JS
80 ;
81
82 // add js to document head
83 JFactory::getDocument()->addScriptDeclaration($js);
84 }
85
86 /**
87 * Declares a list of functions that can be used to manage the items
88 * inside the cart, useful for the appointment confirmation page and
89 * the cart module.
90 *
91 * @return void
92 */
93 public static function cart()
94 {
95 static $loaded = 0;
96
97 if ($loaded)
98 {
99 // do not load again
100 return;
101 }
102
103 $loaded = 1;
104
105 // use current Item ID for correct routing
106 $itemid = JFactory::getApplication()->input->getUint('Itemid');
107
108 $vik = VAPApplication::getInstance();
109
110 // create AJAX URL for remove item end-point
111 $remove_item_url = $vik->ajaxUrl('index.php?option=com_vikappointments&task=cart.removeitem' . ($itemid ? '&Itemid=' . $itemid : ''));
112
113 // create AJAX URL for add option end-point
114 $add_option_url = $vik->ajaxUrl('index.php?option=com_vikappointments&task=cart.addoption' . ($itemid ? '&Itemid=' . $itemid : ''));
115
116 // create AJAX URL for add option end-point
117 $remove_option_url = $vik->ajaxUrl('index.php?option=com_vikappointments&task=cart.removeoption' . ($itemid ? '&Itemid=' . $itemid : ''));
118
119 // register generic error message
120 JText::script('VAPWAITLISTADDED0');
121
122 $js =
123 <<<JS
124 function vapRemoveCartItemRequest(id_service, id_employee, checkin) {
125 // prepare request argument
126 const args = {
127 id_ser: id_service,
128 id_emp: id_employee,
129 checkin: checkin,
130 };
131
132 return new Promise((resolve, reject) => {
133 UIAjax.do(
134 '{$remove_item_url}',
135 args,
136 (resp) => {
137 // resolve promise
138 resolve(resp);
139
140 // inject received parameters within the event to dispatch
141 const event = jQuery.Event('cart.removeitem');
142 // merge response with request arguments
143 event.params = Object.assign(resp, args);
144
145 // trigger event to notify any subscriber
146 jQuery(window).trigger(event);
147 },
148 (err) => {
149 // reject promise
150 reject(err.responseText || Joomla.JText._('VAPWAITLISTADDED0'));
151 }
152 );
153 });
154 }
155
156 function vapAddCartOptionRequest(id_option, id_service, id_employee, checkin, units) {
157 // prepare request argument
158 const args = {
159 id_opt: id_option,
160 id_ser: id_service,
161 id_emp: id_employee,
162 checkin: checkin,
163 units: typeof units === 'undefined' ? 1 : units,
164 };
165
166 return new Promise((resolve, reject) => {
167 UIAjax.do(
168 '{$add_option_url}',
169 args,
170 (resp) => {
171 // resolve promise
172 resolve(resp);
173
174 // inject received parameters within the event to dispatch
175 const event = jQuery.Event('cart.addoption');
176 // merge response with request arguments
177 event.params = Object.assign(resp, args);
178
179 // trigger event to notify any subscriber
180 jQuery(window).trigger(event);
181 },
182 (err) => {
183 // reject promise
184 reject(err.responseText || Joomla.JText._('VAPWAITLISTADDED0'));
185 }
186 );
187 });
188 }
189
190 function vapRemoveCartOptionRequest(id_option, id_service, id_employee, checkin, units) {
191 // prepare request argument
192 const args = {
193 id_opt: id_option,
194 id_ser: id_service,
195 id_emp: id_employee,
196 checkin: checkin,
197 units: typeof units === 'undefined' ? 1 : units,
198 };
199
200 return new Promise((resolve, reject) => {
201 UIAjax.do(
202 '{$remove_option_url}',
203 args,
204 (resp) => {
205 // resolve promise
206 resolve(resp);
207
208 // inject received parameters within the event to dispatch
209 const event = jQuery.Event('cart.removeoption');
210 // merge response with request arguments
211 event.params = Object.assign(resp, args);
212
213 // trigger event to notify any subscriber
214 jQuery(window).trigger(event);
215 },
216 (err) => {
217 // reject promise
218 reject(err.responseText || Joomla.JText._('VAPWAITLISTADDED0'));
219 }
220 );
221 });
222 }
223 JS
224 ;
225
226 // add js to document head
227 JFactory::getDocument()->addScriptDeclaration($js);
228 }
229
230 /**
231 * Animates the document in case the specified selector
232 * is currently not visible within the screen.
233 *
234 * @param mixed $selector The page will be animated as long as the
235 * specified element is not on top of the page.
236 * @param integer $maring An optional margin to use as threshold.
237 *
238 * @return void
239 */
240 public static function animate($selector = null, $margin = 20)
241 {
242 /**
243 * Check whether the pages animation has been disabled.
244 * It is possible safely disable the animation of the
245 * pages by inserting a new record within the configuration
246 * database table of VikAppointments.
247 *
248 * INSERT INTO `#__vikappointments_config` (`param`, `setting`)
249 * VALUES ('animatepages', 0);
250 */
251 $disabled = VAPFactory::getConfig()->getBool('animatepages', true);
252
253 if (!$disabled)
254 {
255 // pages animation has been disabled globally
256 return;
257 }
258
259 if (!$selector)
260 {
261 // use default "main" container, which seems to be the
262 // default identifier for both Joomla and WP platforms
263 $selector = '#main';
264 }
265
266 // use a valid margin
267 $margin = (int) $margin;
268
269 JFactory::getDocument()->addScriptDeclaration(
270 <<<JS
271 jQuery(function($) {
272 // flag used to check whether the page
273 // has been scrolled before executing the
274 // animation
275 var hasScrolled = false;
276
277 var scrollDetector = function() {
278 hasScrolled = true;
279
280 // self turn off scroll detector
281 $(window).off('scroll', scrollDetector);
282 }
283
284 $(window).on('scroll', scrollDetector);
285
286 onDocumentReady().then(function() {
287 // get element
288 var elem = $('$selector');
289
290 if (elem.length == 0 && '$selector' == '#main') {
291 // the template is not using the default notation,
292 // try to observe the beginning of VikAppointments
293 elem = $('.vikappointments-start-body');
294 }
295
296 // make sure the page hasn't been already scrolled in order to
297 // avoid debounces, then make sure the element exists and it is not visible
298 if (!hasScrolled && elem.length && isBoxOutOfMonitor(elem, $margin)) {
299 $('html, body').animate({
300 scrollTop: elem.offset().top - $margin,
301 });
302 }
303 });
304 });
305 JS
306 );
307 }
308
309 /**
310 * Includes the script to trigger the browser print function
311 * after completing the page loading.
312 *
313 * @param integer $delay The number of milliseconds to wait.
314 *
315 * @return void
316 */
317 public static function winprint($delay = null)
318 {
319 // at least wait 1 ms
320 $delay = max(array(1, (int) $delay));
321
322 // include script for document printing
323 JFactory::getDocument()->addScriptDeclaration(
324 <<<JS
325 (function($) {
326 'use strict';
327
328 $(function() {
329 setTimeout(() => {
330 window.print();
331 }, $delay);
332 });
333 })(jQuery);
334 JS
335 );
336 }
337
338 /**
339 * Auto set CSRF token to ajaxSetup so all jQuery ajax call will contains CSRF token.
340 *
341 * @return void
342 *
343 * @see JHtmlJquery::csrf()
344 */
345 public static function ajaxcsrf($name = 'csrf.token')
346 {
347 static $loaded = 0;
348
349 if ($loaded)
350 {
351 // do not load again
352 return;
353 }
354
355 $loaded = 1;
356
357 try
358 {
359 // rely on system helper
360 JHtml::fetch('jquery.token');
361 }
362 catch (Exception $e)
363 {
364 // Helper not declared, installed CMS too old (lower than J3.8).
365 // Fallback to our internal helper.
366 $csrf = addslashes(JSession::getFormToken());
367
368 JFactory::getDocument()->addScriptDeclaration(
369 <<<JS
370 ;(function($) {
371 $.ajaxSetup({
372 headers: {
373 'X-CSRF-Token': '{$csrf}',
374 },
375 });
376 })(jQuery);
377 JS
378 );
379 }
380 }
381 }
382