24 if (isset($cache[$workgroupId]))
26 return $cache[$workgroupId];
32 'OLD_RELATIONS' => [],
36 !Loader::includeModule(
'socialnetwork')
37 || !Loader::includeModule(
'intranet')
45 $oldRelationList = [];
47 $groupItem = \Bitrix\Socialnetwork\Item\Workgroup::getById($workgroupId);
48 $groupFields = $groupItem->getFields();
51 isset($groupFields[
'UF_SG_DEPT'][
'VALUE'])
52 && !empty($groupFields[
'UF_SG_DEPT'][
'VALUE'])
55 $newDeptList = array_map(
'intval', $groupFields[
'UF_SG_DEPT'][
'VALUE']);
57 $res = \CIntranetUtils::getDepartmentEmployees($newDeptList,
true,
false,
'Y', [
'ID' ]);
58 while ($departmentMember = $res->fetch())
60 if ((
int)$departmentMember[
'ID'] !== (
int)$groupFields[
'OWNER_ID'])
62 $newUserList[] = (int)$departmentMember[
'ID'];
66 foreach ($newDeptList as $deptId)
68 $managerId = (int)\CIntranetUtils::getDepartmentManagerId($deptId);
71 $newUserList[] = $managerId;
76 $res = UserToGroupTable::getList([
78 '=GROUP_ID' => (
int) ($groupFields[
'ID'] ?? 0),
80 '=AUTO_MEMBER' =>
'Y',
82 'select' => [
'ID',
'USER_ID' ]
84 while ($relation = $res->fetch())
86 $oldUserList[] = (int)$relation[
'USER_ID'];
87 $oldRelationList[$relation[
'USER_ID']] = $relation[
'ID'];
91 $res = UserToGroupTable::getList([
93 '=GROUP_ID' => (
int)$groupFields[
'ID'],
96 'select' => [
'ID',
'USER_ID' ]
98 while ($relation = $res->fetch())
100 $membersList[] = (int)$relation[
'USER_ID'];
104 'PLUS' => array_diff($newUserList, $membersList),
105 'MINUS' => array_diff($oldUserList, $newUserList),
106 'OLD_RELATIONS' => $oldRelationList,
109 $cache[$workgroupId] = $result;
118 $workgroupsToSync = Option::get(
'socialnetwork',
'workgroupsToSync');
119 $workgroupsToSync = ($workgroupsToSync !==
'' ? @unserialize($workgroupsToSync, [
'allowed_classes' =>
false ]) : []);
122 is_array($workgroupsToSync)
123 && !empty($workgroupsToSync)
128 $nonEmptyWorkgroupList = [];
130 foreach ($workgroupsToSync as $workgroupData)
132 $workgroupId = $workgroupData[
'groupId'];
139 && is_array($data[
'PLUS'])
142 $groupCounter += count($data[
'PLUS']);
146 isset($data[
'MINUS'])
147 && is_array($data[
'MINUS'])
150 foreach ($data[
'MINUS'] as $userId)
152 if (isset($data[
'OLD_RELATIONS'][$userId]))
159 if ($groupCounter > 0)
161 $nonEmptyWorkgroupList[] = [
162 'groupId' => $workgroupId,
163 'initiatorId' => $workgroupData[
'initiatorId'],
164 'exclude' => ($workgroupData[
'exclude'] ??
false),
166 $result += $groupCounter;
170 Option::set(
'socialnetwork',
'workgroupsToSync', serialize($nonEmptyWorkgroupList));
179 Loader::includeModule(
'socialnetwork')
180 && Loader::includeModule(
'intranet')
188 $params = Option::get(
'socialnetwork',
'workgroupdeptsync');
189 $params = ($params !==
'' ? @unserialize($params, [
'allowed_classes' =>
false ]) : []);
190 $params = (is_array($params) ? $params : []);
197 'count' => $countRemain,
201 if ($countRemain > 0)
204 $result[
'progress'] = 1;
205 $result[
'steps'] =
'';
206 $result[
'count'] = $params[
'count'];
211 $workgroupsToSync = Option::get(
'socialnetwork',
'workgroupsToSync');
212 $workgroupsToSync = ($workgroupsToSync !==
'' ? @unserialize($workgroupsToSync, [
'allowed_classes' =>
false ]) : []);
215 is_array($workgroupsToSync)
216 && !empty($workgroupsToSync)
221 foreach ($workgroupsToSync as $workgroupData)
223 $workgroupId = $workgroupData[
'groupId'];
233 && is_array($data[
'PLUS'])
239 isset($data[
'MINUS'])
240 && is_array($data[
'MINUS'])
246 isset($data[
'OLD_RELATIONS'])
247 && is_array($data[
'OLD_RELATIONS'])
248 ? $data[
'OLD_RELATIONS']
252 foreach ($userListMinus as $userId)
254 if (isset($oldRelationList[$userId]))
256 if ($counter >= self::STEP_SIZE)
263 isset($workgroupData[
'exclude'])
264 && $workgroupData[
'exclude']
267 \CSocNetUserToGroup::delete($oldRelationList[$userId]);
271 UserToGroup::changeRelationAutoMembership([
272 'RELATION_ID' => $oldRelationList[$userId],
285 && !empty($userListPlus)
289 $res = UserToGroupTable::getList([
291 '=GROUP_ID' => $workgroupId,
292 '@USER_ID' => $userListPlus,
295 'select' => [
'ID',
'USER_ID' ],
297 while ($relation = $res->fetch())
299 $memberList[] = $relation[
'USER_ID'];
301 $userListPlus = array_diff($userListPlus, $memberList);
302 if (!empty($userListPlus))
304 $res = UserToGroupTable::getList([
306 '=GROUP_ID' => $workgroupId,
307 '@USER_ID' => $userListPlus,
309 '=AUTO_MEMBER' =>
'N',
311 'select' => [
'ID',
'USER_ID',
'GROUP_ID' ]
313 while ($relation = $res->fetch())
315 if ($counter >= self::STEP_SIZE)
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'],
333 $addList = array_diff($userListPlus, $changeList);
337 foreach ($addList as $addUserId)
339 if ($counter >= self::STEP_SIZE)
345 UserToGroup::addRelationAutoMembership([
346 'CURRENT_USER_ID' => $workgroupData[
'initiatorId'],
347 'USER_ID' => $addUserId,
348 'GROUP_ID' => $workgroupId,
359 \CSocNetGroup::setStat($workgroupId);
362 $params[
'number'] += $counter;
364 Option::set(
'socialnetwork',
'workgroupdeptsync', serialize($params));
369 Option::delete(
'socialnetwork', [
'name' =>
'workgroupdeptsync' ]);
372 $result[
'progress'] = (int)($params[
'number'] * 100 / $params[
'count']);
373 $result[
'steps'] = $params[
'number'];
377 Option::delete(
'socialnetwork', [
'name' =>
'workgroupdeptsync' ]);