Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chatuserprovider.php
1<?php
3
18
20{
21 protected const MAX_CHATS_IN_SAMPLE = 20;
22
23 public function isAvailable(): bool
24 {
25 if (!Loader::includeModule('intranet'))
26 {
27 return false;
28 }
29
30 return parent::isAvailable();
31 }
32
33 public static function getChats(array $options = []): array
34 {
35 $searchQueryOption = $options['searchQuery'] ?? null;
36 if (!is_string($searchQueryOption))
37 {
38 return [];
39 }
40
41 $options['searchQuery'] = trim($searchQueryOption);
42 if (
43 !isset($options['searchableChatTypes'])
44 || !is_array($options['searchableChatTypes'])
45 || mb_strlen($options['searchQuery']) < Filter\Helper::getMinTokenSize()
46 )
47 {
48 return [];
49 }
50
51 $chatTypeList = [];
52 foreach (static::getSearchableChatTypes() as $chatType)
53 {
54 if (static::shouldSearchChatType($chatType, $options))
55 {
56 $chatTypeList[] = $chatType;
57 }
58 }
59 if (empty($chatTypeList))
60 {
61 return [];
62 }
63
64 $options['order'] ??= ['LAST_MESSAGE_ID' => 'DESC'];
65 $chatIdList = static::getChatIdList($options['searchQuery'], $chatTypeList, $options['order']);
66 if (empty($chatIdList))
67 {
68 return [];
69 }
70
71 $query = ChatTable::query();
72 $query
73 ->addSelect('*')
74 ->addSelect('RELATION.USER_ID', 'RELATION_USER_ID')
75 ->addSelect('RELATION.NOTIFY_BLOCK', 'RELATION_NOTIFY_BLOCK')
76 //->addSelect('RELATION.COUNTER', 'RELATION_COUNTER')
77 ->addSelect('RELATION.START_COUNTER', 'RELATION_START_COUNTER')
78 ->addSelect('RELATION.LAST_ID', 'RELATION_LAST_ID')
79 //->addSelect('RELATION.STATUS', 'RELATION_STATUS')
80 //->addSelect('RELATION.UNREAD_ID', 'RELATION_UNREAD_ID')
81 ->addSelect('ALIAS.ALIAS', 'ALIAS_NAME')
82 ;
83 $query->registerRuntimeField(
84 'RELATION',
85 (new Reference(
86 'RELATION',
87 RelationTable::class,
88 Join::on('this.ID', 'ref.CHAT_ID'),
89 ))->configureJoinType(Join::TYPE_INNER)
90 );
91
92 $query->where('RELATION.USER_ID', User::getInstance()->getId());
93 $query->whereIn('ID', $chatIdList);
94 $query->setOrder($options['order']);
95 $query->setLimit(static::MAX_CHATS_IN_SAMPLE);
96
97 return Chat::fillCounterData($query->fetchAll());
98 }
99
100 protected static function getChatIdList(string $searchQuery, array $chatTypeList, array $order): array
101 {
102 $query = ChatTable::query();
103 $query->addSelect('ID');
104
105 $query->registerRuntimeField(
106 'RELATION',
107 (new Reference(
108 'RELATION',
109 RelationTable::class,
110 Join::on('this.ID', 'ref.CHAT_ID'),
111 ))->configureJoinType(Join::TYPE_INNER)
112 );
113
114 $query->registerRuntimeField(
115 'CHAT_SEARCH',
116 (new Reference(
117 'CHAT_SEARCH',
118 static::getDerivedTableEntity($chatTypeList, $searchQuery),
119 Join::on('this.ID', 'ref.CHAT_ID')
120 ))->configureJoinType(Join::TYPE_INNER)
121 );
122
123 $query->where('RELATION.USER_ID', User::getInstance()->getId());
124 $query->setOrder($order);
125 $query->setLimit(static::MAX_CHATS_IN_SAMPLE);
126
127 $chatIdList = [];
128 foreach ($query->exec() as $row)
129 {
130 $chatIdList[] = (int)$row['ID'];
131 }
132
133 return $chatIdList;
134 }
135
136 protected static function getSearchableChatTypes(): array
137 {
138 return [
141 ];
142 }
143
144 protected static function getEntityId(): string
145 {
146 return 'im-chat-user';
147 }
148
149 protected static function addFilterBySearchQuery(Filter\ConditionTree $filter, string $searchQuery): void
150 {
151 $searchText = ChatIndex::matchAgainstWildcard(Content::prepareStringToken($searchQuery) , '');
152
153 if ($searchText === '')
154 {
155 $filter->whereLike('USER_INDEX.SEARCH_USER_CONTENT', $searchQuery . '%');
156
157 return;
158 }
159
160 $filter->whereMatch('USER_INDEX.SEARCH_USER_CONTENT', $searchText);
161 }
162
163 private static function getDerivedTableEntity(array $chatTypeList, string $searchQuery): Entity
164 {
165 $derivedTableQuery = self::getDerivedTableQuery($chatTypeList, $searchQuery);
166
167 return Entity::getInstanceByQuery($derivedTableQuery);
168 }
169
170 private static function getDerivedTableQuery(array $chatTypeList, string $searchQuery): Query
171 {
172 $query = RelationTable::query();
173
174 $query->addSelect('CHAT_ID');
175
176 $query->registerRuntimeField(
177 'USER',
178 (new Reference(
179 'USER',
180 UserTable::class,
181 Join::on('this.USER_ID', 'ref.ID'),
182 ))->configureJoinType(Join::TYPE_INNER)
183 );
184 $query->registerRuntimeField(
185 'USER_INDEX',
186 (new Reference(
187 'USER_INDEX',
188 UserIndexTable::class,
189 Join::on('this.USER_ID', 'ref.USER_ID'),
190 ))->configureJoinType(Join::TYPE_INNER)
191 );
192
193 $query->whereIn('MESSAGE_TYPE', $chatTypeList);
194 $query->where('USER.IS_REAL_USER', 'Y');
195
196 $matchFilter = Query::filter();
197 static::addFilterBySearchQuery($matchFilter, $searchQuery);
198 $query->where($matchFilter);
199
200 $query->addGroup('CHAT_ID');
201
202 return $query;
203 }
204
205}
static fillCounterData(array $chats)
Definition chat.php:1406
const TYPE_OPEN
Definition chat.php:22
const TYPE_GROUP
Definition chat.php:24
static addFilterBySearchQuery(Filter\ConditionTree $filter, string $searchQuery)
static getChatIdList(string $searchQuery, array $chatTypeList, array $order)
static matchAgainstWildcard($phrase, $leftWildcard='+', $rightWildcard=' *', $minTokenSize=null)
static getInstance($userId=null)
Definition user.php:44