35 $eventDM = EventTable::query()
37 ->where(
'OWNER_ID', $ownerId)
44 while ($event = $eventDM->fetch())
46 $collection->add((
new Core\Builders\EventBuilderFromArray($event))->build());
59 protected function createEntity($entity, array $params = []): ?Core\
Base\EntityInterface
61 $entity->setDateModified(
new Core\
Base\
Date());
62 $result = $this->save($entity, $params);
64 if ($result->isSuccess())
67 return $this->
getById($result->getId());
81 protected function updateEntity($entity, array $params = []): ?Core\
Base\EntityInterface
83 $entity->setDateModified(
new Core\
Base\
Date());
84 $data = $this->convertToArray($entity);
86 $params = array_merge($params, [
88 'originalFrom' => $params[
'originalFrom'] ??
null,
89 'checkPermissions' =>
false,
90 'userId' => $params[
'userId']
91 ?? $entity->getOwner() ? $entity->getOwner()->getId() :
null,
95 if (($id = CCalendar::SaveEvent($params)) && is_numeric($id))
97 return $this->
getById((
int)$id);
114 $params = array_merge($params,[
115 'Event' => $this->prepareArrayForDelete($entity),
116 'id' => $entity->getId(),
117 'checkPermissions' =>
false,
118 'bMarkDeleted' =>
true,
119 'userId' => $params[
'userId']
120 ?? $entity->getOwner() ? $entity->getOwner()->getId() :
null,
123 if (CCalendar::DeleteEvent($entity->getId(),
true, $params) && !empty($params[
'bMarkDeleted']))
125 $entity->setIsDeleted(
true);
138 private function prepareArrayForDelete(Core\
Event\
Event $event): ?array
142 return $this->convertToArray($event);
158 private function save(Core\Event\Event $event, array $params = []): AddResult
160 $data = $this->convertToArray($event);
162 $params = array_merge($params, [
164 'checkPermissions' =>
false,
165 'userId' => $event->getOwner() ? $event->getOwner()->getId() : null,
168 $id = CCalendar::SaveEvent($params);
170 $result =
new AddResult();
173 $result->setPrimary($id);
177 $result->addError(
new Error(
'Error of create event'));
191 private function prepareReminders(Core\Event\Properties\RemindCollection $collection, Core\Base\Date $start): array
194 $collection->setEventStart(clone $start);
195 $collection->deDuplicate();
197 foreach ($collection->getCollection() as $reminder)
205 if ($reminder->getDaysBefore() !==
null)
207 $remind[
'type'] = Core\Event\Properties\Remind::UNIT_DAY_BEFORE;
208 $remind[
'before'] = $reminder->getDaysBefore();
209 $remind[
'time'] = $reminder->getTimeOffset();
211 elseif ($reminder->isSimpleType())
213 $remind[
'type'] = str_replace(
'minutes',
'min', $reminder->getUnits()) ;
214 $remind[
'count'] = $reminder->getTime();
218 $remind[
'type'] = Core\Event\Properties\Remind::UNIT_DATES;
219 $remind[
'value'] = $reminder->getSpecificTime();
234 private function convertToArray(Core\Event\Event $event): array
237 'ID' => $event->getId(),
238 'ACTIVE' => $event->isActive() ?
'Y' :
'N',
239 'DELETED' => $event->isDeleted() ?
'Y' :
'N',
240 'DT_SKIP_TIME' => $event->isFullDayEvent() ?
'Y' :
'N',
241 'DAV_XML_ID' => $event->getUid(),
242 'TZ_FROM' => (string)$event->getStartTimeZone(),
243 'TZ_TO' => (string)$event->getEndTimeZone(),
244 'NAME' => $event->getName(),
245 'DATE_FROM' => (string)$event->getStart(),
246 'DATE_TO' => (string)$event->getEnd(),
247 'ORIGINAL_DATE_FROM' => $this->prepareOriginalDateFrom($event),
248 'DESCRIPTION' => $event->getDescription(),
249 'ACCESSIBILITY' => $event->getAccessibility(),
250 'PRIVATE_EVENT' => $event->isPrivate(),
251 'IMPORTANCE' => $event->getImportance(),
252 'OWNER_ID' => $event->getOwner() ? $event->getOwner()->getId() :
null,
253 'CREATED_BY' => $event->getOwner() ? $event->getOwner()->getId() :
null,
254 'CAL_TYPE' => $event->getCalendarType(),
255 'EVENT_TYPE' => $event->getSpecialLabel(),
256 'LOCATION' => $event->getLocation() ? $event->getLocation()->getActualLocation() :
'',
257 'REMIND' => ($event->getRemindCollection() && $event->getRemindCollection()->count())
258 ? $this->prepareReminders($event->getRemindCollection(), $event->getStart())
260 'RRULE' => $event->isRecurrence()
261 ? $event->getRecurringRule()->
toArray()
263 'EXDATE' => $event->getExcludedDateCollection()
264 ? $event->getExcludedDateCollection()->toString()
266 'RECURRENCE_ID' => $event->getRecurrenceId(),
267 'IS_MEETING' => $event->isMeeting(),
268 'MEETING_STATUS' => $event->getMeetingStatus(),
269 'MEETING_HOST' => $event->getEventHost() ? $event->getEventHost()->getId() : null,
270 'MEETING' => $event->getMeetingDescription() ? $event->getMeetingDescription()->
getFields() : null,
271 'ATTENDEES_CODES' => $event->getAttendeesCollection()
272 ? $event->getAttendeesCollection()->
getFields()[
'attendeesCodesCollection']
276 'RELATIONS' => $event->getRelations() ? $event->getRelations()->
getFields() : null,
291 $eventData = EventTable::query()
327 return Core\Event\EventMap::class;
341 $params[
'select'] = $params[
'select'] ?? [
"*"];
342 return EventTable::getList($params);
352 $eventDM = EventTable::query()
353 ->where(Query::filter()
355 ->where(
'PARENT_ID', $id)
356 ->where(
'RECURRENCE_ID', $id)
358 ->where(
'OWNER_ID', $ownerId)
365 while ($event = $eventDM->fetch())
367 $eventMap->add((
new Core\Builders\EventBuilderFromArray($event))->build(), (
int)$event[
'ID']);
384 $eventDM = EventTable::query()
388 if ($additionalParams)
391 if (isset($additionalParams[
'filter'])
392 && is_iterable($additionalParams[
'filter'])
395 foreach ($additionalParams[
'filter'] as $filter)
397 $eventDM->addFilter($filter[
'key'], $filter[
'value']);
402 if (isset($additionalParams[
'fields'])
403 && is_array($additionalParams[
'fields'])
406 $eventDM->setSelect($additionalParams[
'fields']);
410 $eventDM->setSelect([
'*']);
415 $eventDM->setSelect([
'*']);
418 $event = $eventDM->exec()->fetchObject();
435 'EXDATE' => (string)$event->getExcludedDateCollection(),
438 $result = EventTable::update(
440 array_intersect_key($eventFields, array_flip($fields))
443 if ($result->isSuccess())
462 return $this->
getCacheMap()->getItem($fields[
'ID']);
466 $this->
getCacheMap()->add($entity, $entity->getId());
478 return (
new Core\Builders\EventBuilderFromArray($fields))->build();
486 return Core\Event\Event::class;
494 private function prepareOriginalDateFrom(Core\
Event\
Event $event): ?string
497 if ($event->getOriginalDateFrom())
499 if ($event->getStartTimeZone())
501 $event->getOriginalDateFrom()->setTimezone($event->getStartTimeZone()->getTimeZone());
503 $result = (string)$event->getOriginalDateFrom();