Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventmanager.php
1<?php
2
4
6use Bitrix\Calendar\Core;
18use Generator;
19
21{
34 public function create(Event $event, EventContext $context): Result
35 {
36 $result = new Result();
37
38 $data = $this->getApiService()->createEvent($context->getSectionConnection()->getVendorSectionId(), $event);
39
40 if ($this->getApiService()->getError())
41 {
42 $this->processConnectionError($this->connection, $this->getApiService()->getError());
43 }
44
45 if ($data && is_array($data))
46 {
47 $result->setData([
48 'event' => [
49 'id' => $data['XML_ID'],
50 'version' => $data['MODIFICATION_LABEL'],
51 'etag' => $data['MODIFICATION_LABEL'],
52 ],
53 ]);
54 }
55 else
56 {
57 $result->addError(new Error('Error while trying to save event'));
58 }
59
60 return $result;
61 }
62
75 public function update(Event $event, EventContext $context): Result
76 {
77 $result = new Result();
78 $event->setUid($context->getEventConnection()->getVendorEventId());
79
80 if ($event->getRecurringRule())
81 {
82 return $this->saveInstance($event, $context);
83 }
84
85 $data = $this->getApiService()->updateEvent(
86 $context->getSectionConnection()->getVendorSectionId(),
87 $event,
88 $context->getEventConnection()->getData()
89 );
90
91 if ($this->getApiService()->getError())
92 {
93 $this->processConnectionError($this->connection, $this->getApiService()->getError());
94 }
95
96 if ($data && is_array($data))
97 {
98 $result->setData([
99 'event' => [
100 'id' => $data['XML_ID'],
101 'version' => $data['MODIFICATION_LABEL'],
102 'etag' => $data['MODIFICATION_LABEL'],
103 ],
104 ]);
105 }
106 else
107 {
108 $result->addError(new Error('Error while trying to update event'));
109 }
110
111 return $result;
112 }
113
123 public function delete(Event $event, EventContext $context): Result
124 {
125 $result = new Result();
126 $sectionId = $context->getSectionConnection()->getVendorSectionId();
127 $event->setUid($context->getEventConnection()->getVendorEventId());
128
129 $data = $this->getApiService()->deleteEvent($sectionId, $event);
130
131 if ($this->getApiService()->getError())
132 {
133 $this->processConnectionError($this->connection, $this->getApiService()->getError());
134 }
135
136 if (!$data)
137 {
138 $result->addError(new Error('Error while trying delete event'));
139 }
140
141 return $result;
142 }
143
156 public function createInstance(Event $event, EventContext $context): Result
157 {
158 return $this->saveInstance($event, $context);
159 }
160
173 public function updateInstance(Event $event, EventContext $context): Result
174 {
175 return $this->saveInstance($event, $context);
176 }
177
190 private function saveInstance(Event $event, EventContext $context): Result
191 {
192 $result = new Result();
193 $masterLink = $context->getEventConnection();
194
195 if (!$masterLink)
196 {
197 $result->addError(new Error('Master link not found'));
198
199 return $result;
200 }
201
202 $sectionId = $context->getSectionConnection()->getVendorSectionId();
203 $masterEvent = $masterLink->getEvent();
204 $masterEvent->setUid($masterLink->getVendorEventId());
205
206 $data = $this->getApiService()->saveInstance(
207 $sectionId,
208 $masterEvent,
209 $masterLink->getData()
210 );
211
212 if ($this->getApiService()->getError())
213 {
214 $this->processConnectionError($this->connection, $this->getApiService()->getError());
215 }
216
217 if ($data && is_array($data))
218 {
219 $result->setData([
220 'event' => [
221 'id' => $data['XML_ID'],
222 'version' => $data['MODIFICATION_LABEL'],
223 'etag' => $data['MODIFICATION_LABEL'],
224 ],
225 ]);
226 }
227 else
228 {
229 $result->addError(new Error('Error while trying to save instance'));
230 }
231
232 return $result;
233 }
234
247 public function deleteInstance(Event $event, EventContext $context): Result
248 {
249 $result = new Result();
250 $masterLink = $context->getEventConnection();
251
252 if (!$masterLink)
253 {
254 $result->addError(new Error('Master link not found'));
255
256 return $result;
257 }
258
259 $sectionId = $context->getSectionConnection()->getVendorSectionId();
260 $excludeDate = $context->sync['excludeDate'];
261 $masterEvent = $masterLink->getEvent();
262 $masterEvent->setUid($masterLink->getVendorEventId());
263
264 $data = $this->getApiService()->saveInstance(
265 $sectionId,
266 $masterEvent,
267 $masterLink->getData(),
268 $excludeDate
269 );
270
271 if ($this->getApiService()->getError())
272 {
273 $this->processConnectionError($this->connection, $this->getApiService()->getError());
274 }
275
276 if ($data && is_array($data))
277 {
278 $result->setData([
279 'event' => [
280 'id' => $data['XML_ID'],
281 'version' => $data['MODIFICATION_LABEL'],
282 'etag' => $data['MODIFICATION_LABEL'],
283 ],
284 ]);
285 }
286 else
287 {
288 $result->addError(new Error('Error while trying to delete instance'));
289 }
290
291 return $result;
292 }
293
307 public function saveRecurrence(
308 SyncEvent $recurrenceEvent,
309 SectionConnection $sectionConnection,
310 Context $context
311 ): Result
312 {
313 $result = new Result();
314 $sectionId = $context->sync['vendorSectionId'];
315
316 $data = $this->getApiService()->saveRecurrence($sectionId, $recurrenceEvent);
317
318 if ($this->getApiService()->getError())
319 {
320 $this->processConnectionError($sectionConnection->getConnection(), $this->getApiService()->getError());
321 }
322
323 if ($data && is_array($data))
324 {
325 $masterEvent = $recurrenceEvent;
326
327 $this->prepareLink($masterEvent, $sectionConnection, $data['XML_ID'], $data['MODIFICATION_LABEL']);
328
330 foreach ($recurrenceEvent->getInstanceMap()->getCollection() as $instance)
331 {
332 $this->prepareLink($instance, $sectionConnection, $data['XML_ID']);
333 $instance->getEventConnection()->setRecurrenceId($data['XML_ID']);
334 }
335 }
336 else
337 {
338 $result->addError(new Error('Error while trying to save recurrence event'));
339 }
340
341 return $result;
342 }
343
356 public function createRecurrence(
357 SyncEvent $recurrenceEvent,
358 SectionConnection $sectionConnection,
359 Context $context
360 ): Result
361 {
362 return $this->saveRecurrence($recurrenceEvent, $sectionConnection, $context);
363 }
364
377 public function updateRecurrence(
378 SyncEvent $recurrenceEvent,
379 SectionConnection $sectionConnection,
380 Context $context
381 ): Result
382 {
383 return $this->saveRecurrence($recurrenceEvent, $sectionConnection, $context);
384 }
385
389 private function getApiService(): ApiService
390 {
391 if (!$this->apiService)
392 {
393 $this->apiService = new ApiService();
394 }
395
396 return $this->apiService;
397 }
398
407 private function prepareLink(
408 SyncEvent $event,
409 SectionConnection $connection,
410 string $vendorId,
411 ?string $entityTag = null
412 ): void
413 {
414 if ($event->getEventConnection())
415 {
416 $event->getEventConnection()
417 ->setLastSyncStatus(Calendar\Sync\Dictionary::SYNC_STATUS['success'])
418 ->setEntityTag($entityTag);
419 }
420 else
421 {
422 $link = (new EventConnection())
423 ->setId(null)
424 ->setEvent($event->getEvent())
425 ->setVersion($event->getEvent()->getVersion())
426 ->setConnection($connection->getConnection())
427 ->setVendorEventId($vendorId)
428 ->setEntityTag($entityTag)
429 ->setLastSyncStatus(Calendar\Sync\Dictionary::SYNC_STATUS['success'])
430 ;
431 $event->setEventConnection($link);
432 }
433 }
434
441 private function processConnectionError(Connection $connection, array $error): void
442 {
443 $parsedError = '[' . $error[0] . '] ' . $error[1];
444 \CDavConnection::SetLastResult($connection->getId(), $parsedError);
445 }
446}
updateRecurrence(SyncEvent $recurrenceEvent, SectionConnection $sectionConnection, Context $context)
createRecurrence(SyncEvent $recurrenceEvent, SectionConnection $sectionConnection, Context $context)
create(Event $event, EventContext $context)
updateInstance(Event $event, EventContext $context)
deleteInstance(Event $event, EventContext $context)
createInstance(Event $event, EventContext $context)
update(Event $event, EventContext $context)