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

620 lines 14.5 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 * Binds the widget options at runtime when multitask data is available.
242 * Options live on a separate layer than regular multitask data.
243 *
244 * @param VBOMultitaskData|VBOMultitaskOptions|array|object $options options to bind.
245 *
246 * @return self
247 *
248 * @since 1.16.5 (J) - 1.6.5 (WP)
249 */
250 public function bindOptions($options)
251 {
252 if ($options instanceof VBOMultitaskData) {
253 // set options by extracting them from multitask data
254 $this->options->setProperties($options->getDataOptions());
255 } elseif ($options instanceof VBOMultitaskOptions) {
256 // replace options with given instance
257 $this->options = $options;
258 } elseif (is_array($options) || is_object($options)) {
259 // bind new options
260 $this->options = VBOMultitaskOptions::getInstance($options);
261 }
262
263 return $this;
264 }
265
266 /**
267 * Gets all widget options previously set through Multitask data.
268 * Options live on a separate layer than regular Multitask data.
269 *
270 * @param bool $public true to get only the public options.
271 *
272 * @return array associative list of options, if any.
273 *
274 * @since 1.16.5 (J) - 1.6.5 (WP)
275 */
276 public function getOptions($public = false)
277 {
278 return $this->options->getProperties($public);
279 }
280
281 /**
282 * Proxy to access the multitask options object.
283 *
284 * @return VBOMultitaskOptions
285 *
286 * @since 1.16.5 (J) - 1.6.5 (WP)
287 */
288 public function options()
289 {
290 return $this->options;
291 }
292
293 /**
294 * Sets a widget option value.
295 *
296 * @param string $key the option key identifier.
297 * @param mixed $value the option value to set.
298 *
299 * @return self
300 *
301 * @since 1.16.5 (J) - 1.6.5 (WP)
302 */
303 public function setOption($key, $value)
304 {
305 $this->options->set($key, $value);
306
307 return $this;
308 }
309
310 /**
311 * Gets the value for the given widget option key.
312 *
313 * @param string $key the option key identifier.
314 * @param mixed $default the default value to get.
315 *
316 * @return mixed
317 *
318 * @since 1.16.5 (J) - 1.6.5 (WP)
319 */
320 public function getOption($key, $default = null)
321 {
322 return $this->options->get($key, $default);
323 }
324
325 /**
326 * Returns an associative array with the current widget details.
327 *
328 * @return array
329 *
330 * @since 1.16.7 (J) - 1.6.7 (WP)
331 */
332 public function getWidgetDetails()
333 {
334 return [
335 'id' => $this->getIdentifier(),
336 'name' => $this->getName(),
337 'descr' => $this->getDescription(),
338 'icon' => $this->getIcon(),
339 'style' => $this->getStyleName(),
340 ];
341 }
342
343 /**
344 * Extending Classes should define this method to render the actual
345 * output of the admin widget. Multitask data can be passed along.
346 *
347 * @param VBOMultitaskData $data optional multitask data object.
348 *
349 * @since 1.15.0 (J) - 1.5.0 (WP) type hint added for $data argument.
350 */
351 abstract public function render(VBOMultitaskData $data = null);
352
353 /**
354 * Returns the default icon for a widget.
355 *
356 * @return string the default widget icon HTML string.
357 */
358 protected function getDefaultIcon()
359 {
360 return '<i class="' . VikBookingIcons::i('cube') . '"></i>';
361 }
362
363 /**
364 * Tells the widget if its being rendered via AJAX.
365 *
366 * @return bool true if rendering is being made via AJAX.
367 */
368 protected function isAjaxRendering()
369 {
370 $widget_id = VikRequest::getString('widget_id', '', 'request');
371 $call = VikRequest::getString('call', '', 'request');
372
373 return ($widget_id == $this->getIdentifier() && $call == 'render');
374 }
375
376 /**
377 * Tells the widget if its being rendered in the multitask panel.
378 * This kind of rendering could be in an AJAX context or in a regular
379 * loading flow. Use the apposite method to see if it's an AJAX rendering.
380 *
381 * @return bool true if rendering is handled by the multitask panel.
382 *
383 * @since 1.15.0 (J) - 1.5.0 (WP)
384 */
385 protected function isMultitaskRendering()
386 {
387 $multitask = VikRequest::getInt('multitask', 0, 'request');
388
389 return ($multitask > 0 || $this->is_multitask === true);
390 }
391
392 /**
393 * Tells the widget if VCM is available.
394 *
395 * @return bool true if Vik Channel Manager is available.
396 *
397 * @since 1.16.0 (J) - 1.6.0 (WP)
398 */
399 protected function hasChannelManager()
400 {
401 return is_file(VCM_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikchannelmanager.php');
402 }
403
404 /**
405 * Gets the configuration parameter name for the widget's settings.
406 *
407 * @return string the param name of the settings record.
408 */
409 protected function getSettingsParamName()
410 {
411 return 'admin_widget_' . $this->getIdentifier();
412 }
413
414 /**
415 * Loads the widget's settings from the configuration table, if any.
416 * If no record found for this widget, an empty record will be inserted.
417 *
418 * @return mixed the widget settings.
419 */
420 protected function loadSettings()
421 {
422 $dbo = JFactory::getDbo();
423
424 $param_name = $this->getSettingsParamName();
425
426 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name);
427 $dbo->setQuery($q, 0, 1);
428 $settings = $dbo->loadResult();
429
430 if (!$settings) {
431 return null;
432 }
433
434 if (in_array(substr($settings, 0, 1), array('{', '['))) {
435 // we have detected a JSON string, try to decoded it
436 $decoded = json_decode($settings);
437 if (function_exists('json_last_error') && json_last_error()) {
438 // json is broken, reset settings and return null
439 $this->resetSettings();
440 return null;
441 }
442 // return the decoded settings
443 return $decoded;
444 }
445
446 // return the plain db value otherwise
447 return $settings;
448 }
449
450 /**
451 * Updates the widget's settings in the configuration table.
452 *
453 * @param mixed $data the settings to store, must be a scalar.
454 *
455 * @return bool true on success, false otherwise.
456 */
457 protected function updateSettings($data)
458 {
459 if ($data === null || !is_scalar($data)) {
460 return false;
461 }
462
463 $param_name = $this->getSettingsParamName();
464
465 $config = VBOFactory::getConfig();
466 $config->set($param_name, $data);
467
468 return true;
469 }
470
471 /**
472 * Resets the settings of the widget.
473 *
474 * @return bool
475 */
476 public function resetSettings()
477 {
478 $dbo = JFactory::getDbo();
479
480 $param_name = $this->getSettingsParamName();
481
482 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";";
483 $dbo->setQuery($q);
484 $dbo->execute();
485 if (!$dbo->getNumRows()) {
486 // settings never used
487 return false;
488 }
489
490 $q = "DELETE FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";";
491 $dbo->setQuery($q);
492 $dbo->execute();
493
494 return true;
495 }
496
497 /**
498 * Method invoked during AJAX requests for removing an instance of this widget.
499 * By default we try to unset the passed instance index from the settings array.
500 * If the widget does not use settings, nothing is done. Widgets could override this method.
501 *
502 * @return bool true if settings needed to be updated, false otherwise.
503 */
504 public function removeInstance()
505 {
506 $settings = $this->loadSettings();
507 if ($settings === null || !is_array($settings)) {
508 return false;
509 }
510
511 $widget_instance = VikRequest::getInt('widget_instance', -1, 'request');
512
513 if (!isset($settings[$widget_instance])) {
514 // settings index not found
515 return false;
516 }
517
518 // splice the array to remove the requested settings instance
519 array_splice($settings, $widget_instance, 1);
520
521 // update widget's settings
522 $this->updateSettings(json_encode($settings));
523
524 return true;
525 }
526
527 /**
528 * Method invoked during AJAX requests for moving an instance of this widget.
529 * This occurs when dragging and dropping the same type of widget to a different position.
530 * If the widget does not use settings, nothing is done. Widgets could override this method.
531 *
532 * @return bool true if settings needed to be updated, false otherwise.
533 */
534 public function sortInstance()
535 {
536 $settings = $this->loadSettings();
537 if ($settings === null || !is_array($settings)) {
538 return false;
539 }
540
541 $widget_index_old = VikRequest::getInt('widget_index_old', -1, 'request');
542 $widget_index_new = VikRequest::getInt('widget_index_new', -1, 'request');
543
544 if (!isset($settings[$widget_index_old]) || !isset($settings[$widget_index_new])) {
545 // settings index not found
546 return false;
547 }
548
549 // move the settings requested from the old index to the new index
550 $extracted = array_splice($settings, $widget_index_old, 1);
551 array_splice($settings, $widget_index_new, 0, $extracted);
552
553 // update widget's settings
554 $this->updateSettings(json_encode($settings));
555
556 return true;
557 }
558
559 /**
560 * Returns the name of the user currently logged in.
561 *
562 * @return string the name of the current user.
563 */
564 protected function getLoggedUserName()
565 {
566 $user = JFactory::getUser();
567 $name = $user->name;
568
569 return $name;
570 }
571
572 /**
573 * Rewrites a given URI to perform an AJAX request.
574 *
575 * @param string $uri the optional URI plus query to force.
576 *
577 * @return string the proper AJAX uri for the current platform.
578 *
579 * @since 1.15.0 (J) - 1.5.0 (WP)
580 */
581 protected function getExecWidgetAjaxUri($uri = '')
582 {
583 if (empty($uri)) {
584 $uri = 'index.php?option=com_vikbooking&task=exec_admin_widget';
585 }
586
587 return VikBooking::ajaxUrl($uri);
588 }
589
590 /**
591 * Returns the date format in VBO of a particular type.
592 *
593 * @param string $type
594 *
595 * @return string
596 *
597 * @since 1.15.0 (J) - 1.5.0 (WP)
598 */
599 protected function getDateFormat($type = 'date')
600 {
601 if ($this->df == 'd/m/Y') {
602 $juidf = 'dd/mm/yy';
603 } elseif ($this->df == 'm/d/Y') {
604 $juidf = 'mm/dd/yy';
605 } else {
606 $juidf = 'yy/mm/dd';
607 }
608
609 switch ($type) {
610 case 'jui':
611 return $juidf;
612 case 'joomla':
613 case 'wordpress':
614 return VikBooking::getDateFormat(true);
615 default:
616 return $this->df;
617 }
618 }
619 }
620