• Модуль: im
  • Путь к файлу: ~/bitrix/modules/im/lib/integration/ui/entityselector/botprovider.php
  • Класс: BitrixImIntegrationUIEntitySelectorBotProvider
  • Вызов: BotProvider::getBots
static function getBots(array $options = []): EO_User_Collection
{
	$query = UserTable::query();

	$query->setSelect([
		'ID',
		'NAME',
		'LAST_NAME',
		'SECOND_NAME',
		'PERSONAL_PHOTO',
		'WORK_POSITION',
		'BOT_TYPE' => 'im_bot.TYPE',
		'BOT_COUNT_MESSAGE' => 'im_bot.COUNT_MESSAGE',
	]);

	$query->registerRuntimeField(
		new Reference(
			'im_bot',
			BotTable::class,
			Join::on('this.ID', 'ref.BOT_ID'),
			['join_type' => Join::TYPE_INNER]
		)
	);

	if (!empty($options['searchQuery']) && is_string($options['searchQuery']))
	{
		$query->registerRuntimeField(
			new Reference(
				'USER_INDEX',
				BitrixMainUserIndexTable::class,
				Join::on('this.ID', 'ref.USER_ID'),
				['join_type' => 'INNER']
			)
		);

		$query->whereMatch(
			'USER_INDEX.SEARCH_USER_CONTENT',
			FilterHelper::matchAgainstWildcard(
				Content::prepareStringToken($options['searchQuery']), '*', 1
			)
		);
	}

	if (isset($options['searchableBotTypes']) && is_array($options['searchableBotTypes']))
	{
		$query->whereIn('BOT_TYPE', $options['searchableBotTypes']);
	}

	$userIds = [];
	$userFilter = isset($options['userId']) ? 'userId' : (isset($options['!userId']) ? '!userId' : null);
	if (isset($options[$userFilter]))
	{
		if (is_array($options[$userFilter]) && !empty($options[$userFilter]))
		{
			foreach ($options[$userFilter] as $id)
			{
				$id = (int)$id;
				if ($id > 0)
				{
					$userIds[] = $id;
				}
			}

			$userIds = array_unique($userIds);

			if (!empty($userIds))
			{
				if ($userFilter === 'userId')
				{
					$query->whereIn('ID', $userIds);
				}
				else
				{
					$query->whereNotIn('ID', $userIds);
				}
			}
		}
		else if (!is_array($options[$userFilter]) && (int)$options[$userFilter] > 0)
		{
			if ($userFilter === 'userId')
			{
				$query->where('ID', (int)$options[$userFilter]);
			}
			else
			{
				$query->whereNot('ID', (int)$options[$userFilter]);
			}
		}
	}

	if (isset($options['limit']) && is_int($options['limit']))
	{
		$query->setLimit($options['limit']);
	}
	else
	{
		$query->setLimit(100);
	}

	if (!empty($options['order']) && is_array($options['order']))
	{
		$query->setOrder($options['order']);
	}
	else
	{
		$query->setOrder([
			'BOT_COUNT_MESSAGE' => 'DESC'
		]);
	}

	$result = $query->exec();

	return $result->fetchCollection();
}