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

602 lines 14.1 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 * Extending Classes should define this method to render the actual
327 * output of the admin widget. Multitask data can be passed along.
328 *
329 * @param VBOMultitaskData $data optional multitask data object.
330 *
331 * @since 1.15.0 (J) - 1.5.0 (WP) type hint added for $data argument.
332 */
333 abstract public function render(VBOMultitaskData $data = null);
334
335 /**
336 * Returns the default icon for a widget.
337 *
338 * @return string the default widget icon HTML string.
339 */
340 protected function getDefaultIcon()
341 {
342 return '<i class="' . VikBookingIcons::i('cube') . '"></i>';
343 }
344
345 /**
346 * Tells the widget if its being rendered via AJAX.
347 *
348 * @return bool true if rendering is being made via AJAX.
349 */
350 protected function isAjaxRendering()
351 {
352 $widget_id = VikRequest::getString('widget_id', '', 'request');
353 $call = VikRequest::getString('call', '', 'request');
354
355 return ($widget_id == $this->getIdentifier() && $call == 'render');
356 }
357
358 /**
359 * Tells the widget if its being rendered in the multitask panel.
360 * This kind of rendering could be in an AJAX context or in a regular
361 * loading flow. Use the apposite method to see if it's an AJAX rendering.
362 *
363 * @return bool true if rendering is handled by the multitask panel.
364 *
365 * @since 1.15.0 (J) - 1.5.0 (WP)
366 */
367 protected function isMultitaskRendering()
368 {
369 $multitask = VikRequest::getInt('multitask', 0, 'request');
370
371 return ($multitask > 0 || $this->is_multitask === true);
372 }
373
374 /**
375 * Tells the widget if VCM is available.
376 *
377 * @return bool true if Vik Channel Manager is available.
378 *
379 * @since 1.16.0 (J) - 1.6.0 (WP)
380 */
381 protected function hasChannelManager()
382 {
383 return is_file(VCM_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikchannelmanager.php');
384 }
385
386 /**
387 * Gets the configuration parameter name for the widget's settings.
388 *
389 * @return string the param name of the settings record.
390 */
391 protected function getSettingsParamName()
392 {
393 return 'admin_widget_' . $this->getIdentifier();
394 }
395
396 /**
397 * Loads the widget's settings from the configuration table, if any.
398 * If no record found for this widget, an empty record will be inserted.
399 *
400 * @return mixed the widget settings.
401 */
402 protected function loadSettings()
403 {
404 $dbo = JFactory::getDbo();
405
406 $param_name = $this->getSettingsParamName();
407
408 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name);
409 $dbo->setQuery($q, 0, 1);
410 $settings = $dbo->loadResult();
411
412 if (!$settings) {
413 return null;
414 }
415
416 if (in_array(substr($settings, 0, 1), array('{', '['))) {
417 // we have detected a JSON string, try to decoded it
418 $decoded = json_decode($settings);
419 if (function_exists('json_last_error') && json_last_error()) {
420 // json is broken, reset settings and return null
421 $this->resetSettings();
422 return null;
423 }
424 // return the decoded settings
425 return $decoded;
426 }
427
428 // return the plain db value otherwise
429 return $settings;
430 }
431
432 /**
433 * Updates the widget's settings in the configuration table.
434 *
435 * @param mixed $data the settings to store, must be a scalar.
436 *
437 * @return bool true on success, false otherwise.
438 */
439 protected function updateSettings($data)
440 {
441 if ($data === null || !is_scalar($data)) {
442 return false;
443 }
444
445 $param_name = $this->getSettingsParamName();
446
447 $config = VBOFactory::getConfig();
448 $config->set($param_name, $data);
449
450 return true;
451 }
452
453 /**
454 * Resets the settings of the widget.
455 *
456 * @return bool
457 */
458 public function resetSettings()
459 {
460 $dbo = JFactory::getDbo();
461
462 $param_name = $this->getSettingsParamName();
463
464 $q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";";
465 $dbo->setQuery($q);
466 $dbo->execute();
467 if (!$dbo->getNumRows()) {
468 // settings never used
469 return false;
470 }
471
472 $q = "DELETE FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";";
473 $dbo->setQuery($q);
474 $dbo->execute();
475
476 return true;
477 }
478
479 /**
480 * Method invoked during AJAX requests for removing an instance of this widget.
481 * By default we try to unset the passed instance index from the settings array.
482 * If the widget does not use settings, nothing is done. Widgets could override this method.
483 *
484 * @return bool true if settings needed to be updated, false otherwise.
485 */
486 public function removeInstance()
487 {
488 $settings = $this->loadSettings();
489 if ($settings === null || !is_array($settings)) {
490 return false;
491 }
492
493 $widget_instance = VikRequest::getInt('widget_instance', -1, 'request');
494
495 if (!isset($settings[$widget_instance])) {
496 // settings index not found
497 return false;
498 }
499
500 // splice the array to remove the requested settings instance
501 array_splice($settings, $widget_instance, 1);
502
503 // update widget's settings
504 $this->updateSettings(json_encode($settings));
505
506 return true;
507 }
508
509 /**
510 * Method invoked during AJAX requests for moving an instance of this widget.
511 * This occurs when dragging and dropping the same type of widget to a different position.
512 * If the widget does not use settings, nothing is done. Widgets could override this method.
513 *
514 * @return bool true if settings needed to be updated, false otherwise.
515 */
516 public function sortInstance()
517 {
518 $settings = $this->loadSettings();
519 if ($settings === null || !is_array($settings)) {
520 return false;
521 }
522
523 $widget_index_old = VikRequest::getInt('widget_index_old', -1, 'request');
524 $widget_index_new = VikRequest::getInt('widget_index_new', -1, 'request');
525
526 if (!isset($settings[$widget_index_old]) || !isset($settings[$widget_index_new])) {
527 // settings index not found
528 return false;
529 }
530
531 // move the settings requested from the old index to the new index
532 $extracted = array_splice($settings, $widget_index_old, 1);
533 array_splice($settings, $widget_index_new, 0, $extracted);
534
535 // update widget's settings
536 $this->updateSettings(json_encode($settings));
537
538 return true;
539 }
540
541 /**
542 * Returns the name of the user currently logged in.
543 *
544 * @return string the name of the current user.
545 */
546 protected function getLoggedUserName()
547 {
548 $user = JFactory::getUser();
549 $name = $user->name;
550
551 return $name;
552 }
553
554 /**
555 * Rewrites a given URI to perform an AJAX request.
556 *
557 * @param string $uri the optional URI plus query to force.
558 *
559 * @return string the proper AJAX uri for the current platform.
560 *
561 * @since 1.15.0 (J) - 1.5.0 (WP)
562 */
563 protected function getExecWidgetAjaxUri($uri = '')
564 {
565 if (empty($uri)) {
566 $uri = 'index.php?option=com_vikbooking&task=exec_admin_widget';
567 }
568
569 return VikBooking::ajaxUrl($uri);
570 }
571
572 /**
573 * Returns the date format in VBO of a particular type.
574 *
575 * @param string $type
576 *
577 * @return string
578 *
579 * @since 1.15.0 (J) - 1.5.0 (WP)
580 */
581 protected function getDateFormat($type = 'date')
582 {
583 if ($this->df == 'd/m/Y') {
584 $juidf = 'dd/mm/yy';
585 } elseif ($this->df == 'm/d/Y') {
586 $juidf = 'mm/dd/yy';
587 } else {
588 $juidf = 'yy/mm/dd';
589 }
590
591 switch ($type) {
592 case 'jui':
593 return $juidf;
594 case 'joomla':
595 case 'wordpress':
596 return VikBooking::getDateFormat(true);
597 default:
598 return $this->df;
599 }
600 }
601 }
602