• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/lib/sync/factories/synceventfactory.php
  • Класс: BitrixCalendarSyncFactoriesSyncEventFactory
  • Вызов: SyncEventFactory::getSyncEventMapBySyncSectionIdCollectionForExport
public function getSyncEventMapBySyncSectionIdCollectionForExport(
	array $sectionIdList,
	int $userId,
	int $connectionId
): ?SyncEntitiesSyncEventMap
{
	Loader::includeModule('dav');

	if (!$sectionIdList)
	{
		return null;
	}

	$timestamp = time() - self::TIME_SLICE;
	$eventDb = EventTable::query()
		->setSelect([
            '*',
            'EVENT_CONNECTION.*',
            'EVENT_CONNECTION.CONNECTION',
            'EVENT_CONNECTION.EVENT',
            ])
		->where('OWNER_ID', $userId)
		->where('CAL_TYPE', Dictionary::EVENT_TYPE['user'])
		->where('DELETED', 'N')
		->where('DATE_TO_TS_UTC', '>', $timestamp)
		// ->whereNot('MEETING_STATUS', 'N')
		->where(Query::filter() // TODO: it's better to optimize it and don't use 'OR' logic here
				 ->logic('or')
				 ->whereNot('MEETING_STATUS', 'N')
				 ->whereNull('MEETING_STATUS')
		)
		->whereIn('SECTION_ID', $sectionIdList)
		->registerRuntimeField('EVENT_CONNECTION',
			new ReferenceField(
				'SYNC_DATA',
				EventConnectionTable::getEntity(),
				Join::on('ref.EVENT_ID', 'this.ID')
					->where('ref.CONNECTION_ID', $connectionId)
				,
				['join_type' => Join::TYPE_LEFT]
			)
		)
		->addOrder('ID')
		->exec()
	;

	$map = new SyncEntitiesSyncEventMap();
	$impatientSyncEventInstanceList = [];

	while ($eventDM = $eventDb->fetchObject())
	{
		$action = SyncDictionary::SYNC_EVENT_ACTION['create'];
		$syncEvent = new SyncEntitiesSyncEvent();

		$event = (new EventBuilderFromEntityObject($eventDM))->build();
		$syncEvent->setEvent($event);

		/** @var EO_SectionConnection $sectionConnectionDM */
		if ($eventConnectionDM = $eventDM->get('EVENT_CONNECTION'))
		{
			$eventConnection = (new SyncBuildersBuilderEventConnectionFromDM($eventConnectionDM))->build();
			$eventConnection->setEvent($event);
			$syncEvent->setEventConnection($eventConnection);

			if (
				in_array($eventConnection->getLastSyncStatus(), SyncDictionary::SYNC_EVENT_ACTION, true)
				&& ($eventConnection->getLastSyncStatus() !== SyncDictionary::SYNC_EVENT_ACTION['success'])
			)
			{
				$action = $eventConnection->getLastSyncStatus();
			}
			elseif ($event->getVersion() > $eventConnection->getVersion())
			{
				$action = SyncDictionary::SYNC_EVENT_ACTION['update'];
			}
			else
			{
				$action = SyncDictionary::SYNC_EVENT_ACTION['success'];
			}
		}

		if ($syncEvent->isInstance())
		{
			$syncEvent->setAction($action);
			/** @var SyncEvent $masterEvent */
			$masterEvent = $map->getItem($event->getUid());
			if ($masterEvent)
			{
				if (
					$masterEvent->getAction() === SyncDictionary::SYNC_EVENT_ACTION['success']
					&& $syncEvent->getAction() !== SyncDictionary::SYNC_EVENT_ACTION['success']
				)
				{
					$masterEvent->setAction(SyncDictionary::SYNC_EVENT_ACTION['update']);
				}

				$masterEvent->addInstance($syncEvent);

				continue;
			}

			$impatientSyncEventInstanceList[$event->getUid()][] = $syncEvent;
		}
		else
		{
			if ($instanceList = ($impatientSyncEventInstanceList[$event->getUid()] ?? null))
			{
				$syncEvent->addInstanceList($instanceList);
				unset($impatientSyncEventInstanceList[$event->getUid()]);
				if (
					$syncEvent->getAction() !== SyncDictionary::SYNC_EVENT_ACTION['success']
					&& $this->hasCandidatesForUpdate($instanceList)
				)
				{
					$action = SyncDictionary::SYNC_EVENT_ACTION['update'];
				}
			}
			$syncEvent->setAction($action);
			$map->add($syncEvent, $event->getUid());
		}
	}

	return $map;
}