parser
3 days ago
renderer
3 days ago
block.php
3 days ago
factory.php
3 days ago
index.html
3 days ago
parser.php
3 days ago
renderer.php
3 days ago
block.php
336 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * Timeline block wrapper. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | class VAPAvailabilityTimelineBlock implements JsonSerializable |
| 20 | { |
| 21 | /** |
| 22 | * The check-in date time. |
| 23 | * |
| 24 | * @var JDate |
| 25 | */ |
| 26 | protected $checkin; |
| 27 | |
| 28 | /** |
| 29 | * The check-out date time. |
| 30 | * |
| 31 | * @var JDate |
| 32 | */ |
| 33 | protected $checkout; |
| 34 | |
| 35 | /** |
| 36 | * The availability status. |
| 37 | * |
| 38 | * @var integer |
| 39 | */ |
| 40 | protected $status; |
| 41 | |
| 42 | /** |
| 43 | * An optional cost for this time slot. |
| 44 | * |
| 45 | * @var float |
| 46 | */ |
| 47 | protected $price = null; |
| 48 | |
| 49 | /** |
| 50 | * A list of matching rates. |
| 51 | * |
| 52 | * @var array |
| 53 | */ |
| 54 | protected $ratesTrace = array(); |
| 55 | |
| 56 | /** |
| 57 | * The total number of participants. |
| 58 | * |
| 59 | * @var integer |
| 60 | */ |
| 61 | protected $occupancy = null; |
| 62 | |
| 63 | /** |
| 64 | * The maximum number of allowed participants. |
| 65 | * |
| 66 | * @var integer |
| 67 | */ |
| 68 | protected $capacity = null; |
| 69 | |
| 70 | /** |
| 71 | * Class constructor. |
| 72 | * |
| 73 | * @param mixed $date Either a date string or a JDate instance. |
| 74 | * @param integer $status The availability status. |
| 75 | * @param integer $duration The time slot duration (in minutes). |
| 76 | * @param float $price An optional time slot price. |
| 77 | * @param array $occupancy An optional occupancy array. |
| 78 | */ |
| 79 | public function __construct($date, $status, $duration, $price = null, $occupancy = null) |
| 80 | { |
| 81 | if ($date instanceof JDate) |
| 82 | { |
| 83 | // register date instance |
| 84 | $this->checkin = $date; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | // create date instance |
| 89 | $this->checkin = JFactory::getDate($date); |
| 90 | } |
| 91 | |
| 92 | // create check-out date by adding the duration to the check-in |
| 93 | $this->checkout = clone $this->checkin; |
| 94 | $this->checkout->modify('+' . $duration . ' minutes'); |
| 95 | |
| 96 | $this->status = (int) $status; |
| 97 | |
| 98 | if (!is_null($price)) |
| 99 | { |
| 100 | $this->setPrice($price); |
| 101 | } |
| 102 | |
| 103 | if (!is_null($occupancy)) |
| 104 | { |
| 105 | $this->setOccupancy($occupancy); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Magic method used to safely access internal properties |
| 111 | * without giving the possibility to alter them. |
| 112 | * |
| 113 | * @param string $name The property name. |
| 114 | * |
| 115 | * @return mixed The property value. |
| 116 | */ |
| 117 | public function __get($name) |
| 118 | { |
| 119 | if ($name == 'date') |
| 120 | { |
| 121 | // create a clone of the date |
| 122 | return clone $this->checkin; |
| 123 | } |
| 124 | |
| 125 | // make sure the property is set |
| 126 | if (isset($this->{$name})) |
| 127 | { |
| 128 | return $this->{$name}; |
| 129 | } |
| 130 | |
| 131 | // the specified property does not exist |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Checks whether the time slot is available. |
| 137 | * |
| 138 | * @return boolean |
| 139 | */ |
| 140 | public function isAvailable() |
| 141 | { |
| 142 | return $this->status == 1; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Checks whether the time slot is occupied. |
| 147 | * |
| 148 | * @return boolean |
| 149 | */ |
| 150 | public function isOccupied() |
| 151 | { |
| 152 | return $this->status == 0; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Updates the status of the time slot. |
| 157 | * |
| 158 | * @param mixed Either an integer or a boolean. |
| 159 | * |
| 160 | * @return self This object to support chaining. |
| 161 | */ |
| 162 | public function setStatus($status) |
| 163 | { |
| 164 | $this->status = (int) $status; |
| 165 | |
| 166 | return $this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Returns the check-in date time string according to the specified format. |
| 171 | * |
| 172 | * @param string $format The format to use. |
| 173 | * @param mixed $tz An optional timezone. |
| 174 | * |
| 175 | * @return string The formatted check-in. |
| 176 | */ |
| 177 | public function checkin($format = 'Y-m-d H:i:s', $tz = null) |
| 178 | { |
| 179 | // format check-in |
| 180 | return $this->format('checkin', $format, $tz); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the check-out date time string according to the specified format. |
| 185 | * |
| 186 | * @param string $format The format to use. |
| 187 | * @param mixed $tz An optional timezone. |
| 188 | * |
| 189 | * @return string The formatted check-out. |
| 190 | */ |
| 191 | public function checkout($format = 'Y-m-d H:i:s', $tz = null) |
| 192 | { |
| 193 | // format check-in |
| 194 | return $this->format('checkout', $format, $tz); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns a date time string according to the specified format. |
| 199 | * |
| 200 | * @param string $what The date to format (checkin or checkout). |
| 201 | * @param string $format The format to use. |
| 202 | * @param mixed $tz An optional timezone. |
| 203 | * |
| 204 | * @return string The formatted date-time. |
| 205 | */ |
| 206 | protected function format($what, $format, $tz = null) |
| 207 | { |
| 208 | if ($tz) |
| 209 | { |
| 210 | // get previous timezone |
| 211 | $old_tz = $this->{$what}->getTimezone(); |
| 212 | |
| 213 | if (!$tz instanceof DateTimeZone) |
| 214 | { |
| 215 | // create timezone instance first |
| 216 | $tz = new DateTimeZone($tz); |
| 217 | } |
| 218 | |
| 219 | // set new timezone |
| 220 | $this->{$what}->setTimezone($tz); |
| 221 | } |
| 222 | |
| 223 | // format date time adjusted to local area |
| 224 | $str = $this->{$what}->format($format, $local = true); |
| 225 | |
| 226 | if ($tz && !empty($old_tz)) |
| 227 | { |
| 228 | // restore previous timezone |
| 229 | $this->{$what}->setTimezone($old_tz); |
| 230 | } |
| 231 | |
| 232 | return $str; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets the time slot price. |
| 237 | * |
| 238 | * @param float $price The time slot price. |
| 239 | * |
| 240 | * @return self This object to support chaining. |
| 241 | */ |
| 242 | public function setPrice($price) |
| 243 | { |
| 244 | // price cannot be lower than 0 |
| 245 | $this->price = max(array(0, (float) $price)); |
| 246 | |
| 247 | return $this; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Sets a trace of matching special rates. |
| 252 | * |
| 253 | * @param array $trace The rates trace. |
| 254 | * |
| 255 | * @return self This object to support chaining. |
| 256 | */ |
| 257 | public function setRatesTrace(array $trace) |
| 258 | { |
| 259 | $this->ratesTrace = $trace; |
| 260 | |
| 261 | return $this; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Sets the time slot occupancy. |
| 266 | * |
| 267 | * @param float $price The time slot price. |
| 268 | * |
| 269 | * @return self This object to support chaining. |
| 270 | */ |
| 271 | public function setOccupancy($count, $max = null) |
| 272 | { |
| 273 | if (is_array($count)) |
| 274 | { |
| 275 | // set occupancy from array |
| 276 | $this->occupancy = (int) $count[0]; |
| 277 | $this->capacity = (int) $count[1]; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | // set occupancy from arguments |
| 282 | $this->occupancy = (int) $count; |
| 283 | $this->capacity = (int) $max; |
| 284 | } |
| 285 | |
| 286 | // the total capacity cannot be lower than 1 |
| 287 | $this->capacity = max(array(1, $this->capacity)); |
| 288 | |
| 289 | // the occupancy cannot be lower than 0 and cannot |
| 290 | // be higher than the max capacity |
| 291 | $this->occupancy = max(array(0, $this->occupancy)); |
| 292 | $this->occupancy = min(array($this->occupancy, $this->capacity)); |
| 293 | |
| 294 | return $this; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Converts this object into an associative array. |
| 299 | * |
| 300 | * @return array |
| 301 | */ |
| 302 | public function toArray() |
| 303 | { |
| 304 | $arr = get_object_vars($this); |
| 305 | |
| 306 | $arr['utc'] = $arr['checkin']->format('Y-m-d\TH:i:s'); |
| 307 | |
| 308 | /** |
| 309 | * Include the check-out UTC version. |
| 310 | * |
| 311 | * @since 1.7.5 |
| 312 | */ |
| 313 | $arr['utc_checkout'] = $arr['checkout']->format('Y-m-d\TH:i:s'); |
| 314 | |
| 315 | // replace date objects with formatted date-time |
| 316 | $arr['checkin'] = $this->checkin('Y-m-d\TH:i:s'); |
| 317 | $arr['checkout'] = $this->checkout('Y-m-d\TH:i:s'); |
| 318 | |
| 319 | return $arr; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Creates an associative array, containing all the supported properties, |
| 324 | * to be used when this class is passed to "json_encode()". |
| 325 | * |
| 326 | * @return array |
| 327 | * |
| 328 | * @see JsonSerializable |
| 329 | */ |
| 330 | #[ReturnTypeWillChange] |
| 331 | public function jsonSerialize() |
| 332 | { |
| 333 | return $this->toArray(); |
| 334 | } |
| 335 | } |
| 336 |