25 if (!Loader::includeModule(
'intranet'))
30 return parent::isAvailable();
35 $searchQueryOption =
$options[
'searchQuery'] ??
null;
36 if (!is_string($searchQueryOption))
41 $options[
'searchQuery'] = trim($searchQueryOption);
43 !isset(
$options[
'searchableChatTypes'])
44 || !is_array(
$options[
'searchableChatTypes'])
45 || mb_strlen(
$options[
'searchQuery']) < Filter\Helper::getMinTokenSize()
52 foreach (static::getSearchableChatTypes() as $chatType)
54 if (static::shouldSearchChatType($chatType,
$options))
56 $chatTypeList[] = $chatType;
59 if (empty($chatTypeList))
64 $options[
'order'] ??= [
'LAST_MESSAGE_ID' =>
'DESC'];
65 $chatIdList = static::getChatIdList(
$options[
'searchQuery'], $chatTypeList,
$options[
'order']);
66 if (empty($chatIdList))
71 $query = ChatTable::query();
74 ->addSelect(
'RELATION.USER_ID',
'RELATION_USER_ID')
75 ->addSelect(
'RELATION.NOTIFY_BLOCK',
'RELATION_NOTIFY_BLOCK')
77 ->addSelect(
'RELATION.START_COUNTER',
'RELATION_START_COUNTER')
78 ->addSelect(
'RELATION.LAST_ID',
'RELATION_LAST_ID')
81 ->addSelect(
'ALIAS.ALIAS',
'ALIAS_NAME')
83 $query->registerRuntimeField(
88 Join::on(
'this.ID',
'ref.CHAT_ID'),
89 ))->configureJoinType(Join::TYPE_INNER)
93 $query->whereIn(
'ID', $chatIdList);
95 $query->setLimit(static::MAX_CHATS_IN_SAMPLE);
100 protected static function getChatIdList(
string $searchQuery, array $chatTypeList, array $order): array
102 $query = ChatTable::query();
103 $query->addSelect(
'ID');
105 $query->registerRuntimeField(
109 RelationTable::class,
110 Join::on(
'this.ID',
'ref.CHAT_ID'),
111 ))->configureJoinType(Join::TYPE_INNER)
114 $query->registerRuntimeField(
118 static::getDerivedTableEntity($chatTypeList, $searchQuery),
119 Join::on(
'this.ID',
'ref.CHAT_ID')
120 ))->configureJoinType(Join::TYPE_INNER)
124 $query->setOrder($order);
125 $query->setLimit(static::MAX_CHATS_IN_SAMPLE);
128 foreach ($query->exec() as $row)
130 $chatIdList[] = (int)$row[
'ID'];
146 return 'im-chat-user';
153 if ($searchText ===
'')
155 $filter->whereLike(
'USER_INDEX.SEARCH_USER_CONTENT', $searchQuery .
'%');
160 $filter->whereMatch(
'USER_INDEX.SEARCH_USER_CONTENT', $searchText);
163 private static function getDerivedTableEntity(array $chatTypeList,
string $searchQuery):
Entity
165 $derivedTableQuery = self::getDerivedTableQuery($chatTypeList, $searchQuery);
167 return Entity::getInstanceByQuery($derivedTableQuery);
170 private static function getDerivedTableQuery(array $chatTypeList,
string $searchQuery):
Query
172 $query = RelationTable::query();
174 $query->addSelect(
'CHAT_ID');
176 $query->registerRuntimeField(
181 Join::on(
'this.USER_ID',
'ref.ID'),
182 ))->configureJoinType(Join::TYPE_INNER)
184 $query->registerRuntimeField(
188 UserIndexTable::class,
189 Join::on(
'this.USER_ID',
'ref.USER_ID'),
190 ))->configureJoinType(Join::TYPE_INNER)
193 $query->whereIn(
'MESSAGE_TYPE', $chatTypeList);
194 $query->where(
'USER.IS_REAL_USER',
'Y');
196 $matchFilter = Query::filter();
197 static::addFilterBySearchQuery($matchFilter, $searchQuery);
198 $query->where($matchFilter);
200 $query->addGroup(
'CHAT_ID');