13use Bitrix\Main\Entity\ReferenceField;
22 public const TYPE =
'location';
25 private ?
Room $room =
null;
27 private ?
Error $error =
null;
40 return (
new self())->setRoom($room);
68 private function addError(
Error $error): void
70 $this->error = $error;
91 $this->room->create();
93 if ($this->room->getError())
95 $this->addError($this->room->getError());
114 $this->room->update();
116 if ($this->room->getError())
118 $this->addError($this->room->getError());
137 if (!$this->room->getName())
139 $this->room->setName($this->getRoomName($this->room->getId()));
142 $this->room->delete();
144 if ($this->room->getError())
146 $this->addError($this->room->getError());
160 $roomQuery = LocationTable::query()
162 'LOCATION_ID' =>
'ID',
167 'NAME' =>
'SECTION.NAME',
168 'COLOR' =>
'SECTION.COLOR',
169 'OWNER_ID' =>
'SECTION.OWNER_ID',
170 'CAL_TYPE' =>
'SECTION.CAL_TYPE',
172 ->registerRuntimeField(
'SECTION',
175 SectionTable::getEntity(),
176 Query\Join::on(
'ref.ID',
'this.SECTION_ID'),
177 [
'join_type' =>
Query\Join::TYPE_INNER]
180 ->setOrder([
'ID' =>
'ASC'])
186 [$roomsId, $result] = self::prepareRoomsQueryData($roomQuery);
190 \CCalendarSect::CreateDefault([
191 'type' => self::TYPE,
194 LocationTable::cleanCache();
199 $result = self::getRoomsAccess($roomsId, $result);
201 foreach ($result as $room)
203 \CCalendarSect::HandlePermission($room);
206 return \CCalendarSect::GetSectionPermission($result);
217 public static function getRoomById(
int $id, array $params = []): array
219 $roomQuery = LocationTable::query()
221 'LOCATION_ID' =>
'ID',
226 'NAME' =>
'SECTION.NAME',
227 'COLOR' =>
'SECTION.COLOR',
228 'OWNER_ID' =>
'SECTION.OWNER_ID',
229 'CAL_TYPE' =>
'SECTION.CAL_TYPE',
231 ->where(
'SECTION.ID', $id)
232 ->registerRuntimeField(
'SECTION',
235 SectionTable::getEntity(),
236 Query\Join::on(
'ref.ID',
'this.SECTION_ID'),
237 [
'join_type' =>
Query\Join::TYPE_INNER]
245 [$roomsId, $result] = self::prepareRoomsQueryData($roomQuery);
246 $result = self::getRoomsAccess($roomsId, $result);
248 foreach ($result as $room)
250 \CCalendarSect::HandlePermission($room);
253 $applyPermission = $params[
'checkPermission'] ??
true;
254 if ($applyPermission !==
false)
256 return \CCalendarSect::GetSectionPermission($result);
273 $params[
'checkPermission'] = $params[
'checkPermission'] ??
null;
274 $params[
'room_id'] = $params[
'room_id'] ??
null;
275 $roomList =
self::getRoomById((
int)$params[
'room_id'], [
'checkPermission' => $params[
'checkPermission']]);
278 !$roomList || empty($roomList[0])
279 || empty($roomList[0][
'NAME'])
281 empty($roomList[0][
'PERM'][
'view_full'])
282 && $params[
'checkPermission'] !==
false
289 $createdBy = $params[
'parentParams'][
'arFields'][
'CREATED_BY']
290 ?? $params[
'parentParams'][
'arFields'][
'MEETING_HOST']
295 'ID' => $params[
'room_event_id'] ??
null,
296 'SECTIONS' => $params[
'room_id'] ??
null,
297 'DATE_FROM' => $params[
'parentParams'][
'arFields'][
'DATE_FROM'] ??
null,
298 'DATE_TO' => $params[
'parentParams'][
'arFields'][
'DATE_TO'] ??
null,
299 'TZ_FROM' => $params[
'parentParams'][
'arFields'][
'TZ_FROM'] ??
null,
300 'TZ_TO' => $params[
'parentParams'][
'arFields'][
'TZ_TO'] ??
null,
301 'SKIP_TIME' => $params[
'parentParams'][
'arFields'][
'SKIP_TIME'] ??
null,
302 'RRULE' => $params[
'parentParams'][
'arFields'][
'RRULE'] ??
null,
303 'EXDATE' => $params[
'parentParams'][
'arFields'][
'EXDATE'] ??
null,
306 if (!$params[
'room_event_id'])
308 $arFields[
'CREATED_BY'] = $createdBy;
309 $arFields[
'NAME'] = \CCalendar::GetUserName($createdBy);
313 return \CCalendarEvent::Edit([
314 'arFields' => $arFields,
329 'recursion_mode' => $params[
'recursion_mode'] ??
null,
330 'parent_event_id' => $params[
'parent_event_id'] ??
null,
331 'section_id' => $params[
'section_id'] ??
null,
332 'current_event_date_from' => $params[
'current_event_date_from'] ??
null,
333 'current_event_date_to' => $params[
'current_event_date_to'] ??
null,
334 'owner_id' => $params[
'owner_id'] ??
null,
342 if($params[
'recursion_mode'] ===
'all' || $params[
'recursion_mode'] ===
'next')
344 $event = \CCalendarEvent::GetById($params[
'parent_event_id']);
346 $params[
'frequency'] = $event[
'RRULE'][
'FREQ'] ??
null;
347 if($params[
'recursion_mode'] ===
'all')
349 $params[
'current_event_date_from'] = $event[
'DATE_FROM'] ??
null;
350 $params[
'current_event_date_to'] = $event[
'DATE_TO'] ??
null;
354 $result = \CCalendar::SaveEventEx([
355 'recursionEditMode' => $params[
'recursion_mode'],
356 'currentEventDateFrom' => $params[
'current_event_date_from'],
357 'checkPermission' =>
false,
358 'sendInvitations' =>
false,
359 'userId' => $params[
'owner_id'],
361 'ID' => $params[
'parent_event_id'],
362 'DATE_FROM' => $params[
'current_event_date_from'],
363 'DATE_TO' => $params[
'current_event_date_to'],
368 $params[
'event_id'] = $result[
'recEventId'] ?? $result[
'id'] ??
null;
370 $this->sendCancelBookingNotification($params);
374 private function sendCancelBookingNotification(array $params): void
377 'section_id' => $params[
'section_id'],
378 'event_id' => $params[
'event_id'],
379 'owner_id' => $params[
'owner_id'],
380 'current_event_date_from' => $params[
'current_event_date_from'],
381 'recursion_mode' => $params[
'recursion_mode'],
384 $section = \CCalendarSect::GetById($params[
'section_id']);
385 $userId = \CCalendar::GetCurUserId();
386 $event = \CCalendarEvent::GetById($params[
'event_id'],
false);
388 \CCalendarNotify::Send([
389 'eventId' => $params[
'event_id'],
390 'mode' =>
'cancel_booking',
391 'location' => $section[
'NAME'] ??
null,
392 'locationId' => $params[
'section_id'],
393 'guestId' => $params[
'owner_id'],
395 'from' => $params[
'current_event_date_from'],
396 'eventName' => $event[
'NAME'] ??
null,
397 'recursionMode' => $params[
'recursion_mode'],
411 return \CCalendar::DeleteEvent(
412 (
int)($params[
'room_event_id'] ??
null),
415 'checkPermissions' =>
false,
416 'markDeleted' =>
false
433 \CCalendarSect::SetClearOperationCache(
true);
434 \CCalendar::clearCache([
438 LocationTable::cleanCache();
453 \CCalendarSect::CleanAccessTable();
471 $event = EventTable::query()
472 ->setSelect([
'LOCATION'])
477 if (!empty($event[
'LOCATION']))
479 $location = Util::parseLocation($event[
'LOCATION']);
480 if ($location[
'room_id'] && $location[
'room_event_id'])
482 EventTable::update($location[
'room_event_id'], [
502 $sectionList = \CCalendar::GetSectionList([
503 'CAL_TYPE' => self::TYPE,
505 'checkPermissions' =>
true,
506 'getPermissions' =>
true,
509 $sectionList = array_merge(
511 \CCalendar::getSectionListAvailableForUser(\CCalendar::GetUserId())
513 $result[
'sections'] = $sectionList;
526 $userId = \CCalendar::GetUserId();
530 $sectionList = \CCalendar::GetSectionList([
531 'CAL_TYPE' => self::TYPE,
533 'ADDITIONAL_IDS' => $followedSectionList,
535 $sectionList = array_merge($sectionList, \CCalendar::getSectionListAvailableForUser($userId));
537 $sectionAccessTasks = \CCalendar::GetAccessTasks(
'calendar_section',
'location');
541 'type' => self::TYPE,
545 $defaultSectionAccess = \CCalendarSect::GetDefaultAccess(
551 $result[
'sections'] = $sectionList;
552 $result[
'config'] = [
553 'locationAccess' => Util::getLocationAccess($userId),
554 'hiddenSections' => $hiddenSections,
558 'defaultSectionAccess' => $defaultSectionAccess,
559 'sectionAccessTasks' => $sectionAccessTasks,
560 'showTasks' =>
false,
561 'accessNames' => \CCalendar::GetAccessNames(),
581 ExecuteModuleEventEx($event, [
582 $this->room->getId(),
596 \Bitrix\Calendar\Util::addPullEvent(
598 $this->room->getCreatedBy(),
600 'ID' => $this->room->getId()
615 private function getRoomName(
int $id): ?string
617 $section = SectionTable::query()
618 ->setSelect([
'NAME'])
623 return $section ? $section[
'NAME'] :
null;
663 $idTemp =
"(#ID#, ''),";
665 $id = $this->room->getId();
666 $locationName = $this->room->getName();
667 $locationId =
'calendar_' . $id;
669 $events = $DB->Query(
"
670 SELECT ID, PARENT_ID, OWNER_ID, CREATED_BY, LOCATION
671 FROM b_calendar_event
672 WHERE LOCATION LIKE '" . $locationId .
"%';
675 while ($event = $events->Fetch())
677 if ($event[
'ID'] === $event[
'PARENT_ID'])
679 $guestsId[] = $event[
'OWNER_ID'];
681 $updateString .= str_replace(
'#ID#', $event[
'ID'], $idTemp);
686 $updateString = substr($updateString, 0, -1);
688 INSERT INTO b_calendar_event (ID, LOCATION)
689 VALUES ".$updateString.
"
690 ON DUPLICATE KEY UPDATE LOCATION = VALUES(LOCATION)
692 $guestsId = array_unique($guestsId);
693 $userId = \CCalendar::GetCurUserId();
695 foreach ($guestsId as $guestId)
697 \CCalendarNotify::Send([
698 'mode' =>
'delete_location',
699 'location' => $locationName,
701 'guestId' => (
int)$guestId,
723 $events = self::getLocationEventsId($this->room->getId());
725 foreach ($events as $event)
727 if ($this->room->getCreatedBy())
729 \Bitrix\Calendar\Util::addPullEvent(
731 $this->room->getCreatedBy(),
750 \CCalendarEvent::DeleteEmpty($this->room->getId());
763 private static function getLocationEventsId(
int $roomId): array
765 return EventTable::query()
771 ->where(
'SECTION_ID', $roomId)
772 ->where(
'DELETED',
'N')
787 $access = $this->room->getAccess();
788 $id = $this->room->getId();
792 \CCalendarSect::SavePermissions(
799 \CCalendarSect::SavePermissions(
801 \CCalendarSect::GetDefaultAccess(
802 $this->room->getType(),
803 $this->room->getCreatedBy()
816 private static function prepareRoomsQueryData(
Query\
Result $query): array
821 while ($room = $query->fetch())
823 $room[
'ID'] = $room[
'SECTION_ID'];
824 unset($room[
'SECTION_ID']);
826 if (!empty($room[
'NAME']))
828 $room[
'NAME'] = Emoji::decode($room[
'NAME']);
830 $roomId = (int)$room[
'ID'];
831 $roomsId[] = $roomId;
832 $result[$roomId] = $room;
835 return [$roomsId, $result];
847 private static function getRoomsAccess(array $roomsId, array $rooms): array
860 ->whereIn(
'SECT_ID', $roomsId)
864 while ($access = $accessQuery->fetch())
866 if (!isset($rooms[$access[
'SECT_ID']][
'ACCESS']))
868 $rooms[$access[
'SECT_ID']][
'ACCESS'] = [];
870 $rooms[$access[
'SECT_ID']][
'ACCESS'][$access[
'ACCESS_CODE']] = (int)$access[
'TASK_ID'];
static setEventIdForLocation(int $id)
static createInstanceWithRoom(Room $room)
deleteLocationFromEvents()
cancelBooking(array $params=[])
static checkRoomName(?string $name)
static getRoomById(int $id, array $params=[])
static prepareRoomManagerData()
static releaseRoom(array $params=[])
static reserveRoom(array $params=[])
static getFollowedSectionIdList($userId=false)
static getHiddenSections($userId=false, $options=[])
static loadMessages($file)