Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vendorsyncservice.php
1<?php
2
4
12use Bitrix\Calendar\Sync\Internals\HasContextTrait;
20use Generator;
21
23{
24 use HasContextTrait;
25
29 private ApiService $apiService;
30
39 public function __construct(Office365Context $context)
40 {
41 $this->context = $context;
42 $this->apiService = $context->getApiService();
43 }
44
57 public function getSections(array $params = []): array
58 {
59 $result = $this->apiService->getCalendarList($params);
60 return array_map(function ($row){
61 return new SectionDto($row);
62 }, $result);
63 }
64
77 public function getEvents(array $params): array
78 {
79 $result = $this->apiService->getEventList($params);
80
81 return array_map(function ($row){
82 return new EventDto($row);
83 }, $result);
84 }
85
98 public function getEvent(array $params): array
99 {
100 $result = $this->apiService->getEvent($params);
101
102 return array_map(function ($row){
103 return new EventDto($row);
104 }, $result);
105 }
106
119 public function createSection(SectionDto $sectionDto): SectionDto
120 {
121 $newSection = $this->apiService->createSection($sectionDto);
122
123 return new SectionDto($newSection);
124 }
125
138 public function updateSection(SectionDto $sectionDto): SectionDto
139 {
140 $newSection = $this->apiService->updateSection($sectionDto);
141
142 return new SectionDto($newSection);
143 }
144
158 public function createEvent(EventDto $dto, string $sectionId): ?EventDto
159 {
160 if ($newEvent = $this->apiService->createEvent($dto, $sectionId))
161 {
162 return new EventDto($newEvent);
163 }
164
165 return null;
166 }
167
180 public function getCalendarDelta(SectionConnection $sectionLink): Generator
181 {
182 foreach ($this->apiService->getCalendarDelta($sectionLink) as $batch)
183 {
184 $events = [];
185 foreach ($batch as $item) {
186 if (!empty($item['@removed']))
187 {
188 $events[$item['id']][Helper::EVENT_TYPES['deleted']] = new EventDto($item);
189 }
190 elseif ($item['type'] === Helper::EVENT_TYPES['single'])
191 {
192 $events[$item['id']][$item['type']] = new EventDto($item);
193 }
194 elseif ($item['type'] === Helper::EVENT_TYPES['series'])
195 {
196 $events[$item['id']][$item['type']] = new EventDto($item);
197 }
198 elseif ($item['type'] === Helper::EVENT_TYPES['exception'])
199 {
200 $events[$item['seriesMasterId']][Helper::EVENT_TYPES['exception']][$item['id']] = new EventDto($item);
201 $events[$item['seriesMasterId']][Helper::EVENT_TYPES['occurrence']][] = new DateTimeDto($item['start']);
202 }
203 elseif ($item['type'] === Helper::EVENT_TYPES['occurrence'])
204 {
205 $events[$item['seriesMasterId']][Helper::EVENT_TYPES['occurrence']][] = new DateTimeDto($item['start']);
206 }
207 }
208 foreach ($events as $id => $eventDelta)
209 {
210 yield $id => $eventDelta;
211 }
212 }
213 }
214
228 public function updateEvent(string $vendorEventId, EventDto $eventDto): ?EventDto
229 {
230 if ($event = $this->apiService->updateEvent($eventDto, $vendorEventId))
231 {
232 return new EventDto($event);
233 }
234
235 return null;
236 }
237
250 public function getEventInstances(array $params): array
251 {
252 $result = $this->apiService->getEventInstances($params);
253
254 if (!empty($result['value']))
255 {
256 return array_map(function ($row){
257 return new EventDto($row);
258 }, $result['value']) ?? [];
259 }
260
261 return [];
262 }
263
276 public function deleteEvent(string $vendorEventId)
277 {
278 $this->apiService->deleteEvent($vendorEventId);
279 }
280
293 public function deleteSection(SectionDto $dto)
294 {
295 $this->apiService->deleteSection($dto->id);
296 }
297
310 public function subscribeSection(SectionConnection $link): ?array
311 {
312 $channelId = $this->getChannelId($link);
313 $result = $this->apiService->addSectionSubscription(
314 $link->getVendorSectionId(),
315 $channelId
316 );
317
318 if ($result)
319 {
320 $result['channelId'] = $channelId;
321 }
322
323 return $result;
324 }
325
338 public function resubscribe(string $subscribeId): array
339 {
340 return $this->apiService->renewSectionSubscription($subscribeId);
341 }
342
354 public function unsubscribe(string $subscribeId): array
355 {
356 return $this->apiService->deleteSectionSubscription($subscribeId);
357 }
358
363 private function getChannelId(SectionConnection $link): string
364 {
365 return 'BX_OFFICE_SC_' . $link->getConnection()->getOwner()->getId() . '_' . md5($link->getId() . time());
366 }
367}
updateEvent(string $vendorEventId, EventDto $eventDto)