20 protected ?\CDavGroupdavClientCalendar
$davClient =
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']
58 $userId = \CCalendar::GetUserId();
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();
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'
138 $calendars =
$davClient->GetCalendarList($server[
'path']);
139 if (!is_array($calendars) || empty($calendars))
160 $xmlId =
$event->getUid();
178 $xmlId =
$event->getUid();
184 if (
$event->getExcludedDateCollection()->getCollection())
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);
290 if ($this->davClient->getError())
292 $this->addError($this->davClient->getError());
301 'XML_ID' =>
$result[0][
'href'],
302 'MODIFICATION_LABEL' =>
$result[0][
'getctag'],
319 $content = SectionBuilder::getInstance()->getUpdateSectionContent($section);
322 if ($this->davClient->getError())
324 $this->addError($this->davClient->getError());
333 'XML_ID' =>
$result[0][
'href'],
334 'MODIFICATION_LABEL' =>
$result[0][
'getctag'],
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');
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')
467 $path .=
'/' . $xmlId .
'.ics';
485 private function editEvent(
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?->format(
'Ymd');
527 $instances = EventTable::query()
529 ->where(
'RECURRENCE_ID',
$event->getParentId())
530 ->where(
'DELETED',
'N')
531 ->where(
'OWNER_ID',
$event->getOwner()->getId())
532 ->where(Query::filter()
534 ->whereNot(
'MEETING_STATUS',
'N')
535 ->whereNull(
'MEETING_STATUS')
537 ->exec()->fetchCollection()
542 $originalDate =
$instance->getOriginalDateFrom()
543 ?
$instance->getOriginalDateFrom()->format(
'Ymd')
544 :
$instance->getDateFrom()->format(
'Ymd')
546 if ($originalDate === $excludedInstance)
552 $instancesOriginalDate[] = $originalDate;
561 foreach ($exDates->getCollection() as
$key => $exDate)
563 if (in_array($exDate->format(
'Ymd'), $instancesOriginalDate,
true))
565 $exDates->remove(
$key);
568 $event->setExcludedDateCollection($exDates);
571 $eventPath = $this->davClient->GetRequestEventPath(
$path, $xmlId);
572 $eventPath = $this->getPath($eventPath, $xmlId);
573 $calendarData[] = EventBuilder::getInstance()->getContent(
$event,
$data);
577 $instanceObject = (
new EventBuilderFromEntityObject(
$instance))->build();
578 $instanceObject->setUid($xmlId);
579 $calendarData[] = EventBuilder::getInstance()->getContent($instanceObject,
$data);
583 $calendarData = (
new RecurrenceEventBuilder($calendarData))->Render();
586 return [$eventPath, $calendarData];
600 private function prepareRecurrenceData(SyncEvent $recurrenceEvent,
string $path, $xmlId):
array
603 $exDates = $recurrenceEvent->getEvent()->getExcludedDateCollection();
606 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as
$instance)
608 $instanceDates[] =
$instance->getEvent()->getOriginalDateFrom()
609 ?
$instance->getEvent()->getOriginalDateFrom()->format(
'Ymd')
610 :
$instance->getEvent()->getStart()->format(
'Ymd')
620 foreach ($exDates->getCollection() as
$key => $date)
622 if (in_array($date->format(
'Ymd'), $instanceDates,
true))
624 $exDates->remove(
$key);
628 $recurrenceEvent->getEvent()->setExcludedDateCollection($exDates);
631 $eventPath = $this->davClient->GetRequestEventPath(
$path, $xmlId);
632 $eventPath = $this->getPath($eventPath, $xmlId);
633 $calendarData[] = EventBuilder::getInstance()->getContent($recurrenceEvent->getEvent());
635 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as
$instance)
638 $calendarData[] = EventBuilder::getInstance()->getContent(
$instance->getEvent());
643 $calendarData = (
new RecurrenceEventBuilder($calendarData))->Render();
646 return [$eventPath, $calendarData];
656 private function sendPutAction(
string $path, $calendarData): ?
array
658 $result = (int)$this->apiClient->put(
$path, $calendarData);
660 if ($this->davClient->getError())
662 $this->addError($this->davClient->getError());
667 $result = $this->davClient->GetCalendarItemsList(
677 'XML_ID' => $this->davClient::getBasenameWithoutExtension(
$result[0][
'href']),
678 'MODIFICATION_LABEL' =>
$result[0][
'getetag'],