Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
workgroupeventprocessor.php
1<?php
2
4
10
12{
13 private array $groupOldFields = [];
14 private array $groupNewFields = [];
15
16 public function process(): void
17 {
18 $workgroupsPushList = [];
19
20 $workgroupEventsList = [
28 ];
29
30 $addedEventTypes = [
32 ];
33 $changedEventTypes = [
36 ];
37 $deletedEventTypes = [
39 ];
40 $userChangedEventTypes = [
44 ];
45 $userAddedAndDeletedEventTypes = [
48 ];
49
50 $added = [];
51 $changed = [];
52 $deleted = [];
53 $userChanged = [];
54
55 $events = (EventCollection::getInstance())->list();
56 foreach ($events as $event)
57 {
58 /* @var Event $event */
59 $event->collectNewData();
60 $eventType = $event->getType();
61 $groupId = $event->getGroupId();
62
63 if (!in_array($eventType, $workgroupEventsList, true))
64 {
65 continue;
66 }
67
68 $this->groupOldFields[$groupId] = $event->getOldFields();
69 $this->groupNewFields[$groupId] = $event->getNewFields();
70
71 if (in_array($eventType, $addedEventTypes, true))
72 {
73 $added[] = $groupId;
74 }
75 elseif (in_array($eventType, $changedEventTypes, true))
76 {
77 $changed[] = $groupId;
78 }
79 elseif (in_array($eventType, $deletedEventTypes, true))
80 {
81 $deleted[] = $groupId;
82 }
83 elseif (in_array($eventType, $userChangedEventTypes, true))
84 {
85 $userChanged[] = $groupId;
86 }
87 }
88
89 $added = array_diff($added, $deleted);
90 $changed = array_diff($changed, $added, $deleted);
91 $changed = $this->clearNotRealChanges($changed);
92 $userChanged = array_diff($userChanged, $added, $changed, $deleted);
93
94 $notVisibleGroupsUsers = $this->getNotVisibleGroupsUsers([
95 $added,
96 $changed,
97 $deleted,
98 $userChanged
99 ]);
100
101 foreach ($events as $event)
102 {
103 /* @var Event $event */
104 $eventType = $event->getType();
105 $groupId = $event->getGroupId();
106 $userId = $event->getUserId();
107
108 if (
109 in_array($eventType, $userAddedAndDeletedEventTypes, true)
110 && in_array($groupId, $userChanged, true)
111 )
112 {
113 (new WorkgroupSender())->sendForUserAddedAndRemoved($event, $notVisibleGroupsUsers);
114 unset($userChanged[$groupId]);
115 }
116
117 if (
118 (
119 in_array($eventType, $addedEventTypes, true)
120 && !in_array($groupId, $added, true)
121 )
122 || (
123 in_array($eventType, $changedEventTypes, true)
124 && !in_array($groupId, $changed, true)
125 )
126 || (
127 in_array($eventType, $userChangedEventTypes, true)
128 && !in_array($groupId, $userChanged, true)
129 )
130 )
131 {
132 continue;
133 }
134
135 $workgroupsPushList[] = [
136 'EVENT' => $eventType,
137 'GROUP_ID' => $groupId,
138 'USER_ID' => $userId,
139 ];
140 }
141
142 if (!empty($workgroupsPushList))
143 {
144 (new WorkgroupSender())->send($workgroupsPushList, $notVisibleGroupsUsers);
145 }
146 }
147
148 private function clearNotRealChanges(array $changed): array
149 {
150 $realChanges = [
151 'NAME',
152 'PROJECT_DATE_START',
153 'PROJECT_DATE_FINISH',
154 'IMAGE_ID',
155 'AVATAR_TYPE',
156 'OPENED',
157 'CLOSED',
158 'VISIBLE',
159 'PROJECT',
160 'KEYWORDS',
161 ];
162
163 foreach ($changed as $key => $groupId)
164 {
165 $changes = $this->getChanges($groupId);
166 if (!array_intersect_key($changes, array_flip($realChanges)))
167 {
168 unset($changed[$key]);
169 }
170 }
171
172 return $changed;
173 }
174
175 private function getNotVisibleGroupsUsers(array $groupIds): array
176 {
177 $userList = [];
178
179 if (empty($groupIds = $this->getNotVisibleGroupIds($groupIds)))
180 {
181 return $userList;
182 }
183
184 $relations = UserToGroupTable::getList([
185 'select' => [ 'GROUP_ID', 'USER_ID' ],
186 'filter' => [
187 '@GROUP_ID' => $groupIds,
189 ],
190 ])->fetchCollection();
191
192 foreach ($relations as $relation)
193 {
194 $groupId = $relation->getGroupId();
195 $userId = $relation->getUserId();
196
197 if (!isset($userList[$groupId]))
198 {
199 $userList[$groupId] = [];
200 }
201
202 $userList[$groupId][] = $userId;
203 }
204
205 return $userList;
206 }
207
208 private function getNotVisibleGroupIds($groups): array
209 {
210 [ $added, $changed, $deleted, $userChanged ] = $groups;
211
212 $oldFields = $this->groupOldFields;
213 $newFields = $this->groupNewFields;
214
215 $filter = function($groupId) use ($newFields) {
216 return (($newFields[$groupId]['VISIBLE'] ?? null) === 'N');
217 };
218 $changedFilter = function($groupId) use ($oldFields, $newFields) {
219 return (
220 $newFields[$groupId]['VISIBLE'] === 'N'
221 && $oldFields[$groupId]['VISIBLE'] === 'N'
222 );
223 };
224 $deletedFilter = function($groupId) use ($oldFields) {
225 return ($oldFields[$groupId]['VISIBLE'] === 'N');
226 };
227
228 return array_merge(
229 array_filter($added, $filter),
230 array_filter($changed, $changedFilter),
231 array_filter($deleted, $deletedFilter),
232 array_filter($userChanged, $filter)
233 );
234 }
235
236 private function getChanges(int $groupId): array
237 {
238 $oldFields = $this->groupOldFields[$groupId] ?? [];
239 $newFields = $this->groupNewFields[$groupId] ?? [];
240
241 $changes = [];
242
243 foreach ($newFields as $key => $value)
244 {
245 if (mb_strpos($key, '~') === 0)
246 {
247 continue;
248 }
249
250 if (isset($oldFields[$key]) && $oldFields[$key] !== $value)
251 {
252 $changes[$key] = $value;
253 }
254 }
255
256 return $changes;
257 }
258}