Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
RelationCollection.php
1<?php
2
3namespace Bitrix\Im\V2;
4
10
17{
18 public const COMMON_FIELDS = ['ID', 'MESSAGE_TYPE', 'CHAT_ID', 'USER_ID', 'START_ID', 'LAST_FILE_ID', 'LAST_ID', 'UNREAD_ID', 'NOTIFY_BLOCK', 'MANAGER'];
19
20 protected static array $startIdStaticCache = [];
21
22 protected array $relationsByUserId = [];
23
24 public static function getCollectionElementClass(): string
25 {
26 return Relation::class;
27 }
28
29 public static function find(
30 array $filter,
31 array $order = [],
32 ?int $limit = null,
33 ?Context $context = null,
34 array $select = self::COMMON_FIELDS
35 ): self
36 {
37 $query = RelationTable::query()->setSelect($select);
38
39 if (isset($limit))
40 {
41 $query->setLimit($limit);
42 }
43
44 static::processFilters($query, $filter, $order);
45
46 return new static($query->fetchCollection());
47 }
48
49 public static function getStartId(int $userId, int $chatId): int
50 {
51 if (isset(self::$startIdStaticCache[$chatId][$userId]))
52 {
53 return self::$startIdStaticCache[$chatId][$userId];
54 }
55
56 $relation = static::find(['CHAT_ID' => $chatId, 'USER_ID' => $userId], [], 1)->getByUserId($userId, $chatId);
57
58 if ($relation === null)
59 {
60 return 0;
61 }
62
63 return $relation->getStartId() ?? 0;
64 }
65
66 public function getByUserId(int $userId, int $chatId): ?Relation
67 {
68 return $this->relationsByUserId[$chatId][$userId] ?? null;
69 }
70
71 public function hasUser(int $userId, int $chatId): bool
72 {
73 return isset($this->relationsByUserId[$chatId][$userId]);
74 }
75
76 public function getUserIds(): array
77 {
78 $userIds = [];
79 foreach ($this as $relation)
80 {
81 $userIds[$relation->getUserId()] = $relation->getUserId();
82 }
83
84 return $userIds;
85 }
86
87
88 public function getUsers(): UserCollection
89 {
90 return new UserCollection($this->getUserIds());
91 }
92
93 protected static function processFilters(Query $query, array $filter, array $order): void
94 {
95 $orderField = null;
96 $relationOrder = [];
97
98 if (isset($filter['CHAT_ID']))
99 {
100 $query->where('CHAT_ID', (int)$filter['CHAT_ID']);
101 }
102
103 if (isset($filter['MANAGER']))
104 {
105 $query->where('MANAGER', (string)$filter['MANAGER']);
106 }
107
108 if (isset($filter['USER_ID']))
109 {
110 if (is_array($filter['USER_ID']) && !empty($filter['USER_ID']))
111 {
112 $query->whereIn('USER_ID', $filter['USER_ID']);
113 }
114 else
115 {
116 $query->where('USER_ID', (int)$filter['USER_ID']);
117 }
118 }
119
120 if (isset($filter['!USER_ID']))
121 {
122 if (is_array($filter['!USER_ID']) && !empty($filter['!USER_ID']))
123 {
124 $query->whereNotIn('USER_ID', $filter['!USER_ID']);
125 }
126 else
127 {
128 $query->whereNot('USER_ID', (int)$filter['!USER_ID']);
129 }
130 }
131
132 if (isset($filter['MESSAGE_TYPE']))
133 {
134 $query->where('MESSAGE_TYPE', (string)$filter['MESSAGE_TYPE']);
135 }
136
137 foreach (['ID', 'USER_ID', 'LAST_SEND_MESSAGE_ID'] as $allowedFieldToOrder)
138 {
139 if (isset($order[$allowedFieldToOrder]))
140 {
141 $orderField = $allowedFieldToOrder;
142 $relationOrder[$allowedFieldToOrder] = $order[$allowedFieldToOrder];
143 break;
144 }
145 }
146
147 if (isset($orderField))
148 {
149 $query->setOrder($relationOrder);
150 }
151
152 if (isset($filter['LAST_ID']))
153 {
154 $operator = '<';
155 if (isset($orderField) && $relationOrder[$orderField] === 'ASC')
156 {
157 $operator = '>';
158 }
159 $query->where($orderField, $operator, (int)$filter['LAST_ID']);
160 }
161
162 if (isset($filter['ACTIVE']))
163 {
164 $query->where('USER.ACTIVE', $filter['ACTIVE']);
165 }
166
167 if (isset($filter['ONLY_INTERNAL_TYPE']) && $filter['ONLY_INTERNAL_TYPE'])
168 {
169 $query->where(
170 Query::filter()
171 ->logic('or')
172 ->whereNotIn('USER.EXTERNAL_AUTH_ID', UserTable::getExternalUserTypes())
173 ->whereNull('USER.EXTERNAL_AUTH_ID')
174 );
175 }
176 }
177
178 public function offsetSet($key, $value): void
179 {
181 parent::offsetSet($key, $value);
182
183 if ($value->getUserId() !== null && $value->getChatId() !== null)
184 {
185 $this->relationsByUserId[$value->getChatId()][$value->getUserId()] = $value;
186 if ($value->getStartId() !== null)
187 {
188 static::$startIdStaticCache[$value->getChatId()][$value->getUserId()] = $value->getStartId();
189 }
190 }
191 }
192}
offsetSet($offset, $entry)
static processFilters(Query $query, array $filter, array $order)
static getStartId(int $userId, int $chatId)
static find(array $filter, array $order=[], ?int $limit=null, ?Context $context=null, array $select=self::COMMON_FIELDS)
getByUserId(int $userId, int $chatId)
hasUser(int $userId, int $chatId)