15use Bitrix\Main\Entity\Query\Filter\ConditionTree;
44 if (self::$defaultPresetId)
49 OptionGroupTable::query()
51 ->where(
'NAME', self::DEFAULT_PRESET_NAME)
57 self::$defaultPresetId = (int)$row[
'ID'];
68 \Bitrix\Im\Model\OptionGroupTable::add([
82 if (Loader::includeModule(
'intranet'))
85 OptionAccessTable::add([
86 'GROUP_ID' => $defaultGroupId,
87 'ACCESS_CODE' => $topDepartmentId ?
'DR' . $topDepartmentId :
'AU'
91 Option::set(
'im', self::DEFAULT_PRESET_SETTING_NAME, (
int)$defaultGroupId);
93 return (
int)$defaultGroupId;
114 $preset[
'notify'][
'settings'] =
115 array_replace_recursive(
117 ($preset[
'notify'][
'settings'] ?? [])
121 $preset[
'general'][
'settings'] =
122 array_replace_recursive(
124 ($preset[
'general'][
'settings'] ?? [])
131 $query = OptionGroupTable::query()
137 'NOTIFY_GROUP_ID' =>
'OPTION_USER.NOTIFY_GROUP_ID',
138 'GENERAL_GROUP_ID' =>
'OPTION_USER.GENERAL_GROUP_ID'
140 ->registerRuntimeField(
144 OptionUserTable::class,
145 Join::on(
'this.ID',
'ref.NOTIFY_GROUP_ID')
147 ->whereColumn(
'this.ID',
'ref.GENERAL_GROUP_ID'),
148 [
'join_type' => Join::TYPE_INNER]
151 ->where(
'OPTION_USER.USER_ID', $userId)
155 $rows = $query->fetchAll();
161 if ($presetId === self::getDefaultPresetId())
170 'general' => $preset,
174 self::setUserPresetInCache($userId, $userPreset);
181 foreach ($rows as $preset)
183 if ((
int)$preset[
'ID'] === (
int)$preset[
'NOTIFY_GROUP_ID'])
186 'id' => $preset[
'ID'],
187 'name' => self::getPresetName($preset),
188 'sort' => $preset[
'SORT'],
189 'userId' => $preset[
'USER_ID'],
194 if ((
int)$preset[
'ID'] === (
int)$preset[
'GENERAL_GROUP_ID'])
197 'id' => $preset[
'ID'],
198 'name' => self::getPresetName($preset),
199 'sort' => $preset[
'SORT'],
200 'userId' => $preset[
'USER_ID'],
207 if (empty($notifyPreset))
211 if (empty($generalPreset))
217 'notify' => $notifyPreset,
218 'general' => $generalPreset
220 self::setUserPresetInCache($userId, $userPreset);
240 OptionGroupTable::query()
252 'name' => $row[
'NAME'],
253 'sort' => (int)$row[
'SORT'],
254 'userId' => $row[
'USER_ID'],
255 'settings' => $settings,
265 'name' => self::getPresetName([
'NAME' =>
'default'])
272 'name' => self::getPresetName([
'NAME' =>
'default'])
276 'notify' => $notifyPreset,
277 'general' => $generalPreset
291 OptionUserTable::query()
292 ->setSelect([
'NOTIFY_GROUP_ID',
'GENERAL_GROUP_ID'])
293 ->where(
'USER_ID', $userId)
303 'notify' => (int)$ids[
'NOTIFY_GROUP_ID'],
304 'general' => (
int)$ids[
'GENERAL_GROUP_ID']
320 OptionGroupTable::query()
321 ->setSelect([
'ID',
'NAME'])
322 ->registerRuntimeField(
326 OptionAccessTable::class,
327 Join::on(
'this.ID',
'ref.GROUP_ID'),
328 [
'join_type' => Join::TYPE_INNER]
331 ->registerRuntimeField(
335 UserAccessTable::class,
336 Join::on(
'this.OPTION_ACCESS.ACCESS_CODE',
'ref.ACCESS_CODE'),
337 [
'join_type' => Join::TYPE_INNER]
340 ->where(
'USER_ACCESS.USER_ID', $userId)
343 foreach ($query->exec() as $row)
347 'name' => self::getPresetName($row),
367 if (empty($settings))
375 $bindingPresetToUser = [];
376 if (!empty($settings[
'notify']))
378 $bindingPresetToUser[
'NOTIFY_GROUP_ID'] = $groupId;
380 if (!empty($settings[
'general']))
382 $bindingPresetToUser[
'GENERAL_GROUP_ID'] = $groupId;
385 if (!empty($bindingPresetToUser))
387 OptionUserTable::update($userId, $bindingPresetToUser);
403 $userPreset = OptionGroupTable::query()
405 ->where(
'USER_ID', $userId)
413 'USER_ID' => $userId,
414 'GENERAL_GROUP_ID' => $presetId,
415 'NOTIFY_GROUP_ID' => $presetId
418 'GENERAL_GROUP_ID' => $presetId,
419 'NOTIFY_GROUP_ID' => $presetId,
422 OptionUserTable::merge($insertFields, $updateFields);
449 array $settings = [],
450 int $sort = self::DEFAULT_SORT,
454 if ($sort >= self::USER_PRESET_SORT)
461 if (empty($settings))
470 UserAccessTable::query()
471 ->addSelect(
'USER_ID')
472 ->registerRuntimeField(
476 OptionUserTable::class,
477 Join::on(
'this.USER_ID',
'ref.USER_ID'),
478 [
'join_type' => Join::TYPE_INNER]
481 ->whereIn(
'ACCESS_CODE', $accessCodes)
484 foreach ($rowCandidates->exec() as $rowCandidate)
486 $candidates[] = $rowCandidate[
'USER_ID'];
491 OptionUserTable::updateMulti(
494 'NOTIFY_GROUP_ID' => $newGroupId,
495 'GENERAL_GROUP_ID' => $newGroupId
503 self::updateGroupForUsers($newGroupId, $candidates, $sort, self::NOTIFY_GROUP);
504 self::updateGroupForUsers($newGroupId, $candidates, $sort, self::GENERAL_GROUP);
517 OptionGroupTable::update(
521 'MODIFY_BY_ID' => $modifyId
526 OptionUserTable::query()
527 ->addSelect(
'USER_ID')
530 ->where(
'GENERAL_GROUP_ID', $presetId)
531 ->where(
'NOTIFY_GROUP_ID', $presetId)
535 foreach($query->exec() as $row)
537 $usersId[] = (int)$row[
'USER_ID'];
556 OptionUserTable::query()
557 ->addSelect(
'USER_ID')
560 ->where(
'GENERAL_GROUP_ID', $presetId)
561 ->where(
'NOTIFY_GROUP_ID', $presetId)
565 foreach($query->exec() as $row)
567 $usersId[] = (int)$row[
'USER_ID'];
572 OptionGroupTable::update(
575 'MODIFY_BY_ID' => $modifyId
587 if ($presetId === self::getDefaultPresetId())
592 self::replaceGroupForUsers($presetId, self::NOTIFY_GROUP);
593 self::replaceGroupForUsers($presetId, self::GENERAL_GROUP);
613 OptionGroupTable::query()
615 ->where(
'ID', $presetId)
619 OptionUserTable::query()
620 ->addSelect(
'USER_ID')
621 ->registerRuntimeField(
625 OptionGroupTable::class,
626 Join::on(
'this.GROUP_ID',
'ref.ID'),
627 [
'join_type' => Join::TYPE_INNER]
630 ->whereIn(
'USER_ID', $userList)
631 ->where(
'OPTION_GROUP.SORT',
'>=', (
int)$sort)
635 foreach ($query->exec() as $user)
637 $users = $user[
'USER_ID'];
642 OptionUserTable::updateMulti(
645 'NOTIFY_GROUP_ID' => $presetId,
646 'GENERAL_GROUP_ID' => $presetId
661 OptionUserTable::update(
664 'NOTIFY_GROUP_ID' => $presetId,
665 'GENERAL_GROUP_ID' => $presetId
669 CacheManager::getUserCache($userId)->clearCache();
677 private static function replaceGroupForUsers(
int $groupId,
string $groupType): void
680 OptionUserTable::query()
681 ->addSelect(
'USER_ID')
684 if ($groupType === self::NOTIFY_GROUP)
686 $rowUsers->where(
'NOTIFY_GROUP_ID', $groupId);
688 elseif ($groupType === self::GENERAL_GROUP)
690 $rowUsers->where(
'GENERAL_GROUP_ID', $groupId);
694 foreach ($rowUsers->exec() as $user)
696 $usersId[] = (int)$user[
'USER_ID'];
697 self::replaceGroupForUser((
int)$user[
'USER_ID'], $groupId, $groupType);
710 private static function replaceGroupForUser(
int $userId,
int $groupId,
string $groupType): void
713 OptionGroupTable::query()
715 ->registerRuntimeField(
719 OptionAccessTable::class,
720 Join::on(
'this.ID',
'ref.GROUP_ID'),
721 [
'join_type' => Join::TYPE_INNER]
724 ->registerRuntimeField(
728 UserAccessTable::class,
729 Join::on(
'this.OPTION_ACCESS.ACCESS_CODE',
'ref.ACCESS_CODE'),
730 [
'join_type' => Join::TYPE_INNER]
733 ->registerRuntimeField(
737 OptionUserTable::class,
738 Join::on(
'this.USER_ACCESS.USER_ID',
'ref.USER_ID'),
739 [
'join_type' => Join::TYPE_INNER]
742 ->registerRuntimeField(
746 OptionStateTable::class,
747 Join::on(
'this.ID',
'ref.GROUP_ID'),
748 [
'join_type' => Join::TYPE_INNER]
751 ->where(
'OPTION_USER.USER_ID', $userId)
752 ->where(
'ID',
'!=', $groupId)
753 ->where(Query::expr()->count(
'OPTION_STATE.NAME'),
'>', 0)
754 ->setOrder([
'SORT' =>
'DESC',
'ID' =>
'DESC'])
757 $replacedGroup = $query->fetch()[
'ID'];
759 if ($groupType === self::NOTIFY_GROUP)
761 OptionUserTable::update($userId, [
'NOTIFY_GROUP_ID' => $replacedGroup]);
763 elseif ($groupType === self::GENERAL_GROUP)
765 OptionUserTable::update($userId, [
'GENERAL_GROUP_ID' => $replacedGroup]);
779 "DELETE FROM b_im_option_state WHERE GROUP_ID = $groupId"
783 "DELETE FROM b_im_option_access WHERE GROUP_ID = $groupId"
787 "DELETE FROM b_im_option_group WHERE ID = $groupId"
802 int $sort = self::DEFAULT_SORT
806 OptionGroupTable::add([
809 'CREATE_BY_ID' => $creator
814 foreach ($accessCodes as $accessCode)
817 'GROUP_ID' => $newGroupId,
818 'ACCESS_CODE' => $accessCode
821 OptionAccessTable::addMulti($rows,
true);
837 UserAccessTable::query()
838 ->addSelect(
'ACCESS_CODE')
839 ->where(
'USER_ID', $creator)
840 ->whereLike(
'ACCESS_CODE',
'U%')
841 ->fetch()[
'ACCESS_CODE']
845 OptionGroupTable::add([
846 'USER_ID' => $creator,
847 'SORT' => self::USER_PRESET_SORT,
848 'CREATE_BY_ID' => $creator
851 OptionAccessTable::add([
852 'GROUP_ID' => $newGroupId,
853 'ACCESS_CODE' => $userAccessCode
864 private static function updateGroupForUsers(
int $groupId, array $candidates,
int $sort,
string $groupType): void
866 $join =
new ConditionTree();
868 if ($groupType === self::GENERAL_GROUP)
870 $join = Join::on(
'this.GENERAL_GROUP_ID',
'ref.ID');
872 elseif ($groupType === self::NOTIFY_GROUP)
874 $join = Join::on(
'this.NOTIFY_GROUP_ID',
'ref.ID');
878 OptionUserTable::query()
879 ->addSelect(
'USER_ID')
880 ->registerRuntimeField(
884 OptionGroupTable::class,
886 [
'join_type' => Join::TYPE_INNER]
889 ->whereIn(
'USER_ID', $candidates)
890 ->where(
'OPTION_GROUP.SORT',
'<=', $sort)
894 foreach ($query->exec() as $row)
896 $users[] = $row[
"USER_ID"];
900 if ($groupType === self::GENERAL_GROUP)
902 OptionUserTable::updateMulti($users, [
'GENERAL_GROUP_ID' => $groupId]);
904 elseif ($groupType === self::NOTIFY_GROUP)
906 OptionUserTable::updateMulti($users, [
'NOTIFY_GROUP_ID' => $groupId]);
910 private static function getPresetName($preset): string
912 switch ($preset[
'NAME'])
919 return $preset[
'NAME'];
927 $userCache = CacheManager::getUserCache($userId);
928 $currentUserPresets = $userCache->getValue();
930 if (isset($currentUserPresets[
'notifyPreset']))
932 $notifyPresetCache = CacheManager::getPresetCache($currentUserPresets[
'notifyPreset']);
933 $notifyPreset = $notifyPresetCache->getValue();
934 if (!empty($notifyPreset))
936 $result[
'notify'] = [
937 'id' => $notifyPreset[
'id'],
938 'name' => $notifyPreset[
'name'],
939 'sort' => $notifyPreset[
'sort'],
940 'settings' => $notifyPreset[
'notify']
945 if (isset($currentUserPresets[
'generalPreset']))
947 $generalPresetCache = CacheManager::getPresetCache($currentUserPresets[
'generalPreset']);
948 $generalPreset = $generalPresetCache->getValue();
949 if (!empty($generalPreset))
951 $result[
'general'] = [
952 'id' => $generalPreset[
'id'],
953 'name' => $generalPreset[
'name'],
954 'sort' => $generalPreset[
'sort'],
955 'settings' => $generalPreset[
'general']
963 private static function setUserPresetInCache(
int $userId, array $preset): void
965 CacheManager::getUserCache($userId)->clearCache();
966 CacheManager::getPresetCache($preset[
'general'][
'id'])->clearCache();
967 CacheManager::getPresetCache($preset[
'notify'][
'id'])->clearCache();
969 CacheManager::getUserCache($userId)->setValue([
970 CacheManager::GENERAL_PRESET => $preset[
'general'][
'id'],
971 CacheManager::NOTIFY_PRESET => $preset[
'notify'][
'id'],
974 if ($preset[
'general'][
'id'] === $preset[
'notify'][
'id'])
976 CacheManager::getPresetCache($preset[
'general'][
'id'])->setValue([
977 'id' => $preset[
'general'][
'id'],
978 'name' => $preset[
'general'][
'name'],
979 'sort' => $preset[
'general'][
'sort'],
980 'general' => $preset[
'general'][
'settings'],
981 'notify' => $preset[
'notify'][
'settings'],
987 CacheManager::getPresetCache($preset[
'general'][
'id'])->setValue([
988 'id' => $preset[
'general'][
'id'],
989 'name' => $preset[
'general'][
'name'],
990 'sort' => $preset[
'general'][
'sort'],
991 'general' => $preset[
'general'][
'settings'],
994 CacheManager::getPresetCache($preset[
'notify'][
'id'])->setValue([
995 'id' => $preset[
'notify'][
'id'],
996 'name' => $preset[
'notify'][
'name'],
997 'sort' => $preset[
'notify'][
'sort'],
998 'notify' => $preset[
'notify'][
'settings'],
1010 $cache = Cache::createInstance();
1011 foreach ($usersId as $userId)
1013 $cacheName = self::CACHE_NAME.
"_$userId";
1014 $cache->clean($cacheName, self::CACHE_DIR);
1026 $cache = Cache::createInstance();
1027 $cacheName = self::CACHE_NAME.
"_$userId";
1028 $cache->clean($cacheName, self::CACHE_DIR);
1038 $cache = Cache::createInstance();
1039 $cache->cleanDir(self::CACHE_DIR);
static getUserPresetFromCache(int $userId)
static getDefaultUserPreset()
static getDefaultPresetId()
static chooseExistingPreset(int $presetId, int $userId)
static setExistingPresetToUsers(int $presetId, array $userList, bool $force=false)
const DEFAULT_PRESET_SETTING_NAME
static createUserPreset(int $userId, array $settings=[])
static createSharedGroup(string $name, array $accessCodes, int $creator, int $sort=self::DEFAULT_SORT)
static updateNameSharedPreset(int $presetId, int $modifyId, string $newName)
static cleanUserCache(int $userId)
static deleteGroup(int $groupId)
static updatePresetSettings(int $presetId, int $modifyId, array $settings)
static cleanUsersCache(array $usersId)
static getPreset(int $id)
static getUserPresetIds(int $userId)
static getListAvailablePresets(int $userId)
const DEFAULT_PRESET_NAME
static createDefaultPreset()
static deletePreset(int $presetId)
static createSharedPreset(array $accessCodes, string $presetName, int $creatorId, array $settings=[], int $sort=self::DEFAULT_SORT, bool $force=false)
static getUserPreset(int $userId)
static restoreBindings(int $userId)
static createPersonalGroup(int $creator)
static getTopDepartmentId()
static updateGroupSettings(int $groupId, array $settings)
static setSettings(int $groupId, array $settings=[], bool $forInitialize=false)
static getDefaultSettings()
static getGroupSettings(int $groupId)
static updateGroupSettings(int $groupId, array $settings)
static setSettings(int $groupId, array $settings=[], bool $forInitialize=false)
static getDefaultSettings()
static getGroupSettings(int $groupId)
static getSimpleNotifySettings(array $generalSettings)
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)