20 protected ?\CDavGroupdavClientCalendar
$davClient =
null;
32 public function __construct(array $server = [],
int $userId =
null)
34 $this->helper =
new Helper();
37 $this->davClient = $this->createDavInstance(
38 $server[
'SERVER_SCHEME'],
39 $server[
'SERVER_HOST'],
40 $server[
'SERVER_PORT'],
41 $server[
'SERVER_USERNAME'],
42 $server[
'SERVER_PASSWORD']
45 $this->apiClient =
new ApiClient($this->davClient, $userId);
58 $userId = \CCalendar::GetUserId();
63 $connection[
'SERVER_USERNAME'],
64 $connection[
'SERVER_PASSWORD']
68 $principlesXml = $this->apiClient->propfind(
70 [
'current-user-principal'],
76 return $this->getXmlStringData(
78 '/response/propstat/prop/current-user-principal/href'
94 $userId = \CCalendar::GetUserId();
99 $connection[
'SERVER_USERNAME'],
100 $connection[
'SERVER_PASSWORD']
104 $calendarXml = $this->apiClient->propfind(
106 [[
'calendar-home-set',
'urn:ietf:params:xml:ns:caldav']],
113 return $this->getXmlStringData(
115 '/response/propstat/prop/calendar-home-set/href'
134 $connection[
'SERVER_USERNAME'],
135 $connection[
'SERVER_PASSWORD']
138 $calendars =
$davClient->GetCalendarList($server[
'path']);
139 if (!is_array($calendars) || empty($calendars))
160 $xmlId = $event->
getUid();
162 return $this->editEvent($path, $xmlId, $event);
178 $xmlId = $event->
getUid();
189 $eventPath = $this->davClient->GetRequestEventPath($path, $xmlId);
191 return $this->editEvent($eventPath, $xmlId, $event, $data);
203 $xmlId = $event->
getUid();
204 $acceptCodes = [200, 201, 204, 404];
211 $eventPath = $this->davClient->GetRequestEventPath($path, $xmlId);
213 $result = (int)$this->apiClient->delete($eventPath);
215 if ($this->davClient->getError())
217 $this->addError($this->davClient->getError());
220 if (in_array($result, $acceptCodes))
243 $xmlId = $event->
getUid();
250 [$eventPath, $calendarData] = $this->prepareInstanceData($event, $path, $xmlId, $data, $excludeDate);
252 return $this->sendPutAction($eventPath, $calendarData);
267 if (!$recurrenceEvent->getEvent()->getUid())
271 $xmlId = $recurrenceEvent->getEvent()->getUid();
273 [$eventPath, $calendarData] = $this->prepareRecurrenceData($recurrenceEvent, $path, $xmlId);
275 return $this->sendPutAction($eventPath, $calendarData);
287 $content = SectionBuilder::getInstance()->getCreateSectionContent($section);
288 $result = (int)$this->apiClient->mkcol($path, $content);
290 if ($this->davClient->getError())
292 $this->addError($this->davClient->getError());
295 if ($result === 200 || $result === 201)
297 $result = $this->davClient->GetCalendarList($path);
298 if ($result && is_array($result))
301 'XML_ID' => $result[0][
'href'],
302 'MODIFICATION_LABEL' => $result[0][
'getctag'],
319 $content = SectionBuilder::getInstance()->getUpdateSectionContent($section);
320 $result = (int)$this->apiClient->proppatch($path, $content);
322 if ($this->davClient->getError())
324 $this->addError($this->davClient->getError());
329 $result = $this->davClient->GetCalendarList($path);
330 if ($result && is_array($result))
333 'XML_ID' => $result[0][
'href'],
334 'MODIFICATION_LABEL' => $result[0][
'getctag'],
356 $result = (int)$this->apiClient->delete($path);
358 if ($this->davClient->getError())
360 $this->addError($this->davClient->getError());
363 $acceptCodes = [200, 201, 204, 404];
365 if (in_array($result, $acceptCodes))
375 return $this->davClient->GetCalendarList($path);
380 return $this->davClient->GetCalendarItemsBySyncToken($path, $syncToken);
385 return $this->davClient->GetCalendarItemsList($path, $hrefs,
true);
390 $parsed = parse_url($url);
391 if (empty($parsed[
'port']))
393 $parsed[
'port'] = ($parsed[
'scheme'] ===
'https'
408 private function getXmlStringData($xml, $path): string
411 $responsePath = $xml->GetPath(
'/*/response');
412 foreach ($responsePath as $response)
416 $dataXml = $response->GetPath($path);
417 if (!empty($dataXml))
419 $data = urldecode($dataXml[0]->GetContent());
436 private function createDavInstance(
442 ): \CDavGroupdavClientCalendar
462 private function getPath(
string $path, ?
string $xmlId): string
464 if (mb_substr($path, -mb_strlen(
'/' . $xmlId .
'.ics')) !=
'/' . $xmlId .
'.ics')
466 $path = rtrim($path,
'/');
467 $path .=
'/' . $xmlId .
'.ics';
485 private function editEvent(
492 $path = $this->getPath($path, $xmlId);
493 $calendarData = EventBuilder::getInstance()->getContent($event, $data);
496 $calendarData = (new \CDavICalendar($calendarData))->Render();
499 return $this->sendPutAction($path, $calendarData);
515 private function prepareInstanceData(
520 Date $excludeDate =
null
523 $instancesOriginalDate = [];
524 $exDates = $event->getExcludedDateCollection();
525 $excludedInstance = $excludeDate ? $excludeDate->format(
'Ymd') :
null;
527 $instances = EventTable::query()
529 ->where(
'RECURRENCE_ID', $event->getParentId())
530 ->where(
'DELETED',
'N')
531 ->where(
'OWNER_ID', $event->getOwner()->getId())
533 ->where(Query::filter()
535 ->whereNot(
'MEETING_STATUS',
'N')
536 ->whereNull(
'MEETING_STATUS')
538 ->exec()->fetchCollection()
541 foreach ($instances as $instance)
543 $originalDate = $instance->getOriginalDateFrom()
544 ? $instance->getOriginalDateFrom()->format(
'Ymd')
545 : $instance->getDateFrom()->format(
'Ymd')
547 if ($originalDate === $excludedInstance)
549 $instances->remove($instance);
553 $instancesOriginalDate[] = $originalDate;
562 foreach ($exDates->getCollection() as $key => $exDate)
564 if (in_array($exDate->format(
'Ymd'), $instancesOriginalDate,
true))
566 $exDates->remove($key);
569 $event->setExcludedDateCollection($exDates);
572 $eventPath = $this->davClient->GetRequestEventPath($path, $xmlId);
573 $eventPath = $this->getPath($eventPath, $xmlId);
574 $calendarData[] = EventBuilder::getInstance()->getContent($event, $data);
576 foreach ($instances as $instance)
578 $instanceObject = (
new EventBuilderFromEntityObject($instance))->build();
579 $instanceObject->setUid($xmlId);
580 $calendarData[] = EventBuilder::getInstance()->getContent($instanceObject, $data);
584 $calendarData = (
new RecurrenceEventBuilder($calendarData))->Render();
587 return [$eventPath, $calendarData];
601 private function prepareRecurrenceData(SyncEvent $recurrenceEvent,
string $path, $xmlId): array
604 $exDates = $recurrenceEvent->getEvent()->getExcludedDateCollection();
607 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
609 $instanceDates[] = $instance->getEvent()->getOriginalDateFrom()
610 ? $instance->getEvent()->getOriginalDateFrom()->format(
'Ymd')
611 : $instance->getEvent()->getStart()->format(
'Ymd')
621 foreach ($exDates->getCollection() as $key => $date)
623 if (in_array($date->format(
'Ymd'), $instanceDates,
true))
625 $exDates->remove($key);
629 $recurrenceEvent->getEvent()->setExcludedDateCollection($exDates);
632 $eventPath = $this->davClient->GetRequestEventPath($path, $xmlId);
633 $eventPath = $this->getPath($eventPath, $xmlId);
634 $calendarData[] = EventBuilder::getInstance()->getContent($recurrenceEvent->getEvent());
636 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
638 $instance->getEvent()->setUid($xmlId);
639 $calendarData[] = EventBuilder::getInstance()->getContent($instance->getEvent());
644 $calendarData = (
new RecurrenceEventBuilder($calendarData))->Render();
647 return [$eventPath, $calendarData];
657 private function sendPutAction(
string $path, $calendarData): ?array
659 $result = (int)$this->apiClient->put($path, $calendarData);
661 if ($this->davClient->getError())
663 $this->addError($this->davClient->getError());
666 if ($result === 201 || $result === 204)
668 $result = $this->davClient->GetCalendarItemsList(
675 if ($result && is_array($result))
678 'XML_ID' => $this->davClient::getBasenameWithoutExtension($result[0][
'href']),
679 'MODIFICATION_LABEL' => $result[0][
'getetag'],
691 private function addError(array
$error)