| 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 |
* Conditional Rule parent Class of all sub-classes. |
| 15 |
* |
| 16 |
* @since 1.4.0 |
| 17 |
*/ |
| 18 |
abstract class VikBookingConditionalRule |
| 19 |
{ |
| 20 |
/** |
| 21 |
* The name of the rule. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $ruleName = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* The description of the rule. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $ruleDescr = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* The rule identifier. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $ruleId = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* The rule params. |
| 43 |
* |
| 44 |
* @var mixed |
| 45 |
*/ |
| 46 |
protected $ruleParams = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* The booking array. |
| 50 |
* |
| 51 |
* @var array |
| 52 |
*/ |
| 53 |
protected $booking = null; |
| 54 |
|
| 55 |
/** |
| 56 |
* The rooms array. |
| 57 |
* |
| 58 |
* @var array |
| 59 |
*/ |
| 60 |
protected $rooms = null; |
| 61 |
|
| 62 |
/** |
| 63 |
* The rates array. |
| 64 |
* |
| 65 |
* @var array |
| 66 |
*/ |
| 67 |
protected $rates = null; |
| 68 |
|
| 69 |
/** |
| 70 |
* The options array. |
| 71 |
* |
| 72 |
* @var array |
| 73 |
*/ |
| 74 |
protected $options = null; |
| 75 |
|
| 76 |
/** |
| 77 |
* An extra data array. |
| 78 |
* |
| 79 |
* @var array |
| 80 |
*/ |
| 81 |
protected $extra_data = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* The VBO application object. |
| 85 |
* |
| 86 |
* @var object |
| 87 |
*/ |
| 88 |
protected $vbo_app = null; |
| 89 |
|
| 90 |
/** |
| 91 |
* The date format with wildcards. |
| 92 |
* |
| 93 |
* @var string |
| 94 |
*/ |
| 95 |
protected $wdf = ''; |
| 96 |
|
| 97 |
/** |
| 98 |
* The date format. |
| 99 |
* |
| 100 |
* @var string |
| 101 |
*/ |
| 102 |
protected $df = ''; |
| 103 |
|
| 104 |
/** |
| 105 |
* The date separator. |
| 106 |
* |
| 107 |
* @var string |
| 108 |
*/ |
| 109 |
protected $datesep = ''; |
| 110 |
|
| 111 |
/** |
| 112 |
* Class constructors should define some vars for the rule in use. |
| 113 |
*/ |
| 114 |
public function __construct() |
| 115 |
{ |
| 116 |
$this->vbo_app = VikBooking::getVboApplication(); |
| 117 |
$this->wdf = VikBooking::getDateFormat(true); |
| 118 |
if ($this->wdf == "%d/%m/%Y") { |
| 119 |
$this->df = 'd/m/Y'; |
| 120 |
} elseif ($this->wdf == "%m/%d/%Y") { |
| 121 |
$this->df = 'm/d/Y'; |
| 122 |
} else { |
| 123 |
$this->df = 'Y/m/d'; |
| 124 |
} |
| 125 |
$this->datesep = VikBooking::getDateSeparator(true); |
| 126 |
|
| 127 |
$this->booking = array(); |
| 128 |
$this->rooms = array(); |
| 129 |
$this->rates = array(); |
| 130 |
$this->options = array(); |
| 131 |
$this->extra_data = array(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Gets the name of the current rule. |
| 136 |
* |
| 137 |
* @return string the rule name. |
| 138 |
*/ |
| 139 |
public function getName() |
| 140 |
{ |
| 141 |
return $this->ruleName; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Gets the description for the current rule. |
| 146 |
* |
| 147 |
* @return string the rule description. |
| 148 |
*/ |
| 149 |
public function getDescription() |
| 150 |
{ |
| 151 |
return $this->ruleDescr; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Gets the identifier of the current rule. |
| 156 |
* |
| 157 |
* @return string the rule identifier. |
| 158 |
*/ |
| 159 |
public function getIdentifier() |
| 160 |
{ |
| 161 |
return $this->ruleId; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Sets the rule params. |
| 166 |
* |
| 167 |
* @param mixed $params the current params of the rule. |
| 168 |
* |
| 169 |
* @return self |
| 170 |
*/ |
| 171 |
public function setParams($params = null) |
| 172 |
{ |
| 173 |
$this->ruleParams = $params; |
| 174 |
|
| 175 |
return $this; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the rule params. |
| 180 |
* |
| 181 |
* @return mixed the current params of the rule. |
| 182 |
*/ |
| 183 |
public function getParams() |
| 184 |
{ |
| 185 |
return $this->ruleParams; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Gets the requested param. |
| 190 |
* |
| 191 |
* @param string $key the param's property. |
| 192 |
* @param mixed $def the default value to return. |
| 193 |
* |
| 194 |
* @return mixed the requested param value. |
| 195 |
*/ |
| 196 |
public function getParam($key, $def = null) |
| 197 |
{ |
| 198 |
if (is_null($this->ruleParams)) { |
| 199 |
return $def; |
| 200 |
} |
| 201 |
|
| 202 |
if (is_object($this->ruleParams) && isset($this->ruleParams->{$key})) { |
| 203 |
return $this->ruleParams->{$key}; |
| 204 |
} |
| 205 |
|
| 206 |
if (is_array($this->ruleParams) && isset($this->ruleParams[$key])) { |
| 207 |
return $this->ruleParams[$key]; |
| 208 |
} |
| 209 |
|
| 210 |
return $def; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Sets a param value. |
| 215 |
* |
| 216 |
* @param string $key the param's property. |
| 217 |
* @param string $val the param's value. |
| 218 |
* |
| 219 |
* @return self |
| 220 |
*/ |
| 221 |
public function setParam($key, $val) |
| 222 |
{ |
| 223 |
if (is_null($this->ruleParams)) { |
| 224 |
$this->ruleParams = new stdClass; |
| 225 |
} |
| 226 |
|
| 227 |
if (is_object($this->ruleParams)) { |
| 228 |
$this->ruleParams->{$key} = $val; |
| 229 |
} |
| 230 |
|
| 231 |
if (is_array($this->ruleParams)) { |
| 232 |
$this->ruleParams[$key] = $val; |
| 233 |
} |
| 234 |
|
| 235 |
return $this; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Builds a unique input field name for the rule. |
| 240 |
* |
| 241 |
* @param string $name the param property (input) name. |
| 242 |
* @param bool $multi whether the param is an array. |
| 243 |
* |
| 244 |
* @return string the name of the input field to use. |
| 245 |
*/ |
| 246 |
public function inputName($name, $multi = false) |
| 247 |
{ |
| 248 |
return basename($this->getIdentifier(), '.php') . "[{$name}]" . ($multi ? '[]' : ''); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Builds a unique input field ID for the rule. |
| 253 |
* |
| 254 |
* @param string $name the param property (input) name/id. |
| 255 |
* |
| 256 |
* @return string the name of the input field to use. |
| 257 |
*/ |
| 258 |
public function inputID($name) |
| 259 |
{ |
| 260 |
return basename($this->getIdentifier(), '.php') . "_{$name}"; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Sets the booking information array. |
| 265 |
* |
| 266 |
* @param mixed $data the booking information array or booking ID. |
| 267 |
* |
| 268 |
* @return self |
| 269 |
*/ |
| 270 |
public function setBooking($data) |
| 271 |
{ |
| 272 |
if (!is_scalar($data) && !is_array($data)) { |
| 273 |
$data = (array)$data; |
| 274 |
} |
| 275 |
|
| 276 |
if (is_scalar($data)) { |
| 277 |
// booking ID expected, fetch the booking information |
| 278 |
$data = VikBooking::getBookingInfoFromID($data); |
| 279 |
} |
| 280 |
|
| 281 |
if (!is_array($data)) { |
| 282 |
$data = array(); |
| 283 |
} |
| 284 |
|
| 285 |
$this->booking = $data; |
| 286 |
|
| 287 |
return $this; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Helper method to populate internal properties. |
| 292 |
* |
| 293 |
* @param mixed $key the key or array of keys to set. |
| 294 |
* @param mixed $val the value or array of values to set. |
| 295 |
* |
| 296 |
* @return self |
| 297 |
*/ |
| 298 |
public function setProperties($key, $val = null) |
| 299 |
{ |
| 300 |
if (!is_string($key) && !is_array($key)) { |
| 301 |
return $this; |
| 302 |
} |
| 303 |
|
| 304 |
if (is_string($key)) { |
| 305 |
$key = array($key); |
| 306 |
} |
| 307 |
if (!is_array($val)) { |
| 308 |
$val = array($val); |
| 309 |
} |
| 310 |
|
| 311 |
foreach ($key as $i => $prop) { |
| 312 |
if (!isset($val[$i])) { |
| 313 |
continue; |
| 314 |
} |
| 315 |
if (property_exists($this, $prop)) { |
| 316 |
$this->{$prop} = $val[$i]; |
| 317 |
} |
| 318 |
$this->extra_data[$prop] = $val[$i]; |
| 319 |
} |
| 320 |
|
| 321 |
return $this; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Gets the requested property. |
| 326 |
* |
| 327 |
* @param string $key the param's property. |
| 328 |
* @param mixed $def the default value to return. |
| 329 |
* |
| 330 |
* @return mixed the requested param value. |
| 331 |
*/ |
| 332 |
public function getProperty($key, $def = null) |
| 333 |
{ |
| 334 |
if (property_exists($this, $key)) { |
| 335 |
return $this->{$key}; |
| 336 |
} |
| 337 |
|
| 338 |
if (is_array($this->extra_data) && isset($this->extra_data[$key])) { |
| 339 |
return $this->extra_data[$key]; |
| 340 |
} |
| 341 |
|
| 342 |
return $def; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Gets the requested property value. |
| 347 |
* Like $this->booking['ts'] for getting the timestamp. |
| 348 |
* |
| 349 |
* @param string $key the param's property. |
| 350 |
* @param string $val the property value. |
| 351 |
* @param mixed $def the default value to return. |
| 352 |
* |
| 353 |
* @return mixed the requested param value. |
| 354 |
*/ |
| 355 |
public function getPropVal($key, $val, $def = null) |
| 356 |
{ |
| 357 |
$prop = $this->getProperty($key, $def); |
| 358 |
if ($prop === $def) { |
| 359 |
return $def; |
| 360 |
} |
| 361 |
|
| 362 |
if (is_array($prop) && isset($prop[$val])) { |
| 363 |
return $prop[$val]; |
| 364 |
} |
| 365 |
|
| 366 |
if (is_object($prop) && isset($prop->{$val})) { |
| 367 |
return $prop->{$val}; |
| 368 |
} |
| 369 |
|
| 370 |
return $def; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Checks whether the installation files of Vik Channel Manager are available. |
| 375 |
* |
| 376 |
* @return bool true if VCM is installed, false otherwise. |
| 377 |
* |
| 378 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 379 |
*/ |
| 380 |
protected function isChannelManagerAvailable() |
| 381 |
{ |
| 382 |
return is_file(VCM_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikchannelmanager.php'); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Extending Classes should define this method |
| 387 |
* to render the params of the rule. |
| 388 |
* |
| 389 |
* @return void |
| 390 |
*/ |
| 391 |
abstract public function renderParams(); |
| 392 |
|
| 393 |
/** |
| 394 |
* Extending Classes should define this method |
| 395 |
* to tell whether the rule is compliant. |
| 396 |
* |
| 397 |
* @return bool True on success, false otherwise. |
| 398 |
*/ |
| 399 |
abstract public function isCompliant(); |
| 400 |
|
| 401 |
/** |
| 402 |
* Extending Classes could implement this method |
| 403 |
* to override it, and to execute some actions. |
| 404 |
* By default, rules that do not override this method |
| 405 |
* will produce no actions, they will serve as filters. |
| 406 |
* |
| 407 |
* @return void |
| 408 |
*/ |
| 409 |
public function callbackAction() |
| 410 |
{ |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Extending Classes could implement this method |
| 416 |
* to override it, and to manipulate the message. |
| 417 |
* |
| 418 |
* @param string $msg the conditional text message. |
| 419 |
* |
| 420 |
* @return string |
| 421 |
* |
| 422 |
* @since 1.15.5 (J) - 1.5.11 (WP) |
| 423 |
*/ |
| 424 |
public function manipulateMessage($msg) |
| 425 |
{ |
| 426 |
return $msg; |
| 427 |
} |
| 428 |
} |
| 429 |
|