• Модуль: timeman
  • Путь к файлу: ~/bitrix/modules/timeman/lib/service/schedule/calendarservice.php
  • Класс: BitrixTimemanServiceScheduleCalendarService
  • Вызов: CalendarService::saveWithExclusions
private function saveWithExclusions($calendar, $calendarForm)
{
	$parentCalendar = null;
	if ($calendar->getParentCalendarId() > 0)
	{
		$parentCalendar = $this->calendarRepository->findByIdWithExclusions($calendar->getParentCalendarId());
		if (!$parentCalendar || $parentCalendar->getParentCalendarId() > 0)
		{
			$calendar->setParentCalendarId(0);
		}
	}
	$res = $this->calendarRepository->save($calendar);
	if (!$res->isSuccess())
	{
		return CalendarServiceResult::createByResult($res);
	}
	if ($parentCalendar && $calendar->getParentCalendarId() > 0 && !$calendar->obtainParentCalendar())
	{
		$calendar->setParentCalendar($parentCalendar);
	}

	$this->calendarRepository->deleteCalendarExclusions($calendar->getId());
	$calendar->unsetExclusions();

	$dates = CalendarFormHelper::convertDatesToDbFormat($calendarForm->dates);
	foreach ($dates as $year => $yearDatesToSave)
	{
		if ($parentCalendar && $parentCalendar->obtainExclusionsByYear($year))
		{
			$parentYearDates = $parentCalendar->obtainExclusionsByYear($year)->getDates();
			foreach ($parentYearDates as $month => $days)
			{
				if (array_key_exists($month, $yearDatesToSave))
				{
					if ($this->isSameDates($yearDatesToSave[$month], $days))
					{
						unset($yearDatesToSave[$month]);
					}
				}
			}
			$yearDatesToSave = array_filter($yearDatesToSave);
		}
		if (!empty($yearDatesToSave))
		{
			$exclusions = CalendarExclusion::create($calendar->getId(), $year, $yearDatesToSave);

			$exclusionsResult = $this->calendarRepository->save($exclusions);
			if (!$exclusionsResult->isSuccess())
			{
				return CalendarServiceResult::createByResult($exclusionsResult);
			}
			$calendar->addToExclusions($exclusions);
		}
	}
	return (new CalendarServiceResult())->setCalendar($calendar);
}