PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.8
VikBooking Hotel Booking Engine & PMS v1.6.8
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.6.8, at admin/helpers/admin_widget.php

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