Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Manager.php
1<?php
2
4
13use CModule;
14use COption;
15
17{
18 private const GENERAL = 'general';
19 private const NOTIFY = 'notify';
20
21 private const PRIVACY_SEARCH = 'privacySearch';
22 private const STATUS = 'status';
23
24 public static function getUserSettings(int $userId): Result
25 {
26 $result = new Result();
27 try
28 {
29 $preset = Configuration::getUserPreset($userId);
30 }
32 {
33 $result->addError(new Error($exception->getMessage(), $exception->getCode()));
34 return $result;
35 }
36
37 $result->setData($preset);
38 return $result;
39 }
40
49 public static function setUserSettings(int $userId, array $settings): Result
50 {
51 $result = new Result();
52 if (
53 !array_key_exists(self::NOTIFY, $settings)
54 || !array_key_exists(self::GENERAL, $settings)
55 )
56 {
57 $result->addError(new Error('Incorrect data when receiving chat settings', 400));
58 return $result;
59 }
60
61 self::updateUserStatus($userId, $settings['general']);
62
63 self::sendSettingsChangeEvent($userId, $settings['general']);
64
65 self::disableUserSearch($userId, $settings['general']);
66
67 if (isset($settings['general']['notifyScheme']) && $settings['general']['notifyScheme'] === 'simple')
68 {
69 $settings['notify'] = Notification::getSimpleNotifySettings($settings['general']);
70 }
71
72 $userPresetId =
73 \Bitrix\Im\Model\OptionGroupTable::query()
74 ->addSelect('ID')
75 ->where('USER_ID', $userId)
76 ->fetch()
77 ;
78
79 if (!$userPresetId)
80 {
81 Configuration::createUserPreset($userId, $settings);
82
83 CacheManager::getUserCache($userId)->clearCache();
84 self::enableUserSearch($userId, $settings['general']);
85
86 return $result;
87 }
88
89 $userPresetId = $userPresetId['ID'];
90 Configuration::updatePresetSettings($userPresetId, $userId, $settings);
91 Configuration::chooseExistingPreset($userPresetId, $userId);
92
93 CacheManager::getPresetCache($userPresetId)->clearCache();
94
95 self::enableUserSearch($userId, $settings['general']);
96
97 return $result;
98 }
99
100 public static function setUserSetting(int $userId, string $type, array $settings): Result
101 {
102 $result = new Result();
103 if (!in_array($type, [self::NOTIFY, self::GENERAL], true))
104 {
105 $result->addError(new Error('Incorrect data when receiving chat settings', 400));
106 return $result;
107 }
108
109 $userPresetId =
110 \Bitrix\Im\Model\OptionGroupTable::query()
111 ->addSelect('ID')
112 ->where('USER_ID', $userId)
113 ->fetch()
114 ;
115 $userPresetId = $userPresetId['ID'] ?? null;
116
117 if ($type === self::NOTIFY)
118 {
119 if (!$userPresetId)
120 {
121 $preset['notify'] = $settings;
122 $preset['general'] = [];
123 Configuration::createUserPreset($userId, $preset);
124
125 return $result;
126 }
127 Notification::updateGroupSettings($userPresetId, $settings);
128 }
129
130 if ($type === self::GENERAL)
131 {
132 self::updateUserStatus($userId, $settings);
133
134 self::sendSettingsChangeEvent($userId, $settings);
135
136 self::disableUserSearch($userId, $settings);
137
138 if (!$userPresetId)
139 {
140 $preset['general'] = array_replace_recursive(General::getDefaultSettings(), $settings);
141 $preset['notify'] = [];
142 Configuration::createUserPreset($userId, $preset);
143
144 return $result;
145 }
146 General::updateGroupSettings($userPresetId, $settings);
147
148 self::enableUserSearch($userId, $settings);
149 }
150
151 CacheManager::getPresetCache($userPresetId)->clearCache();
152 CacheManager::getUserCache($userId)->clearCache();
153
154 return $result;
155 }
156
157 public static function isSettingsMigrated(): bool
158 {
159 return
160 COption::GetOptionString('im', 'migration_to_new_settings') === 'Y'
161 || COption::GetOptionString('im', \Bitrix\Im\Configuration\Configuration::DEFAULT_PRESET_SETTING_NAME, null) !== null
162 ;
163 }
164
165 public static function isUserMigrated(int $userId): bool
166 {
167 $lastConvertedId = COption::GetOptionInt('im', 'last_converted_user');
168 return $userId < $lastConvertedId;
169 }
170
171 public static function getNotifyAccess($userId, $moduleId, $eventId, $type)
172 {
173 $generalSettings = General::createWithUserId($userId);
174 $notifyScheme = $generalSettings->getValue('notifyScheme');
175
176 if ($notifyScheme !== 'expert')
177 {
178 if ($type === Notification::SITE)
179 {
180 return $generalSettings->getValue('notifySchemeSendSite');
181 }
182 if ($type === Notification::MAIL)
183 {
184 return $generalSettings->getValue('notifySchemeSendEmail');
185 }
186 if ($type === Notification::PUSH)
187 {
188 return $generalSettings->getValue('notifySchemeSendPush');
189 }
190 if ($type === Notification::XMPP)
191 {
192 return $generalSettings->getValue('notifySchemeSendXmpp');
193 }
194 }
195
196 return (new Notification($moduleId, $eventId))->isAllowed($userId, $type);
197 }
198
199 private static function sendSettingsChangeEvent(int $userId, array $generalSettings): void
200 {
201 // TODO: refactoring required for the new interface
202 if (isset($generalSettings['openDesktopFromPanel']) && CModule::IncludeModule('pull'))
203 {
204 Event::add($userId, [
205 'module_id' => 'im',
206 'command' => 'settingsUpdate',
207 'expiry' => 5,
208 'params' => [
209 'openDesktopFromPanel' => $generalSettings['openDesktopFromPanel'],
210 ],
211 'extra' => Common::getPullExtra()
212 ]);
213 }
214 }
215
216 private static function disableUserSearch(int $userId, array $generalSettings): void
217 {
218 $defaultSettings = General::getDefaultSettings();
219 if (
220 array_key_exists(self::PRIVACY_SEARCH, $generalSettings)
221 && $defaultSettings[self::PRIVACY_SEARCH] === $generalSettings[self::PRIVACY_SEARCH]
222 )
223 {
224 global $USER_FIELD_MANAGER;
225 $USER_FIELD_MANAGER->Update("USER", $userId, ['UF_IM_SEARCH' => '']);
226 }
227 }
228
229 private static function enableUserSearch(int $userId, array $generalSettings): void
230 {
231 if (isset($generalSettings[self::PRIVACY_SEARCH]))
232 {
233 global $USER_FIELD_MANAGER;
234 $USER_FIELD_MANAGER->Update(
235 "USER",
236 $userId,
237 [
238 'UF_IM_SEARCH' => $generalSettings[self::PRIVACY_SEARCH],
239 ]
240 );
241 }
242 }
243
244 private static function updateUserStatus(int $userId, array $generalSettings): void
245 {
246 if (isset($generalSettings[self::STATUS]))
247 {
248 \CIMStatus::Set($userId, ['STATUS' => $generalSettings[self::STATUS]]);
249 }
250 }
251
252}
static getPullExtra()
Definition common.php:128
static chooseExistingPreset(int $presetId, int $userId)
static createUserPreset(int $userId, array $settings=[])
static updatePresetSettings(int $presetId, int $modifyId, array $settings)
static createWithUserId(int $userId)
Definition General.php:44
static updateGroupSettings(int $groupId, array $settings)
Definition General.php:445
static getUserSettings(int $userId)
Definition Manager.php:24
static isUserMigrated(int $userId)
Definition Manager.php:165
static getNotifyAccess($userId, $moduleId, $eventId, $type)
Definition Manager.php:171
static setUserSetting(int $userId, string $type, array $settings)
Definition Manager.php:100
static setUserSettings(int $userId, array $settings)
Definition Manager.php:49
static updateGroupSettings(int $groupId, array $settings)
static getSimpleNotifySettings(array $generalSettings)