31 if (!empty($params[
'CURRENT_USER_ID']))
33 $currentUserId = (int)$params[
'CURRENT_USER_ID'];
35 elseif (is_object($USER))
37 $currentUserId = $USER->getId();
40 if ($currentUserId <= 0)
45 static $helper =
false;
47 $userId = (int)($params[
'USER_ID'] ?? 0);
48 $groupId = (int)($params[
'GROUP_ID'] ?? 0);
50 $notyfy = (isset($params[
'NOTIFY']) && $params[
'NOTIFY'] ===
"N" ? $params[
'NOTIFY'] :
'Y');
63 $helper = $connection->getSqlHelper();
67 'AUTO_MEMBER' => $value,
69 'GROUP_ID' => $groupId,
71 isset($params[
'ROLE'])
77 'INITIATED_BY_USER_ID' => $currentUserId,
78 '=DATE_CREATE' => $helper->getCurrentDateTimeFunction(),
79 '=DATE_UPDATE' => $helper->getCurrentDateTimeFunction(),
82 $relationId = \CSocNetUserToGroup::add($addFields);
93 self::notifyAutoMembership([
94 'GROUP_ID' => $groupId,
96 'RELATION_ID' => $relationId,
100 'group_id' => $groupId,
101 'user_id' => $userId,
102 'action' => self::CHAT_ACTION_IN,
103 'role' => $addFields[
'ROLE'],
109 static $helper =
false;
111 $relationId = (int)($params[
'RELATION_ID'] ?? 0);
112 $userId = (int)($params[
'USER_ID'] ?? 0);
113 $groupId = (int)($params[
'GROUP_ID'] ?? 0);
115 isset($params[
'VALUE'])
120 $notyfy = (isset($params[
'NOTIFY']) && $params[
'NOTIFY'] ===
'N' ? $params[
'NOTIFY'] :
'Y');
122 if ($relationId <= 0)
130 $helper = $connection->getSqlHelper();
134 'AUTO_MEMBER' => $value,
135 '=DATE_UPDATE' => $helper->getCurrentDateTimeFunction(),
138 isset($params[
'ROLE'])
142 $updateFields[
'ROLE'] = $params[
'ROLE'];
144 \CSocNetUserToGroup::update($relationId, $updateFields);
156 self::notifyAutoMembership([
157 'GROUP_ID' => $groupId,
158 'USER_ID' => $userId,
159 'RELATION_ID' => $relationId,
162 self::addInfoToChat([
163 'group_id' => $groupId,
164 'user_id' => $userId,
165 'action' => self::CHAT_ACTION_IN,
166 'role' => ($params[
'ROLE'] ??
false),
218 isset($fields[
'ACTIVE'])
219 && $fields[
'ACTIVE'] !==
'Y'
227 $deparmentIdList = [];
229 isset($fields[
'UF_DEPARTMENT'])
230 && is_array($fields[
'UF_DEPARTMENT'])
231 && (
int)$fields[
'UF_DEPARTMENT'][0] > 0
234 $deparmentIdList = $fields[
'UF_DEPARTMENT'];
237 if (Loader::includeModule(
'intranet'))
239 $deparmentIdList = array_merge($deparmentIdList, \CIntranetUtils::getSubordinateDepartments($fields[
'ID'],
false));
242 $deparmentIdList = array_unique($deparmentIdList);
245 empty($deparmentIdList)
246 || !ModuleManager::isModuleInstalled(
'intranet')
247 || !Loader::includeModule(
'iblock')
253 $groupList = self::getConnectedGroups($deparmentIdList);
254 if (empty($groupList))
259 foreach($groupList as $groupId)
261 self::addRelationAutoMembership([
262 'USER_ID' => $fields[
'ID'],
263 'GROUP_ID' => $groupId,
271 if (!self::checkUF())
277 (
int)$userFields[
'ID'] <= 0
278 || !isset($userFields[
'UF_DEPARTMENT'])
279 || !ModuleManager::isModuleInstalled(
'intranet')
280 || !Loader::includeModule(
'iblock')
287 $oldGroupAutoList = [];
290 $res = UserToGroupTable::getList([
292 'USER_ID' => (
int)$userFields[
'ID']
294 'select' => [
'GROUP_ID',
'AUTO_MEMBER' ]
297 while($relation = $res->fetch())
299 $oldGroupList[] = $relation[
'GROUP_ID'];
300 if ($relation[
'AUTO_MEMBER'] ===
'Y')
302 $oldGroupAutoList[] = $relation[
'GROUP_ID'];
305 $oldGroupList = array_unique($oldGroupList);
306 $oldGroupAutoList = array_unique($oldGroupAutoList);
309 !empty($userFields[
'UF_DEPARTMENT'])
310 && is_array($userFields[
'UF_DEPARTMENT'])
313 $userFields[
'UF_DEPARTMENT'] = array_values($userFields[
'UF_DEPARTMENT']);
317 !is_array($userFields[
'UF_DEPARTMENT'])
318 || empty($userFields[
'UF_DEPARTMENT'])
319 || (int)$userFields[
'UF_DEPARTMENT'][0] <= 0
321 : $userFields[
'UF_DEPARTMENT']
324 if (Loader::includeModule(
'intranet'))
326 $departmentList = array_merge($departmentList, \CIntranetUtils::getSubordinateDepartments($userFields[
'ID'],
false));
328 $departmentList = array_unique($departmentList);
330 if (!empty($departmentList))
332 $newGroupList = self::getConnectedGroups($departmentList);
334 $groupListPlus = array_diff($newGroupList, $oldGroupList);
335 $groupListMinus = array_diff($oldGroupAutoList, $newGroupList);
336 $groupListMinus = array_diff($groupListMinus, $groupListPlus);
338 if (!empty($groupListMinus))
340 $res = UserToGroupTable::getList([
342 '=USER_ID' => (
int)$userFields[
'ID'],
343 '@GROUP_ID' => $groupListMinus,
347 'select' => [
'ID' ],
349 while($relation = $res->fetch())
351 \CSocNetUserToGroup::delete($relation[
'ID']);
355 $changeList = $addList = $noChangeList = [];
356 if (!empty($groupListPlus))
358 $res = UserToGroupTable::getList([
360 '=USER_ID' => (
int)$userFields[
"ID"],
361 '@GROUP_ID' => $groupListPlus,
364 'select' => [
'ID',
'GROUP_ID',
'AUTO_MEMBER' ],
367 while ($relation = $res->fetch())
370 $relation[
'AUTO_MEMBER'] ===
'Y'
374 $noChangeList[] = $relation[
'GROUP_ID'];
378 $noChangeList[] = $relation[
'GROUP_ID'];
379 self::changeRelationAutoMembership([
380 'RELATION_ID' => (
int)$relation[
'ID'],
381 'USER_ID' => (
int)$userFields[
"ID"],
382 'GROUP_ID' => (
int)$relation[
'GROUP_ID'],
383 'ROLE' => $relation[
'ROLE'],
390 $groupListPlus = array_diff($groupListPlus, $noChangeList);
393 if (!empty($groupListPlus))
395 $res = UserToGroupTable::getList([
397 '=USER_ID' => (
int)$userFields[
'ID'],
398 '@GROUP_ID' => $groupListPlus,
400 'AUTO_MEMBER' =>
'N',
402 'select' => [
'ID',
'USER_ID',
'GROUP_ID' ],
405 while ($relation = $res->fetch())
407 $changeList[] = (int)$relation[
'GROUP_ID'];
408 self::changeRelationAutoMembership([
409 'RELATION_ID' => (
int)$relation[
'ID'],
410 'USER_ID' => (
int)$relation[
'USER_ID'],
411 'GROUP_ID' => (
int)$relation[
'GROUP_ID'],
416 $addList = array_diff($groupListPlus, $changeList);
419 foreach ($addList as $addGroupId)
421 self::addRelationAutoMembership([
422 'USER_ID' => (
int)$userFields[
'ID'],
423 'GROUP_ID' => $addGroupId,
432 static $structureIBlockId =
false;
433 static $departmentChainCache = [];
437 if ($structureIBlockId ===
false)
439 $structureIBlockId = (int)Option::get(
'intranet',
'iblock_structure', 0);
442 if ((
int)$structureIBlockId <= 0)
447 $userDepartmentList = [];
449 foreach($departmentList as $departmentId)
451 $departmentChain = [];
452 if (isset($departmentChainCache[$departmentId]))
454 $departmentChain = $departmentChainCache[$departmentId];
458 $res = \CIBlockSection::getNavChain($structureIBlockId, $departmentId, [
'ID' ]);
459 while ($section = $res->fetch())
461 if ((
int)$section[
'ID'] > 0)
463 $departmentChain[] = (int)$section[
'ID'];
466 $departmentChainCache[$departmentId] = $departmentChain;
468 $userDepartmentList = array_merge($userDepartmentList, $departmentChain);
471 $userDepartmentList = array_unique($userDepartmentList);
473 if (!empty($userDepartmentList))
475 $res = WorkgroupTable::getList([
477 '@UF_SG_DEPT' => $userDepartmentList,
479 'select' => [
'ID' ],
481 while ($group = $res->fetch())
483 if ((
int)$group[
'ID'] > 0)
485 $result[] = (int)$group[
'ID'];
490 return array_unique($result);
506 || !isset($params[
'group_id'], $params[
'user_id'], $params[
'action'])
507 || (
int)$params[
'group_id'] <= 0
508 || (
int)$params[
'user_id'] <= 0
509 || !Integration\Im\Chat\Workgroup::getUseChat()
510 || !Loader::includeModule(
'im')
511 || !in_array($params[
'action'], self::getChatActionList(),
true)
517 $groupId = (int)$params[
'group_id'];
518 $userId = (int)$params[
'user_id'];
519 $role = ($params[
'role'] ??
false);
522 !isset($params[
'sendMessage'])
523 || $params[
'sendMessage']
526 $chatData = Integration\Im\Chat\Workgroup::getChatData([
527 'group_id' => $groupId,
528 'skipAvailabilityCheck' =>
true,
533 || empty($chatData[$groupId])
534 || (
int)$chatData[$groupId] <= 0
540 $res = \CUser::getById($userId);
541 $user = $res->fetch();
546 isset($user[
'ACTIVE'])
547 && $user[
'ACTIVE'] ===
'N'
555 $projectSuffix = ($groupItem->isProject() ?
'_PROJECT' :
'');
557 $userName = \CUser::formatName(\CSite::getNameFormat(), $user,
true,
false);
558 switch($user[
'PERSONAL_GENDER'])
561 $genderSuffix =
'_M';
564 $genderSuffix =
'_F';
570 $chatId = $chatData[$groupId];
571 $chat = new \CIMChat(0);
573 switch ($params[
'action'])
575 case self::CHAT_ACTION_IN:
576 if ($chat->addUser($chatId, $userId,
false,
true,
true))
580 \Bitrix\Im\Chat::mute($chatId,
true, $userId);
582 $chatMessage = str_replace(
'#USER_NAME#', $userName,
Loc::getMessage(
'SOCIALNETWORK_ITEM_USERTOGROUP_CHAT_USER_ADD' . $projectSuffix . $genderSuffix));
586 $sendMessage =
false;
589 case self::CHAT_ACTION_OUT:
590 if ($chat->deleteUser($chatId, $userId,
false,
true))
592 $chatMessage = str_replace(
'#USER_NAME#', $userName,
Loc::getMessage(
'SOCIALNETWORK_ITEM_USERTOGROUP_CHAT_USER_DELETE' . $projectSuffix . $genderSuffix));
596 $sendMessage =
false;
601 $sendMessage =
false;
606 $chatMessageFields = [
607 "MESSAGE" => $chatMessage,
609 "INCREMENT_COUNTER" =>
"N",
613 $availableChatData = Integration\Im\Chat\Workgroup::getChatData([
614 'group_id' => $groupId,
618 !empty($availableChatData)
619 && !empty($availableChatData[$groupId])
620 && (
int)$availableChatData[$groupId] > 0
623 return \CIMChat::addMessage(array_merge(
624 $chatMessageFields, [
625 'TO_CHAT_ID' => $chatId,
651 || !isset($params[
'group_id'], $params[
'user_id'])
652 || (
int)$params[
'group_id'] <= 0
653 || empty($params[
'user_id'])
659 $groupId = (int)$params[
'group_id'];
661 is_array($params[
'user_id'])
663 : [ $params[
'user_id'] ]
666 isset($params[
'current_user_id'])
667 && (int)$params[
'current_user_id'] > 0
668 ? (
int)$params[
'current_user_id']
671 && $USER->isAuthorized()
682 $ownerRelationIdList = [];
683 $memberRelationIdList = [];
684 $otherRelationIdList = [];
686 $resRelation = UserToGroupTable::getList([
688 'GROUP_ID' => $groupId,
689 '@USER_ID' => $userIdList
691 'select' => [
'ID',
'USER_ID',
'ROLE' ],
694 while ($relation = $resRelation->fetch())
698 $memberRelationIdList[$relation[
'USER_ID']] = $relation[
'ID'];
702 $ownerRelationIdList[$relation[
'USER_ID']] = $relation[
'ID'];
706 $otherRelationIdList[$relation[
'USER_ID']] = $relation[
'ID'];
710 if (!empty($memberRelationIdList))
712 \CSocNetUserToGroup::transferMember2Moderator($currentUserId, $groupId, $memberRelationIdList);
715 foreach ($userIdList as $userId)
718 !array_key_exists($userId, $memberRelationIdList)
719 && !array_key_exists($userId, $ownerRelationIdList)
722 if (array_key_exists($userId, $otherRelationIdList))
724 $relationId = \CSocNetUserToGroup::update($otherRelationIdList[$userId], [
726 '=DATE_UPDATE' => \CDatabase::CurrentTimeFunction(),
731 $relationId = \CSocNetUserToGroup::add([
732 'USER_ID' => $userId,
733 'GROUP_ID' => $groupId,
735 '=DATE_CREATE' => \CDatabase::CurrentTimeFunction(),
736 '=DATE_UPDATE' => \CDatabase::CurrentTimeFunction(),
739 'INITIATED_BY_USER_ID' => $currentUserId,
746 \CSocNetUserToGroup::notifyModeratorAdded([
747 'userId' => $currentUserId,
748 'groupId' => $groupId,
749 'relationId' => $relationId