1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
RelationCollection.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2;
4
5use Bitrix\Im\Model\RelationTable;
6use Bitrix\Im\V2\Chat\OpenLineLiveChat;
7use Bitrix\Im\V2\Entity\User\UserCollection;
8use Bitrix\Im\V2\Entity\User\UserPopupItem;
9use Bitrix\Im\V2\Rest\PopupData;
10use Bitrix\Im\V2\Rest\PopupDataAggregatable;
11use Bitrix\Im\V2\Rest\RestConvertible;
12use Bitrix\Im\V2\Service\Context;
13use Bitrix\Main\ORM\Query\Query;
14use Bitrix\Main\UserTable;
15
22{
23 public const COMMON_FIELDS = [
24 'ID',
25 'MESSAGE_TYPE',
26 'CHAT_ID',
27 'USER_ID',
28 'START_ID',
29 'LAST_FILE_ID',
30 'LAST_ID',
31 'LAST_SEND_MESSAGE_ID',
32 'UNREAD_ID',
33 'NOTIFY_BLOCK',
34 'MANAGER',
35 'REASON',
36 'IS_HIDDEN',
37 ];
38
39 protected static array $startIdStaticCache = [];
40
41 protected array $relationsByUserId = [];
42 protected ?self $activeOnly = null;
43 protected ?self $notifyOnly = null;
44
45 public static function getCollectionElementClass(): string
46 {
47 return Relation::class;
48 }
49
50 public static function find(
52 array $order = [],
53 ?int $limit = null,
54 ?Context $context = null,
55 array $select = self::COMMON_FIELDS
56 ): self
57 {
58 $query = RelationTable::query()->setSelect($select);
59
60 if (isset($limit))
61 {
62 $query->setLimit($limit);
63 }
64
65 static::processFilters($query, $filter, $order);
66
67 return new static($query->fetchAll());
68 }
69
70 public function toRestFormat(array $option = []): ?array
71 {
72 $rest = [];
73 foreach ($this as $relation)
74 {
75 $rest[] = $relation->toRestFormat($option);
76 }
77
78 return $rest;
79 }
80
81 public static function getRestEntityName(): string
82 {
83 return 'relations';
84 }
85
86 public function getPopupData(array $excludedList = []): PopupData
87 {
88 return new PopupData([new UserPopupItem($this->getUserIds())], $excludedList);
89 }
90
91 public static function createFake(array $userIds, Chat $chat): self
92 {
93 $relations = new static();
94
95 foreach ($userIds as $userId)
96 {
97 $relation = new Relation();
98 $relation
99 ->setId(0)
100 ->setUserId($userId)
101 ->setChatId($chat->getId())
102 ->setMessageType($chat->getType())
103 ->setNotifyBlock(false)
104 ->markAsFake()
105 ;
106 $relations->add($relation);
107 }
108
109 return $relations;
110 }
111
112 public static function getStartId(int $userId, int $chatId): int
113 {
114 if (isset(self::$startIdStaticCache[$chatId][$userId]))
115 {
116 return self::$startIdStaticCache[$chatId][$userId];
117 }
118
119 $relation = static::find(['CHAT_ID' => $chatId, 'USER_ID' => $userId], [], 1)->getByUserId($userId, $chatId);
120
121 if ($relation === null)
122 {
123 return 0;
124 }
125
126 return $relation->getStartId() ?? 0;
127 }
128
129 public function getByUserId(int $userId, int $chatId): ?Relation
130 {
131 return $this->relationsByUserId[$chatId][$userId] ?? null;
132 }
133
134 public function filterActive(): self
135 {
136 if (isset($this->activeOnly))
137 {
138 return $this->activeOnly;
139 }
140
141 $this->activeOnly = new static();
142
143 foreach ($this as $relation)
144 {
145 $isLiveChat = $relation->getChat() instanceof OpenLineLiveChat;
146 if ($relation->getUser()->isActive() && ($isLiveChat || !$relation->getUser()->isConnector()))
147 {
148 $this->activeOnly->add($relation);
149 }
150 }
151
152 return $this->activeOnly;
153 }
154
155 public function filterNotifySubscribed(): self
156 {
157 if (isset($this->notifyOnly))
158 {
159 return $this->notifyOnly;
160 }
161
162 $this->notifyOnly = new static();
163
164 foreach ($this as $relation)
165 {
166 if (!($relation->getNotifyBlock() ?? false))
167 {
168 $this->notifyOnly->add($relation);
169 }
170 }
171
172 return $this->notifyOnly;
173 }
174
175 public function hasUser(int $userId, int $chatId): bool
176 {
177 return isset($this->relationsByUserId[$chatId][$userId]);
178 }
179
180 public function getUserIds(): array
181 {
182 $userIds = [];
183 foreach ($this as $relation)
184 {
185 $userIds[$relation->getUserId()] = $relation->getUserId();
186 }
187
188 return $userIds;
189 }
190
191
192 public function getUsers(): UserCollection
193 {
194 return new UserCollection($this->getUserIds());
195 }
196
197 protected static function processFilters(Query $query, array $filter, array $order): void
198 {
199 $orderField = null;
200 $relationOrder = [];
201
202 if (isset($filter['CHAT_ID']))
203 {
204 $query->where('CHAT_ID', (int)$filter['CHAT_ID']);
205 }
206
207 if (isset($filter['MANAGER']))
208 {
209 $query->where('MANAGER', (string)$filter['MANAGER']);
210 }
211
212 if (isset($filter['USER_ID']))
213 {
214 if (is_array($filter['USER_ID']) && !empty($filter['USER_ID']))
215 {
216 $query->whereIn('USER_ID', $filter['USER_ID']);
217 }
218 else
219 {
220 $query->where('USER_ID', (int)$filter['USER_ID']);
221 }
222 }
223
224 if (isset($filter['!USER_ID']))
225 {
226 if (is_array($filter['!USER_ID']) && !empty($filter['!USER_ID']))
227 {
228 $query->whereNotIn('USER_ID', $filter['!USER_ID']);
229 }
230 else
231 {
232 $query->whereNot('USER_ID', (int)$filter['!USER_ID']);
233 }
234 }
235
236 if (isset($filter['MESSAGE_TYPE']))
237 {
238 $query->where('MESSAGE_TYPE', (string)$filter['MESSAGE_TYPE']);
239 }
240
241 foreach (['ID', 'USER_ID', 'LAST_SEND_MESSAGE_ID'] as $allowedFieldToOrder)
242 {
243 if (isset($order[$allowedFieldToOrder]))
244 {
245 $orderField = $allowedFieldToOrder;
246 $relationOrder[$allowedFieldToOrder] = $order[$allowedFieldToOrder];
247 break;
248 }
249 }
250
251 if (isset($orderField))
252 {
253 $query->setOrder($relationOrder);
254 }
255
256 if (isset($filter['LAST_ID']))
257 {
258 $operator = '<';
259 if (isset($orderField) && $relationOrder[$orderField] === 'ASC')
260 {
261 $operator = '>';
262 }
263 $query->where($orderField, $operator, (int)$filter['LAST_ID']);
264 }
265
266 if (isset($filter['ACTIVE']))
267 {
268 $query->where('USER.ACTIVE', $filter['ACTIVE']);
269 }
270
271 if (isset($filter['ONLY_INTERNAL_TYPE']) && $filter['ONLY_INTERNAL_TYPE'])
272 {
273 $query->where(
274 Query::filter()
275 ->logic('or')
276 ->whereNotIn('USER.EXTERNAL_AUTH_ID', UserTable::getExternalUserTypes())
277 ->whereNull('USER.EXTERNAL_AUTH_ID')
278 );
279 }
280
281 if (isset($filter['ONLY_INTRANET']) && $filter['ONLY_INTRANET'])
282 {
283 if (\Bitrix\Main\Loader::includeModule('extranet'))
284 {
285 $query->where('USER.IS_INTRANET_USER', true);
286 }
287 }
288
289 if (isset($filter['REASON']))
290 {
291 $query->where('REASON', (string)$filter['REASON']);
292 }
293
294 if (isset($filter['IS_HIDDEN']))
295 {
296 $query->where('IS_HIDDEN', $filter['IS_HIDDEN']);
297 }
298 }
299
300 public function offsetSet($key, $value): void
301 {
303 parent::offsetSet($key, $value);
304
305 if ($value->getUserId() !== null && $value->getChatId() !== null)
306 {
307 $this->relationsByUserId[$value->getChatId()][$value->getUserId()] = $value;
308 if ($value->getStartId() !== null)
309 {
310 static::$startIdStaticCache[$value->getChatId()][$value->getUserId()] = $value->getStartId();
311 }
312 }
313
314 $this->activeOnly = null;
315 $this->notifyOnly = null;
316 }
317
318 public function offsetUnset(mixed $key)
319 {
321 $value = $this[$key] ?? null;
322 if ($value)
323 {
324 unset($this->relationsByUserId[$value->getChatId()][$value->getUserId()]);
325 unset(static::$startIdStaticCache[$value->getChatId()][$value->getUserId()]);
326 }
327 parent::offsetUnset($key);
328
329 $this->activeOnly = null;
330 $this->notifyOnly = null;
331 }
332
333 public function onAfterRelationDelete(int $chatId, int $userId): void
334 {
335 $relation = $this->getByUserId($userId, $chatId);
336 if ($relation)
337 {
338 unset($this[$relation->getId()]);
339 }
340 unset($this->relationsByUserId[$chatId][$userId]);
341 unset(self::$startIdStaticCache[$chatId][$userId]);
342 }
343}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getType($chatData, bool $camelCase=true)
Определения chat.php:45
toRestFormat(array $option=[])
Определения RelationCollection.php:70
static getCollectionElementClass()
Определения RelationCollection.php:45
static processFilters(Query $query, array $filter, array $order)
Определения RelationCollection.php:197
static getStartId(int $userId, int $chatId)
Определения RelationCollection.php:112
getPopupData(array $excludedList=[])
Определения RelationCollection.php:86
static array $startIdStaticCache
Определения RelationCollection.php:39
static find(array $filter, array $order=[], ?int $limit=null, ?Context $context=null, array $select=self::COMMON_FIELDS)
Определения RelationCollection.php:50
static createFake(array $userIds, Chat $chat)
Определения RelationCollection.php:91
onAfterRelationDelete(int $chatId, int $userId)
Определения RelationCollection.php:333
getByUserId(int $userId, int $chatId)
Определения RelationCollection.php:129
hasUser(int $userId, int $chatId)
Определения RelationCollection.php:175
offsetSet($offset, $value)
Определения collection.php:944
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
$context
Определения csv_new_setup.php:223
Определения culture.php:9
Определения chain.php:3
$order
Определения payment.php:8
if(empty($signedUserToken)) $key
Определения quickway.php:257
$option
Определения options.php:1711