| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Package\CalDav\ICal; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use DateTime; |
| 7 |
use DateTimeZone; |
| 8 |
|
| 9 |
class Event implements \JsonSerializable |
| 10 |
{ |
| 11 |
protected $data = []; |
| 12 |
|
| 13 |
protected $generatedEventData = ''; |
| 14 |
|
| 15 |
protected $dateTimeFormat = 'Ymd\THis\Z'; |
| 16 |
|
| 17 |
public function __construct($array = []) |
| 18 |
{ |
| 19 |
$this->data = $array; |
| 20 |
|
| 21 |
$this->setDefaults(); |
| 22 |
} |
| 23 |
|
| 24 |
public function preCompile() |
| 25 |
{ |
| 26 |
return $this->prepareEventData(); |
| 27 |
} |
| 28 |
|
| 29 |
protected function prepareEventData() |
| 30 |
{ |
| 31 |
$this->setDateTime(); |
| 32 |
$this->setCategories(); |
| 33 |
$this->setAttendees(); |
| 34 |
$this->setOrganizer(); |
| 35 |
$this->setAlarm(); |
| 36 |
$this->setRecurrence(); |
| 37 |
$this->setAttachments(); |
| 38 |
} |
| 39 |
|
| 40 |
protected function setDefaults() |
| 41 |
{ |
| 42 |
if (!isset($this->data['uid'])) { |
| 43 |
$currentDateTime = $this->getCurrentDateTime(); |
| 44 |
$this->data['created'] = $currentDateTime; |
| 45 |
$this->data['dtstamp'] = $currentDateTime; |
| 46 |
$this->data['last-modified'] = $currentDateTime; |
| 47 |
$this->data['uid'] = md5(__NAMESPACE__) . '-' . wp_generate_uuid4(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
protected function getCurrentDateTime() |
| 52 |
{ |
| 53 |
return ( |
| 54 |
new DateTime('now', new DateTimeZone('UTC')) |
| 55 |
)->format($this->dateTimeFormat); |
| 56 |
} |
| 57 |
|
| 58 |
protected function setDateTime() |
| 59 |
{ |
| 60 |
$dates = [ |
| 61 |
'dtstart' => $this->getDtStart(), |
| 62 |
'dtend' => $this->getDtEnd() |
| 63 |
]; |
| 64 |
|
| 65 |
foreach ($dates as $key => $value) { |
| 66 |
|
| 67 |
$dateTime = $value; |
| 68 |
|
| 69 |
if (is_string($value)) { |
| 70 |
$dateTime = new DateTime($value, new DateTimeZone('UTC')); |
| 71 |
} |
| 72 |
|
| 73 |
if ($dateTime instanceof DateTime) { |
| 74 |
$this->data[$key] = $dateTime->format($this->dateTimeFormat); |
| 75 |
} else { |
| 76 |
throw new Exception("Invalid value: {$value} for {$key}."); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
protected function getDtStart() |
| 82 |
{ |
| 83 |
if (!isset($this->data['dtstart'])) { |
| 84 |
if (!isset($this->data['DTSTART'])) { |
| 85 |
throw new Exception('The Event needs an dtstart/DTSTART property.'); |
| 86 |
} else { |
| 87 |
$dtStart = $this->data['DTSTART']; |
| 88 |
} |
| 89 |
} else { |
| 90 |
$dtStart = $this->data['dtstart']; |
| 91 |
} |
| 92 |
|
| 93 |
return $dtStart; |
| 94 |
} |
| 95 |
|
| 96 |
protected function getDtEnd() |
| 97 |
{ |
| 98 |
if (!isset($this->data['dtend'])) { |
| 99 |
if (!isset($this->data['DTEND'])) { |
| 100 |
throw new Exception('The Event needs an dtend/DTEND property.'); |
| 101 |
} else { |
| 102 |
$dtEnd = $this->data['DTEND']; |
| 103 |
} |
| 104 |
} else { |
| 105 |
$dtEnd = $this->data['dtend']; |
| 106 |
} |
| 107 |
|
| 108 |
return $dtEnd; |
| 109 |
} |
| 110 |
|
| 111 |
protected function setCategories() |
| 112 |
{ |
| 113 |
if (!isset($this->data['categories'])) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
$categories = $this->data['categories']; |
| 118 |
|
| 119 |
if (is_array($categories)) { |
| 120 |
$categories = implode(',', $categories); |
| 121 |
} |
| 122 |
|
| 123 |
$categories = str_replace(', ', ',', $categories); |
| 124 |
|
| 125 |
$this->data['categories'] = $categories; |
| 126 |
} |
| 127 |
|
| 128 |
protected function setAttendees() |
| 129 |
{ |
| 130 |
if (!isset($this->data['attendees'])) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
foreach ($this->data['attendees'] as &$attendee) { |
| 135 |
|
| 136 |
if (!isset($attendee['email'])) { |
| 137 |
throw new Exception('An attendee must have an email.'); |
| 138 |
} |
| 139 |
|
| 140 |
if (!isset($attendee['name'])) { |
| 141 |
$attendee['name'] = $attendee['email']; |
| 142 |
} |
| 143 |
|
| 144 |
$str = ''; |
| 145 |
|
| 146 |
foreach ($attendee as $key => $value) { |
| 147 |
$key = strtolower($key); |
| 148 |
|
| 149 |
if ($key == 'name') { |
| 150 |
$str .= "CN={$value};"; |
| 151 |
} elseif ($key == 'role') { |
| 152 |
$value = strtoupper($value); |
| 153 |
$str .= "ROLE={$value};"; |
| 154 |
} elseif ($key == 'rsvp') { |
| 155 |
if (is_bool($value)) { |
| 156 |
$value = $value === true ? 'TRUE' : 'FALSE'; |
| 157 |
} |
| 158 |
$value = strtoupper($value); |
| 159 |
$str .= "RSVP={$value};"; |
| 160 |
} elseif ($key == 'partstat') { |
| 161 |
$value = strtoupper($value); |
| 162 |
$str .= "PARTSTAT={$value};"; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
$str = rtrim($str, ';'); |
| 167 |
|
| 168 |
$str .= ":mailto:{$attendee['email']}"; |
| 169 |
|
| 170 |
$attendee['str'] = $str; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
protected function setOrganizer() |
| 175 |
{ |
| 176 |
if (!isset($this->data['organizer'])) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
$organizer = &$this->data['organizer']; |
| 181 |
|
| 182 |
if (!isset($organizer['email'])) { |
| 183 |
throw new \Exception('An organizer must have an email.'); |
| 184 |
} |
| 185 |
|
| 186 |
if (!isset($organizer['name'])) { |
| 187 |
$organizer['name'] = $organizer['email']; |
| 188 |
} |
| 189 |
|
| 190 |
$str = ''; |
| 191 |
|
| 192 |
foreach ($organizer as $key => $value) { |
| 193 |
|
| 194 |
$key = strtolower($key); |
| 195 |
|
| 196 |
if ($key == 'name') { |
| 197 |
$str .= "CN={$value};"; |
| 198 |
} elseif ($key == 'language') { |
| 199 |
$str .= "LANGUAGE={$value};"; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (isset($organizer['sent_by'])) { |
| 204 |
$str .= "SENT-BY=mailto:{$organizer['sent_by']}"; |
| 205 |
} |
| 206 |
|
| 207 |
$str = rtrim($str, ';'); |
| 208 |
|
| 209 |
$str .= ":mailto:{$organizer['email']}"; |
| 210 |
|
| 211 |
$organizer['str'] = $str; |
| 212 |
} |
| 213 |
|
| 214 |
protected function setAlarm() |
| 215 |
{ |
| 216 |
if (!isset($this->data['alarm'])) { |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
$alarm = &$this->data['alarm']; |
| 221 |
|
| 222 |
$action = strtoupper($alarm['action']); |
| 223 |
|
| 224 |
$alarm['action'] = $action; |
| 225 |
|
| 226 |
if (isset($alarm['trigger'])) { |
| 227 |
|
| 228 |
// For exact time of the event |
| 229 |
if ($alarm['trigger'] === true) { |
| 230 |
$alarm['trigger_str'] = "RELATED=START:PT0S"; |
| 231 |
|
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
if (!isset($alarm['trigger']['after'])) { |
| 236 |
|
| 237 |
if (isset($alarm['trigger']['before'])) { |
| 238 |
$isBefore = $alarm['trigger']['before'] === true; |
| 239 |
} else { |
| 240 |
$isBefore = true; |
| 241 |
} |
| 242 |
} else { |
| 243 |
$isBefore = $alarm['trigger']['after'] === false; |
| 244 |
} |
| 245 |
|
| 246 |
$period = $isBefore ? '-P' : 'P'; |
| 247 |
|
| 248 |
$time = ''; |
| 249 |
|
| 250 |
foreach ($alarm['trigger'] as $key => $value) { |
| 251 |
if ($key == 'years' && is_numeric($value) && $value) { |
| 252 |
$period = $period . $value . 'Y'; |
| 253 |
} elseif ($key == 'months' && is_numeric($value) && $value) { |
| 254 |
$period = $period . $value . 'M'; |
| 255 |
} elseif ($key == 'days' && is_numeric($value) && $value) { |
| 256 |
$period = $period . $value . 'D'; |
| 257 |
} elseif ($key == 'hours' && is_numeric($value) && $value) { |
| 258 |
$time = $time . $value . 'H'; |
| 259 |
} elseif ($key == 'minutes' && is_numeric($value) && $value) { |
| 260 |
$time = $time . $value . 'M'; |
| 261 |
} elseif ($key == 'seconds' && is_numeric($value) && $value) { |
| 262 |
$time = $time . $value . 'S'; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if ($time) { |
| 268 |
$period = $period . 'T' . $time; |
| 269 |
} |
| 270 |
|
| 271 |
$alarm['trigger_str'] = "RELATED=START:{$period}"; |
| 272 |
} |
| 273 |
|
| 274 |
protected function setRecurrence() |
| 275 |
{ |
| 276 |
if (!isset($this->data['repeat'])) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
$str = ''; |
| 281 |
|
| 282 |
$recurrence = &$this->data['repeat']; |
| 283 |
|
| 284 |
foreach ($recurrence as $key => $value) { |
| 285 |
|
| 286 |
$value = is_array($value) ? implode(',', $value) : $value; |
| 287 |
|
| 288 |
$str .= strtoupper($key) . '=' . strtoupper($value) . ';'; |
| 289 |
} |
| 290 |
|
| 291 |
$str = rtrim($str, ';'); |
| 292 |
|
| 293 |
$recurrence['str'] = $str; |
| 294 |
} |
| 295 |
|
| 296 |
public function setAttachments() |
| 297 |
{ |
| 298 |
if (!isset($this->data['attachments'])) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
$attachments = &$this->data['attachments']; |
| 303 |
|
| 304 |
foreach ($this->data['attachments'] as $attachment) { |
| 305 |
|
| 306 |
if (!isset($attachment['url'], $attachment['name'])) { |
| 307 |
throw new Exception('An attachment requires a name and url key.'); |
| 308 |
} |
| 309 |
|
| 310 |
$url = $attachment['url']; |
| 311 |
|
| 312 |
$filename = $attachment['name']; |
| 313 |
|
| 314 |
$attachments['list'][] = 'ATTACH;FILENAME=' . $filename . ':' . $url; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
public function compile() |
| 319 |
{ |
| 320 |
$this->preCompile(); |
| 321 |
|
| 322 |
if (!isset($this->data['sequence'])) { |
| 323 |
$this->data['sequence'] = 0; |
| 324 |
} else { |
| 325 |
$this->data['sequence'] += 1; |
| 326 |
} |
| 327 |
|
| 328 |
$this->data['last-modified'] = $this->getCurrentDateTime(); |
| 329 |
|
| 330 |
$appNS = strtolower(explode('\\', __NAMESPACE__)[0]); |
| 331 |
$calendar[] = "BEGIN:VCALENDAR"; |
| 332 |
$calendar[] = "VERSION:2.0"; |
| 333 |
$calendar[] = "CALSCALE:GREGORIAN"; |
| 334 |
$calendar[] = "PRODID:-//authlab.{$appNS}//CalDAV Client//EN"; |
| 335 |
$calendar[] = "BEGIN:VEVENT"; |
| 336 |
$calendar[] = "CREATED:{$this->data['created']}"; |
| 337 |
$calendar[] = "DTSTAMP:{$this->data['dtstamp']}"; |
| 338 |
$calendar[] = "LAST-MODIFIED:{$this->data['last-modified']}"; |
| 339 |
$calendar[] = "SEQUENCE:{$this->data['sequence']}"; |
| 340 |
$calendar[] = "UID:{$this->data['uid']}"; |
| 341 |
$calendar[] = "DTSTART:{$this->data['dtstart']}"; |
| 342 |
$calendar[] = "DTEND:{$this->data['dtend']}"; |
| 343 |
|
| 344 |
if (isset($this->data['transp'])) { |
| 345 |
$calendar[] = "TRANSP:{$this->data['transp']}"; |
| 346 |
} else { |
| 347 |
$calendar[] = "TRANSP:TRANSPARENT"; |
| 348 |
} |
| 349 |
|
| 350 |
if (isset($this->data['status'])) { |
| 351 |
$eventStatus = strtoupper($this->data['status']); |
| 352 |
$calendar[] = "STATUS:{$eventStatus}"; |
| 353 |
} |
| 354 |
|
| 355 |
if (isset($this->data['summary'])) { |
| 356 |
$calendar[] = "SUMMARY:{$this->data['summary']}"; |
| 357 |
} |
| 358 |
|
| 359 |
if (isset($this->data['description'])) { |
| 360 |
$eventDescription = str_replace("\n", " ", $this->data['description']); |
| 361 |
$calendar[] = "DESCRIPTION:{$eventDescription}"; |
| 362 |
} |
| 363 |
|
| 364 |
if (isset($this->data['location'])) { |
| 365 |
$calendar[] = "LOCATION:{$this->data['location']}"; |
| 366 |
} |
| 367 |
|
| 368 |
if (isset($this->data['categories'])) { |
| 369 |
$calendar[] = "CATEGORIES:{$this->data['categories']}"; |
| 370 |
} |
| 371 |
|
| 372 |
if (isset($this->data['repeat'])) { |
| 373 |
$calendar[] = "RRULE:{$this->data['repeat']['str']}"; |
| 374 |
} |
| 375 |
|
| 376 |
if (isset($this->data['attendees'])) { |
| 377 |
foreach ($this->data['attendees'] as $attendee) { |
| 378 |
$calendar[] = "ATTENDEE;{$attendee['str']}"; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
if (isset($this->data['organizer'])) { |
| 383 |
$calendar[] = "ORGANIZER;{$this->data['organizer']['str']}"; |
| 384 |
} |
| 385 |
|
| 386 |
if (isset($this->data['attachments'])) { |
| 387 |
$calendar[] = implode("\n", $this->data['attachments']['list']); |
| 388 |
} |
| 389 |
|
| 390 |
if (isset($this->data['alarm'])) { |
| 391 |
$calendar[] = "BEGIN:VALARM"; |
| 392 |
|
| 393 |
if (isset($this->data['alarm']['action'])) { |
| 394 |
$calendar[] = "ACTION:{$this->data['alarm']['action']}"; |
| 395 |
} |
| 396 |
|
| 397 |
if (isset($this->data['alarm']['description'])) { |
| 398 |
|
| 399 |
$alarmDescription = str_replace( |
| 400 |
"\n", " ", $this->data['alarm']['description'] |
| 401 |
); |
| 402 |
|
| 403 |
$calendar[] = "DESCRIPTION:{$alarmDescription}"; |
| 404 |
} |
| 405 |
|
| 406 |
if (isset($this->data['alarm']['trigger'])) { |
| 407 |
$calendar[] = "TRIGGER;{$this->data['alarm']['trigger_str']}"; |
| 408 |
} |
| 409 |
|
| 410 |
$calendar[] = "END:VALARM"; |
| 411 |
} |
| 412 |
|
| 413 |
$calendar[] = "END:VEVENT"; |
| 414 |
$calendar[] = "END:VCALENDAR"; |
| 415 |
|
| 416 |
$this->generatedEventData = implode("\n", $calendar); |
| 417 |
|
| 418 |
return $this->generatedEventData; |
| 419 |
} |
| 420 |
|
| 421 |
public function getUid() |
| 422 |
{ |
| 423 |
return $this->data['uid']; |
| 424 |
} |
| 425 |
|
| 426 |
public function __get($key) |
| 427 |
{ |
| 428 |
return $this->data[$key]; |
| 429 |
} |
| 430 |
|
| 431 |
public function __set($key, $value) |
| 432 |
{ |
| 433 |
$this->data[$key] = $value; |
| 434 |
} |
| 435 |
|
| 436 |
#[\ReturnTypeWillChange] |
| 437 |
public function jsonSerialize() |
| 438 |
{ |
| 439 |
return $this->data; |
| 440 |
} |
| 441 |
} |
| 442 |
|