Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
workgroupuserevent.php
1<?php
2
4
11
19{
24 protected function prepareData(array $data = []): array
25 {
26 $this->collectOldData();
27
28 $validFields = [
29 'GROUP_ID',
30 'USER_ID',
31 'ROLE',
32 ];
33
34 foreach ($data as $key => $row)
35 {
36 if (!in_array($key, $validFields, true))
37 {
38 unset($data[$key]);
39 }
40 }
41
42 return $data;
43 }
44
45 public function process(): void
46 {
47 $this->collectNewData();
48 $events = (EventCollection::getInstance())->list();
49
50 $workgroupsPushList = [];
51
52 $workgroupUserEventsList = [
56 ];
57
58 $added = [];
59 $changed = [];
60 $deleted = [];
61
62 foreach ($events as $event)
63 {
64 /* @var $event Event */
65 $eventType = $event->getType();
66 $relationKey = $event->getRelationKey();
67
68 if (in_array($eventType, $workgroupUserEventsList, true))
69 {
71 {
72 $added[] = $relationKey;
73 }
75 {
76 $changed[] = $relationKey;
77 }
79 {
80 $deleted[] = $relationKey;
81 }
82 }
83 }
84
85 $added = array_diff($added, $deleted);
86 $changed = array_diff($changed, $added, $deleted);
87 $changed = $this->clearNotRealChanges($changed);
88
89 $newFields = $this->getNewFields();
90
91 foreach ($events as $event)
92 {
93 /* @var $event Event */
94 $eventType = $event->getType();
95 $groupId = $event->getGroupId();
96 $userId = $event->getGroupId();
97 $relationKey = $event->getRelationKey();
98
99 $role = ($newFields[$relationKey] ?? null);
100 if (
101 !$role
102 || (
104 && !in_array($relationKey, $added, true)
105 )
106 || (
108 && !in_array($relationKey, $changed, true)
109 )
110 )
111 {
112 continue;
113 }
114
115 $workgroupUserPushList[] = [
116 'EVENT' => $eventType,
117 'GROUP_ID' => $groupId,
118 'USER_ID' => $userId,
119 'ROLE' => $role,
120 ];
121 }
122
123 if (!empty($workgroupUserPushList))
124 {
125 $notVisibleGroupsUsers = $this->getNotVisibleGroupsUsers([
126 $added,
127 $changed,
128 $deleted,
129 ]);
130
131 (new WorkgroupUserSender())->send($workgroupsPushList, $notVisibleGroupsUsers);
132 }
133 }
134
135 protected function collectOldData(): void
136 {
137 $relationKey = $this->getRelationKey();
138 $oldFields = $this->getOldFields();
139
140 if (
141 $relationKey !== ''
142 && !isset($oldFields[$relationKey])
143 )
144 {
145 $oldFields[$relationKey] = UserToGroupTable::getList([
146 'filter' => [
147 'USER_ID' => $this->getUserId(),
148 'GROUP_ID' => $this->getGroupId(),
149 ],
150 'select' => [
151 'ROLE',
152 'GROUP_VISIBLE' => 'GROUP.VISIBLE',
153 ],
154 ])->fetch();
155 }
156 }
157
158 protected function collectNewData(): void
159 {
160 $events = (EventCollection::getInstance())->list();
161 $newFields = $this->getNewFields();
162
163 foreach ($events as $event)
164 {
165 $relationKey = $event->getRelationKey();
166
167 if (
168 $relationKey !== ''
169 && !isset($newFields[$relationKey])
170 )
171 {
172 $newFields[$relationKey] = UserToGroupTable::getList([
173 'filter' => [
174 'USER_ID' => $event->getUserId(),
175 'GROUP_ID' => $event->getGroupId(),
176 ],
177 'select' => [
178 'ROLE',
179 'GROUP_VISIBLE' => 'GROUP.VISIBLE',
180 ],
181 ])->fetch();
182 }
183 }
184
185 $this->setNewFields($newFields);
186 }
187
188 private function getNotVisibleGroupsUsers(array $relationKeyList): array
189 {
190 $userList = [];
191
192 if (empty($groupIds = $this->getNotVisibleGroupIds($relationKeyList)))
193 {
194 return $userList;
195 }
196
197 $relations = UserToGroupTable::getList([
198 'select' => [ 'GROUP_ID', 'USER_ID' ],
199 'filter' => [
200 '@GROUP_ID' => $groupIds,
202 ],
203 ])->fetchCollection();
204
205 foreach ($relations as $relation)
206 {
207 $groupId = $relation->getGroupId();
208 $userId = $relation->getUserId();
209
210 if (!isset($userList[$groupId]))
211 {
212 $userList[$groupId] = [];
213 }
214
215 $userList[$groupId][] = $userId;
216 }
217
218 return $userList;
219 }
220
221 private function getNotVisibleGroupIds($relationKeyList): array
222 {
223 [ $added, $changed, $deleted ] = $relationKeyList;
224
225 $oldFields = $this->getOldFields();
226 $newFields = $this->getNewFields();
227
228 $filter = static function($relationKey) use ($newFields) {
229 return ($newFields[$relationKey]['GROUP_VISIBLE'] === 'N');
230 };
231 $changedFilter = static function($relationKey) use ($oldFields, $newFields) {
232 return (
233 $newFields[$relationKey]['GROUP_VISIBLE'] === 'N'
234 && $oldFields[$relationKey]['GROUP_VISIBLE'] === 'N'
235 );
236 };
237 $deletedFilter = static function($relationKey) use ($oldFields) {
238 return ($oldFields[$relationKey]['GROUP_VISIBLE'] === 'N');
239 };
240
241 $relationKeyList = array_merge(
242 array_filter($added, $filter),
243 array_filter($changed, $changedFilter),
244 array_filter($deleted, $deletedFilter),
245 );
246
247 return array_map(static function($relationKey) {
248 [ $groupId, ] = explode('_', $relationKey);
249 return $groupId;
250 }, $relationKeyList);
251 }
252
253 private function clearNotRealChanges(array $changed): array
254 {
255 $realChanges = [
256 'ROLE',
257 ];
258
259 foreach ($changed as $key => $relationKey)
260 {
261 $changes = $this->getChanges($relationKey);
262 if (!array_intersect_key($changes, array_flip($realChanges)))
263 {
264 unset($changed[$key]);
265 }
266 }
267
268 return $changed;
269 }
270
271 protected function setOldFields($oldFields): void
272 {
273 WorkgroupUserService::getInstance()->setOldFields($oldFields);
274 }
275
276 protected function getOldFields(): array
277 {
278 return WorkgroupUserService::getInstance()->getOldFields();
279 }
280
281 protected function setNewFields($newFields): void
282 {
283 WorkgroupUserService::getInstance()->setNewFields($newFields);
284 }
285
286 protected function getNewFields(): array
287 {
288 return WorkgroupUserService::getInstance()->getNewFields();
289 }
290}
send($sender=null)
Definition event.php:139