• Модуль: intranet
  • Путь к файлу: ~/bitrix/modules/intranet/classes/general/event_calendar.php
  • Класс: CEventCalendar
  • Вызов: CEventCalendar::SaveEvent
function SaveEvent($arParams)
{
	global $DB;
	$iblockId = $arParams['iblockId'];
	$ownerType = $arParams['ownerType'];
	$ownerId = $arParams['ownerId'];
	$bCheckPermissions = $arParams["bCheckPermissions"] !== false;

	$calendarId = intval($arParams['calendarId']);
	$sectionId = $arParams['sectionId'];
	$fullUrl = $arParams['fullUrl'];
	$userId = $arParams['userId'];
	$bIsInvitingEvent = $arParams['isMeeting'] && intval($arParams['prop']['PARENT']) > 0;
	$bExchange = CEventCalendar::IsExchangeEnabled() && $ownerType == 'USER';
	$bCalDav = CEventCalendar::IsCalDAVEnabled() && $ownerType == 'USER';

	if (!$bIsInvitingEvent)
	{
		// *** ADD MEETING ROOM ***
		$loc_old = CEventCalendar::ParseLocation($arParams['location']['old']);
		$loc_new = CEventCalendar::ParseLocation($arParams['location']['new']);

		if ($loc_old['mrid'] !== false && $loc_old['mrevid'] !== false && ($loc_old['mrid'] !== $loc_new['mrid'] || $arParams['location'])) // Release MR
		{
			if($loc_old['mrid'] == $arParams['VMiblockId']) // video meeting
			{
				CEventCalendar::ReleaseVR(array(
					'mrevid' => $loc_old['mrevid'],
					'mrid' => $loc_old['mrid'],
					'VMiblockId' => $arParams['VMiblockId'],
					'allowVideoMeeting' => $arParams['allowVideoMeeting'],
				));
			}
			else
			{
				CEventCalendar::ReleaseMR(array(
					'mrevid' => $loc_old['mrevid'],
					'mrid' => $loc_old['mrid'],
					'RMiblockId' => $arParams['RMiblockId'],
					'allowResMeeting' => $arParams['allowResMeeting'],
				));
			}
		}

		if ($loc_new['mrid'] !== false) // Reserve MR
		{
			if($loc_new['mrid'] == $arParams['VMiblockId']) // video meeting
			{
				$mrevid = CEventCalendar::ReserveVR(array(
					'mrid' => $loc_new['mrid'],
					'dateFrom' => $arParams['dateFrom'],
					'dateTo' => $arParams['dateTo'],
					'name' => $arParams['name'],
					'description' => GetMessage('EC_RESERVE_FOR_EVENT').': '.$arParams['name'],
					'persons' => count($arParams['guests']),
					'members' => $arParams['guests'],
					'regularity' => $arParams['prop']['PERIOD_TYPE'],
					'regularity_count' => $arParams['prop']['PERIOD_COUNT'],
					'regularity_length' => $arParams['prop']['EVENT_LENGTH'],
					'regularity_additional' => $arParams['prop']['PERIOD_ADDITIONAL'],
					'VMiblockId' => $arParams['VMiblockId'],
					'allowVideoMeeting' => $arParams['allowVideoMeeting'],
				));
			}
			else
			{
				$mrevid = CEventCalendar::ReserveMR(array(
					'mrid' => $loc_new['mrid'],
					'dateFrom' => $arParams['dateFrom'],
					'dateTo' => $arParams['dateTo'],
					'name' => $arParams['name'],
					'description' => GetMessage('EC_RESERVE_FOR_EVENT').': '.$arParams['name'],
					'persons' => $arParams['isMeeting'] && count($arParams['guests']) > 0 ? count($arParams['guests']) : 1,
					'regularity' => $arParams['prop']['PERIOD_TYPE'],
					'regularity_count' => $arParams['prop']['PERIOD_COUNT'],
					'regularity_length' => $arParams['prop']['EVENT_LENGTH'],
					'regularity_additional' => $arParams['prop']['PERIOD_ADDITIONAL'],
					'RMiblockId' => $arParams['RMiblockId'],
					'allowResMeeting' => $arParams['allowResMeeting'],
				));
			}

			if ($mrevid && $mrevid != 'reserved' && $mrevid != 'expire' && $mrevid > 0)
			{
				$loc_new = 'ECMR_'.$loc_new['mrid'].'_'.$mrevid;
				$arParams["prop"]['LOCATION'] = $loc_new;
			}
			else
			{
				$arParams["prop"]['LOCATION'] = '';
				if($mrevid == 'reserved')
					$loc_new = 'bxec_error_reserved';
				elseif($mrevid == 'expire')
					$loc_new = 'bxec_error_expire';
				else
					$loc_new = 'bxec_error';
			}
		}
		else
		{
			$loc_new = $loc_new['str'];
			$arParams["prop"]['LOCATION'] = $loc_new;
		}
	}

	//$bSocNetLog = (!isset($arParams['bSocNetLog']) || $arParams['bSocNetLog'] != false) && !$arParams["prop"]["PRIVATE"];
	//if(cmodule::includemodule('security'))

	if(CModule::IncludeModule("security"))
	{
		$filter = new CSecurityFilter;
		$arParams['desc'] = $filter->TestXSS($arParams['desc'], 'replace');
	}
	else
	{
		$arParams['desc'] = htmlspecialcharsex($arParams['desc']);
	}

	if ($calendarId > 0) // We've got subsection id - 'calendar'
	{
		//cheking permissions and correct nesting
		//if (!CEventCalendar::CheckCalendar(array('iblockId' => $iblockId, 'ownerId' => $ownerId, 'ownerType' => $ownerType, 'calendarId' => $calendarId, 'sectionId' => $sectionId)))
		//	return CEventCalendar::ThrowError(GetMessage('EC_CALENDAR_CREATE_ERROR').' '.GetMessage('EC_CAL_INCORRECT_ERROR'));
	}
	else
	{
		// Creating default calendar section for owner
		$bDisplayCalendar = !$arParams["notDisplayCalendar"]; // Output js with calendar description
		$newSectionId = 'none'; // by reference
		$calendarId = CECCalendar::CreateDefault(array(
			'ownerType' => $ownerType,
			'ownerId' => $ownerId,
			'iblockId' => $iblockId,
			'sectionId' => $sectionId
		), $bDisplayCalendar, $newSectionId);

		if (!$calendarId)
			return CEventCalendar::ThrowError('2'.GetMessage('EC_CALENDAR_CREATE_ERROR'));

		if ($newSectionId != 'none')
			$arParams['sectionId'] = $newSectionId;
	}

	$arParams['calendarId'] = $calendarId;

	if ($bIsInvitingEvent && !isset($arParams["CONFIRMED"]) && isset($arParams["status"]))
	{
		$arParams["prop"]["CONFIRMED"] = CEventCalendar::GetConfirmedID($iblockId, $arParams["status"]);
	}
	else
	{
		if($arParams["CONFIRMED"] == "Q")
			$arParams["prop"]["CONFIRMED"] = CEventCalendar::GetConfirmedID($iblockId, "Q");
		elseif($arParams["CONFIRMED"] == "Y")
			$arParams["prop"]["CONFIRMED"] = CEventCalendar::GetConfirmedID($iblockId, "Y");
		else
			unset($arParams["prop"]["CONFIRMED"]);
	}

	if (isset($arParams["remind"]))
	{
		if($arParams["remind"] !== false)
			$arParams["prop"]["REMIND_SETTINGS"] = $arParams["remind"]['count'].'_'.$arParams["remind"]['type'];
		else if(!$arParams['bNew'])
			$arParams["prop"]["REMIND_SETTINGS"] = '';
	}

	if (!isset($arParams['prop']['VERSION']))
	{
		if (!$arParams['bNew'])
		{
			$dbProp = CIBlockElement::GetProperty($iblockId, $arParams['id'], 'sort', 'asc', array('CODE' => 'VERSION'));
			if ($arProp = $dbProp->Fetch())
				$arParams['prop']['VERSION'] = intval($arProp['VALUE']);
		}
		if ($arParams['prop']['VERSION'] <= 0) $arParams['prop']['VERSION'] = 1;
		$arParams['prop']['VERSION']++;
	}

	if ($arParams['isMeeting'])
		$arParams['prop']['IS_MEETING'] = 'Y';

	if (!$bIsInvitingEvent)
	{
		$arParams['prop']['HOST_IS_ABSENT'] = ($arParams['isMeeting'] && !in_array($userId, $arParams['guests'])) ? 'Y' : 'N';
		if ($arParams['isMeeting'] && mb_strlen($arParams['meetingText']))
			$arParams['prop']['MEETING_TEXT'] = array('VALUE' => array("TYPE" => 'text', "TEXT" => $arParams['meetingText']));
	}

	$arFields = Array(
		"ACTIVE" => "Y",
		"IBLOCK_SECTION" => $calendarId,
		"IBLOCK_ID" => $iblockId,
		"NAME" => $arParams['name'],
		"ACTIVE_FROM" => $arParams['dateFrom'],
		"ACTIVE_TO" => $arParams['dateTo'],
		"DETAIL_TEXT" => $arParams['desc'],
		"DETAIL_TEXT_TYPE" => 'html',
		"MODIFIED_BY" => $GLOBALS['USER']->GetID(),
		"PROPERTY_VALUES" => $arParams['prop']
	);
	if ($ownerType == 'GROUP' && $ownerId > 0)
		$arFields['SOCNET_GROUP_ID'] = $ownerId;

	if ($bExchange || $bCalDav)
	{
		foreach($arFields["PROPERTY_VALUES"] as $prKey => $prVal)
			$arFields["PROPERTY_".$prKey] = $prVal;
	}

	// If it's EXCHANGE - we try to save event to exchange
	if ($bExchange)
	{
		$calendarXmlId = CECCalendar::GetExchangeXmlId($iblockId, $calendarId);
		if ($calendarXmlId <> '' && $calendarXmlId !== 0) // Synchronize with Exchange
		{
			if ($arParams['bNew'])
			{
				$exchRes = CDavExchangeCalendar::DoAddItem($ownerId, $calendarXmlId, $arFields);
			}
			else
			{
				$eventModLabel = CECEvent::GetExchModLabel($iblockId, $arParams['id']);
				$eventXmlId = CECEvent::GetExchangeXmlId($iblockId, $arParams['id']);
				$exchRes = CDavExchangeCalendar::DoUpdateItem($ownerId, $eventXmlId, $eventModLabel, $arFields);
			}

			if (!is_array($exchRes) || !array_key_exists("XML_ID", $exchRes))
				return CEventCalendar::ThrowError(CEventCalendar::CollectExchangeErros($exchRes));

			// It's ok, we successfuly save event to exchange calendar - and save it to DB
			$arFields['XML_ID'] = $exchRes['XML_ID'];
			$arFields['PROPERTY_VALUES']['BXDAVEX_LABEL'] = $exchRes['MODIFICATION_LABEL'];
		}
	}

	if ($bCalDav)
	{
		$connectionId = CECCalendar::GetCalDAVConnectionId($iblockId, $calendarId);
		if ($connectionId > 0)  // Synchronize with CalDav
		{
			$calendarCalDAVXmlId = CECCalendar::GetCalDAVXmlId($iblockId, $calendarId);
			if ($arParams['bNew'])
			{
				$DAVRes = CDavGroupdavClientCalendar::DoAddItem($connectionId, $calendarCalDAVXmlId, $arFields);
			}
			else
			{
				$eventCalDAVModLabel = CECEvent::GetCalDAVModLabel($iblockId, $arParams['id']);
				$eventXmlId = CECEvent::GetExchangeXmlId($iblockId, $arParams['id']);
				$DAVRes = CDavGroupdavClientCalendar::DoUpdateItem($connectionId, $calendarCalDAVXmlId, $eventXmlId, $eventCalDAVModLabel, $arFields);
			}

			if (!is_array($DAVRes) || !array_key_exists("XML_ID", $DAVRes))
				return CEventCalendar::ThrowError(CEventCalendar::CollectCalDAVErros($DAVRes));

			// // It's ok, we successfuly save event to caldav calendar - and save it to DB
			$arFields['XML_ID'] = $DAVRes['XML_ID'];
			$arFields['PROPERTY_VALUES']['BXDAVCD_LABEL'] = $DAVRes['MODIFICATION_LABEL'];
		}
	}

	$bs = new CIBlockElement;
	$res = false;
	if (!$arParams['bNew'])
	{
		$ID = $arParams['id'];
		if($ID > 0)
			$res = $bs->Update($ID, $arFields, false);
	}
	else
	{
		//This sets appropriate owner if event created by owner of the meeting and this calendar belongs to guest which is not current user
		if($ownerType == 'USER' && $ownerId > 0 && $userId != $ownerId)
			$arFields['CREATED_BY'] = $ownerId;

		$ID = $bs->Add($arFields, false);
		$res = ($ID > 0);
	}

	if ($arParams['isMeeting'] && !$bIsInvitingEvent)
	{
		$this->CheckParentProperty($arParams['userIblockId'], $iblockId);
		$arGuestConfirm = $this->InviteGuests($ID, $arFields, $arParams['guests'], $arParams);
	}

	if(!$res)
		return CEventCalendar::ThrowError('4'.$bs->LAST_ERROR);
	else
		CIBlockElement::RecalcSections($ID);

	if(!$bPeriodic && !$arParams["notDisplayCalendar"])
	{
		if ($arParams['bNew'])
		{
			?>
ClearCache($this->cachePath.'events/'.$iblockId.'/');

	if($bSocNetLog && $ownerType) // log changes for socnet
	{
		CEventCalendar::SocNetLog(
			array(
				'iblockId' => $iblockId,
				'ownerType' => $ownerType,
				'ownerId' => $ownerId,
				'target' => $arParams['bNew'] ? 'add_event' : 'edit_event',
				'id' => $ID,
				'name' => $arParams['name'],
				'desc' => $arParams['desc'],
				'from' => $arParams['dateFrom'],
				'to' => $arParams['dateTo'],
				'calendarId' => $calendarId,
				'accessibility' => $arParams["prop"]["ACCESSIBILITY"],
				'importance' => $arParams["prop"]["IMPORTANCE"],
				'pathToGroupCalendar' =>  $arParams["pathToGroupCalendar"],
				'pathToUserCalendar' =>  $arParams["pathToUserCalendar"]
			)
		);
	}

	if(array_key_exists("remind", $arParams))
	{
		CECEvent::AddReminder(
			array(
				'iblockId' => $iblockId,
				'ownerType' => $ownerType,
				'ownerId' => $ownerId,
				'userId' => $userId,
				'fullUrl' => $fullUrl,
				'id' => $ID,
				'dateFrom' => $arParams['dateFrom'],
				'remind' => $arParams["remind"],
				'bNew' => $arParams['bNew']
			)
		);
	}

	return $ID;
}