Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
googleapibatch.php
1<?php
2
4
11use CCalendarEvent;
12
17{
18 protected const FINISH = true;
19 protected const NEXT = false;
20
24 public static function createInstance(): GoogleApiBatch
25 {
26 return new self();
27 }
28
32 public function __construct()
33 {
34 }
35
36 public function syncLocalEvents(array $events, int $userId, string $gApiCalendarId): array
37 {
38 return $this->syncEvents($events, $userId, $gApiCalendarId);
39 }
40
41
42 private function syncEvents($events, $userId, $gApiCalendarId): array
43 {
44 return $this->mergeLocalEventWithExternalData(
45 $events, $this->getEventsExternalFields($userId, $events, $gApiCalendarId));
46 }
47
52 public function syncLocalInstances(array $instances, int $userId, string $gApiCalendarId): array
53 {
54 return $this->mergeLocalEventWithExternalData(
55 $instances,
56 $this->getInstancesExternalFields($userId, $instances, $gApiCalendarId)
57 );
58 }
59
68 private function calculateExDate(int $eventId, string $exDates): string
69 {
70 return implode(';', array_diff(
71 explode(';', $exDates), $this->getExDatesByInstances($eventId)
72 ));
73 }
74
82 private function getInstanceByRecurrenceId(int $eventId): array
83 {
84 $instancesDb = Internals\EventTable::getList([
85 'filter' => Query::filter()->where('RECURRENCE_ID', $eventId),
86 ]);
87
88 $instances =[];
89 while ($instance = $instancesDb->fetch())
90 {
91 $instances[] = $instance;
92 }
93
94 return $instances;
95 }
96
97
105 private function preparedEventToBatch(array $events): array
106 {
107 return $events;
108 }
109
117 private function getExDatesByInstances(int $originalEventId): array
118 {
119 $instances = $this->getInstanceByRecurrenceId($originalEventId);
120
121 $excludeDates = [];
122 foreach ($instances as $instance)
123 {
124 $excludeDates[] = \CCalendar::Date(\CCalendar::Timestamp($instance['DATE_FROM']), false);
125 }
126
127 return $excludeDates;
128 }
129
135 private function prepareInstanceToBatch(array $instances): array
136 {
137 $batch = [];
138 foreach ($instances as $instance)
139 {
140 $currentInstance = $instance;
141 if (empty($instance['ORIGINAL_DATE_FROM']))
142 {
143 $instanceDate = \CCalendar::GetOriginalDate(
144 $instance['PARENT_DATE_FROM'],
145 $instance['DATE_FROM'],
146 $instance['PARENT_TZ_FROM']
147 );
148 }
149 else
150 {
151 $instanceDate = $instance['ORIGINAL_DATE_FROM'];
152 }
154 $eventOriginalStart = Util::getDateObject($instanceDate, false, $instance['PARENT_TZ_FROM']);
155 $currentInstance['ORIGINAL_DATE_FROM'] =
156 $eventOriginalStart->format(Type\Date::convertFormatToPhp(FORMAT_DATETIME));
157 $currentInstance['DAV_XML_ID'] = $instance['PARENT_DAV_XML_ID'];
158 $currentInstance['gEventId'] = $instance['PARENT_G_EVENT_ID'] . '_'
159 . $eventOriginalStart->setTimeZone(Util::prepareTimezone())->format('Ymd\THis\Z');
160 $currentInstance['G_EVENT_ID'] = $currentInstance['gEventId'];
161 $batch[] = $currentInstance;
162 }
163
164 return $batch;
165 }
166
172 private function mergeLocalEventWithExternalData(array $events, array $externalFields): array
173 {
174 $resultEvents = [];
175
176 foreach ($events as $event)
177 {
178 if (isset($externalFields[$event['ID']]))
179 {
180 $event['SYNC_STATUS'] = Dictionary::SYNC_STATUS['success'];
181 $resultEvents[] = array_merge($event, $externalFields[$event['ID']]);
182 }
183 else
184 {
185 \CCalendarEvent::updateSyncStatus($event['ID'], Dictionary::SYNC_STATUS['undefined']);
186 }
187 }
188
189 return $resultEvents;
190 }
191
200 private function getInstancesExternalFields(int $userId, array $instances, string $gApiCalendarId): array
201 {
202 return (new GoogleApiSync($userId))
203 ->saveBatchEvents($this->prepareInstanceToBatch($instances), $gApiCalendarId, ['method' => HttpClient::HTTP_PUT])
204 ;
205 }
206
216 private function getEventsExternalFields(int $userId, array $events, string $gApiCalendarId): array
217 {
218 return (new GoogleApiSync($userId))
219 ->saveBatchEvents($this->preparedEventToBatch($events), $gApiCalendarId, ['method' => HttpClient::HTTP_POST])
220 ;
221 }
222}
syncLocalInstances(array $instances, int $userId, string $gApiCalendarId)
syncLocalEvents(array $events, int $userId, string $gApiCalendarId)
static prepareTimezone(?string $tz=null)
Definition util.php:75
static getDateObject(string $date=null, ?bool $fullDay=true, ?string $tz='UTC')
Definition util.php:102