• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/classes/general/event_calendar.php
  • Класс: CEventCalendar
  • Вызов: CEventCalendar::SyncCalendars
static function SyncCalendars($connectionType, $arCalendars, $entityType, $entityId, $siteId, $connectionId = null)
{
	//Array(
	//	[0] => Array(
	//		[XML_ID] => calendar
	//		[NAME] => calendar
	//	)
	//	[1] => Array(
	//		[XML_ID] => AQATAGFud...
	//		[NAME] => geewgvwe 1
	//		[DESCRIPTION] => gewgvewgvw
	//		[COLOR] => #FF0000
	//		[MODIFICATION_LABEL] => af720e7c7b6a
	//	)
	//)
	$entityType = mb_strtoupper($entityType);

	if ($entityType == "USER")
		$iblockId = self::GetUserCalendarIBlockId($siteId);
	else
		$iblockId = self::GetGroupCalendarIBlockId($siteId);

	$accountRootSectionId = CEventCalendar::GetAccountRootSectionId($entityId, $entityType, $iblockId);
	if (intval($accountRootSectionId) <= 0)
	{
		$cal = self::GetInstance();
		$accountRootSectionId = $cal->CreateSectionForOwner($entityId, $entityType, $iblockId);
	}
	$sectionObject = new CIBlockSection();

	$arCalendarNames = array();
	foreach ($arCalendars as $value)
		$arCalendarNames[$value["XML_ID"]] = $value;

	if ($connectionType == 'exchange')
		$xmlIdField = "UF_BXDAVEX_EXCH";
	elseif ($connectionType == 'caldav')
		$xmlIdField = "UF_BXDAVEX_CDAV";
	else
		return array();

	$arResult = array();

	$arCalFilter = array(
		'IBLOCK_ID' => $iblockId,
		"SECTION_ID" => $accountRootSectionId,
		"ACTIVE" => "Y",
		"CHECK_PERMISSIONS" => 'N',
		"!".$xmlIdField => false
	);

	if ($connectionType == 'caldav')
		$arCalFilter["UF_BXDAVEX_CDAV_COL"] = $connectionId;

	$dbSections = CIBlockSection::GetList(
		array('ID' => 'ASC'),
		$arCalFilter,
		false,
		array("ID", "NAME", "DESCRIPTION", "UF_USER_CAL_COL", $xmlIdField, $xmlIdField."_LBL")
	);
	while ($arSection = $dbSections->Fetch())
	{
		$xmlId = $arSection[$xmlIdField];
		$modificationLabel = $arSection[$xmlIdField."_LBL"];

		if (empty($xmlId))
			continue;

		if (!array_key_exists($xmlId, $arCalendarNames))
		{
			CIBlockSection::Delete($arSection["ID"], false);
		}
		else
		{
			if ($modificationLabel != $arCalendarNames[$xmlId]["MODIFICATION_LABEL"])
			{
				$sectionObject->Update(
					$arSection["ID"],
					array(
						"NAME" => $arCalendarNames[$xmlId]["NAME"],
						"DESCRIPTION" => $arCalendarNames[$xmlId]["DESCRIPTION"],
						"UF_USER_CAL_COL" => $arCalendarNames[$xmlId]["COLOR"],
						"CHECK_PERMISSIONS" => "N",
						$xmlIdField."_LBL" => $arCalendarNames[$xmlId]["MODIFICATION_LABEL"],
					)
				);
			}

			if (empty($modificationLabel) || ($modificationLabel != $arCalendarNames[$xmlId]["MODIFICATION_LABEL"]))
			{
				$arResult[] = array(
					"XML_ID" => $xmlId,
					"CALENDAR_ID" => array($iblockId, $accountRootSectionId, $arSection["ID"], mb_strtolower($entityType), $entityId)
				);
			}

			unset($arCalendarNames[$xmlId]);
		}
	}

	foreach ($arCalendarNames as $key => $value)
	{
		$arFields = array(
			"IBLOCK_ID" => $iblockId,
			"IBLOCK_SECTION_ID" => $accountRootSectionId,
			"NAME" => $value["NAME"],
			"DESCRIPTION" => $value["DESCRIPTION"],
			"UF_USER_CAL_COL" => $value["COLOR"],
			$xmlIdField => $key,
			"CHECK_PERMISSIONS" => "N",
			$xmlIdField."_LBL" => $value["MODIFICATION_LABEL"],
		);

		if ($connectionType == 'caldav')
			$arFields["UF_BXDAVEX_CDAV_COL"] = $connectionId;

		if ($entityType == 'USER')
			$arFields["CREATED_BY"] = $entityId;
		elseif ($entityType == 'GROUP')
			$arFields['SOCNET_GROUP_ID'] = $entityId;

		$id = $sectionObject->Add($arFields);

		$arResult[] = array(
			"XML_ID" => $key,
			"CALENDAR_ID" => array($iblockId, $accountRootSectionId, $id, mb_strtolower($entityType), $entityId)
		);
	}

	return $arResult;
}