PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.2
VikBooking Hotel Booking Engine & PMS v1.7.2
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / admin_widget.php

admin_widget.php in VikBooking Hotel Booking Engine & PMS 1.7.2, at admin/helpers/admin_widget.php

662 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 * @link https://vikwp.com
9 */
10
11 defined('ABSPATH') or die('No script kiddies please!');
12
13 /**
14 * Admin Widget parent Class of all sub-classes.
15 *
16 * @since 1.14 (J) - 1.4.0 (WP)
17 */
18 abstract class VikBookingAdminWidget
19 {
20 /**
21 * The name of the widget.
22 *
23 * @var string
24 */
25 protected $widgetName = null;
26
27 /**
28 * The description of the widget.
29 *
30 * @var string
31 */
32 protected $widgetDescr = '';
33
34 /**
35 * The icon of the widget in HTML.
36 *
37 * @var string
38 *
39 * @since 1.15.0 (J) - 1.5.0 (WP)
40 */
41 protected $widgetIcon = '';
42
43 /**
44 * The style name of the widget.
45 *
46 * @var string
47 *
48 * @since 1.15.0 (J) - 1.5.0 (WP)
49 */
50 protected $widgetStyleName = 'regular';
51
52 /**
53 * The widget settings.
54 *
55 * @var mixed
56 */
57 protected $widgetSettings = null;
58
59 /**
60 * The VBO application object.
61 *
62 * @var object
63 */
64 protected $vbo_app = null;
65
66 /**
67 * The date format.
68 *
69 * @var string
70 */
71 protected $df = '';
72
73 /**
74 * The date separator.
75 *
76 * @var string
77 */
78 protected $datesep = '';
79
80 /**
81 * Whether the multitask panel
82 * is invoking the widget.
83 *
84 * @var bool
85 */
86 protected $is_multitask = false;
87
88 /**
89 * The widget identifier.
90 *
91 * @var string
92 */
93 protected $widgetId = null;
94
95 /**
96 * The widget multitask options registry.
97 *
98 * @var VBOMultitaskOptions
99 *
100 * @since 1.16.5 (J) - 1.6.5 (WP)
101 */
102 protected $options;
103
104 /**
105 * Class constructors should define some vars for the widget in use.
106 */
107 public function __construct()
108 {
109 $this->vbo_app = VikBooking::getVboApplication();
110 $this->datesep = VikBooking::getDateSeparator(true);
111
112 $nowdf = VikBooking::getDateFormat(true);
113 if ($nowdf == "%d/%m/%Y") {
114 $this->df = 'd/m/Y';
115 } elseif ($nowdf == "%m/%d/%Y") {
116 $this->df = 'm/d/Y';
117 } else {
118 $this->df = 'Y/m/d';
119 }
120
121 $this->options = VBOMultitaskOptions::getInstance();
122 }
123
124 /**
125 * Tells if the admin-widget can be installed on the current environment.
126 *
127 * @return bool
128 *
129 * @since 1.16.10 (J) - 1.6.10 (WP)
130 */
131 public function preflight()
132 {
133 return true;
134 }
135
136 /**
137 * Returns the admin-widget priority for a custom ordering.
138 *
139 * @return int
140 *
141 * @since 1.16.10 (J) - 1.6.10 (WP)
142 */
143 public function getPriority()
144 {
145 return 10;
146 }
147
148 /**
149 * Gets the name of the current widget.
150 *
151 * @return string the widget name.
152 */
153 public function getName()
154 {
155 return $this->widgetName;
156 }
157
158 /**
159 * Gets the description for the current widget.
160 *
161 * @return string the widget description.
162 */
163 public function getDescription()
164 {
165 return $this->widgetDescr;
166 }
167
168 /**
169 * Gets the icon for the current widget.
170 *
171 * @return string the widget HTML string icon.
172 */
173 public function getIcon()
174 {
175 return !empty($this->widgetIcon) ? $this->widgetIcon : $this->getDefaultIcon();
176 }
177
178 /**
179 * Gets the style name for the current widget.
180 *
181 * @return string the widget style name.
182 */
183 public function getStyleName()
184 {
185 return $this->widgetStyleName;
186 }
187
188 /**
189 * Gets the identifier of the current widget.
190 *
191 * @return string the widget identifier.
192 */
193 public function getIdentifier()
194 {
195 if (!$this->widgetId) {
196 // fetch widget ID from class name
197 $this->widgetId = preg_replace("/^VikBookingAdminWidget/i", '', get_class($this));
198 // place an underscore between each camelCase
199 $this->widgetId = strtolower(preg_replace("/([a-z])([A-Z])/", '$1_$2', $this->widgetId));
200 $this->widgetId = strtolower($this->widgetId);
201 }
202
203 return $this->widgetId;
204 }
205
206 /**
207 * Turns on/off the flag to detect multitask rendering.
208 *
209 * @param bool $in true if in multitask panel, or false.
210 *
211 * @return self
212 *
213 * @since 1.15.0 (J) - 1.5.0 (WP)
214 */
215 public function setInMultitask($in = false)
216 {
217 $this->is_multitask = (bool)$in;
218
219 return $this;
220 }
221
222 /**
223 * Default method to preload the necessary assets of the widget.
224 * Extending classes needing certain assets to be available should
225 * override this method and let it load the CSS/JS assets.
226 * Useful to avoid JS errors during the AJAX generation of a widget.
227 *
228 * The method can also be helpful for a widget to register watch data.
229 * For example, the last ID retrieved of a certain record can be used
230 * to receive it back during the periodic data-watching and be able
231 * to detect if a new event has occurred to fire a browser notification.
232 *
233 * @return mixed watch-data object or null.
234 *
235 * @since 1.15.0 (J) - 1.5.0 (WP)
236 */
237 public function preload()
238 {
239 return null;
240 }
241
242 /**
243 * Default method to return a list of watch-data and new browser notifications.
244 * Extending classes needing to periodically watch for new events and capable
245 * of emitting browser notifications, can override this method. The last data
246 * to watch should be returned during the preloading. To be used with list().
247 *
248 * @param VBONotificationWatchdata $watch_data the preloaded watch-data object.
249 *
250 * @return array data object to watch next and notifications array.
251 *
252 * @see preload()
253 *
254 * @since 1.15.0 (J) - 1.5.0 (WP)
255 */
256 public function getNotifications(VBONotificationWatchdata $watch_data = null)
257 {
258 $watch_next = null;
259 $notifications = null;
260
261 return [$watch_next, $notifications];
262 }
263
264 /**
265 * Default method to return a list of browser events to dispatch.
266 * Extending classes needing to periodically watch for new events
267 * and capable of emitting browser events, can override this method.
268 *
269 * @param VBONotificationWatchdata $watch_data the preloaded watch-data object.
270 *
271 * @return array list of browser events to emit.
272 *
273 * @see preload()
274 *
275 * @since 1.16.8 (J) - 1.6.8 (WP)
276 */
277 public function getNotificationEvents(VBONotificationWatchdata $watch_data = null)
278 {
279 return [];
280 }
281
282 /**
283 * Binds the widget options at runtime when multitask data is available.
284 * Options live on a separate layer than regular multitask data.
285 *
286 * @param VBOMultitaskData|VBOMultitaskOptions|array|object $options options to bind.
287 *
288 * @return self
289 *
290 * @since 1.16.5 (J) - 1.6.5 (WP)
291 */
292 public function bindOptions($options)
293 {
294 if ($options instanceof VBOMultitaskData) {
295 // set options by extracting them from multitask data
296 $this->options->setProperties($options->getDataOptions());
297 } elseif ($options instanceof VBOMultitaskOptions) {
298 // replace options with given instance
299 $this->options = $options;
300 } elseif (is_array($options) || is_object($options)) {
301 // bind new options
302 $this->options = VBOMultitaskOptions::getInstance($options);
303 }
304
305 return $this;
306 }
307
308 /**
309 * Gets all widget options previously set through Multitask data.
310 * Options live on a separate layer than regular Multitask data.
311 *
312 * @param bool $public true to get only the public options.
313 *
314 * @return array associative list of options, if any.
315 *
316 * @since 1.16.5 (J) - 1.6.5 (WP)
317 */
318 public function getOptions($public = false)
319 {
320 return $this->options->getProperties($public);
321 }
322
323 /**
324 * Proxy to access the multitask options object.
325 *
326 * @return VBOMultitaskOptions
327 *
328 * @since 1.16.5 (J) - 1.6.5 (WP)
329 */
330 public function options()
331 {
332 return $this->options;
333 }
334
335 /**
336 * Sets a widget option value.
337 *
338 * @param string $key the option key identifier.
339 * @param mixed $value the option value to set.
340 *
341 * @return self
342 *
343 * @since 1.16.5 (J) - 1.6.5 (WP)
344 */
345 public function setOption($key, $value)
346 {
347 $this->options->set($key, $value);
348
349 return $this;
350 }
351
352 /**
353 * Gets the value for the given widget option key.
354 *
355 * @param string $key the option key identifier.
356 * @param mixed $default the default value to get.
357 *
358 * @return mixed
359 *
360 * @since 1.16.5 (J) - 1.6.5 (WP)
361 */
362 public function getOption($key, $default = null)
363 {
364 return $this->options->get($key, $default);
365 }
366
367 /**
368 * Returns an associative array with the current widget details.
369 *
370 * @return array
371 *
372 * @since 1.16.7 (J) - 1.6.7 (WP)
373 */
374 public function getWidgetDetails()
375 {
376 return [
377 'id' => $this->getIdentifier(),
378 'name' => $this->getName(),
379 'descr' => $this->getDescription(),
380 'icon' => $this->getIcon(),
381 'style' => $this->getStyleName(),
382 ];
383 }
384
385 /**
386 * Extending Classes should define this method to render the actual
387 * output of the admin widget. Multitask data can be passed along.
388 *
389 * @param VBOMultitaskData $data optional multitask data object.
390 *
391 * @since 1.15.0 (J) - 1.5.0 (WP) type hint added for $data argument.
392 */
393 abstract public function render(VBOMultitaskData $data = null);
394
395 /**
396 * Returns the default icon for a widget.
397 *
398 * @return string the default widget icon HTML string.
399 */
400 protected function getDefaultIcon()
401 {
402 return '<i class="' . VikBookingIcons::i('cube') . '"></i>';
403 }
404
405 /**
406 * Tells the widget if its being rendered via AJAX.
407 *
408 * @return bool true if rendering is being made via AJAX.
409 */
410 protected function isAjaxRendering()
411 {
412 $widget_id = VikRequest::getString('widget_id', '', 'request');
413 $call = VikRequest::getString('call', '', 'request');
414
415 return ($widget_id == $this->getIdentifier() && $call == 'render');
416 }
417
418 /**
419 * Tells the widget if its being rendered in the multitask panel.
420 * This kind of rendering could be in an AJAX context or in a regular
421 * loading flow. Use the apposite method to see if it's an AJAX rendering.
422 *
423 * @return bool true if rendering is handled by the multitask panel.
424 *
425 * @since 1.15.0 (J) - 1.5.0 (WP)
426 */
427 protected function isMultitaskRendering()
428 {
429 $multitask = VikRequest::getInt('multitask', 0, 'request');
430
431 return ($multitask > 0 || $this->is_multitask === true);
432 }
433
434 /**
435 * Tells the widget if VCM is available.
436 *
437 * @return bool true if Vik Channel Manager is available.
438 *
439 * @since 1.16.0 (J) - 1.6.0 (WP)
440 */
441 protected function hasChannelManager()
442 {
443 return is_file(VCM_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikchannelmanager.php');
444 }
445
446 /**
447 * Gets the configuration parameter name for the widget's settings.
448 *
449 * @return string the param name of the settings record.
450 */
451 protected function getSettingsParamName()
452 {
453 return 'admin_widget_' . $this->getIdentifier();
454 }
455
456 /**
457 * Loads the widget's settings from the configuration table, if any.
458 * If no record found for this widget, an empty record will be inserted.
459 *
460 * @return mixed the widget settings.
461 */
462 protected function loadSettings()
463 {
464 $dbo = JFactory::getDbo();
465
466 $param_name = $this->getSettingsParamName();
467
468 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name);
469 $dbo->setQuery($q, 0, 1);
470 $settings = $dbo->loadResult();
471
472 if (!$settings) {
473 return null;
474 }
475
476 if (in_array(substr($settings, 0, 1), array('{', '['))) {
477 // we have detected a JSON string, try to decoded it
478 $decoded = json_decode($settings);
479 if (function_exists('json_last_error') && json_last_error()) {
480 // json is broken, reset settings and return null
481 $this->resetSettings();
482 return null;
483 }
484 // return the decoded settings
485 return $decoded;
486 }
487
488 // return the plain db value otherwise
489 return $settings;
490 }
491
492 /**
493 * Updates the widget's settings in the configuration table.
494 *
495 * @param mixed $data the settings to store, must be a scalar.
496 *
497 * @return bool true on success, false otherwise.
498 */
499 protected function updateSettings($data)
500 {
501 if ($data === null || !is_scalar($data)) {
502 return false;
503 }
504
505 $param_name = $this->getSettingsParamName();
506
507 $config = VBOFactory::getConfig();
508 $config->set($param_name, $data);
509
510 return true;
511 }
512
513 /**
514 * Resets the settings of the widget.
515 *
516 * @return bool
517 */
518 public function resetSettings()
519 {
520 $dbo = JFactory::getDbo();
521
522 $param_name = $this->getSettingsParamName();
523
524 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";";
525 $dbo->setQuery($q);
526 $dbo->execute();
527 if (!$dbo->getNumRows()) {
528 // settings never used
529 return false;
530 }
531
532 $q = "DELETE FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";";
533 $dbo->setQuery($q);
534 $dbo->execute();
535
536 return true;
537 }
538
539 /**
540 * Method invoked during AJAX requests for removing an instance of this widget.
541 * By default we try to unset the passed instance index from the settings array.
542 * If the widget does not use settings, nothing is done. Widgets could override this method.
543 *
544 * @return bool true if settings needed to be updated, false otherwise.
545 */
546 public function removeInstance()
547 {
548 $settings = $this->loadSettings();
549 if ($settings === null || !is_array($settings)) {
550 return false;
551 }
552
553 $widget_instance = VikRequest::getInt('widget_instance', -1, 'request');
554
555 if (!isset($settings[$widget_instance])) {
556 // settings index not found
557 return false;
558 }
559
560 // splice the array to remove the requested settings instance
561 array_splice($settings, $widget_instance, 1);
562
563 // update widget's settings
564 $this->updateSettings(json_encode($settings));
565
566 return true;
567 }
568
569 /**
570 * Method invoked during AJAX requests for moving an instance of this widget.
571 * This occurs when dragging and dropping the same type of widget to a different position.
572 * If the widget does not use settings, nothing is done. Widgets could override this method.
573 *
574 * @return bool true if settings needed to be updated, false otherwise.
575 */
576 public function sortInstance()
577 {
578 $settings = $this->loadSettings();
579 if ($settings === null || !is_array($settings)) {
580 return false;
581 }
582
583 $widget_index_old = VikRequest::getInt('widget_index_old', -1, 'request');
584 $widget_index_new = VikRequest::getInt('widget_index_new', -1, 'request');
585
586 if (!isset($settings[$widget_index_old]) || !isset($settings[$widget_index_new])) {
587 // settings index not found
588 return false;
589 }
590
591 // move the settings requested from the old index to the new index
592 $extracted = array_splice($settings, $widget_index_old, 1);
593 array_splice($settings, $widget_index_new, 0, $extracted);
594
595 // update widget's settings
596 $this->updateSettings(json_encode($settings));
597
598 return true;
599 }
600
601 /**
602 * Returns the name of the user currently logged in.
603 *
604 * @return string the name of the current user.
605 */
606 protected function getLoggedUserName()
607 {
608 $user = JFactory::getUser();
609 $name = $user->name;
610
611 return $name;
612 }
613
614 /**
615 * Rewrites a given URI to perform an AJAX request.
616 *
617 * @param string $uri the optional URI plus query to force.
618 *
619 * @return string the proper AJAX uri for the current platform.
620 *
621 * @since 1.15.0 (J) - 1.5.0 (WP)
622 */
623 protected function getExecWidgetAjaxUri($uri = '')
624 {
625 if (empty($uri)) {
626 $uri = 'index.php?option=com_vikbooking&task=exec_admin_widget';
627 }
628
629 return VikBooking::ajaxUrl($uri);
630 }
631
632 /**
633 * Returns the date format in VBO of a particular type.
634 *
635 * @param string $type
636 *
637 * @return string
638 *
639 * @since 1.15.0 (J) - 1.5.0 (WP)
640 */
641 protected function getDateFormat($type = 'date')
642 {
643 if ($this->df == 'd/m/Y') {
644 $juidf = 'dd/mm/yy';
645 } elseif ($this->df == 'm/d/Y') {
646 $juidf = 'mm/dd/yy';
647 } else {
648 $juidf = 'yy/mm/dd';
649 }
650
651 switch ($type) {
652 case 'jui':
653 return $juidf;
654 case 'joomla':
655 case 'wordpress':
656 return VikBooking::getDateFormat(true);
657 default:
658 return $this->df;
659 }
660 }
661 }
662