Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
workgroupdeptsync.php
1<?php
2
4
11
12Loc::loadMessages(__FILE__);
13
14final class WorkgroupDeptSync extends Stepper
15{
16 public const STEP_SIZE = 5;
17
18 protected static $moduleId = 'socialnetwork';
19
20 public static function getUsers($workgroupId)
21 {
22 static $cache = [];
23
24 if (isset($cache[$workgroupId]))
25 {
26 return $cache[$workgroupId];
27 }
28
29 $result = [
30 'PLUS' => [],
31 'MINUS' => [],
32 'OLD_RELATIONS' => [],
33 ];
34
35 if (
36 !Loader::includeModule('socialnetwork')
37 || !Loader::includeModule('intranet')
38 )
39 {
40 return $result;
41 }
42
43 $newUserList = [];
44 $oldUserList = [];
45 $oldRelationList = [];
46
47 $groupItem = \Bitrix\Socialnetwork\Item\Workgroup::getById($workgroupId);
48 $groupFields = $groupItem->getFields();
49
50 if (
51 isset($groupFields['UF_SG_DEPT']['VALUE'])
52 && !empty($groupFields['UF_SG_DEPT']['VALUE'])
53 )
54 {
55 $newDeptList = array_map('intval', $groupFields['UF_SG_DEPT']['VALUE']);
56
57 $res = \CIntranetUtils::getDepartmentEmployees($newDeptList, true, false, 'Y', [ 'ID' ]);
58 while ($departmentMember = $res->fetch())
59 {
60 if ((int)$departmentMember['ID'] !== (int)$groupFields['OWNER_ID'])
61 {
62 $newUserList[] = (int)$departmentMember['ID'];
63 }
64 }
65
66 foreach ($newDeptList as $deptId)
67 {
68 $managerId = (int)\CIntranetUtils::getDepartmentManagerId($deptId);
69 if ($managerId > 0)
70 {
71 $newUserList[] = $managerId;
72 }
73 }
74 }
75
76 $res = UserToGroupTable::getList([
77 'filter' => [
78 '=GROUP_ID' => (int) ($groupFields['ID'] ?? 0),
80 '=AUTO_MEMBER' => 'Y',
81 ],
82 'select' => [ 'ID', 'USER_ID' ]
83 ]);
84 while ($relation = $res->fetch())
85 {
86 $oldUserList[] = (int)$relation['USER_ID'];
87 $oldRelationList[$relation['USER_ID']] = $relation['ID'];
88 }
89
90 $membersList = [];
91 $res = UserToGroupTable::getList([
92 'filter' => [
93 '=GROUP_ID' => (int)$groupFields['ID'],
95 ],
96 'select' => [ 'ID', 'USER_ID' ]
97 ]);
98 while ($relation = $res->fetch())
99 {
100 $membersList[] = (int)$relation['USER_ID'];
101 }
102
103 $result = [
104 'PLUS' => array_diff($newUserList, $membersList),
105 'MINUS' => array_diff($oldUserList, $newUserList),
106 'OLD_RELATIONS' => $oldRelationList,
107 ];
108
109 $cache[$workgroupId] = $result;
110
111 return $result;
112 }
113
114 protected static function getCount(): int
115 {
116 $result = 0;
117
118 $workgroupsToSync = Option::get('socialnetwork', 'workgroupsToSync');
119 $workgroupsToSync = ($workgroupsToSync !== '' ? @unserialize($workgroupsToSync, [ 'allowed_classes' => false ]) : []);
120
121 if (
122 is_array($workgroupsToSync)
123 && !empty($workgroupsToSync)
124 )
125 {
126 $workgroupsToSync = self::reduceList($workgroupsToSync);
127
128 $nonEmptyWorkgroupList = [];
129
130 foreach ($workgroupsToSync as $workgroupData)
131 {
132 $workgroupId = $workgroupData['groupId'];
133 $groupCounter = 0;
134
135 $data = self::getUsers($workgroupId);
136
137 if (
138 isset($data['PLUS'])
139 && is_array($data['PLUS'])
140 )
141 {
142 $groupCounter += count($data['PLUS']);
143 }
144
145 if (
146 isset($data['MINUS'])
147 && is_array($data['MINUS'])
148 )
149 {
150 foreach ($data['MINUS'] as $userId)
151 {
152 if (isset($data['OLD_RELATIONS'][$userId]))
153 {
154 $groupCounter++;
155 }
156 }
157 }
158
159 if ($groupCounter > 0)
160 {
161 $nonEmptyWorkgroupList[] = [
162 'groupId' => $workgroupId,
163 'initiatorId' => $workgroupData['initiatorId'],
164 'exclude' => ($workgroupData['exclude'] ?? false),
165 ];
166 $result += $groupCounter;
167 }
168 }
169
170 Option::set('socialnetwork', 'workgroupsToSync', serialize($nonEmptyWorkgroupList));
171 }
172
173 return $result;
174 }
175
176 public function execute(array &$result)
177 {
178 if (!(
179 Loader::includeModule('socialnetwork')
180 && Loader::includeModule('intranet')
181 ))
182 {
183 return false;
184 }
185
186 $return = false;
187
188 $params = Option::get('socialnetwork', 'workgroupdeptsync');
189 $params = ($params !== '' ? @unserialize($params, [ 'allowed_classes' => false ]) : []);
190 $params = (is_array($params) ? $params : []);
191
192 $countRemain = self::getCount();
193 if (empty($params))
194 {
195 $params = [
196 'number' => 0,
197 'count' => $countRemain,
198 ];
199 }
200
201 if ($countRemain > 0)
202 {
203 $result['title'] = Loc::getMessage('FUPD_WORKGROUP_DEPT_SYNC_TITLE');
204 $result['progress'] = 1;
205 $result['steps'] = '';
206 $result['count'] = $params['count'];
207
208 $counter = 0;
209 $breakFlag = false;
210
211 $workgroupsToSync = Option::get('socialnetwork', 'workgroupsToSync');
212 $workgroupsToSync = ($workgroupsToSync !== '' ? @unserialize($workgroupsToSync, [ 'allowed_classes' => false ]) : []);
213
214 if (
215 is_array($workgroupsToSync)
216 && !empty($workgroupsToSync)
217 )
218 {
219 $workgroupsToSync = self::reduceList($workgroupsToSync);
220
221 foreach ($workgroupsToSync as $workgroupData)
222 {
223 $workgroupId = $workgroupData['groupId'];
224 if ($breakFlag)
225 {
226 break;
227 }
228
229 $data = self::getUsers($workgroupId);
230
231 $userListPlus = (
232 isset($data['PLUS'])
233 && is_array($data['PLUS'])
234 ? $data['PLUS']
235 : []
236 );
237
238 $userListMinus = (
239 isset($data['MINUS'])
240 && is_array($data['MINUS'])
241 ? $data['MINUS']
242 : []
243 );
244
245 $oldRelationList = (
246 isset($data['OLD_RELATIONS'])
247 && is_array($data['OLD_RELATIONS'])
248 ? $data['OLD_RELATIONS']
249 : []
250 );
251
252 foreach ($userListMinus as $userId)
253 {
254 if (isset($oldRelationList[$userId]))
255 {
256 if ($counter >= self::STEP_SIZE)
257 {
258 $breakFlag = true;
259 break;
260 }
261
262 if (
263 isset($workgroupData['exclude'])
264 && $workgroupData['exclude']
265 )
266 {
267 \CSocNetUserToGroup::delete($oldRelationList[$userId]);
268 }
269 else
270 {
271 UserToGroup::changeRelationAutoMembership([
272 'RELATION_ID' => $oldRelationList[$userId],
273 'VALUE' => 'N',
274 ]);
275 }
276
277 $counter++;
278 }
279 }
280
281 $changeList = [];
282
283 if (
284 !$breakFlag
285 && !empty($userListPlus)
286 )
287 {
288 $memberList = [];
289 $res = UserToGroupTable::getList([
290 'filter' => [
291 '=GROUP_ID' => $workgroupId,
292 '@USER_ID' => $userListPlus,
294 ],
295 'select' => [ 'ID', 'USER_ID' ],
296 ]);
297 while ($relation = $res->fetch())
298 {
299 $memberList[] = $relation['USER_ID'];
300 }
301 $userListPlus = array_diff($userListPlus, $memberList);
302 if (!empty($userListPlus))
303 {
304 $res = UserToGroupTable::getList([
305 'filter' => [
306 '=GROUP_ID' => $workgroupId,
307 '@USER_ID' => $userListPlus,
309 '=AUTO_MEMBER' => 'N',
310 ],
311 'select' => [ 'ID', 'USER_ID', 'GROUP_ID' ]
312 ]);
313 while ($relation = $res->fetch())
314 {
315 if ($counter >= self::STEP_SIZE)
316 {
317 $breakFlag = true;
318 break;
319 }
320
321 $changeList[] = (int)$relation['USER_ID'];
322 UserToGroup::changeRelationAutoMembership([
323 'RELATION_ID' => (int)$relation['ID'],
324 'USER_ID' => (int)$relation['USER_ID'],
325 'GROUP_ID' => (int)$relation['GROUP_ID'],
327 'VALUE' => 'Y',
328 ]);
329
330 $counter++;
331 }
332
333 $addList = array_diff($userListPlus, $changeList);
334
335 if (!$breakFlag)
336 {
337 foreach ($addList as $addUserId)
338 {
339 if ($counter >= self::STEP_SIZE)
340 {
341 $breakFlag = true;
342 break;
343 }
344
345 UserToGroup::addRelationAutoMembership([
346 'CURRENT_USER_ID' => $workgroupData['initiatorId'],
347 'USER_ID' => $addUserId,
348 'GROUP_ID' => $workgroupId,
350 'VALUE' => 'Y',
351 ]);
352
353 $counter++;
354 }
355 }
356 }
357 }
358
359 \CSocNetGroup::setStat($workgroupId);
360 }
361
362 $params['number'] += $counter;
363
364 Option::set('socialnetwork', 'workgroupdeptsync', serialize($params));
365 $return = true;
366 }
367 else
368 {
369 Option::delete('socialnetwork', [ 'name' => 'workgroupdeptsync' ]);
370 }
371
372 $result['progress'] = (int)($params['number'] * 100 / $params['count']);
373 $result['steps'] = $params['number'];
374 }
375 else
376 {
377 Option::delete('socialnetwork', [ 'name' => 'workgroupdeptsync' ]);
378 }
379
380 return $return;
381 }
382
383 protected static function reduceList(array $workgroupsToSync = []): array
384 {
385 $result = [];
386
387 foreach ($workgroupsToSync as $workgroupData)
388 {
389 $workgroupId = (int)$workgroupData['groupId'];
390 $result[$workgroupId] = $workgroupData;
391 }
392
393 return array_values($result);
394 }
395}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static reduceList(array $workgroupsToSync=[])