Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UserConfiguration.php
1<?php
2
4
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 (!empty($bindings))
40 {
41 $this->generalPreset = Preset::getInstance($bindings[CacheManager::GENERAL_PRESET]);
42 $this->notifyPreset = Preset::getInstance($bindings[CacheManager::NOTIFY_PRESET]);
43
44 return $result->setResult(true);
45 }
46
47 $query =
48 OptionUserTable::query()
49 ->addSelect('NOTIFY_GROUP_ID', CacheManager::NOTIFY_PRESET)
50 ->addSelect('GENERAL_GROUP_ID', CacheManager::GENERAL_PRESET)
51 ->where('USER_ID', $userId)
52 ->setLimit(1)
53 ;
54
55 $bindings = $query->fetch();
56 if ($bindings === false)
57 {
58 $presetId = Configuration::restoreBindings($userId);
59
60 $bindings = [
62 CacheManager::NOTIFY_PRESET => $presetId,
63 ];
64 }
65
66 $this->generalPreset = Preset::getInstance($bindings[CacheManager::GENERAL_PRESET]);
67 $this->notifyPreset = Preset::getInstance($bindings[CacheManager::NOTIFY_PRESET]);
68
69 $cache->setValue([
70 CacheManager::NOTIFY_PRESET => $this->notifyPreset->getId(),
71 CacheManager::GENERAL_PRESET => $this->generalPreset->getId(),
72 ]);
73
74 return $result->setResult(true);
75 }
76
77 public function updateGeneralSetting(array $settingsConfiguration)
78 {
79 $settingsBeforeUpdate = ($settingsConfiguration['name'] === 'pinnedChatSort')
80 ? $this->getGeneralSettings()
81 : null
82 ;
83
84 if (!$this->generalPreset->isPersonal($this->userId))
85 {
86 $personalPreset = Preset::getPersonal($this->userId);
87
88 if ($personalPreset->isExist())
89 {
90 $this->generalPreset = $personalPreset;
91 }
92 else
93 {
94 $personalPreset = Preset::getInstance();
95 $personalPreset->initPersonal($this->userId);
96 $this->generalPreset = $personalPreset;
97 }
98
99 $this->generalPreset->bindToUser($this->userId, [Preset::BIND_GENERAL]);
100 CacheManager::getUserCache($this->userId)->clearCache();
101 }
102
103 $this->generalPreset->general->updateSetting($settingsConfiguration);
104 $this->perfomSideEffect($settingsConfiguration, $settingsBeforeUpdate);
105
106 if (!$this->generalPreset->general->shouldUpdateSimpleNotifySettings($settingsConfiguration))
107 {
108 CacheManager::getPresetCache($this->generalPreset->getId())->clearCache();
109
110 return;
111 }
112 if (!$this->notifyPreset->isPersonal($this->userId))
113 {
114 $this->notifyPreset = $this->generalPreset;
115 $this->notifyPreset->bindToUser($this->userId, [Preset::BIND_NOTIFY]);
116 CacheManager::getUserCache($this->userId)->clearCache();
117 }
118
119 $simpleSchema = $this->generalPreset->general->getSimpleNotifyScheme();
120 $this->notifyPreset->notify->updateSimpleSettings($simpleSchema);
121
122 CacheManager::getPresetCache($this->notifyPreset->getId())->clearCache();
123 }
124
129 public function updateNotifySetting(array $settingsConfiguration)
130 {
131 if (!$this->notifyPreset->isPersonal($this->userId))
132 {
133 $notifyPreset = Preset::getPersonal($this->userId);
134
135 if (!$notifyPreset->isExist())
136 {
137 $notifyPreset = Preset::getInstance();
138 $notifyPreset->initPersonal($this->userId);
139 }
140 $this->notifyPreset = $notifyPreset;
141
142 $this->notifyPreset->bindToUser($this->userId, [Preset::BIND_NOTIFY]);
143 CacheManager::getUserCache($this->userId)->clearCache();
144 }
145
146 $this->notifyPreset->notify->updateSetting($settingsConfiguration);
147
148 CacheManager::getPresetCache($this->notifyPreset->getId())->clearCache();
149 }
150
151 public function updateStatus(string $status): bool
152 {
153 $this->updateGeneralSetting(['name' => 'status', 'value' => $status]);
154
155 return \CIMStatus::Set($this->userId, ['STATUS' => $status]);
156 }
157
158 public function getGeneralSettings(): array
159 {
160 if ($this->generalPreset->getId() === null)
161 {
162 $this->recoveryBinding(Preset::BIND_GENERAL);
163 }
164
165 if ($this->generalPreset->general === null)
166 {
167 $this->generalPreset = Preset::getDefaultPreset();
168 }
169
170 return $this->generalPreset->general->toRestFormat();
171 }
172
173 public function getNotifySettings(): array
174 {
175 if ($this->notifyPreset->getId() === null)
176 {
177 $this->recoveryBinding(Preset::BIND_NOTIFY);
178 }
179
180 if ($this->notifyPreset->notify === null)
181 {
182 $this->notifyPreset = Preset::getDefaultPreset();
183 }
184
185 return $this->notifyPreset->notify->toRestFormat();
186 }
187
188 protected function perfomSideEffect(array $settingConfiguration, ?array $settingsBeforeUpdate)
189 {
190 $this->updateUserSearch($settingConfiguration);
191 $this->openDesktopFromPanel($settingConfiguration);
192
193 if (isset($settingsBeforeUpdate))
194 {
195 $this->updatePinSortCost($settingConfiguration, $settingsBeforeUpdate);
196 }
197 }
198
199 private function updateUserSearch(array $settingsConfiguration): void
200 {
201 $defaultSettings = General::getDefaultSettings();
202
203 if (
204 $settingsConfiguration['name'] === Entity\General::PRIVACY_SEARCH
205 && $this->checkUserSearch($settingsConfiguration['value'])
206 )
207 {
208 $value =
209 $defaultSettings[Entity\General::PRIVACY_SEARCH] === $settingsConfiguration['value']
210 ? ''
211 : $settingsConfiguration['value']
212 ;
213
214 \Bitrix\Main\Application::getUserTypeManager()->Update(
215 "USER",
216 $this->userId,
217 [
218 'UF_IM_SEARCH' => $value
219 ]
220 );
221 }
222 }
223
224 private function checkUserSearch($settingValue): bool
225 {
226 return in_array($settingValue, [General::PRIVACY_RESULT_ALL, General::PRIVACY_RESULT_CONTACT], true);
227 }
228
229 private function recoveryBinding(string $toEntity)
230 {
231 $userPreset = Preset::getPersonal($this->userId);
232
233 $bindingPreset =
234 $userPreset->isExist()
235 ? $userPreset
236 : Preset::getDefaultPreset()
237 ;
238 $bindingPreset->bindToUser($this->userId, [$toEntity]);
239
240 if ($toEntity === Preset::BIND_GENERAL)
241 {
242 $this->generalPreset = $bindingPreset;
243 }
244 else
245 {
246 $this->notifyPreset = $bindingPreset;
247 }
248
249 CacheManager::getUserCache($this->userId)->clearCache();
250 }
251
252 private function openDesktopFromPanel(array $settingsConfiguration): void
253 {
254 if (
255 $settingsConfiguration['name'] === Entity\General::OPEN_DESKTOP_FROM_PANEL
256 && CModule::IncludeModule('pull')
257 )
258 {
259 Event::add($this->userId, [
260 'module_id' => 'im',
261 'command' => 'settingsUpdate',
262 'expiry' => 5,
263 'params' => [
264 'openDesktopFromPanel' => $settingsConfiguration['value'],
265 ],
266 'extra' => Common::getPullExtra()
267 ]);
268 }
269 }
270
271 public function checkIsPersonalGeneralPreset(): bool
272 {
273 return $this->generalPreset->isPersonal($this->userId);
274 }
275
276 public function getPersonalGeneralPresetId(): ?int
277 {
278 return $this->generalPreset->getId();
279 }
280
281 private function updatePinSortCost(array $settingsConfiguration, array $settingsBeforeUpdate): void
282 {
283 if ($settingsConfiguration['name'] === 'pinnedChatSort'
284 && $settingsConfiguration['value'] !== 'byDate'
285 )
286 {
287 if ($settingsBeforeUpdate['pinnedChatSort'] !== 'byCost')
288 {
289 Recent::updatePinSortCost($this->userId);
290 }
291 }
292 }
293}
static getPullExtra()
Definition common.php:128
static updatePinSortCost(int $userId)
Definition recent.php:1285
static getUserCache(?int $userId=null)
static getPresetCache(?int $presetId=null)
bindToUser(int $userId, array $bindingConfiguration)
Definition Preset.php:150
updateGeneralSetting(array $settingsConfiguration)
updateNotifySetting(array $settingsConfiguration)
perfomSideEffect(array $settingConfiguration, ?array $settingsBeforeUpdate)