2namespace Bitrix\Im\Configuration;
4use Bitrix\Iblock\ORM\Query;
5use Bitrix\Im\Model\OptionAccessTable;
6use Bitrix\Im\Model\OptionGroupTable;
7use Bitrix\Im\Model\OptionStateTable;
8use Bitrix\Im\Model\OptionUserTable;
9use Bitrix\Im\V2\Settings\CacheManager;
10use Bitrix\Main\Application;
11use Bitrix\Main\ArgumentException;
12use Bitrix\Main\Config\Option;
13use Bitrix\Main\Data\Cache;
14use Bitrix\Main\DB\SqlQueryException;
15use Bitrix\Main\Entity\Query\Filter\ConditionTree;
16use Bitrix\Main\Loader;
17use Bitrix\Main\Localization\Loc;
18use Bitrix\Main\ObjectPropertyException;
19use Bitrix\Main\ORM\Fields\Relations\Reference;
20use Bitrix\Main\ORM\Query\Join;
21use Bitrix\Main\SystemException;
22use Bitrix\Main\UserAccessTable;
44 if (self::$defaultPresetId)
46 return self::$defaultPresetId;
49 OptionGroupTable::query()
51 ->where(
'NAME', self::DEFAULT_PRESET_NAME)
57 self::$defaultPresetId = (int)$row[
'ID'];
59 return self::$defaultPresetId;
62 return self::createDefaultPreset();
82 Option::set(
'im', self::DEFAULT_PRESET_SETTING_NAME, (
int)$defaultGroupId);
87 OptionAccessTable::add([
88 'GROUP_ID' => $defaultGroupId,
89 'ACCESS_CODE' => $topDepartmentId ?
'DR' . $topDepartmentId :
'AU'
93 return (
int)$defaultGroupId;
110 $preset = self::getUserPresetFromCache(
$userId);
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)
159 $presetId = self::restoreBindings(
$userId);
161 if ($presetId === self::getDefaultPresetId())
163 $userPreset = self::getDefaultUserPreset();
167 $preset = self::getPreset($presetId);
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))
209 $notifyPreset = self::getDefaultUserPreset()[
'notify'];
211 if (empty($generalPreset))
213 $generalPreset = self::getDefaultUserPreset()[
'general'];
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'],
263 'id' => self::getDefaultPresetId(),
265 'name' => self::getPresetName([
'NAME' =>
'default'])
270 'id' => self::getDefaultPresetId(),
272 'name' => self::getPresetName([
'NAME' =>
'default'])
276 'notify' => $notifyPreset,
277 'general' => $generalPreset
291 OptionUserTable::query()
292 ->setSelect([
'NOTIFY_GROUP_ID',
'GENERAL_GROUP_ID'])
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),
365 $groupId = self::createPersonalGroup(
$userId);
375 $bindingPresetToUser = [];
378 $bindingPresetToUser[
'NOTIFY_GROUP_ID'] = $groupId;
382 $bindingPresetToUser[
'GENERAL_GROUP_ID'] = $groupId;
385 if (!empty($bindingPresetToUser))
387 OptionUserTable::update(
$userId, $bindingPresetToUser);
403 $userPreset = OptionGroupTable::query()
410 $presetId = $userPreset ? (int)$userPreset[
'ID'] : self::getDefaultPresetId();
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);
450 int $sort = self::DEFAULT_SORT,
454 if ($sort >= self::USER_PRESET_SORT)
459 $newGroupId = self::createSharedGroup($presetName, $accessCodes, $creatorId, $sort);
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);
505 self::cleanUsersCache($candidates);
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'];
540 self::cleanUsersCache($usersId);
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'];
570 self::cleanUsersCache($usersId);
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);
595 self::deleteGroup($presetId);
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]
631 ->where(
'OPTION_GROUP.SORT',
'>=', (
int)$sort)
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);
700 self::cleanUsersCache($usersId);
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 OptionGroupTable::add([
838 'USER_ID' => $creator,
839 'SORT' => self::USER_PRESET_SORT,
840 'CREATE_BY_ID' => $creator,
844 OptionAccessTable::add([
845 'GROUP_ID' => $newGroupId,
846 'ACCESS_CODE' =>
'U' . $creator,
857 private static function updateGroupForUsers(
int $groupId,
array $candidates,
int $sort,
string $groupType): void
861 if ($groupType === self::GENERAL_GROUP)
863 $join = Join::on(
'this.GENERAL_GROUP_ID',
'ref.ID');
865 elseif ($groupType === self::NOTIFY_GROUP)
867 $join = Join::on(
'this.NOTIFY_GROUP_ID',
'ref.ID');
871 OptionUserTable::query()
872 ->addSelect(
'USER_ID')
873 ->registerRuntimeField(
877 OptionGroupTable::class,
879 [
'join_type' => Join::TYPE_INNER]
882 ->whereIn(
'USER_ID', $candidates)
883 ->where(
'OPTION_GROUP.SORT',
'<=', $sort)
887 foreach (
$query->exec() as $row)
889 $users[] = $row[
"USER_ID"];
893 if ($groupType === self::GENERAL_GROUP)
895 OptionUserTable::updateMulti($users, [
'GENERAL_GROUP_ID' => $groupId]);
897 elseif ($groupType === self::NOTIFY_GROUP)
899 OptionUserTable::updateMulti($users, [
'NOTIFY_GROUP_ID' => $groupId]);
903 private static function getPresetName($preset): string
905 switch ($preset[
'NAME'])
908 return Loc::getMessage(
"IM_CONFIGURATION_PERSONAL_PRESET_NAME");
910 return Loc::getMessage(
"IM_CONFIGURATION_DEFAULT_PRESET_NAME");
912 return $preset[
'NAME'];
920 $userCache = CacheManager::getUserCache(
$userId);
921 $currentUserPresets = $userCache->getValue();
923 if (isset($currentUserPresets[
'notifyPreset']))
925 $notifyPresetCache = CacheManager::getPresetCache($currentUserPresets[
'notifyPreset']);
926 $notifyPreset = $notifyPresetCache->getValue();
927 if (!empty($notifyPreset))
930 'id' => $notifyPreset[
'id'],
931 'name' => $notifyPreset[
'name'],
932 'sort' => $notifyPreset[
'sort'],
933 'settings' => $notifyPreset[
'notify']
938 if (isset($currentUserPresets[
'generalPreset']))
940 $generalPresetCache = CacheManager::getPresetCache($currentUserPresets[
'generalPreset']);
941 $generalPreset = $generalPresetCache->getValue();
942 if (!empty($generalPreset))
945 'id' => $generalPreset[
'id'],
946 'name' => $generalPreset[
'name'],
947 'sort' => $generalPreset[
'sort'],
948 'settings' => $generalPreset[
'general']
956 private static function setUserPresetInCache(
int $userId,
array $preset): void
958 CacheManager::getUserCache(
$userId)->clearCache();
959 CacheManager::getPresetCache($preset[
'general'][
'id'])->clearCache();
960 CacheManager::getPresetCache($preset[
'notify'][
'id'])->clearCache();
962 CacheManager::getUserCache(
$userId)->setValue([
963 CacheManager::GENERAL_PRESET => $preset[
'general'][
'id'],
964 CacheManager::NOTIFY_PRESET => $preset[
'notify'][
'id'],
967 if ($preset[
'general'][
'id'] === $preset[
'notify'][
'id'])
969 CacheManager::getPresetCache($preset[
'general'][
'id'])->setValue([
970 'id' => $preset[
'general'][
'id'],
971 'name' => $preset[
'general'][
'name'],
972 'sort' => $preset[
'general'][
'sort'],
973 'general' => $preset[
'general'][
'settings'],
974 'notify' => $preset[
'notify'][
'settings'],
980 CacheManager::getPresetCache($preset[
'general'][
'id'])->setValue([
981 'id' => $preset[
'general'][
'id'],
982 'name' => $preset[
'general'][
'name'],
983 'sort' => $preset[
'general'][
'sort'],
984 'general' => $preset[
'general'][
'settings'],
987 CacheManager::getPresetCache($preset[
'notify'][
'id'])->setValue([
988 'id' => $preset[
'notify'][
'id'],
989 'name' => $preset[
'notify'][
'name'],
990 'sort' => $preset[
'notify'][
'sort'],
991 'notify' => $preset[
'notify'][
'settings'],
1003 $cache = Cache::createInstance();
1006 $cacheName = self::CACHE_NAME.
"_$userId";
1007 $cache->clean($cacheName, self::CACHE_DIR);
1019 $cache = Cache::createInstance();
1020 $cacheName = self::CACHE_NAME.
"_$userId";
1021 $cache->clean($cacheName, self::CACHE_DIR);
1031 $cache = Cache::createInstance();
1032 $cache->cleanDir(self::CACHE_DIR);
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
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 set($moduleId, $name, $value="", $siteId="")
static includeModule($moduleName)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)