Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
department.php
1<?php
2
4
6
7class Department extends Handler
8{
9 public const DO_UPDATE_ALL_EVENTS = true;
10
15 public static function onAfterUserAdd($arFields)
16 {
17 if (!($arFields['RESULT'] ?? null) || !Loader::includeModule("iblock"))
18 {
19 return;
20 }
21
22 $departmentIBlockId = (int)\Bitrix\Main\Config\Option::get('intranet', 'iblock_structure', 0);
23 if ($departmentIBlockId <= 0)
24 {
25 return;
26 }
27
28 if(!empty($arFields['UF_DEPARTMENT']))
29 {
30 $arFieldsDepartments =
31 is_array($arFields['UF_DEPARTMENT']) ? $arFields['UF_DEPARTMENT'] : [$arFields['UF_DEPARTMENT']]
32 ;
33
34 self::$storedData = self::getAffectedDepartments($arFieldsDepartments, $departmentIBlockId);
35 }
36
37 self::sendBatchOfMessagesToQueue(self::prepareBatchOfMessagesData());
38 }
39
44 public static function onBeforeUserUpdate($arFields): void
45 {
46 if (
47 (!isset($arFields['UF_DEPARTMENT']) && !isset($arFields['ACTIVE']))
48 || empty($arFields['ID'])
49 || !Loader::includeModule("iblock")
50 )
51 {
52 return;
53 }
54
55 $departmentIBlockId = (int)\Bitrix\Main\Config\Option::get('intranet', 'iblock_structure', 0);
56 if ($departmentIBlockId <= 0)
57 {
58 return;
59 }
60
61 $user = \CUser::GetByID($arFields['ID'])->Fetch();
62
63 if (!isset($user['UF_DEPARTMENT']))
64 {
65 $oldDepartments = [];
66 }
67 elseif (is_array($user['UF_DEPARTMENT']))
68 {
69 $oldDepartments = $user['UF_DEPARTMENT'];
70 }
71 else
72 {
73 $oldDepartments = [$user['UF_DEPARTMENT']];
74 }
75
76 if (is_array($arFields['UF_DEPARTMENT']))
77 {
78 $newDepartments = $arFields['UF_DEPARTMENT'];
79 }
80 else
81 {
82 $newDepartments = [$arFields['UF_DEPARTMENT']];
83 }
84
85 if (!self::isUserDepartmentsUpdated($oldDepartments, $newDepartments))
86 {
87 return;
88 }
89
90 $departments = array_unique(
91 array_merge(
92 $newDepartments,
93 $oldDepartments,
94 )
95 );
96
97 self::$storedData = self::getAffectedDepartments($departments, $departmentIBlockId);
98 }
99
100 private static function isUserDepartmentsUpdated(array $oldValue, array $newValue): bool
101 {
102 return self::convertArrayValuesToInteger($oldValue) !== self::convertArrayValuesToInteger($newValue);
103 }
104
105 private static function convertArrayValuesToInteger(array $array): array
106 {
107 return array_map(static function($value){
108 return (int)$value;
109 }, $array);
110 }
111
116 public static function onAfterUserUpdate($arFields): void
117 {
118 if (empty(self::$storedData) || !($arFields['RESULT'] ?? null) ||!Loader::includeModule("iblock"))
119 {
120 return;
121 }
122
123 self::sendBatchOfMessagesToQueue(self::prepareBatchOfMessagesData());
124 }
125
130 public static function onBeforeUserDelete(int $userId): void
131 {
132 if ($userId <= 0 || !\Bitrix\Main\Loader::includeModule("iblock"))
133 {
134 return;
135 }
136
137 $user = \CUser::GetByID($userId)->Fetch();
138
139 $departmentIBlockId = (int)\Bitrix\Main\Config\Option::get('intranet', 'iblock_structure', 0);
140 if ($departmentIBlockId > 0 && !empty($user['UF_DEPARTMENT']))
141 {
142 self::$storedData = self::getAffectedDepartments($user['UF_DEPARTMENT'], $departmentIBlockId);
143 }
144 }
145
150 public static function onAfterUserDelete($userId): void
151 {
152 if (!Loader::includeModule("iblock") || \CUser::GetByID($userId)->Fetch())
153 {
154 return;
155 }
156
157 self::sendBatchOfMessagesToQueue(self::prepareBatchOfMessagesData());
158 }
159
164 public static function OnAfterIBlockSectionAdd($arFields): void
165 {
166 if (
167 empty($arFields['IBLOCK_ID'])
168 || empty($arFields['IBLOCK_SECTION_ID'])
169 || !Loader::includeModule("iblock")
170 || !self::isDepartmentIBlock($arFields['IBLOCK_ID'])
171 )
172 {
173 return;
174 }
175
176 self::$storedData = self::getAffectedDepartments($arFields['IBLOCK_SECTION_ID'], $arFields['IBLOCK_ID']);
177
178 self::sendBatchOfMessagesToQueue(self::prepareBatchOfMessagesData());
179 }
180
185 public static function onBeforeIBlockSectionUpdate($arFields): void
186 {
187 if (
188 empty($arFields['ID'])
189 ||empty($arFields['IBLOCK_ID'])
190 || empty($arFields['IBLOCK_SECTION_ID'])
191 || !Loader::includeModule("iblock")
192 || !self::isDepartmentIBlock($arFields['IBLOCK_ID'])
193 )
194 {
195 return;
196 }
197
198 $updatingDepartment = \CIBlockSection::GetByID($arFields['ID'])->Fetch();
199 if (($arFields['IBLOCK_SECTION_ID']) === $updatingDepartment['IBLOCK_SECTION_ID'])
200 {
201 return;
202 }
203
204 $departments = array_unique(
205 array_merge(
206 [$arFields['IBLOCK_SECTION_ID']],
207 [$updatingDepartment['IBLOCK_SECTION_ID']],
208 )
209 );
210
211 self::$storedData = self::getAffectedDepartments($departments, $arFields['IBLOCK_ID']);
212 }
213
218 public static function onAfterIBlockSectionUpdate($arFields): void
219 {
220 if (empty(self::$storedData) || !($arFields['RESULT'] ?? null) || !Loader::includeModule("iblock"))
221 {
222 return;
223 }
224
225 self::sendBatchOfMessagesToQueue(self::prepareBatchOfMessagesData());
226 }
227
233 private static function getAffectedDepartments($departments, int $departmentIBlockId): array
234 {
235 if (!is_array($departments))
236 {
237 $departments = [$departments];
238 }
239
240 $affectedDepartments = [];
241
242 foreach ($departments as $departmentId)
243 {
244 $result = \CIBlockSection::GetNavChain($departmentIBlockId, $departmentId, ['ID'], true);
245 foreach ($result as $affectedDepartment)
246 {
247 if(!in_array($affectedDepartment, $affectedDepartments))
248 {
249 $affectedDepartments[] = $affectedDepartment;
250 }
251 }
252 }
253
254 return $affectedDepartments;
255 }
256
260 private static function prepareBatchOfMessagesData(): array
261 {
262 $data = [];
263 foreach (self::$storedData as $affectedDepartment)
264 {
265 if (!empty($affectedDepartment['ID']))
266 {
267 $data[] = [
268 'entityType' => self::DEPARTMENT_TYPE,
269 'entityId' => $affectedDepartment['ID'],
270 ];
271 }
272 }
273
274 if(self::DO_UPDATE_ALL_EVENTS)
275 {
276 $data[] = [
277 'entityType' => self::ALL_USERS_TYPE,
278 ];
279 }
280
281 return $data;
282 }
283
288 private static function isDepartmentIBlock(int $IBlockId): bool
289 {
290 $departmentIBlockId = (int)\Bitrix\Main\Config\Option::get('intranet', 'iblock_structure', 0);
291
292 return $departmentIBlockId > 0 && $departmentIBlockId === $IBlockId;
293 }
294}