• Модуль: timeman
  • Путь к файлу: ~/bitrix/modules/timeman/lib/service/agent/initialsettingsagent.php
  • Класс: BitrixTimemanServiceAgentInitialSettingsAgent
  • Вызов: InitialSettingsAgent::installWorkCalendars
private function installWorkCalendars()
{
	if (!Application::getConnection()->isTableExists(CalendarTable::getTableName())
		|| !Application::getConnection()->isTableExists(CalendarExclusionTable::getTableName()))
	{
		return;
	}
	$calendarsExclusions = [];
	$calendarFilePath = BitrixMainApplication::getDocumentRoot() . '/bitrix/modules/timeman/lib/update/calendars/exclusions.php';
	if (BitrixMainIOFile::isFileExists($calendarFilePath))
	{
		$calendarsExclusions = include $calendarFilePath;
	}
	if (empty($calendarsExclusions))
	{
		return;
	}
	foreach ($calendarsExclusions as $calendarsData)
	{
		if (empty($calendarsData['SYSTEM_CODE'])
			|| empty($calendarsData['NAME'])
			|| empty($calendarsData['EXCLUSIONS']))
		{
			continue;
		}
		$res = Application::getConnection()->query("SELECT ID FROM b_timeman_work_calendar WHERE SYSTEM_CODE = '" . $calendarsData['SYSTEM_CODE'] . "' limit 1");
		if ($calendar = $res->fetch())
		{
			$calendarId = $calendar['ID'];
		}
		else
		{
			Application::getConnection()->query("INSERT INTO `b_timeman_work_calendar` (`NAME`, `SYSTEM_CODE`) 
				VALUES ('" . $calendarsData['NAME'] . "', '" . $calendarsData['SYSTEM_CODE'] . "');");
			$calendarId = Application::getConnection()->getInsertedId();
		}
		if ($calendarId > 0)
		{
			foreach ($calendarsData['EXCLUSIONS'] as $year => $datesJson)
			{
				if ($calendar)
				{
					$res = Application::getConnection()->query("SELECT 'x' FROM b_timeman_work_calendar_exclusion
						 WHERE CALENDAR_ID = $calendarId AND `YEAR` = $year limit 1");
					if ($res->fetch())
					{
						continue;
					}
				}
				Application::getConnection()->query("INSERT IGNORE INTO `b_timeman_work_calendar_exclusion` (`CALENDAR_ID`, `YEAR`, `DATES`)
					 VALUES ($calendarId, $year, '$datesJson');");
			}
		}
	}
}