• Модуль: calendar
  • Путь к файлу: ~/bitrix/modules/calendar/classes/general/calendar_event.php
  • Класс: CCalendarEvent
  • Вызов: CCalendarEvent::getListOrm
static function getListOrm($params = [])
{
	$eventList = [];

	$userId = (isset($params['userId']) && $params['userId']) ? (int)$params['userId'] : CCalendar::GetCurUserId();
	$fetchSection = $params['fetchSection'] ?? null;
	$orderFields = $params['arOrder'] ?? [];
	$filterFields = $params['arFilter'] ?? [];
	$selectFields = $params['arSelect'] ?? [];
	$getUf = ($params['getUserfields'] ?? null) !== false;
	$eventFields = self::getEventFields();

	if (isset($filterFields["DELETED"]) && ($filterFields["DELETED"] === false))
	{
		unset($filterFields["DELETED"]);
	}
	elseif (!isset($filterFields["DELETED"]))
	{
		$filterFields["DELETED"] = "N";
	}

	if (($params['setDefaultLimit'] ?? null) !== false) // Deprecated
	{
		if (!isset($filterFields['FROM_LIMIT'])) // default 3 month back
		{
			$filterFields['FROM_LIMIT'] = CCalendar::Date(time() - 31 * 3 * 24 * 3600, false);
		}

		if (!isset($filterFields['TO_LIMIT'])) // default one year into the future
		{
			$filterFields['TO_LIMIT'] = CCalendar::Date(time() + 365 * 24 * 3600, false);
		}
	}

	$query = InternalsEventTable::query();

	if (!empty($filterFields) && is_array($filterFields))
	{
		foreach ($filterFields as $key => $value)
		{
			if (is_string($value) && !$value)
			{
				continue;
			}

			switch ($key)
			{
				case 'FROM_LIMIT':
					$timestamp = (int)CCalendar::Timestamp($value, false);
					if ($timestamp)
					{
						$query->where('DATE_TO_TS_UTC', '>=', $timestamp);
					}
					break;
				case 'TO_LIMIT':
					$timestamp = (int)CCalendar::Timestamp($value, false);
					if ($timestamp)
					{
						$toTimestamp = $timestamp + CCalendar::GetDayLen() - 1;
						$query->where('DATE_FROM_TS_UTC', '<=', $toTimestamp);
					}
					break;
				case 'ID':
				case 'PARENT_ID':
				case 'RECURRENCE_ID':
				case 'OWNER_ID':
				case 'MEETING_HOST':
				case 'CREATED_BY':
					if (is_array($value))
					{
						$value = array_map(static function($item) {
							return (int)$item;
						}, $value);
						
						if (empty($value))
						{
							$value = [''];
						}

						$query->whereIn($key, $value);
					}
					else if ((int)$value)
					{
						$query->where($key, $value);
					}
					break;
				case '>ID':
					if ((int)$value)
					{
						$query->where('ID', '>', $value);
					}
					break;
				case 'SECTION':
					if (!is_array($value))
					{
						$value = [$value];
					}

					if (is_array($value))
					{
						$sections = [];
						foreach ($value as $item)
						{
							if ((int)$item)
							{
								$sections[] = (int)$item;
							}
						}

						if (!empty($sections))
						{
							if (Util::isSectionStructureConverted())
							{
								$query->whereIn('SECTION_ID', $sections);
							}
							else
							{
								$query->whereIn('EVENT_SECT.SECT_ID', $sections);
							}
						}
					}
					break;
				case 'ACTIVE_SECTION':
					if ($value === 'Y' && Util::isSectionStructureConverted())
					{
						$query->where('SECTION.ACTIVE', $value);
					}
					break;
				case '*SEARCHABLE_CONTENT':
					$searchText = BitrixMainORMQueryFilterHelper::matchAgainstWildcard($value);
					$query->whereMatch('SEARCHABLE_CONTENT', $searchText);
					break;
				case '*%SEARCHABLE_CONTENT':
					$query->whereLike('SEARCHABLE_CONTENT', '%' . $value . '%');
					break;
				case '=UF_CRM_CAL_EVENT':
					$query->where('UF_CRM_CAL_EVENT', $value);
					break;
				default:
					if (in_array($key, $eventFields, true))
					{
						if (is_array($value))
						{
							$query->whereIn($key, $value);
						}
						else
						{
							$query->where($key, $value);
						}
					}
					break;
			}
		}
	}

	if (empty($selectFields))
	{
		$selectFields = ['*'];
	}

	if (
		$fetchSection
		&& ($filterFields['ACTIVE_SECTION'] ?? null) === 'Y'
		&& Util::isSectionStructureConverted()
	)
	{
		$selectFields['SECTION_DAV_XML_ID'] = 'SECTION.CAL_DAV_CAL';
	}

	if ($getUf)
	{
		$selectFields[] = 'UF_*';
	}

	$query->setSelect($selectFields);

	$orderList = [];
	foreach ($orderFields as $key => $order)
	{
		if (in_array($key, $eventFields, true))
		{
			$orderList[$key] = (mb_strtoupper($order) === 'DESC') ? 'DESC' : 'ASC';
		}
	}

	if (!empty($orderList))
	{
		$query->setOrder($orderList);
	}

	if (isset($params['limit']) && (int)$params['limit'] > 0)
	{
		$query->setLimit((int)$params['limit']);
	}

	$queryResult = $query->exec();
	$parentMeetingIdList = [];
	$involvedUsersIdList = [];
	$defaultMeetingSection = null;

	while ($event = $queryResult->fetch())
	{
		if (!empty($event['DATE_FROM']))
		{
			$event['DATE_FROM'] = (string)$event['DATE_FROM'];
		}

		if (!empty($event['DATE_TO']))
		{
			$event['DATE_TO'] = (string)$event['DATE_TO'];
		}

		if (!empty($event['ORIGINAL_DATE_FROM']))
		{
			$event['ORIGINAL_DATE_FROM'] = (string)$event['ORIGINAL_DATE_FROM'];
		}

		if (!empty($event['TIMESTAMP_X']))
		{
			$event['TIMESTAMP_X'] = (string)$event['TIMESTAMP_X'];
		}

		if (!empty($event['DATE_CREATE']))
		{
			$event['DATE_CREATE'] = (string)$event['DATE_CREATE'];
		}

		$event['SECT_ID'] = $event['SECTION_ID'];
		$event['IS_MEETING'] = (int)($event['IS_MEETING'] ?? 0) > 0;

		if (!empty($event['NAME']))
		{
			$event['NAME'] = Emoji::decode($event['NAME']);
		}

		if (!empty($event['DESCRIPTION']))
		{
			$event['DESCRIPTION'] = Emoji::decode($event['DESCRIPTION']);
		}

		if (!empty($event['LOCATION']))
		{
			$event['LOCATION'] = Emoji::decode($event['LOCATION']);
		}

		if (!empty($event['DT_LENGTH']) && is_numeric($event['DT_LENGTH']))
		{
			$event['DT_LENGTH'] = (int)$event['DT_LENGTH'];
		}

		if (!empty($event['IS_MEETING']) && !empty($event['PARENT_ID']) && CCalendar::IsIntranetEnabled())
		{
			$parentMeetingIdList[] = $event['PARENT_ID'];
		}

		if (!empty($event['CREATED_BY']))
		{
			$involvedUsersIdList[] = $event['CREATED_BY'];
		}

		if (
			isset($event['IS_MEETING'])
			&& $event['IS_MEETING']
			&& $event['CAL_TYPE'] === 'user'
			&& (int)$event['OWNER_ID'] === $userId
			&& !$event['SECTION_ID']
		)
		{
			if (!$defaultMeetingSection)
			{
				$defaultMeetingSection = CCalendar::GetMeetingSection($userId);
				if (!$defaultMeetingSection || !CCalendarSect::GetById($defaultMeetingSection, false))
				{
					$sectRes = CCalendarSect::GetSectionForOwner($event['CAL_TYPE'], $userId);
					$defaultMeetingSection = $sectRes['sectionId'];
				}
			}

			$event['SECT_ID'] = $defaultMeetingSection;
			$event['SECTION_ID'] = $defaultMeetingSection;
		}

		$eventList[] = $event;
	}

	return [$eventList, $parentMeetingIdList, $involvedUsersIdList];
}