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