1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
UserConfiguration.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Settings;
4
5use Bitrix\Im\Common;
6use Bitrix\Im\Configuration\Configuration;
7use Bitrix\Im\Configuration\General;
8use Bitrix\Im\Model\OptionUserTable;
9use Bitrix\Im\Recent;
10use Bitrix\Im\V2\Result;
11use Bitrix\Im\V2\Settings\Preset\Preset;
12use Bitrix\Im\V2\Settings\Preset\PresetError;
13use Bitrix\Pull\Event;
14use CModule;
15
17{
18
21
22 private ?int $userId;
23
24 public function __construct(?int $userId = null)
25 {
26 if ($userId !== null)
27 {
28 $this->load($userId);
29 }
30 }
31
32 public function load(int $userId): Result
33 {
34 $this->userId = $userId;
35
36 $result = new Result();
37 $cache = CacheManager::getUserCache($userId);
38 $bindings = $cache->getValue();
39 if (
40 !empty($bindings[CacheManager::GENERAL_PRESET])
41 && !empty($bindings[CacheManager::NOTIFY_PRESET])
42 )
43 {
44 $this->generalPreset = Preset::getInstance($bindings[CacheManager::GENERAL_PRESET]);
45 $this->notifyPreset = Preset::getInstance($bindings[CacheManager::NOTIFY_PRESET]);
46
47 return $result->setResult(true);
48 }
49
50 $bindingsUser =
51 OptionUserTable::query()
52 ->addSelect('NOTIFY_GROUP_ID')
53 ->addSelect('GENERAL_GROUP_ID')
54 ->where('USER_ID', $userId)
55 ->setLimit(1)
56 ->fetchObject()
57 ;
58 if ($bindingsUser)
59 {
60 $bindings = [
61 CacheManager::GENERAL_PRESET => $bindingsUser->getGeneralGroupId(),
62 CacheManager::NOTIFY_PRESET => $bindingsUser->getNotifyGroupId(),
63 ];
64 }
65
66 if (!$bindings)
67 {
68 $presetId = Configuration::restoreBindings($userId);
69
70 $bindings = [
72 CacheManager::NOTIFY_PRESET => $presetId,
73 ];
74 }
75
76 $this->generalPreset = Preset::getInstance($bindings[CacheManager::GENERAL_PRESET]);
77 $this->notifyPreset = Preset::getInstance($bindings[CacheManager::NOTIFY_PRESET]);
78
79 $cache->setValue([
80 CacheManager::NOTIFY_PRESET => $this->notifyPreset->getId(),
81 CacheManager::GENERAL_PRESET => $this->generalPreset->getId(),
82 ]);
83
84 return $result->setResult(true);
85 }
86
87 public function updateGeneralSetting(array $settingsConfiguration)
88 {
89 $settingsBeforeUpdate = ($settingsConfiguration['name'] === 'pinnedChatSort')
90 ? $this->getGeneralSettings()
91 : null
92 ;
93
94 if (!$this->generalPreset->isPersonal($this->userId))
95 {
96 $personalPreset = Preset::getPersonal($this->userId);
97
98 if ($personalPreset->isExist())
99 {
100 $this->generalPreset = $personalPreset;
101 }
102 else
103 {
104 $personalPreset = Preset::getInstance();
105 $personalPreset->initPersonal($this->userId);
106 $this->generalPreset = $personalPreset;
107 }
108
109 $this->generalPreset->bindToUser($this->userId, [Preset::BIND_GENERAL]);
110 CacheManager::getUserCache($this->userId)->clearCache();
111 }
112
113 $this->generalPreset->general->updateSetting($settingsConfiguration);
114 $this->perfomSideEffect($settingsConfiguration, $settingsBeforeUpdate);
115
116 if (!$this->generalPreset->general->shouldUpdateSimpleNotifySettings($settingsConfiguration))
117 {
118 CacheManager::getPresetCache($this->generalPreset->getId())->clearCache();
119
120 return;
121 }
122 if (!$this->notifyPreset->isPersonal($this->userId))
123 {
124 $this->notifyPreset = $this->generalPreset;
125 $this->notifyPreset->bindToUser($this->userId, [Preset::BIND_NOTIFY]);
126 CacheManager::getUserCache($this->userId)->clearCache();
127 }
128
129 $simpleSchema = $this->generalPreset->general->getSimpleNotifyScheme();
130 $this->notifyPreset->notify->updateSimpleSettings($simpleSchema);
131
132 CacheManager::getPresetCache($this->notifyPreset->getId())->clearCache();
133 }
134
139 public function updateNotifySetting(array $settingsConfiguration)
140 {
141 if (!$this->notifyPreset->isPersonal($this->userId))
142 {
143 $notifyPreset = Preset::getPersonal($this->userId);
144
145 if (!$notifyPreset->isExist())
146 {
147 $notifyPreset = Preset::getInstance();
148 $notifyPreset->initPersonal($this->userId);
149 }
150 $this->notifyPreset = $notifyPreset;
151
152 $this->notifyPreset->bindToUser($this->userId, [Preset::BIND_NOTIFY]);
153 CacheManager::getUserCache($this->userId)->clearCache();
154 }
155
156 $this->notifyPreset->notify->updateSetting($settingsConfiguration);
157
158 CacheManager::getPresetCache($this->notifyPreset->getId())->clearCache();
159 }
160
161 public function updateStatus(string $status): bool
162 {
163 $this->updateGeneralSetting(['name' => 'status', 'value' => $status]);
164
165 return \CIMStatus::Set($this->userId, ['STATUS' => $status]);
166 }
167
168 public function getGeneralSettings(): array
169 {
170 if ($this->generalPreset->getId() === null)
171 {
172 $this->recoveryBinding(Preset::BIND_GENERAL);
173 }
174
175 if ($this->generalPreset->general === null)
176 {
177 $this->generalPreset = Preset::getDefaultPreset();
178 }
179
180 return $this->generalPreset->general->toRestFormat();
181 }
182
183 public function getNotifySettings(): array
184 {
185 if ($this->notifyPreset->getId() === null)
186 {
187 $this->recoveryBinding(Preset::BIND_NOTIFY);
188 }
189
190 if ($this->notifyPreset->notify === null)
191 {
192 $this->notifyPreset = Preset::getDefaultPreset();
193 }
194
195 return $this->notifyPreset->notify->toRestFormat();
196 }
197
198 protected function perfomSideEffect(array $settingConfiguration, ?array $settingsBeforeUpdate)
199 {
200 $this->updateUserSearch($settingConfiguration);
201 $this->openDesktopFromPanel($settingConfiguration);
202
203 if (isset($settingsBeforeUpdate))
204 {
205 $this->updatePinSortCost($settingConfiguration, $settingsBeforeUpdate);
206 }
207 }
208
209 private function updateUserSearch(array $settingsConfiguration): void
210 {
211 $defaultSettings = General::getDefaultSettings();
212
213 if (
214 $settingsConfiguration['name'] === Entity\General::PRIVACY_SEARCH
215 && $this->checkUserSearch($settingsConfiguration['value'])
216 )
217 {
218 $value =
219 $defaultSettings[Entity\General::PRIVACY_SEARCH] === $settingsConfiguration['value']
220 ? ''
221 : $settingsConfiguration['value']
222 ;
223
225 "USER",
226 $this->userId,
227 [
228 'UF_IM_SEARCH' => $value
229 ]
230 );
231 }
232 }
233
234 private function checkUserSearch($settingValue): bool
235 {
236 return in_array($settingValue, [General::PRIVACY_RESULT_ALL, General::PRIVACY_RESULT_CONTACT], true);
237 }
238
239 private function recoveryBinding(string $toEntity)
240 {
241 $userPreset = Preset::getPersonal($this->userId);
242
243 $bindingPreset =
244 $userPreset->isExist()
245 ? $userPreset
246 : Preset::getDefaultPreset()
247 ;
248 $bindingPreset->bindToUser($this->userId, [$toEntity]);
249
250 if ($toEntity === Preset::BIND_GENERAL)
251 {
252 $this->generalPreset = $bindingPreset;
253 }
254 else
255 {
256 $this->notifyPreset = $bindingPreset;
257 }
258
259 CacheManager::getUserCache($this->userId)->clearCache();
260 }
261
262 private function openDesktopFromPanel(array $settingsConfiguration): void
263 {
264 if (
265 $settingsConfiguration['name'] === Entity\General::OPEN_DESKTOP_FROM_PANEL
266 && CModule::IncludeModule('pull')
267 )
268 {
269 Event::add($this->userId, [
270 'module_id' => 'im',
271 'command' => 'settingsUpdate',
272 'expiry' => 5,
273 'params' => [
274 'openDesktopFromPanel' => $settingsConfiguration['value'],
275 ],
276 'extra' => Common::getPullExtra()
277 ]);
278 }
279 }
280
281 public function checkIsPersonalGeneralPreset(): bool
282 {
283 return $this->generalPreset->isPersonal($this->userId);
284 }
285
286 public function getPersonalGeneralPresetId(): ?int
287 {
288 return $this->generalPreset->getId();
289 }
290
291 private function updatePinSortCost(array $settingsConfiguration, array $settingsBeforeUpdate): void
292 {
293 if ($settingsConfiguration['name'] === 'pinnedChatSort'
294 && $settingsConfiguration['value'] !== 'byDate'
295 )
296 {
297 if ($settingsBeforeUpdate['pinnedChatSort'] !== 'byCost')
298 {
299 Recent::updatePinSortCost($this->userId);
300 }
301 }
302 }
303}
static getPullExtra()
Определения common.php:127
static getUserCache(?int $userId=null)
Определения CacheManager.php:24
static getPresetCache(?int $presetId=null)
Определения CacheManager.php:29
bindToUser(int $userId, array $bindingConfiguration)
Определения Preset.php:150
__construct(?int $userId=null)
Определения UserConfiguration.php:24
updateGeneralSetting(array $settingsConfiguration)
Определения UserConfiguration.php:87
updateNotifySetting(array $settingsConfiguration)
Определения UserConfiguration.php:139
perfomSideEffect(array $settingConfiguration, ?array $settingsBeforeUpdate)
Определения UserConfiguration.php:198
static getUserTypeManager()
Определения application.php:714
Определения result.php:20
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$status
Определения session.php:10
Определения ufield.php:9