• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/classes/general/calendar_event.php
  • Класс: CCalendarEvent
  • Вызов: CCalendarEvent::Delete
static function Delete($params)
{
	global $DB, $CACHE_MANAGER;
	$bCalDav = CCalendar::IsCalDAVEnabled();
	$id = (int)$params['id'];
	$sendNotification = ($params['sendNotification'] ?? null) !== false;
	$params['requestUid'] = $params['requestUid'] ?? null;

	if ($id)
	{
		$userId = (isset($params['userId']) && (int)$params['userId'] > 0)
			? (int)$params['userId']
			: CCalendar::GetCurUserId()
		;

		$arAffectedSections = [];
		$entry = $params['Event'] ?? null;

		if (!isset($entry) || !is_array($entry))
		{
			CCalendar::SetOffset();
			$res = self::GetList([
				'arFilter' => [
					'ID' => $id
				],
				'parseRecursion' => false
			]);
			$entry = $res[0] ?? null;
		}

		if ($entry)
		{
			$entry['PARENT_ID'] = $entry['PARENT_ID'] ?? null;
			if (!empty($entry['IS_MEETING']) && $entry['PARENT_ID'] !== $entry['ID'])
			{
				$parentEvent = self::GetList([
					'arFilter' => [
						"ID" => $entry['PARENT_ID']
					],
					'parseRecursion' => false
                    ]);
				$parentEvent = $parentEvent[0];
				if ($parentEvent)
				{
					$accessController = new EventAccessController($userId);
					$eventModel = self::getEventModelForPermissionCheck(
						(int)($parentEvent['ID'] ?? 0),
						$parentEvent,
						$userId
					);
					$eventModel->setSectionId($parentEvent['SECT_ID']);

					$perm = $accessController->check(ActionDictionary::ACTION_EVENT_EDIT, $eventModel);
					if (!$perm)
					{
						if (in_array($entry['MEETING_STATUS'] ?? null,
							[
								BitrixCalendarCoreEventToolsDictionary::MEETING_STATUS['Yes'],
								BitrixCalendarCoreEventToolsDictionary::MEETING_STATUS['Question'],
							], true))
						{
							self::SetMeetingStatus([
		                        'userId' => $userId,
		                        'eventId' => $entry['ID'],
		                        'status' => 'N'
	                        ]);
						}

						return true;
					}

					return CCalendar::DeleteEvent($parentEvent['ID']);
				}

				return false;
			}
			foreach(GetModuleEvents("calendar", "OnBeforeCalendarEventDelete", true) as $arEvent)
			{
				ExecuteModuleEventEx($arEvent, array($id, $entry));
			}

			if (!empty($entry['PARENT_ID']))
			{
				CCalendarLiveFeed::OnDeleteCalendarEventEntry($entry['PARENT_ID'], $entry);
			}
			else
			{
				CCalendarLiveFeed::OnDeleteCalendarEventEntry($entry['ID'], $entry);
			}

			$arAffectedSections[] = $entry['SECT_ID'];
			// Check location: if reserve meeting was reserved - clean reservation
			if (!empty($entry['LOCATION']))
			{
				$loc = RoomsUtil::parseLocation($entry['LOCATION']);
				if ($loc['mrevid'] || $loc['room_event_id'])
				{
					RoomsUtil::releaseLocation($loc);
				}
			}

			if ($entry['CAL_TYPE'] === 'user')
			{
				$CACHE_MANAGER->ClearByTag('calendar_user_'.$entry['OWNER_ID']);
			}

			if (!empty($entry['IS_MEETING']))
			{
				$isPastEvent = (int)$entry['DATE_TO_TS_UTC'] < (time() - (int)$entry['TZ_OFFSET_TO']);;
				CCalendarNotify::ClearNotifications($entry['PARENT_ID']);

				if (Loader::includeModule("im"))
				{
					CIMNotify::DeleteBySubTag("CALENDAR|INVITE|".$entry['PARENT_ID']);
					CIMNotify::DeleteBySubTag("CALENDAR|STATUS|".$entry['PARENT_ID']);
				}

				$involvedAttendees = [];

				$CACHE_MANAGER->ClearByTag('calendar_user_'.$userId);
				$childEvents = self::GetList([
					'arFilter' => [
						"PARENT_ID" => $id,
					],
					'parseRecursion' => false,
					'checkPermissions' => false,
					'setDefaultLimit' => false,
				]);

				$chEventIds = [];
				$icalManagersCollection = [];
				foreach($childEvents as $chEvent)
				{
					$CACHE_MANAGER->ClearByTag('calendar_user_'.$chEvent["OWNER_ID"]);
					$isSharing = $chEvent['EVENT_TYPE'] === Dictionary::EVENT_TYPE['shared']
						|| $chEvent['EVENT_TYPE'] === Dictionary::EVENT_TYPE['shared_crm']
					;
					if ($chEvent["MEETING_STATUS"] !== "N" && $sendNotification && !$isPastEvent && !$isSharing)
					{
						$fromTo = self::GetEventFromToForUser($entry, $chEvent["OWNER_ID"]);

						if (
							(!empty($chEvent['MEETING_HOST']) && (int)$chEvent['MEETING_HOST'] === $userId)
							|| self::checkAttendeeBelongsToEvent($id, $userId)
						)
						{
							// first problematic place
							CCalendarNotify::Send([
								'mode' => 'cancel',
								'name' => $chEvent['NAME'],
								"from" => $fromTo["DATE_FROM"],
								"to" => $fromTo["DATE_TO"],
								"location" => CCalendar::GetTextLocation($chEvent["LOCATION"]),
								"guestId" => $chEvent["OWNER_ID"],
								"eventId" => $id,
								"userId" => $userId,
							]);
						}
					}
					$chEventIds[] = $chEvent["ID"];

					if ($chEvent["MEETING_STATUS"] === "Q")
					{
						$involvedAttendees[] = $chEvent["OWNER_ID"];
					}

					$bExchange = CCalendar::IsExchangeEnabled($chEvent["OWNER_ID"]);
					if ($bExchange || $bCalDav)
					{
						CCalendarSync::DoDeleteToDav(array(
								'bCalDav' => $bCalDav,
								'bExchangeEnabled' => $bExchange,
								'sectionId' => $chEvent['SECT_ID']
						), $chEvent);
					}

					self::onEventDelete($chEvent, $params);

					$isParent = $chEvent['ID'] === $chEvent['PARENT_ID'];
					if (
						!$isParent
						&& !$isPastEvent
						&& ICalUtil::isMailUser($chEvent['OWNER_ID'])
					)
					{
						if (!empty($chEvent['ATTENDEE_LIST']) && is_array($chEvent['ATTENDEE_LIST']))
						{
							$attendeeIds = [];
							foreach ($chEvent['ATTENDEE_LIST'] as $attendee)
							{
								$attendeeIds[] = $attendee['id'];
							}
						}
						$attendees = null;
						if (!empty($attendeeIds))
						{
							$attendees = ICalUtil::getIndexUsersById($attendeeIds);
						}

						$sender = self::getSenderForIcal($attendees, $chEvent['MEETING_HOST']);
						if (!empty($chEvent['MEETING']['MAIL_FROM']))
						{
							$sender['EMAIL'] = $chEvent['MEETING']['MAIL_FROM'];
							$sender['MAIL_FROM'] = $chEvent['MEETING']['MAIL_FROM'];
						}
						else
						{
							continue;
						}

						$declinedUser = $attendees[$chEvent['OWNER_ID']];
						$declinedUser['STATUS'] = 'declined';
						$additionalChildArFields['ICAL_ORGANIZER'] = self::getOrganizerForIcal($attendees, (int)$chEvent['MEETING_HOST'], $sender['EMAIL']);
						$additionalChildArFields['ICAL_ATTENDEES'] = self::createMailAttendeesCollection([$declinedUser['ID'] => $declinedUser], false);
						/** increment version to delete event in outside service */
						$chEvent['VERSION'] = (int)$chEvent['VERSION'] + 1;

						$icalManagersCollection[] = SenderCancelInvitation::createInstance(
							array_merge(self::prepareChildParamsForIcalInvitation($chEvent), $additionalChildArFields),
							IcalMailContext::createInstance(
								self::getMailAddresser($sender, $chEvent['MEETING']['MAIL_FROM']),
								self::getMailReceiver($attendees[$chEvent['OWNER_ID']])
							)
						);
					}

					$pullUserId = (int)$chEvent['CREATED_BY'] > 0 ? (int)$chEvent['CREATED_BY'] : $userId;
					if ($pullUserId)
					{
						Util::addPullEvent(
							'delete_event',
							$pullUserId,
							[
								'fields' => $chEvent,
								'requestUid' => $params['requestUid']
							]
						);
					}
				}

				if (!empty($icalManagersCollection))
				{
					$managerChunks = array_chunk($icalManagersCollection, 3);
					foreach ($managerChunks as $chunk)
					{
						MailInvitationManager::createAgentSent($chunk);
					}
				}

				// Set flag
				if (!empty($params['bMarkDeleted']))
				{
					$DB->Query("UPDATE b_calendar_event SET ".
						$DB->PrepareUpdate("b_calendar_event", array("DELETED" => "Y")).
						" WHERE PARENT_ID=".$id, false, "File: ".__FILE__."
Line: ".__LINE__); } else // Actual deleting { $strSql = "DELETE from b_calendar_event WHERE PARENT_ID=".$id; $DB->Query($strSql, false, "File: ".__FILE__."
Line: ".__LINE__); if (!Util::isSectionStructureConverted()) { $strChEvent = join(',', $chEventIds); if (!empty($chEventIds)) { $DB->Query("DELETE FROM b_calendar_event_sect WHERE EVENT_ID in (".$strChEvent.")", false, "FILE: ".__FILE__."
LINE: ".__LINE__); } } } if (!empty($involvedAttendees)) { CCalendar::UpdateCounter($involvedAttendees); } } if (!$entry['IS_MEETING'] && $entry['CAL_TYPE'] === 'user') { self::onEventDelete($entry, $params); } if ( $params && is_array($params) && BitrixCalendarSyncUtilRequestLogger::isEnabled() ) { $loggerData = $params; unset($loggerData['Event']); $loggerData['loggerUuid'] = $id; (new BitrixCalendarSyncUtilRequestLogger($userId, 'portal_delete'))->write($loggerData); } if (!empty($params['bMarkDeleted'])) { $DB->Query("UPDATE b_calendar_event SET ". $DB->PrepareUpdate("b_calendar_event", array("DELETED" => "Y")). " WHERE ID=".$id, false, "File: ".__FILE__."
Line: ".__LINE__); } else { // Real deleting $DB->Query("DELETE from b_calendar_event WHERE ID=".$id, false, "File: ".__FILE__."
Line: ".__LINE__); // Del link from table if (!Util::isSectionStructureConverted()) { $DB->Query("DELETE FROM b_calendar_event_sect WHERE EVENT_ID=".$id, false, "FILE: ".__FILE__."
LINE: ".__LINE__); } } if (!empty($arAffectedSections)) { CCalendarSect::UpdateModificationLabel($arAffectedSections); } foreach(EventManager::getInstance()->findEventHandlers("calendar", "OnAfterCalendarEventDelete") as $event) { ExecuteModuleEventEx($event, [$id, $entry]); } CCalendar::ClearCache('event_list'); $pullUserId = (int)$entry['CREATED_BY'] > 0 ? (int)$entry['CREATED_BY'] : $userId; if ($pullUserId) { Util::addPullEvent( 'delete_event', $pullUserId, [ 'fields' => $entry, 'requestUid' => $params['requestUid'] ?? null ] ); } return true; } } return false; }