| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Package\CalDav\Clients; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeZone; |
| 7 |
use DOMXPath; |
| 8 |
use DOMDocument; |
| 9 |
use FluentBooking\Package\CalDav\ICal\ICal; |
| 10 |
|
| 11 |
trait CommonTrait |
| 12 |
{ |
| 13 |
protected $options = [ |
| 14 |
'format_date_times' => false, |
| 15 |
'date_time_format' => null, |
| 16 |
'timezone' => null, |
| 17 |
]; |
| 18 |
|
| 19 |
|
| 20 |
protected $datetimeFields = [ |
| 21 |
'dtstart', |
| 22 |
'dtend', |
| 23 |
'dtstamp', |
| 24 |
'created', |
| 25 |
'last_modified' |
| 26 |
]; |
| 27 |
|
| 28 |
public function formatDateTimes($events) |
| 29 |
{ |
| 30 |
$tz = $this->getTimezone(); |
| 31 |
|
| 32 |
$format = $this->getDateTimeFormat(); |
| 33 |
|
| 34 |
foreach ($events as $event) { |
| 35 |
|
| 36 |
foreach ($this->datetimeFields as $prop) { |
| 37 |
|
| 38 |
$dt = new DateTime($event->{$prop}, $tz); |
| 39 |
|
| 40 |
$event->{$prop} = $dt->format($format); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return $events; |
| 45 |
} |
| 46 |
|
| 47 |
protected function getTimezone() |
| 48 |
{ |
| 49 |
if ($tz = $this->getOption('timezone')) { |
| 50 |
return new DateTimeZone($tz); |
| 51 |
} |
| 52 |
|
| 53 |
return wp_timezone(); |
| 54 |
} |
| 55 |
|
| 56 |
protected function getDateTimeFormat() |
| 57 |
{ |
| 58 |
if ($format = $this->getOption('date_time_format')) { |
| 59 |
return $format; |
| 60 |
} |
| 61 |
|
| 62 |
return get_option('date_format'); |
| 63 |
} |
| 64 |
|
| 65 |
public function setOptions($options = []) |
| 66 |
{ |
| 67 |
foreach ($options as $key => $value) { |
| 68 |
$this->options[$key] = $value; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function getOptions($options = []) |
| 73 |
{ |
| 74 |
return $this->options; |
| 75 |
} |
| 76 |
|
| 77 |
public function setOption($key, $value) |
| 78 |
{ |
| 79 |
$this->options[$key] = $value; |
| 80 |
} |
| 81 |
|
| 82 |
public function getOption($key, $default = null) |
| 83 |
{ |
| 84 |
if (isset($this->options[$key])) { |
| 85 |
return $this->options[$key]; |
| 86 |
} |
| 87 |
|
| 88 |
return $default; |
| 89 |
} |
| 90 |
|
| 91 |
protected function extractReportinformation($content) |
| 92 |
{ |
| 93 |
$doc = new DOMDocument(); |
| 94 |
|
| 95 |
$doc->loadXML($content); |
| 96 |
|
| 97 |
$xpath = new DOMXPath($doc); |
| 98 |
|
| 99 |
$xpath->registerNamespace('d', 'DAV:'); |
| 100 |
|
| 101 |
$xpath->registerNamespace('cal', 'urn:ietf:params:xml:ns:caldav'); |
| 102 |
|
| 103 |
$xpath->registerNamespace('caldav', 'urn:ietf:params:xml:ns:caldav'); |
| 104 |
|
| 105 |
if ($href = $xpath->query('//d:href')->item(0)) { |
| 106 |
$href = $href->nodeValue; |
| 107 |
} |
| 108 |
|
| 109 |
if ($status = $xpath->query('//d:status')->item(0)) { |
| 110 |
$status = $status->nodeValue; |
| 111 |
} |
| 112 |
|
| 113 |
if ($etag = $xpath->query('//d:getetag')->item(0)) { |
| 114 |
$etag = $etag->nodeValue; |
| 115 |
} |
| 116 |
|
| 117 |
$events = []; |
| 118 |
|
| 119 |
foreach ($xpath->query('//cal:calendar-data') as $cal) { |
| 120 |
$events = array_merge( |
| 121 |
$events, $this->extractSingleEvent($cal->nodeValue) |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
if ($this->getOption('format_date_times')) { |
| 126 |
$events = $this->formatDateTimes($events); |
| 127 |
} |
| 128 |
|
| 129 |
return compact('href', 'etag', 'status', 'events'); |
| 130 |
} |
| 131 |
|
| 132 |
protected function extractSingleEvent($content) |
| 133 |
{ |
| 134 |
$ical = new ICal($content, [ |
| 135 |
'defaultTimeZone' => 'UTC' |
| 136 |
]); |
| 137 |
|
| 138 |
return $ical->events(); |
| 139 |
} |
| 140 |
} |
| 141 |
|