15 private const PRESET_INVITED =
'filter_calendar_meeting_status_q';
16 private const PRESET_HOST =
'filter_calendar_host';
17 private const PRESET_ATTENDEE =
'filter_calendar_attendee';
18 private const PRESET_DECLINED =
'filter_calendar_declined';
52 public static function getFilterId($type, $ownerId, $userId): string
54 static::$filterId =
'calendar-filter';
55 if ($type ===
'user' && (
int)$ownerId === (
int)$userId)
57 static::$filterId =
'calendar-filter-personal';
60 $type ===
'company_calendar'
61 || $type ===
'calendar_company'
62 || $type ===
'company'
65 static::$filterId =
'calendar-filter-company';
67 else if ($type ===
'group')
69 static::$filterId =
'calendar-filter-group';
72 return static::$filterId;
93 $presets[self::PRESET_INVITED] = [
94 'id' => self::PRESET_INVITED,
99 'MEETING_STATUS' =>
'Q',
104 $presets[self::PRESET_HOST] = [
105 'id' => self::PRESET_HOST,
109 'MEETING_STATUS' =>
'H',
113 $presets[self::PRESET_ATTENDEE] = [
114 'id' => self::PRESET_ATTENDEE,
119 'MEETING_STATUS' =>
'Y',
123 $presets[self::PRESET_DECLINED] = [
124 'id' => self::PRESET_DECLINED,
129 'MEETING_STATUS' =>
'N',
143 $filterOptions = new \Bitrix\Main\UI\Filter\Options(
$filterId);
144 $fields = $filterOptions->getFilter();
146 $preset = $fields[
'PRESET_ID'] ??
null;
147 $meetingStatus = $fields[
'MEETING_STATUS'] ??
null;
148 if ($meetingStatus ===
'Q')
150 $preset = self::PRESET_INVITED;
152 if ($meetingStatus ===
'N')
154 $preset = self::PRESET_DECLINED;
158 'search' => $filterOptions->getSearchString(),
159 'presetId' => $preset,
163 $sqlHelper = $connection->getSqlHelper();
166 foreach ($fields as $key => $value)
168 if ($key ===
'DATE_from')
170 $result[
'fields'][
'DATE_FROM'] = $value;
172 else if ($key ===
'DATE_to')
174 $result[
'fields'][
'DATE_TO'] = $value;
176 else if ($key ===
'ATTENDEES' || $key ===
'CREATED_BY' || $key ===
'SECTION_ID')
179 foreach ($value as $code)
181 $valueList[] = (int)$code;
183 $result[
'fields'][$key] = $valueList;
185 else if ($key ===
'MEETING_STATUS')
187 $result[
'fields'][
'MEETING_STATUS'] = $sqlHelper->forSql($value);
189 else if (in_array($key, $fieldNames,
true))
191 $result[
'fields'][$key] = $value;
203 if (empty(static::$filters))
205 static::$filters[
'CREATED_BY'] = [
206 'id' =>
'CREATED_BY',
208 'type' =>
'entity_selector',
214 'context' =>
'filter',
219 'inviteEmployeeLink' => false
227 static::$filters[
'ATTENDEES'] = [
230 'type' =>
'entity_selector',
236 'context' =>
'filter',
241 'inviteEmployeeLink' => false
249 static::$filters[
'MEETING_STATUS'] = [
250 'id' =>
'MEETING_STATUS',
264 static::$filters[
'DATE'] = [
272 return static::$filters;
275 private static function getSectionsForFilter(
string $type, ?
string $preset, ?
int $ownerId, ?
int $userId): array
279 $sectionList = \CCalendar::getSectionList([
281 'OWNER_ID' => $ownerId,
282 'checkPermissions' =>
true,
283 'getPermissions' =>
true,
285 $isPersonalCalendarContext = ($type ===
'user' && $userId === $ownerId);
287 $hiddenSections = [];
288 if ($preset !== self::PRESET_INVITED)
294 'ownerId' => $ownerId,
295 'isPersonalCalendarContext' => $isPersonalCalendarContext,
298 $hiddenSections = array_map(
static function($sectionId) {
299 return (
int)$sectionId;
303 foreach ($sectionList as $section)
305 if (in_array((
int)$section[
'ID'], $hiddenSections,
true))
310 $result[] = (int)$section[
'ID'];
327 $sqlHelper = $connection->getSqlHelper();
328 $userId = (int)$params[
'userId'];
329 $ownerId = (int)$params[
'ownerId'];
330 $type = $sqlHelper->forSql($params[
'type']);
333 self::getFilterId($type, $ownerId, $userId)
335 $fields[
'fields'][
'SECTION_ID'] = self::getSectionsForFilter(
343 $type ===
'company_calendar'
344 || $type ===
'calendar_company'
345 || $type ===
'company'
349 return self::getFilterCompanyData($type, $userId, $ownerId, $fields);
366 public static function getFilterUserData(
string $type,
int $userId,
int $ownerId, $fields): array
371 'OWNER_ID' => $ownerId,
373 'ACTIVE_SECTION' =>
'Y',
376 if (isset($fields[
'fields'][
'IS_MEETING']))
378 $filter[
'IS_MEETING'] = $fields[
'fields'][
'IS_MEETING'] ===
'Y';
381 if (isset($fields[
'fields'][
'MEETING_STATUS']))
383 $filter[
'MEETING_STATUS'] = $fields[
'fields'][
'MEETING_STATUS'];
384 if ($filter[
'MEETING_STATUS'] ===
'H')
386 unset($filter[
'MEETING_STATUS']);
387 $filter[
'MEETING_HOST'] = $userId;
391 $filter[
'IS_MEETING'] =
true;
394 if ($fields[
'presetId'] === self::PRESET_INVITED)
396 $filter[
'FROM_LIMIT'] = \CCalendar::Date(time(),
false);
397 $filter[
'TO_LIMIT'] = \CCalendar::Date(time() + \CCalendar::DAY_LENGTH * 90,
false);
398 \CCalendar::UpdateCounter([$ownerId]);
403 if (isset($fields[
'fields'][
'CREATED_BY']))
405 unset($filter[
'OWNER_ID'], $filter[
'CAL_TYPE']);
406 $filter[
'MEETING_HOST'] = $fields[
'fields'][
'CREATED_BY'];
408 $filter[
'CREATED_BY'] = $userId;
411 if (!empty($fields[
'fields'][
'SECTION_ID']))
413 $filter[
'SECTION'] = $fields[
'fields'][
'SECTION_ID'];
419 'entries' => $entries,
420 'counters' => $counters
424 if (isset($fields[
'fields'][
'ATTENDEES']))
426 $query = EventTable::query()
428 ->registerRuntimeField(
432 EventTable::getEntity(),
433 Join::on(
'ref.PARENT_ID',
'this.PARENT_ID'),
434 [
'join_type' => Join::TYPE_LEFT]
437 ->where(
'DELETED',
'N')
438 ->where(
'EVENT_SECOND.DELETED',
'N')
439 ->where(
'CAL_TYPE', $type)
440 ->where(
'CREATED_BY', $userId)
441 ->whereIn(
'EVENT_SECOND.CREATED_BY', $fields[
'fields'][
'ATTENDEES'])
445 while ($event = $query->fetch())
447 $filter[
'ID'][] = (int)$event[
'ID'];
450 if ($filter[
'ID'] ??
false)
452 $filter[
'ID'] = array_unique($filter[
'ID']);
459 $filter[
'IS_MEETING'] =
true;
462 [$filter, $parseRecursion] = self::filterByDate($fields, $filter);
464 if (isset($fields[
'search']) && $fields[
'search'])
466 $filter[(\CCalendarEvent::isFullTextIndexEnabled() ?
'*' :
'*%').
'SEARCHABLE_CONTENT'] = \CCalendarEvent::prepareToken(Emoji::encode($fields[
'search']));
469 $entries = \CCalendarEvent::GetList(
471 'arFilter' => $filter,
472 'fetchAttendees' =>
true,
473 'parseRecursion' => $parseRecursion,
474 'maxInstanceCount' => 50,
475 'preciseLimits' => $parseRecursion,
477 'fetchMeetings' =>
true,
478 'fetchSection' =>
true,
479 'setDefaultLimit' =>
false
482 if ($fields[
'presetId'] !== self::PRESET_DECLINED)
489 'entries' => $entries,
490 'counters' => $counters
505 private static function getFilterCompanyData(
string $type,
int $userId,
int $ownerId, $fields): array
509 'ACTIVE_SECTION' =>
'Y',
513 $query = EventTable::query()
514 ->setSelect([
'PARENT_ID'])
515 ->registerRuntimeField(
519 EventTable::getEntity(),
520 Join::on(
'ref.PARENT_ID',
'this.ID'),
521 [
'join_type' => Join::TYPE_LEFT]
524 ->where(
'CAL_TYPE', $type)
525 ->where(
'DELETED',
'N')
526 ->where(
'EVENT_SECOND.DELETED',
'N')
529 if (isset($fields[
'fields'][
'IS_MEETING']) && $fields[
'fields'][
'IS_MEETING'])
531 $filter[
'IS_MEETING'] = $fields[
'fields'][
'IS_MEETING'] ===
'Y';
534 if (isset($fields[
'fields'][
'MEETING_STATUS']) && $fields[
'fields'][
'MEETING_STATUS'])
536 $query->where(
'EVENT_SECOND.CREATED_BY', $userId);
538 $fields[
'fields'][
'MEETING_STATUS'] ===
'H'
539 && !isset($fields[
'fields'][
'CREATED_BY'])
542 unset($filter[
'IS_MEETING']);
543 $query->where(
'EVENT_SECOND.MEETING_HOST', $userId);
547 $query->where(
'EVENT_SECOND.MEETING_STATUS', $fields[
'fields'][
'MEETING_STATUS']);
548 $filter[
'IS_MEETING'] =
true;
552 if (isset($fields[
'fields'][
'CREATED_BY']) && is_array($fields[
'fields'][
'CREATED_BY']))
554 $query->whereIn(
'EVENT_SECOND.MEETING_HOST', $fields[
'fields'][
'CREATED_BY']);
557 if (isset($fields[
'fields'][
'SECTION_ID']) && is_array($fields[
'fields'][
'SECTION_ID']))
559 $query->whereIn(
'SECTION_ID', $fields[
'fields'][
'SECTION_ID']);
562 if (isset($fields[
'fields'][
'ATTENDEES']) && is_array($fields[
'fields'][
'ATTENDEES']))
564 if (isset($fields[
'fields'][
'MEETING_STATUS']))
567 ->registerRuntimeField(
571 EventTable::getEntity(),
572 Join::on(
'ref.PARENT_ID',
'this.ID'),
573 [
'join_type' => Join::TYPE_LEFT]
576 ->whereIn(
'EVENT_THIRD.CREATED_BY', $fields[
'fields'][
'ATTENDEES'])
581 $query->whereIn(
'EVENT_SECOND.CREATED_BY', $fields[
'fields'][
'ATTENDEES']);
583 $filter[
'IS_MEETING'] =
true;
586 if (isset($fields[
'search']) && $fields[
'search'])
588 $filter[(\CCalendarEvent::isFullTextIndexEnabled() ?
'*' :
'*%').
'SEARCHABLE_CONTENT'] = \CCalendarEvent::prepareToken($fields[
'search']);
591 [$filter, $parseRecursion] = self::filterByDate($fields, $filter);
593 $eventsFromQuery = $query->exec();
595 while ($event = $eventsFromQuery->Fetch())
597 $filter[
'ID'][] = (int)$event[
'PARENT_ID'];
600 if (isset($filter[
'ID']))
602 $filter[
'ID'] = array_unique($filter[
'ID']);
604 $entries = \CCalendarEvent::GetList(
606 'arFilter' => $filter,
607 'fetchAttendees' =>
true,
608 'parseRecursion' => $parseRecursion,
609 'maxInstanceCount' => 50,
610 'preciseLimits' => $parseRecursion,
612 'fetchMeetings' =>
true,
613 'fetchSection' =>
true,
614 'setDefaultLimit' =>
false
618 $entries = self::applyAccessRestrictions($entries);
623 'entries' => $entries,
633 private static function filterByDate($fields, array $filter): array
635 $parseRecursion =
false;
638 if (isset($fields[
'fields'][
'DATE_FROM']))
640 $fromTs = \CCalendar::Timestamp($fields[
'fields'][
'DATE_FROM'],
true,
false);
641 $filter[
'FROM_LIMIT'] = \CCalendar::Date($fromTs,
false);
643 else if (!($filter[
'FROM_LIMIT'] ??
null))
645 $filter[
'FROM_LIMIT'] = \CCalendar::Date(time() - 31 * 12 * 24 * 3600,
false);
648 if (isset($fields[
'fields'][
'DATE_TO']))
650 $toTs = \CCalendar::Timestamp($fields[
'fields'][
'DATE_TO'],
true,
false);
651 $filter[
'TO_LIMIT'] = \CCalendar::Date($toTs,
false);
652 if ($fromTs && $toTs < $fromTs)
654 $filter[
'TO_LIMIT'] = $filter[
'FROM_LIMIT'];
658 if ($fromTs && $toTs && $fromTs <= $toTs)
660 $parseRecursion =
true;
673 private static function applyAccessRestrictions(array $events): array
675 foreach ($events as $i => $event)
678 isset($event[
'IS_ACCESSIBLE_TO_USER'])
679 && $event[
'IS_ACCESSIBLE_TO_USER'] ===
false
686 return array_values($events);
692 $optionShowDeclined = $settings[
'showDeclined'];
694 return array_values(array_filter($entries,
static function($entry) use ($optionShowDeclined) {
695 $hideDeclinedEntry = (!$optionShowDeclined || (int)$entry[
'CREATED_BY'] !== \CCalendar::GetUserId());
696 return !($hideDeclinedEntry && $entry[
'MEETING_STATUS'] ===
'N');
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)