1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
synceventfactory.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Factories;
4
5use Bitrix\Calendar\Core\Builders\EventBuilderFromEntityObject;
6use Bitrix\Calendar\Core\Event\Tools\Dictionary;
7use Bitrix\Calendar\Core\Mappers;
8use Bitrix\Calendar\Internals\EO_SectionConnection;
9use Bitrix\Calendar\Internals\EventConnectionTable;
10use Bitrix\Calendar\Internals\EventTable;
11use Bitrix\Calendar\Sync;
12use Bitrix\Calendar\Sync\Entities\SyncEvent;
13use Bitrix\Main\ArgumentException;
14use Bitrix\Main\DI\ServiceLocator;
15use Bitrix\Main\Entity\ReferenceField;
16use Bitrix\Main\Loader;
17use Bitrix\Main\LoaderException;
18use Bitrix\Main\ObjectPropertyException;
19use Bitrix\Main\ORM\Query\Filter\ConditionTree;
20use Bitrix\Main\ORM\Query\Join;
21use Bitrix\Main\ORM\Query\Query;
22use Bitrix\Main\SystemException;
23
25{
26 const TIME_SLICE = 2600000;
27
28 private Mappers\Event $eventMapper;
29 private Mappers\EventConnection $eventConnectionMapper;
30
31 public function __construct()
32 {
33 $helper = ServiceLocator::getInstance()->get('calendar.service.mappers.factory');
34 $this->eventMapper = $helper->getEvent();
35 $this->eventConnectionMapper = $helper->getEventConnection();
36 }
37
42 public function delete(Sync\Entities\SyncEvent $syncEvent): void
43 {
44 $this->eventMapper->delete($syncEvent->getEvent());
45 $this->eventConnectionMapper->delete($syncEvent->getEventConnection());
46 }
47
55 public function getSyncEventCollectionByVendorIdList(
56 array $vendorEventIdList,
57 int $connectionId
58 ): Sync\Entities\SyncEventMap
59 {
60 $syncEventMap = new Sync\Entities\SyncEventMap();
61
62 if (empty($vendorEventIdList))
63 {
64 return $syncEventMap;
65 }
66
67 $query1 = EventConnectionTable::query()
68 ->setSelect(['ID'])
69 ->where('CONNECTION_ID', $connectionId)
70 ->whereIn('VENDOR_EVENT_ID', $vendorEventIdList)
71 ;
72 $query2 = EventConnectionTable::query()
73 ->setSelect(['ID'])
74 ->where('CONNECTION_ID', $connectionId)
75 ->whereIn('RECURRENCE_ID', $vendorEventIdList)
76 ;
77
78 $query1->unionAll($query2);
79 $result = $query1->exec()->fetchAll();
80
81 $idList = array_map(static function($item) {return (int)$item['ID'];}, $result);
82
83 if (empty($idList))
84 {
85 return $syncEventMap;
86 }
87
88 $filter = [
89 ['ID', 'in', $idList],
90 ];
91 $query = ConditionTree::createFromArray($filter);
92 $eventConnectionMap = $this->eventConnectionMapper->getMap($query);
93
94 $impatientExportSyncEventList = [];
95
97 foreach ($eventConnectionMap as $eventConnection)
98 {
99 $syncEvent = new SyncEvent();
100 $syncEvent
101 ->setEventConnection($eventConnection)
102 ->setEvent($eventConnection->getEvent())
103 ->setAction($eventConnection->getLastSyncStatus())
104 ;
105
106 if ($syncEvent->isInstance())
107 {
109 if ($masterSyncEvent = $syncEventMap->getItem($syncEvent->getVendorRecurrenceId()))
110 {
111 $masterSyncEvent->addInstance($syncEvent);
112 continue;
113 }
114
115 $impatientExportSyncEventList[$syncEvent->getVendorRecurrenceId()][$eventConnection->getVendorEventId()] = $syncEvent;
116 continue;
117 }
118
119 if (
120 $syncEvent->isRecurrence()
121 && $instanceList = ($impatientExportSyncEventList[$syncEvent->getVendorEventId()] ?? null)
122 )
123 {
124 $syncEvent->addInstanceList($instanceList);
125 }
126
127 $syncEventMap->add(
128 $syncEvent,
129 $eventConnection->getVendorEventId()
130 );
131 }
132
133 if ($impatientExportSyncEventList)
134 {
135 foreach ($impatientExportSyncEventList as $syncEventList)
136 {
137 $syncEventMap->addItems($syncEventList);
138 }
139 }
140
141 return $syncEventMap;
142 }
143
156 public function getSyncEventMapBySyncSectionIdCollectionForExport(
157 array $sectionIdList,
158 int $userId,
159 int $connectionId
160 ): ?Sync\Entities\SyncEventMap
161 {
162 Loader::includeModule('dav');
163
164 if (!$sectionIdList)
165 {
166 return null;
167 }
168
169 $timestamp = time() - self::TIME_SLICE;
170 $eventDb = EventTable::query()
171 ->setSelect([
172 '*',
173 'EVENT_CONNECTION.ID',
174 'EVENT_CONNECTION.EVENT_ID',
175 'EVENT_CONNECTION.CONNECTION_ID',
176 'EVENT_CONNECTION.VENDOR_EVENT_ID',
177 'EVENT_CONNECTION.SYNC_STATUS',
178 'EVENT_CONNECTION.RETRY_COUNT',
179 'EVENT_CONNECTION.ENTITY_TAG',
180 'EVENT_CONNECTION.VERSION',
181 'EVENT_CONNECTION.VENDOR_VERSION_ID',
182 'EVENT_CONNECTION.RECURRENCE_ID',
183 'EVENT_CONNECTION.CONNECTION',
184 'EVENT_CONNECTION.EVENT',
185 ])
186 ->where('OWNER_ID', $userId)
187 ->where('CAL_TYPE', Dictionary::CALENDAR_TYPE['user'])
188 ->where('DELETED', 'N')
189 ->where('DATE_TO_TS_UTC', '>', $timestamp)
190 ->where(Query::filter()
191 ->logic('or')
192 ->whereNot('MEETING_STATUS', 'N')
193 ->whereNull('MEETING_STATUS')
194 )
195 ->whereIn('SECTION_ID', $sectionIdList)
196 ->registerRuntimeField('EVENT_CONNECTION',
197 new ReferenceField(
198 'SYNC_DATA',
199 EventConnectionTable::getEntity(),
200 Join::on('ref.EVENT_ID', 'this.ID')
201 ->where('ref.CONNECTION_ID', $connectionId)
202 ,
203 ['join_type' => Join::TYPE_LEFT]
204 )
205 )
206 ->addOrder('ID')
207 ->exec()
208 ;
209
210 $map = new Sync\Entities\SyncEventMap();
211 $impatientSyncEventInstanceList = [];
212
213 while ($eventDM = $eventDb->fetchObject())
214 {
215 $action = Sync\Dictionary::SYNC_EVENT_ACTION['create'];
216 $syncEvent = new Sync\Entities\SyncEvent();
217
218 $event = (new EventBuilderFromEntityObject($eventDM))->build();
219 $syncEvent->setEvent($event);
220
222 if ($eventConnectionDM = $eventDM->get('EVENT_CONNECTION'))
223 {
224 $eventConnection = (new Sync\Builders\BuilderEventConnectionFromDM($eventConnectionDM))->build();
225 $eventConnection->setEvent($event);
226 $syncEvent->setEventConnection($eventConnection);
227
228 if (
229 in_array($eventConnection->getLastSyncStatus(), Sync\Dictionary::SYNC_EVENT_ACTION, true)
230 && ($eventConnection->getLastSyncStatus() !== Sync\Dictionary::SYNC_EVENT_ACTION['success'])
231 )
232 {
233 $action = $eventConnection->getLastSyncStatus();
234 }
235 elseif ($event->getVersion() > $eventConnection->getVersion())
236 {
237 $action = Sync\Dictionary::SYNC_EVENT_ACTION['update'];
238 }
239 else
240 {
241 $action = Sync\Dictionary::SYNC_EVENT_ACTION['success'];
242 }
243 }
244
245 if ($syncEvent->isInstance())
246 {
247 $syncEvent->setAction($action);
249 $masterEvent = $map->getItem($event->getUid());
250 if ($masterEvent)
251 {
252 if (
253 $masterEvent->getAction() === Sync\Dictionary::SYNC_EVENT_ACTION['success']
254 && $syncEvent->getAction() !== Sync\Dictionary::SYNC_EVENT_ACTION['success']
255 )
256 {
257 $masterEvent->setAction(Sync\Dictionary::SYNC_EVENT_ACTION['update']);
258 }
259
260 $masterEvent->addInstance($syncEvent);
261
262 continue;
263 }
264
265 $impatientSyncEventInstanceList[$event->getUid()][] = $syncEvent;
266 }
267 else
268 {
269 if ($instanceList = ($impatientSyncEventInstanceList[$event->getUid()] ?? null))
270 {
271 $syncEvent->addInstanceList($instanceList);
272 unset($impatientSyncEventInstanceList[$event->getUid()]);
273 if (
274 $syncEvent->getAction() !== Sync\Dictionary::SYNC_EVENT_ACTION['success']
275 && $this->hasCandidatesForUpdate($instanceList)
276 )
277 {
278 $action = Sync\Dictionary::SYNC_EVENT_ACTION['update'];
279 }
280 }
281 $syncEvent->setAction($action);
282 $map->add($syncEvent, $event->getUid());
283 }
284 }
285
286 return $map;
287 }
288
294 private function hasCandidatesForUpdate(array $list): bool
295 {
296 return (bool)array_filter($list, function (SyncEvent $syncEvent) {
297 return $syncEvent->getAction() !== Sync\Dictionary::SYNC_EVENT_ACTION['success'];
298 });
299 }
300}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
const SYNC_EVENT_ACTION
Определения dictionary.php:47
</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
$query
Определения get_search.php:11
$filter
Определения iblock_catalog_list.php:54
$map
Определения config.php:5
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$action
Определения file_dialog.php:21