parser
2 days ago
reader
2 days ago
event.php
2 days ago
importer.php
2 days ago
index.html
2 days ago
parser.php
2 days ago
reader.php
2 days ago
event.php
281 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 | * Encapsulates the information of an iCalendar event. |
| 16 | * |
| 17 | * @since 1.7.3 |
| 18 | */ |
| 19 | class VAPIcalEvent |
| 20 | { |
| 21 | /** |
| 22 | * The event unique ID. |
| 23 | * |
| 24 | * @var string|null |
| 25 | */ |
| 26 | protected $uid; |
| 27 | |
| 28 | /** |
| 29 | * The event start date (UTC). |
| 30 | * |
| 31 | * @var string|null |
| 32 | */ |
| 33 | protected $start; |
| 34 | |
| 35 | /** |
| 36 | * The event start date (UTC). |
| 37 | * |
| 38 | * @var string|null |
| 39 | */ |
| 40 | protected $end; |
| 41 | |
| 42 | /** |
| 43 | * The appointment duration, in seconds. |
| 44 | * |
| 45 | * @var integer |
| 46 | */ |
| 47 | protected $duration; |
| 48 | |
| 49 | /** |
| 50 | * The event creation date (UTC). |
| 51 | * |
| 52 | * @var string|null |
| 53 | */ |
| 54 | protected $created; |
| 55 | |
| 56 | /** |
| 57 | * The event last modify date (UTC). |
| 58 | * |
| 59 | * @var string|null |
| 60 | */ |
| 61 | protected $modified; |
| 62 | |
| 63 | /** |
| 64 | * The event summary (title). |
| 65 | * |
| 66 | * @var string|null |
| 67 | */ |
| 68 | protected $summary; |
| 69 | |
| 70 | /** |
| 71 | * The event long description. |
| 72 | * |
| 73 | * @var string|null |
| 74 | */ |
| 75 | protected $description; |
| 76 | |
| 77 | /** |
| 78 | * The event location. |
| 79 | * |
| 80 | * @var string|null |
| 81 | */ |
| 82 | protected $location; |
| 83 | |
| 84 | /** |
| 85 | * The event organizer. |
| 86 | * |
| 87 | * @var string|null |
| 88 | */ |
| 89 | protected $organizer; |
| 90 | |
| 91 | /** |
| 92 | * The list of attendees, separated by a comma. |
| 93 | * |
| 94 | * @var string|null |
| 95 | */ |
| 96 | protected $attendee; |
| 97 | |
| 98 | /** |
| 99 | * Class constructor. |
| 100 | * |
| 101 | * @param array|object $data The event data to bind. |
| 102 | */ |
| 103 | public function __construct($data) |
| 104 | { |
| 105 | if (!is_array($data) && !is_object($data)) |
| 106 | { |
| 107 | // invalid argument |
| 108 | throw new InvalidArgumentException('Invalid iCal event data', 400); |
| 109 | } |
| 110 | |
| 111 | // iterate all attributes/properties |
| 112 | foreach ($data as $k => $v) |
| 113 | { |
| 114 | // set only in case the property has been explicitly declared by the class |
| 115 | if (property_exists($this, $k)) |
| 116 | { |
| 117 | $this->{$k} = $v; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Proxy used to easily access the internal properties of the class without |
| 124 | * allowing their manipulation. |
| 125 | * |
| 126 | * @param string $name The property name. |
| 127 | * |
| 128 | * @return mixed The property value. |
| 129 | * |
| 130 | * @throws UnexpectedValueException |
| 131 | */ |
| 132 | public function __get($name) |
| 133 | { |
| 134 | $getter = 'get' . ucfirst($name); |
| 135 | |
| 136 | // check whether we have an apposite getter to invoke |
| 137 | if (method_exists($this, $getter)) |
| 138 | { |
| 139 | return $this->{$getter}(); |
| 140 | } |
| 141 | |
| 142 | // check whether the property exists |
| 143 | if (property_exists($this, $name)) |
| 144 | { |
| 145 | return $this->{$name}; |
| 146 | } |
| 147 | |
| 148 | // invalid property, raise error |
| 149 | throw new UnexpectedValueException(sprintf('Property [%s] not declared', $name), 500); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Returns the duration of the event in seconds. |
| 154 | * |
| 155 | * @return integer |
| 156 | */ |
| 157 | public function getDuration() |
| 158 | { |
| 159 | if (is_null($this->duration)) |
| 160 | { |
| 161 | // calculate duration only once |
| 162 | $this->duration = VAPDateHelper::diff($this->start, $this->end, 'seconds'); |
| 163 | } |
| 164 | |
| 165 | return $this->duration; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the last modify date. |
| 170 | * |
| 171 | * @return string |
| 172 | */ |
| 173 | public function getLastModify() |
| 174 | { |
| 175 | $dates = []; |
| 176 | |
| 177 | if ($this->created) |
| 178 | { |
| 179 | $dates[] = JFactory::getDate($this->created)->toISO8601(); |
| 180 | } |
| 181 | |
| 182 | if ($this->modified) |
| 183 | { |
| 184 | $dates[] = JFactory::getDate($this->modified)->toISO8601(); |
| 185 | } |
| 186 | |
| 187 | if ($dates) |
| 188 | { |
| 189 | return max($dates); |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the organizer e-mail, if any. |
| 197 | * |
| 198 | * @return string |
| 199 | */ |
| 200 | public function getOrganizer() |
| 201 | { |
| 202 | // strip initial "mailto:" occurrence |
| 203 | return preg_replace("/^mailto:/i", '', (string) $this->organizer); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the attendee e-mails string. |
| 208 | * |
| 209 | * @return string |
| 210 | */ |
| 211 | public function getAttendee() |
| 212 | { |
| 213 | // strip all "mailto:" occurrences |
| 214 | return preg_replace("/(^|,\s*)mailto:/i", '$1', (string) $this->attendee); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns the list of attendees. |
| 219 | * |
| 220 | * @param boolean $organizer True to include the organizer in the list. |
| 221 | * |
| 222 | * @return array A list of attendees. |
| 223 | */ |
| 224 | public function getAttendeesList($organizer = false) |
| 225 | { |
| 226 | $org_mail = $this->getOrganizer(); |
| 227 | |
| 228 | // no specified attendees |
| 229 | if (!$this->attendee) |
| 230 | { |
| 231 | if ($organizer && $org_mail) |
| 232 | { |
| 233 | // include the organizer in the list |
| 234 | return [$org_mail]; |
| 235 | } |
| 236 | |
| 237 | // organizer not specified or exlcuded |
| 238 | return []; |
| 239 | } |
| 240 | |
| 241 | // explode e-mails in attendees list |
| 242 | $attendees = preg_split("/\s*,\s*/", $this->getAttendee()); |
| 243 | |
| 244 | // ignore empty values and the e-mail of the organizer |
| 245 | $attendees = array_values(array_filter($attendees, function($email) use ($org_mail) |
| 246 | { |
| 247 | return $email && $email != $org_mail; |
| 248 | })); |
| 249 | |
| 250 | // in case the organizer is set and should be included, re-add it at the beginning |
| 251 | if ($organizer && $org_mail) |
| 252 | { |
| 253 | array_unshift($attendees, $org_mail); |
| 254 | } |
| 255 | |
| 256 | return $attendees; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Magic method used to represent the event as a string. |
| 261 | * |
| 262 | * @return string |
| 263 | */ |
| 264 | public function __toString() |
| 265 | { |
| 266 | // extract identifier |
| 267 | $str = $this->summary ? $this->summary : $this->uid; |
| 268 | |
| 269 | if ($str) |
| 270 | { |
| 271 | $str .= ' (' . $this->start . ')'; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | $str = $this->start; |
| 276 | } |
| 277 | |
| 278 | return $str; |
| 279 | } |
| 280 | } |
| 281 |