CAllCrmActivity::SaveCalendarEvent

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CAllCrmActivity
  4. SaveCalendarEvent
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_activity.php
  • Класс: \CAllCrmActivity
  • Вызов: CAllCrmActivity::SaveCalendarEvent
static function SaveCalendarEvent(&$arFields)
{
	$responsibleID =  isset($arFields['RESPONSIBLE_ID']) ? (int)$arFields['RESPONSIBLE_ID'] : 0;
	$provider = self::GetActivityProvider($arFields);
	$providerTypeId = isset($arFields['PROVIDER_TYPE_ID']) ? (string) $arFields['PROVIDER_TYPE_ID'] : null;
	$completed = (isset($arFields['COMPLETED']) && $arFields['COMPLETED'] == 'Y');

	if ($provider === null || $responsibleID <= 0 || !CModule::IncludeModule('calendar'))
	{
		return false;
	}

	$arCalEventFields = array(
		'CAL_TYPE' => 'user',
		'OWNER_ID' => $responsibleID,
		'NAME' => isset($arFields['SUBJECT']) ? $arFields['SUBJECT'] : '',
		'DATE_FROM' => isset($arFields['START_TIME']) ? $arFields['START_TIME'] : '',
		'DATE_TO' => isset($arFields['END_TIME']) ? $arFields['END_TIME'] : '',
		'IMPORTANCE' => CCrmActivityPriority::ToCalendarEventImportance(
			isset($arFields['PRIORITY'])
				? intval($arFields['PRIORITY'])
				: CCrmActivityPriority::Low
		),
		'DESCRIPTION' => isset($arFields['DESCRIPTION']) ? $arFields['DESCRIPTION'] : ''
	);

	//convert current user time to calendar owner time
	if ($userTzName = \CCalendar::GetUserTimezoneName($responsibleID, true))
	{
		$userTz = new DateTimeZone($userTzName);
		$format = \Bitrix\Main\Type\DateTime::getFormat();

		if (isset($arFields['START_TIME']))
		{
			$startTime = \Bitrix\Main\Type\DateTime::createFromUserTime($arFields['START_TIME']);
			$startTime->setTimeZone($userTz);
			$arCalEventFields['DATE_FROM'] = $startTime->format($format);
			$arCalEventFields['TZ_FROM'] = $userTzName;
		}
		if (isset($arFields['END_TIME']))
		{
			$endTime = \Bitrix\Main\Type\DateTime::createFromUserTime($arFields['END_TIME']);
			$endTime->setTimeZone($userTz);
			$arCalEventFields['DATE_TO'] = $endTime->format($format);
			$arCalEventFields['TZ_TO'] = $userTzName;
		}
	}

	if (method_exists('CCalendar', 'GetCrmSection'))
	{
		$arCalEventFields['SECTIONS'] = [CCalendar::GetCrmSection($responsibleID, true)];
	}

	$calendarEventId = isset($arFields['CALENDAR_EVENT_ID']) ? (int)$arFields['CALENDAR_EVENT_ID'] : 0;

	if($calendarEventId > 0)
	{
		$arPresentEventFields = CCalendarEvent::GetById($calendarEventId, false);
		if(is_array($arPresentEventFields))
		{
			$presentResponsibleID = isset($arPresentEventFields['OWNER_ID']) ? (int)$arPresentEventFields['OWNER_ID'] : 0;
			if($presentResponsibleID === $responsibleID)
			{
				$arCalEventFields['ID'] = $calendarEventId;
			}

			if(isset($arPresentEventFields['RRULE']) && $arPresentEventFields['RRULE'] != '')
			{
				$arCalEventFields['RRULE'] = CCalendarEvent::ParseRRULE($arPresentEventFields['RRULE']);
			}
		}
	}
	if(isset($arFields['NOTIFY_TYPE']) && $arFields['NOTIFY_TYPE'] != CCrmActivityNotifyType::None)
	{
		$arCalEventFields['REMIND'] = array(
			array(
				'type' => CCrmActivityNotifyType::ToCalendarEventRemind($arFields['NOTIFY_TYPE']),
				'count' => isset($arFields['NOTIFY_VALUE']) ? intval($arFields['NOTIFY_VALUE']) : 15
			)
		);
	}

	// Clear notification for completed activity
	if ($completed)
	{
		$arCalEventFields['REMIND'] = [];
	}

	self::$IGNORE_CALENDAR_EVENTS = true;
	// We must initialize CCalendar!
	$calendar = new CCalendar();
	$calendar->Init(
		array(
			'type'=>'user',
			'userId' => $responsibleID,
			'ownerId' => $responsibleID
		)
	);

	$result = $calendar->SaveEvent(
		array(
			'arFields' => $arCalEventFields,
			'userId' => $responsibleID,
			'autoDetectSection' => true,
			'autoCreateSection' => true
		)
	);

	$ownerID = (int)$arFields['OWNER_ID'];
	$ownerTypeID = (int)$arFields['OWNER_TYPE_ID'];
	$arBindings = isset($arFields['BINDINGS']) ? $arFields['BINDINGS'] : array();
	if(empty($arBindings) && $ownerID > 0 && $ownerTypeID > 0)
	{
		$arBindings[] = array('OWNER_TYPE_ID' => $ownerTypeID, 'OWNER_ID' => $ownerID);
	}

	$entityID = (int)$result;
	if($entityID > 0)
	{
		if(!empty($arBindings))
		{
			$arUserFields = array();
			foreach($arBindings as &$arBinding)
			{
				$entityTypeName = CCrmOwnerType::ResolveName($arBinding['OWNER_TYPE_ID']);
				if (Crm\UserField\UserFieldManager::isEnabledInCalendarUserField($entityTypeName))
				{
					$arUserFields[] = Crm\UserField\Types\ElementType::getValueByIdentifier(
						new Crm\ItemIdentifier(
							$arBinding['OWNER_TYPE_ID'],
							$arBinding['OWNER_ID'],
						)
					);
				}
			}
			unset($arBinding);

			CCalendarEvent::UpdateUserFields(
				$entityID,
				array('UF_CRM_CAL_EVENT' => $arUserFields)
			);
		}

		if ($calendarEventId > 0 && $calendarEventId !== $entityID)
		{
			if ($provider::canKeepReassignedInCalendar($providerTypeId))
			{
				/* TODO: remove bindings?
				CCalendarEvent::UpdateUserFields($entityID, array('UF_CRM_CAL_EVENT' => null));
				*/
			}
			else
			{
				CCalendarEvent::Delete(array('id' => $calendarEventId, 'bMarkDeleted' => true));
			}
		}
	}
	self::$IGNORE_CALENDAR_EVENTS = false;
	return $result;
}

Добавить комментарий