private function syncCalendarSections(
Connection $connection,
array $calendars
): array
{
$calendarNames = [];
$result = [];
foreach ($calendars as $calendar)
{
if (isset($calendar['TYPE']) && $calendar['TYPE'] === 'VEVENT')
{
$calendarNames[$calendar['XML_ID']] = $calendar;
}
}
$sectionsLink = SectionConnectionTable::query()
->setSelect([
'SECTION_CONNECTION_ID' => 'ID',
'NAME' => 'SECTION.NAME',
'EXTERNAL_TYPE' => 'SECTION.EXTERNAL_TYPE',
'VENDOR_SECTION_ID',
'VERSION_ID',
'SECTION_ID',
])
->where('SECTION.CAL_TYPE', self::ENTITY_TYPE)
->where('SECTION.OWNER_ID', $connection->getOwner()->getId())
->where('CONNECTION_ID', $connection->getId())
->exec()
;
while ($link = $sectionsLink->fetch())
{
$xmlId = $link['VENDOR_SECTION_ID'];
if (empty($xmlId))
{
continue;
}
if (!array_key_exists($xmlId, $calendarNames))
{
$section = $this->mapperFactory->getSection()->getById($link['SECTION_ID']);
if ($section)
{
(new IncomingManager($connection))->deleteSection($section, $link['SECTION_CONNECTION_ID']);
}
}
else
{
if (($link['VERSION_ID'] ?? null) !== ($calendarNames[$xmlId]['MODIFICATION_LABEL'] ?? null))
{
$fields = [
'ID' => (int)($link['SECTION_ID'] ?? null),
'NAME' => isset($link['EXTERNAL_TYPE']) && $link['EXTERNAL_TYPE'] === 'local'
? $link['NAME']
: $calendarNames[$xmlId]['NAME']
,
'DESCRIPTION' => $calendarNames[$xmlId]['DESCRIPTION'] ?? null,
'COLOR' => $calendarNames[$xmlId]['COLOR'] ?? null,
];
CCalendarSect::Edit([
'arFields' => $fields,
'bAffectToDav' => false,
'originalFrom' => $connection->getVendor()->getCode(),
]);
SectionConnectionTable::update((int)$link['SECTION_CONNECTION_ID'], [
'LAST_SYNC_DATE' => new DateTime(),
'LAST_SYNC_STATUS' => Dictionary::SYNC_STATUS['success'],
'VERSION_ID' => $calendarNames[$xmlId]['MODIFICATION_LABEL'] ?? null,
]
);
$result[] = [
'XML_ID' => $xmlId,
'SECTION_ID' => $link['SECTION_ID'],
'SECTION_CONNECTION_ID' => $link['SECTION_CONNECTION_ID'],
'SYNC_TOKEN' => $link['VERSION_ID'] ?? null,
'IS_NEW' => false,
];
}
unset($calendarNames[$xmlId]);
}
}
foreach ($calendarNames as $curXmlId => $calendar)
{
$fields = [
'CAL_TYPE' => self::ENTITY_TYPE,
'OWNER_ID' => $connection->getOwner()->getId(),
'CREATED_BY' => $connection->getOwner()->getId(),
'NAME' => $calendar['NAME'],
'DESCRIPTION' => $calendar['DESCRIPTION'],
'COLOR' => $calendar['COLOR'],
'EXPORT' => ['ALLOW' => false],
'EXTERNAL_TYPE' => $connection->getVendor()->getCode(),
];
$id = (int)CCalendarSect::Edit([
'arFields' => $fields,
'bAffectToDav' => false,
'originalFrom' => $connection->getVendor()->getCode(),
]);
if ($id)
{
$linkId = SectionConnectionTable::add([
'SECTION_ID' => $id,
'CONNECTION_ID' => $connection->getId(),
'VENDOR_SECTION_ID' => $curXmlId,
'ACTIVE' => 'Y',
'LAST_SYNC_DATE' => new DateTime(),
'LAST_SYNC_STATUS' => Dictionary::SYNC_STATUS['success'],
'VERSION_ID' => $calendar['MODIFICATION_LABEL'],
]);
$result[] = [
'XML_ID' => $curXmlId,
'SECTION_ID' => $id,
'SECTION_CONNECTION_ID' => $linkId->getId(),
'SYNC_TOKEN' => $calendar['MODIFICATION_LABEL'],
'IS_NEW' => true,
];
}
}
return $result;
}