Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
2
4
5use Bitrix\Calendar\Core;
6use Bitrix\Calendar\Internals\EO_Event;
16use CCalendar;
17use CCalendarEvent;
18
19class Event extends Mapper
20{
33 public function getCollectionByIds(array $ids, int $ownerId, array $fields = ['*']): Core\Event\EventCollection
34 {
35 $eventDM = EventTable::query()
36 ->whereIn('ID', $ids)
37 ->where('OWNER_ID', $ownerId)
38 ->setSelect($fields)
39 ->exec()
40 ;
41
42 $collection = new Core\Event\EventCollection();
43
44 while ($event = $eventDM->fetch())
45 {
46 $collection->add((new Core\Builders\EventBuilderFromArray($event))->build());
47 }
48
49 return $collection;
50 }
51
59 protected function createEntity($entity, array $params = []): ?Core\Base\EntityInterface
60 {
61 $entity->setDateModified(new Core\Base\Date());
62 $result = $this->save($entity, $params);
63
64 if ($result->isSuccess())
65 {
66 // TODO: perhaps need to setup date create and date update
67 return $this->getById($result->getId());
68 }
69
70 return null;
71 }
72
81 protected function updateEntity($entity, array $params = []): ?Core\Base\EntityInterface
82 {
83 $entity->setDateModified(new Core\Base\Date());
84 $data = $this->convertToArray($entity);
85
86 $params = array_merge($params, [
87 'arFields' => $data,
88 'originalFrom' => $params['originalFrom'] ?? null,
89 'checkPermissions' => false,
90 'userId' => $params['userId']
91 ?? $entity->getOwner() ? $entity->getOwner()->getId() : null,
92 ]);
93
94 // TODO: in the future change it to call EventTable::update()
95 if (($id = CCalendar::SaveEvent($params)) && is_numeric($id))
96 {
97 return $this->getById((int)$id);
98 }
99
100 return null;
101 }
102
112 protected function deleteEntity(Core\Base\EntityInterface $entity, array $params): ?Core\Base\EntityInterface
113 {
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,
121 ]);
122
123 if (CCalendar::DeleteEvent($entity->getId(), true, $params) && !empty($params['bMarkDeleted']))
124 {
125 $entity->setIsDeleted(true);
126
127 return $entity;
128 }
129
130 return null;
131 }
132
138 private function prepareArrayForDelete(Core\Event\Event $event): ?array
139 {
140 try
141 {
142 return $this->convertToArray($event);
143 }
144 catch(\Throwable $e)
145 {
146 return null;
147 }
148 }
149
158 private function save(Core\Event\Event $event, array $params = []): AddResult
159 {
160 $data = $this->convertToArray($event);
161
162 $params = array_merge($params, [
163 'arFields' => $data,
164 'checkPermissions' => false,
165 'userId' => $event->getOwner() ? $event->getOwner()->getId() : null, // todo how get userId ?
166 ]);
167
168 $id = CCalendar::SaveEvent($params);
169
170 $result = new AddResult();
171 if ($id)
172 {
173 $result->setPrimary($id);
174 }
175 else
176 {
177 $result->addError(new Error('Error of create event'));
178 }
179
180 return $result;
181 }
182
191 private function prepareReminders(Core\Event\Properties\RemindCollection $collection, Core\Base\Date $start): array
192 {
193 $result = [];
194 $collection->setEventStart(clone $start);
195 $collection->deDuplicate();
197 foreach ($collection->getCollection() as $reminder)
198 {
199 $remind = [
200 'before' => null,
201 'count' => null,
202 'time' => null,
203 'value' => null,
204 ];
205 if ($reminder->getDaysBefore() !== null)
206 {
207 $remind['type'] = Core\Event\Properties\Remind::UNIT_DAY_BEFORE;
208 $remind['before'] = $reminder->getDaysBefore();
209 $remind['time'] = $reminder->getTimeOffset();
210 }
211 elseif ($reminder->isSimpleType())
212 {
213 $remind['type'] = str_replace('minutes', 'min', $reminder->getUnits()) ;
214 $remind['count'] = $reminder->getTime();
215 }
216 else
217 {
218 $remind['type'] = Core\Event\Properties\Remind::UNIT_DATES;
219 $remind['value'] = $reminder->getSpecificTime();
220 }
221 $result[] = $remind;
222 }
223
224 return $result;
225 }
226
234 private function convertToArray(Core\Event\Event $event): array
235 {
236 return [
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())
259 : null,
260 'RRULE' => $event->isRecurrence()
261 ? $event->getRecurringRule()->toArray()
262 : null,
263 'EXDATE' => $event->getExcludedDateCollection()
264 ? $event->getExcludedDateCollection()->toString()
265 : null,
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']
273 : null,
274 'SECTIONS' => $event->getSection() ? [$event->getSection()->getId()] : null,
275 'SECTION_ID' => $event->getSection() ? $event->getSection()->getId() : null,
276 'RELATIONS' => $event->getRelations() ? $event->getRelations()->getFields() : null,
277 ];
278 }
279
289 protected function getOneEntityByFilter(array $filter): ?object
290 {
291 $eventData = EventTable::query()
292 ->setFilter($filter)
293 ->setSelect(['*'])
294 ->fetchObject();
295
296 if ($eventData)
297 {
298 return $this->convertToObject($eventData);
299 }
300
301 return null;
302 }
303
309 protected function convertToObject($objectEO): Core\Base\EntityInterface
310 {
311 return (new Core\Builders\EventBuilderFromEntityObject($objectEO))->build();
312 }
313
317 protected function getEntityName(): string
318 {
319 return 'event';
320 }
321
325 protected function getMapClass(): string
326 {
327 return Core\Event\EventMap::class;
328 }
329
339 protected function getDataManagerResult(array $params): Result
340 {
341 $params['select'] = $params['select'] ?? ["*"];
342 return EventTable::getList($params);
343 }
350 public function getMapFullChainByParentId(int $id, int $ownerId, array $fields = ['*']): Core\Event\EventMap
351 {
352 $eventDM = EventTable::query()
353 ->where(Query::filter()
354 ->logic('or')
355 ->where('PARENT_ID', $id)
356 ->where('RECURRENCE_ID', $id)
357 )
358 ->where('OWNER_ID', $ownerId)
359 ->setSelect($fields)
360 ->exec()
361 ;
362
363 $eventMap = new Core\Event\EventMap();
364
365 while ($event = $eventDM->fetch())
366 {
367 $eventMap->add((new Core\Builders\EventBuilderFromArray($event))->build(), (int)$event['ID']);
368 }
369
370 return $eventMap;
371 }
372
382 public function getEntity(int $id, ?array $additionalParams = null): ?Core\Event\Event
383 {
384 $eventDM = EventTable::query()
385 ->where('ID', $id)
386 ;
387
388 if ($additionalParams)
389 {
390 //filter
391 if (isset($additionalParams['filter'])
392 && is_iterable($additionalParams['filter'])
393 )
394 {
395 foreach ($additionalParams['filter'] as $filter)
396 {
397 $eventDM->addFilter($filter['key'], $filter['value']);
398 }
399 }
400
401 //select
402 if (isset($additionalParams['fields'])
403 && is_array($additionalParams['fields'])
404 )
405 {
406 $eventDM->setSelect($additionalParams['fields']);
407 }
408 else
409 {
410 $eventDM->setSelect(['*']);
411 }
412 }
413 else
414 {
415 $eventDM->setSelect(['*']);
416 }
417
418 $event = $eventDM->exec()->fetchObject();
419
420 return $event
421 ? (new Core\Builders\EventBuilderFromEntityObject($event))->build()
422 : null
423 ;
424 }
425
432 public function patch(Core\Event\Event $event, array $fields): Core\Event\Event
433 {
434 $eventFields = [
435 'EXDATE' => (string)$event->getExcludedDateCollection(),
436 ];
437
438 $result = EventTable::update(
439 $event->getId(),
440 array_intersect_key($eventFields, array_flip($fields))
441 );
442
443 if ($result->isSuccess())
444 {
445 return $event;
446 }
447
448 throw new Core\Base\BaseException('do not patch event');
449 }
450
458 public function getByArray(array $fields)
459 {
460 if ($this->getCacheMap()->has($fields['ID']))
461 {
462 return $this->getCacheMap()->getItem($fields['ID']);
463 }
464
465 $entity = $this->convertFromArray($fields);
466 $this->getCacheMap()->add($entity, $entity->getId());
467
468 return $entity;
469 }
470
476 protected function convertFromArray(array $fields): Core\Event\Event
477 {
478 return (new Core\Builders\EventBuilderFromArray($fields))->build();
479 }
480
484 protected function getEntityClass(): string
485 {
486 return Core\Event\Event::class;
487 }
488
494 private function prepareOriginalDateFrom(Core\Event\Event $event): ?string
495 {
496 $result = null;
497 if ($event->getOriginalDateFrom())
498 {
499 if ($event->getStartTimeZone())
500 {
501 $event->getOriginalDateFrom()->setTimezone($event->getStartTimeZone()->getTimeZone());
502 }
503 $result = (string)$event->getOriginalDateFrom();
504 }
505
506 return $result;
507 }
508}
getMapFullChainByParentId(int $id, int $ownerId, array $fields=[' *'])
Definition event.php:350
updateEntity($entity, array $params=[])
Definition event.php:81
getOneEntityByFilter(array $filter)
Definition event.php:289
deleteEntity(Core\Base\EntityInterface $entity, array $params)
Definition event.php:112
getEntity(int $id, ?array $additionalParams=null)
Definition event.php:382
createEntity($entity, array $params=[])
Definition event.php:59
getDataManagerResult(array $params)
Definition event.php:339
patch(Core\Event\Event $event, array $fields)
Definition event.php:432
getCollectionByIds(array $ids, int $ownerId, array $fields=[' *'])
Definition event.php:33