1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
apiservice.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Office365;
4
5use Bitrix\Calendar\Core\Base\BaseException;
6use Bitrix\Calendar\Sync\Dictionary;
7use Bitrix\Calendar\Sync\Exceptions\ApiException;
8use Bitrix\Calendar\Sync\Exceptions\AuthException;
9use Bitrix\Calendar\Sync\Exceptions\ConflictException;
10use Bitrix\Calendar\Sync\Exceptions\GoneException;
11use Bitrix\Calendar\Sync\Exceptions\NotFoundException;
12use Bitrix\Calendar\Sync\Exceptions\PreconditionFailedException;
13use Bitrix\Calendar\Sync\Exceptions\RemoteAccountException;
14use Bitrix\Calendar\Sync\Internals\ContextInterface;
15use Bitrix\Calendar\Sync\Internals\HasContextTrait;
16use Bitrix\Calendar\Sync\Office365\Dto\EventDto;
17use Bitrix\Calendar\Sync\Office365\Dto\SectionDto;
18use Bitrix\Calendar\Sync\Connection\SectionConnection;
19use Bitrix\Main\ArgumentException;
20use Bitrix\Main\ArgumentNullException;
21use Bitrix\Main\LoaderException;
22use CCalendar;
23use Generator;
24
26{
27 use HasContextTrait;
28
29 private const CALENDAR_SELECT_FIELDS = [
30 'id',
31 'name',
32 'color',
33 'hexColor',
34 'isDefaultCalendar',
35 'changeKey',
36 'canShare',
37 'canViewPrivateItems',
38 'canEdit',
39 'isTallyingResponses',
40 'isRemovable',
41 'owner',
42 ];
43
45 private $helper;
47 private $apiClient;
48
57 public function __construct(ContextInterface $context)
58 {
60 $this->context = $context;
61 $this->apiClient = $this->context->getApiClient();
62 $this->helper = $this->context->getHelper();
63 }
64
78 {
79 $selectParams = self::getCalendarSelectParams();
80
81 $params = array_merge($selectParams, $params);
82
83 $response = $this->apiClient->get('me/calendars', $params);
84
85 return (array)$response['value'];
86 }
87
100 public function getEventList(array $params): array
101 {
102 $response = $this->apiClient->get(
103 'me/calendars/' . $params['filter']['section_id'] . '/events',
104 $params
105 );
106
107 return (array) $response['value'];
108 }
109
123 public function createSection(SectionDto $sectionDto): array
124 {
125 $selectParams = self::getCalendarSelectParams();
126
127 $selectQuery = http_build_query($selectParams);
128
129 return $this->apiClient->post('me/calendars?' . $selectQuery, array_filter($sectionDto->toArray()));
130 }
131
144 public function updateSection(SectionDto $sectionDto): array
145 {
146 return $this->apiClient->patch('me/calendars/' . $sectionDto->id, array_filter($sectionDto->toArray()));
147 }
148
162 public function createEvent(EventDto $eventDto, string $sectionId): array
163 {
164 return $this->apiClient->post(
165 'me/calendars/' . $sectionId . '/events',
166 array_filter($eventDto->toArray(true), static function ($val) {
167 return $val !== [] && $val !== null;
168 })
169 );
170 }
171
184 public function getCalendarDelta(SectionConnection $sectionConnection): Generator
185 {
186 $baseUri = 'me/calendars/' . $sectionConnection->getVendorSectionId() . '/calendarView/delta?';
187 $breakingFlag = false;
188 do {
189 $uri = $this->getDeltaUri($sectionConnection, $baseUri);
190
191 try
192 {
193 $response = $this->apiClient->get($uri);
194 if (!empty($response))
195 {
196 $breakingFlag = $this->processResponseAfterDelta($sectionConnection, $response);
197 }
198 if (!empty($response['value']))
199 {
200 yield $response['value'];
201 }
202 else
203 {
204 break;
205 }
206 }
207 catch(GoneException $e)
208 {
209 if ($sectionConnection->getPageToken())
210 {
211 $sectionConnection->setPageToken(null);
212 }
213 elseif ($sectionConnection->getSyncToken())
214 {
215 $sectionConnection->setSyncToken(null);
216 }
217 }
218 }
219 while(!$breakingFlag);
220 }
221
228 private function getUriParam(string $uri, string $name): ?string
229 {
230 $result = null;
231 if ($urlData = parse_url($uri))
232 {
233 parse_str($urlData['query'], $params);
234 $result = $params[$name] ?? null;
235 }
236
237 return $result;
238 }
239
252 public function getEvent(array $params): array
253 {
254 return $this->apiClient->get(
255 'me/events/' . $params['filter']['event_id'],
256 $params
257 );
258 }
259
273 public function updateEvent(EventDto $eventDto, string $vendorEventId): array
274 {
275 if ($eventDto->isCancelled)
276 {
277 $response = $this->apiClient->post(
278 'me/events/' . $vendorEventId . '/cancel',
279 [
280 'Comment' => 'Deleted from Bitrix',
281 ]
282 );
283 }
284 else
285 {
286 try
287 {
288 $response = $this->apiClient->patch(
289 'me/events/' . $vendorEventId,
290 array_filter($eventDto->toArray(true), static function ($val) {
291 return $val !== [] && $val !== null;
292 })
293 );
294 }
296 {
297 return [];
298 }
299 }
300
301 return $response;
302 }
303
317 {
318 return $this->apiClient->get(
319 'me/events/' . $params['filter']['event_id'] . '/instances',
320 [
321 'startDateTime' => $params['filter']['from'],
322 'endDateTime' => $params['filter']['to'],
323 ]
324 );
325 }
326
339 public function deleteEvent(string $vendorEventId)
340 {
341 try
342 {
343 $this->apiClient->delete(
344 'me/events/' . $vendorEventId,
345 );
346 }
347 catch (NotFoundException $exception)
348 {
349 return;
350 }
351 }
352
365 public function deleteSection(string $vendorSectionId)
366 {
367 $this->apiClient->delete(
368 'me/calendars/' . $vendorSectionId,
369 );
370 }
371
385 public function addSectionSubscription(string $vendorSectionId, string $state = ''): array
386 {
387 $data = [
388 'changeType' => 'created,updated,deleted',
389 'notificationUrl' => $this->getNotificationUrl(),
390 'resource' => "me/calendars/$vendorSectionId/events",
391 'expirationDateTime' => $this->getExpirationDateTime(),
392 'clientState' => $state,
393 'latestSupportedTlsVersion' => 'v1_2',
394 ];
395
396 return $this->apiClient->post('subscriptions', $data);
397 }
398
411 public function renewSectionSubscription(string $subscriptionId): array
412 {
413 return $this->apiClient->patch('subscriptions/' . $subscriptionId, [
414 'expirationDateTime' => $this->getExpirationDateTime(),
415 ]);
416 }
417
429 public function deleteSectionSubscription(string $subscriptionId): array
430 {
431 try
432 {
433 return $this->apiClient->delete('subscriptions/' . $subscriptionId);
434 }
435 catch (NotFoundException|AuthException $exception)
436 {
437 return [];
438 }
439 }
440
446 public function getDeltaUri(SectionConnection $sectionConnection, string $baseUri): string
447 {
448 if ($sectionConnection->getPageToken())
449 {
450 $uri = $baseUri . '$skiptoken=' . $sectionConnection->getPageToken();
451 }
452 elseif ($sectionConnection->getSyncToken())
453 {
454 $uri = $baseUri . '$deltatoken=' . $sectionConnection->getSyncToken();
455 }
456 else
457 {
458 $interval = $this->helper->getDeltaInterval();
459 $uri = $baseUri . 'startDateTime=' . $interval['from']->format($this->helper::TIME_FORMAT_LONG)
460 . '&endDateTime=' . $interval['to']->format($this->helper::TIME_FORMAT_LONG);
461 }
462 return $uri;
463 }
464
473 public static function getCalendarSelectParams(): array
474 {
475 return [
476 '$select' => implode(',', self::CALENDAR_SELECT_FIELDS),
477 ];
478 }
479
486 private function processResponseAfterDelta(SectionConnection $sectionConnection, array $response): bool
487 {
488 $sectionConnection->setLastSyncStatus(Dictionary::SYNC_STATUS['success']);
489 $breakingFlag = true;
490
491 if ($token = $this->getPageToken($response))
492 {
493 $sectionConnection->setPageToken($token);
494 $breakingFlag = false;
495 }
496 elseif ($token = $this->getSyncToken($response))
497 {
498 $sectionConnection->setPageToken(null);
499 $sectionConnection->setSyncToken($token);
500 }
501 else
502 {
503 $sectionConnection->setPageToken(null);
504 $sectionConnection->setSyncToken(null);
505 }
506
507 return $breakingFlag;
508 }
509
515 private function getPageToken(array $response): ?string
516 {
517 return !empty($response['@odata.nextLink'])
518 ? $this->getUriParam($response['@odata.nextLink'], '$skiptoken')
519 : null
520 ;
521 }
527 private function getSyncToken(array $response): ?string
528 {
529 return !empty($response['@odata.deltaLink'])
530 ? $this->getUriParam($response['@odata.deltaLink'], '$deltatoken')
531 : null
532 ;
533 }
534
538 private function getNotificationUrl(): string
539 {
540 return str_replace('http:', 'https:', CCalendar::GetServerPath())
541 . $this->helper::PUSH_PATH;
542 }
543
547 private function getExpirationDateTime(): string
548 {
549 $time = time() + 70 * 60 * 60;
550 return date("c", $time);
551 }
552}
setLastSyncStatus(?string $lastSyncStatus)
Определения sectionconnection.php:207
const SYNC_STATUS
Определения dictionary.php:16
getCalendarDelta(SectionConnection $sectionConnection)
Определения apiservice.php:184
getEventList(array $params)
Определения apiservice.php:100
renewSectionSubscription(string $subscriptionId)
Определения apiservice.php:411
getCalendarList(array $params)
Определения apiservice.php:77
deleteSection(string $vendorSectionId)
Определения apiservice.php:365
createSection(SectionDto $sectionDto)
Определения apiservice.php:123
deleteEvent(string $vendorEventId)
Определения apiservice.php:339
updateSection(SectionDto $sectionDto)
Определения apiservice.php:144
updateEvent(EventDto $eventDto, string $vendorEventId)
Определения apiservice.php:273
getEvent(array $params)
Определения apiservice.php:252
getDeltaUri(SectionConnection $sectionConnection, string $baseUri)
Определения apiservice.php:446
addSectionSubscription(string $vendorSectionId, string $state='')
Определения apiservice.php:385
deleteSectionSubscription(string $subscriptionId)
Определения apiservice.php:429
getEventInstances(array $params)
Определения apiservice.php:316
createEvent(EventDto $eventDto, string $sectionId)
Определения apiservice.php:162
toArray(bool $filterEmptyValue=false)
Определения dto.php:33
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$context
Определения csv_new_setup.php:223
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
Определения urlrewrite.php:61
$name
Определения menu_edit.php:35
$time
Определения payment.php:61
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$val
Определения options.php:1793
$response
Определения result.php:21