| 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 |
* Class constructors should define some vars for the widget in use. |
| 97 |
*/ |
| 98 |
public function __construct() |
| 99 |
{ |
| 100 |
$this->vbo_app = VikBooking::getVboApplication(); |
| 101 |
$nowdf = VikBooking::getDateFormat(true); |
| 102 |
if ($nowdf == "%d/%m/%Y") { |
| 103 |
$this->df = 'd/m/Y'; |
| 104 |
} elseif ($nowdf == "%m/%d/%Y") { |
| 105 |
$this->df = 'm/d/Y'; |
| 106 |
} else { |
| 107 |
$this->df = 'Y/m/d'; |
| 108 |
} |
| 109 |
$this->datesep = VikBooking::getDateSeparator(true); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets the name of the current widget. |
| 114 |
* |
| 115 |
* @return string the widget name. |
| 116 |
*/ |
| 117 |
public function getName() |
| 118 |
{ |
| 119 |
return $this->widgetName; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Gets the description for the current widget. |
| 124 |
* |
| 125 |
* @return string the widget description. |
| 126 |
*/ |
| 127 |
public function getDescription() |
| 128 |
{ |
| 129 |
return $this->widgetDescr; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Gets the icon for the current widget. |
| 134 |
* |
| 135 |
* @return string the widget HTML string icon. |
| 136 |
*/ |
| 137 |
public function getIcon() |
| 138 |
{ |
| 139 |
return !empty($this->widgetIcon) ? $this->widgetIcon : $this->getDefaultIcon(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Gets the style name for the current widget. |
| 144 |
* |
| 145 |
* @return string the widget style name. |
| 146 |
*/ |
| 147 |
public function getStyleName() |
| 148 |
{ |
| 149 |
return $this->widgetStyleName; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Gets the identifier of the current widget. |
| 154 |
* |
| 155 |
* @return string the widget identifier. |
| 156 |
*/ |
| 157 |
public function getIdentifier() |
| 158 |
{ |
| 159 |
if (!$this->widgetId) { |
| 160 |
// fetch widget ID from class name |
| 161 |
$this->widgetId = preg_replace("/^VikBookingAdminWidget/i", '', get_class($this)); |
| 162 |
// place an underscore between each camelCase |
| 163 |
$this->widgetId = strtolower(preg_replace("/([a-z])([A-Z])/", '$1_$2', $this->widgetId)); |
| 164 |
$this->widgetId = strtolower($this->widgetId); |
| 165 |
} |
| 166 |
|
| 167 |
return $this->widgetId; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Turns on/off the flag to detect multitask rendering. |
| 172 |
* |
| 173 |
* @param bool $in true if in multitask panel, or false. |
| 174 |
* |
| 175 |
* @return self |
| 176 |
* |
| 177 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 178 |
*/ |
| 179 |
public function setInMultitask($in = false) |
| 180 |
{ |
| 181 |
$this->is_multitask = (bool)$in; |
| 182 |
|
| 183 |
return $this; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Default method to preload the necessary assets of the widget. |
| 188 |
* Extending classes needing certain assets to be available should |
| 189 |
* override this method and let it load the CSS/JS assets. |
| 190 |
* Useful to avoid JS errors during the AJAX generation of a widget. |
| 191 |
* |
| 192 |
* The method can also be helpful for a widget to register watch data. |
| 193 |
* For example, the last ID retrieved of a certain record can be used |
| 194 |
* to receive it back during the periodic data-watching and be able |
| 195 |
* to detect if a new event has occurred to fire a browser notification. |
| 196 |
* |
| 197 |
* @return mixed watch-data object or null. |
| 198 |
* |
| 199 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 200 |
*/ |
| 201 |
public function preload() |
| 202 |
{ |
| 203 |
return null; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Default method to return a list of watch-data and new browser notifications. |
| 208 |
* Extending classes needing to periodically watch for new events and capable |
| 209 |
* of emitting browser notifications, can override this method. The last data |
| 210 |
* to watch should be returned during the preloading. To be used with list(). |
| 211 |
* |
| 212 |
* @param VBONotificationWatchdata $watch_data the preloaded watch-data object. |
| 213 |
* |
| 214 |
* @return array data object to watch next and notifications array. |
| 215 |
* |
| 216 |
* @see preload() |
| 217 |
* |
| 218 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 219 |
*/ |
| 220 |
public function getNotifications(VBONotificationWatchdata $watch_data = null) |
| 221 |
{ |
| 222 |
$watch_next = null; |
| 223 |
$notifications = null; |
| 224 |
|
| 225 |
return [$watch_next, $notifications]; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Extending Classes should define this method to render the actual |
| 230 |
* output of the admin widget. Multitask data can be passed along. |
| 231 |
* |
| 232 |
* @param VBOMultitaskData $data optional multitask data object. |
| 233 |
* |
| 234 |
* @since 1.15.0 (J) - 1.5.0 (WP) type hint added for $data argument. |
| 235 |
*/ |
| 236 |
abstract public function render(VBOMultitaskData $data = null); |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns the default icon for a widget. |
| 240 |
* |
| 241 |
* @return string the default widget icon HTML string. |
| 242 |
*/ |
| 243 |
protected function getDefaultIcon() |
| 244 |
{ |
| 245 |
return '<i class="' . VikBookingIcons::i('cube') . '"></i>'; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Tells the widget if its being rendered via AJAX. |
| 250 |
* |
| 251 |
* @return bool true if rendering is being made via AJAX. |
| 252 |
*/ |
| 253 |
protected function isAjaxRendering() |
| 254 |
{ |
| 255 |
$widget_id = VikRequest::getString('widget_id', '', 'request'); |
| 256 |
$call = VikRequest::getString('call', '', 'request'); |
| 257 |
|
| 258 |
return ($widget_id == $this->getIdentifier() && $call == 'render'); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Tells the widget if its being rendered in the multitask panel. |
| 263 |
* This kind of rendering could be in an AJAX context or in a regular |
| 264 |
* loading flow. Use the apposite method to see if it's an AJAX rendering. |
| 265 |
* |
| 266 |
* @return bool true if rendering is handled by the multitask panel. |
| 267 |
* |
| 268 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 269 |
*/ |
| 270 |
protected function isMultitaskRendering() |
| 271 |
{ |
| 272 |
$multitask = VikRequest::getInt('multitask', 0, 'request'); |
| 273 |
|
| 274 |
return ($multitask > 0 || $this->is_multitask === true); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Tells the widget if VCM is available. |
| 279 |
* |
| 280 |
* @return bool true if Vik Channel Manager is available. |
| 281 |
* |
| 282 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 283 |
*/ |
| 284 |
protected function hasChannelManager() |
| 285 |
{ |
| 286 |
return is_file(VCM_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikchannelmanager.php'); |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Gets the configuration parameter name for the widget's settings. |
| 291 |
* |
| 292 |
* @return string the param name of the settings record. |
| 293 |
*/ |
| 294 |
protected function getSettingsParamName() |
| 295 |
{ |
| 296 |
return 'admin_widget_' . $this->getIdentifier(); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Loads the widget's settings from the configuration table, if any. |
| 301 |
* If no record found for this widget, an empty record will be inserted. |
| 302 |
* |
| 303 |
* @return mixed the widget settings. |
| 304 |
*/ |
| 305 |
protected function loadSettings() |
| 306 |
{ |
| 307 |
$dbo = JFactory::getDbo(); |
| 308 |
|
| 309 |
$param_name = $this->getSettingsParamName(); |
| 310 |
|
| 311 |
$q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name); |
| 312 |
$dbo->setQuery($q, 0, 1); |
| 313 |
$settings = $dbo->loadResult(); |
| 314 |
|
| 315 |
if (!$settings) { |
| 316 |
return null; |
| 317 |
} |
| 318 |
|
| 319 |
if (in_array(substr($settings, 0, 1), array('{', '['))) { |
| 320 |
// we have detected a JSON string, try to decoded it |
| 321 |
$decoded = json_decode($settings); |
| 322 |
if (function_exists('json_last_error') && json_last_error()) { |
| 323 |
// json is broken, reset settings and return null |
| 324 |
$this->resetSettings(); |
| 325 |
return null; |
| 326 |
} |
| 327 |
// return the decoded settings |
| 328 |
return $decoded; |
| 329 |
} |
| 330 |
|
| 331 |
// return the plain db value otherwise |
| 332 |
return $settings; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Updates the widget's settings in the configuration table. |
| 337 |
* |
| 338 |
* @param mixed $data the settings to store, must be a scalar. |
| 339 |
* |
| 340 |
* @return bool true on success, false otherwise. |
| 341 |
*/ |
| 342 |
protected function updateSettings($data) |
| 343 |
{ |
| 344 |
if ($data === null || !is_scalar($data)) { |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
$param_name = $this->getSettingsParamName(); |
| 349 |
|
| 350 |
$config = VBOFactory::getConfig(); |
| 351 |
$config->set($param_name, $data); |
| 352 |
|
| 353 |
return true; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Resets the settings of the widget. |
| 358 |
* |
| 359 |
* @return bool |
| 360 |
*/ |
| 361 |
public function resetSettings() |
| 362 |
{ |
| 363 |
$dbo = JFactory::getDbo(); |
| 364 |
|
| 365 |
$param_name = $this->getSettingsParamName(); |
| 366 |
|
| 367 |
$q = "SELECT `setting` FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";"; |
| 368 |
$dbo->setQuery($q); |
| 369 |
$dbo->execute(); |
| 370 |
if (!$dbo->getNumRows()) { |
| 371 |
// settings never used |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
$q = "DELETE FROM `#__vikbooking_config` WHERE `param`=" . $dbo->quote($param_name) . ";"; |
| 376 |
$dbo->setQuery($q); |
| 377 |
$dbo->execute(); |
| 378 |
|
| 379 |
return true; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Method invoked during AJAX requests for removing an instance of this widget. |
| 384 |
* By default we try to unset the passed instance index from the settings array. |
| 385 |
* If the widget does not use settings, nothing is done. Widgets could override this method. |
| 386 |
* |
| 387 |
* @return bool true if settings needed to be updated, false otherwise. |
| 388 |
*/ |
| 389 |
public function removeInstance() |
| 390 |
{ |
| 391 |
$settings = $this->loadSettings(); |
| 392 |
if ($settings === null || !is_array($settings)) { |
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
$widget_instance = VikRequest::getInt('widget_instance', -1, 'request'); |
| 397 |
|
| 398 |
if (!isset($settings[$widget_instance])) { |
| 399 |
// settings index not found |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
// splice the array to remove the requested settings instance |
| 404 |
array_splice($settings, $widget_instance, 1); |
| 405 |
|
| 406 |
// update widget's settings |
| 407 |
$this->updateSettings(json_encode($settings)); |
| 408 |
|
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Method invoked during AJAX requests for moving an instance of this widget. |
| 414 |
* This occurs when dragging and dropping the same type of widget to a different position. |
| 415 |
* If the widget does not use settings, nothing is done. Widgets could override this method. |
| 416 |
* |
| 417 |
* @return bool true if settings needed to be updated, false otherwise. |
| 418 |
*/ |
| 419 |
public function sortInstance() |
| 420 |
{ |
| 421 |
$settings = $this->loadSettings(); |
| 422 |
if ($settings === null || !is_array($settings)) { |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
$widget_index_old = VikRequest::getInt('widget_index_old', -1, 'request'); |
| 427 |
$widget_index_new = VikRequest::getInt('widget_index_new', -1, 'request'); |
| 428 |
|
| 429 |
if (!isset($settings[$widget_index_old]) || !isset($settings[$widget_index_new])) { |
| 430 |
// settings index not found |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
// move the settings requested from the old index to the new index |
| 435 |
$extracted = array_splice($settings, $widget_index_old, 1); |
| 436 |
array_splice($settings, $widget_index_new, 0, $extracted); |
| 437 |
|
| 438 |
// update widget's settings |
| 439 |
$this->updateSettings(json_encode($settings)); |
| 440 |
|
| 441 |
return true; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Returns the name of the user currently logged in. |
| 446 |
* |
| 447 |
* @return string the name of the current user. |
| 448 |
*/ |
| 449 |
protected function getLoggedUserName() |
| 450 |
{ |
| 451 |
$user = JFactory::getUser(); |
| 452 |
$name = $user->name; |
| 453 |
|
| 454 |
return $name; |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Rewrites a given URI to perform an AJAX request. |
| 459 |
* |
| 460 |
* @param string $uri the optional URI plus query to force. |
| 461 |
* |
| 462 |
* @return string the proper AJAX uri for the current platform. |
| 463 |
* |
| 464 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 465 |
*/ |
| 466 |
protected function getExecWidgetAjaxUri($uri = '') |
| 467 |
{ |
| 468 |
if (empty($uri)) { |
| 469 |
$uri = 'index.php?option=com_vikbooking&task=exec_admin_widget'; |
| 470 |
} |
| 471 |
|
| 472 |
return VikBooking::ajaxUrl($uri); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Returns the date format in VBO of a particular type. |
| 477 |
* |
| 478 |
* @param string $type |
| 479 |
* |
| 480 |
* @return string |
| 481 |
* |
| 482 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 483 |
*/ |
| 484 |
protected function getDateFormat($type = 'date') |
| 485 |
{ |
| 486 |
if ($this->df == 'd/m/Y') { |
| 487 |
$juidf = 'dd/mm/yy'; |
| 488 |
} elseif ($this->df == 'm/d/Y') { |
| 489 |
$juidf = 'mm/dd/yy'; |
| 490 |
} else { |
| 491 |
$juidf = 'yy/mm/dd'; |
| 492 |
} |
| 493 |
|
| 494 |
switch ($type) { |
| 495 |
case 'jui': |
| 496 |
return $juidf; |
| 497 |
case 'joomla': |
| 498 |
case 'wordpress': |
| 499 |
return VikBooking::getDateFormat(true); |
| 500 |
default: |
| 501 |
return $this->df; |
| 502 |
} |
| 503 |
} |
| 504 |
} |
| 505 |
|