Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
General.php
1<?php
2
4
17use Exception;
18
19class General extends Base
20{
21 public const ENTITY = 'se';
22
23 public const PRIVACY_RESULT_ALL = 'all';
24 public const PRIVACY_RESULT_CONTACT = 'contact';
25 public const PRIVACY_RESULT_NOBODY = 'nobody';
26
28 protected $userId;
29
31 protected $userSettings;
32
34 protected static $instanceList = [];
35
39 protected function __construct()
40 {
41
42 }
43
44 public static function createWithUserId(int $userId): General
45 {
46 if (!isset(self::$instanceList[$userId]))
47 {
48 $instance = new static();
49 $instance->setUserId($userId);
50 $instance->fillUserSettings();
51
52 self::$instanceList[$userId] = $instance;
53 }
54
55 return self::$instanceList[$userId];
56 }
57
63 public function getValue($settingName)
64 {
65 return $this->userSettings[$settingName];
66 }
67
68 protected function setUserId(int $userId): void
69 {
70 $this->userId = $userId;
71 }
72
73 protected function fillUserSettings(): void
74 {
75 $preset = Configuration::getUserPresetFromCache($this->userId);
76 if (!empty($preset) && isset($preset['general']['settings']) && is_array($preset['general']['settings']))
77 {
78 $preset['general']['settings'] =
79 array_replace_recursive(self::getDefaultSettings(), $preset['general']['settings'])
80 ;
81
82 $this->userSettings = $preset['general']['settings'];
83 }
84 else
85 {
86 $this->userSettings = self::getUserSettings($this->userId);
87 }
88 }
89
95 public static function getDefaultSettings(): array
96 {
97 return [
98 'status' => 'online',
99 'backgroundImage' => false,
100 'bxdNotify' => true,
101 'sshNotify' => true,
102 'generalNotify' => true,
103 'trackStatus' => '',
104 'nativeNotify' => true,
105 'openDesktopFromPanel' => true,
106 'viewOffline' => Option::get("im", "view_offline"),
107 'viewGroup' => Option::get("im", "view_group"),
108 'viewLastMessage' => true,
109 'viewBirthday' => true,
110 'viewCommonUsers' => true,
111 'enableSound' => true,
112 'enableBigSmile' => true,
113 'enableDarkTheme' => 'auto',
114 'isCurrentThemeDark' => false,
115 'enableRichLink' => true,
116 'linesTabEnable' => true,
117 'linesNewGroupEnable' => false,
118 'sendByEnter' => Option::get("im", "send_by_enter"),
119 'correctText' => Option::get("im", "correct_text"),
120 'panelPositionHorizontal' => Option::get("im", "panel_position_horizontal"),
121 'panelPositionVertical' => Option::get("im", "panel_position_vertical"),
122 'loadLastMessage' => true,
123 'loadLastNotify' => Option::get("im", "load_last_notify"),
124 'notifyAutoRead' => true,
125 'notifyScheme' => 'simple',
126 'notifySchemeLevel' => 'important',
127 'notifySchemeSendSite' => true,
128 'notifySchemeSendEmail' => !IsModuleInstalled('bitrix24'),
129 'notifySchemeSendXmpp' => true,
130 'notifySchemeSendPush' => true,
131 'privacyMessage' => Option::get("im", "privacy_message"),
132 'privacyChat' => Option::get("im", "privacy_chat"),
133 'privacyCall' => Option::get("im", "privacy_call"),
134 'privacySearch' => Option::get("im", "privacy_search"),
135 'privacyProfile' => Option::get("im", "privacy_profile"),
136 'callAcceptIncomingVideo' => VideoStrategyType::ALLOW_ALL,
137 'backgroundImageId' => 1,
138 'chatAlignment' => 'left',
139 'next' => false,
140 'pinnedChatSort' => 'byCost',
141 ];
142 }
143
149 public static function setSettings(int $groupId, array $settings = [], bool $forInitialize = false): void
150 {
151 if (empty($settings) && !$forInitialize)
152 {
153 return;
154 }
155 $settings = self::checkingValues($settings);
156 $encodedSettings = self::encodeSettings($settings);
157 $defaultSettings = self::encodeSettings(self::getDefaultSettings());
158
159 $encodedSettings = array_merge($defaultSettings, $encodedSettings);
160
161 $rows = [];
162 foreach ($encodedSettings as $name => $value)
163 {
164 $rows[] = [
165 'GROUP_ID' => $groupId,
166 'NAME' => $name,
167 'VALUE' => $value
168 ];
169 }
170
171 OptionStateTable::addMulti($rows, true);
172 }
173
181 public static function getUserSettings(int $userId): array
182 {
183 $defaultSettings = self::getDefaultSettings();
184 $result =
185 OptionUserTable::query()
186 ->addSelect('GENERAL_GROUP_ID')
187 ->where('USER_ID', $userId)
188 ->fetch()
189 ;
190
191 $groupId = is_array($result) ? $result['GENERAL_GROUP_ID'] : null;
192 if (!$groupId)
193 {
194 return $defaultSettings;
195 }
196
197 $query =
198 OptionStateTable::query()
199 ->setSelect(['NAME', 'VALUE'])
200 ->where('GROUP_ID', $groupId)
201 ->whereLike('NAME', static::ENTITY.'%')
202 ;
203
204 $settings = [];
205 foreach ($query->exec() as $rowSetting)
206 {
207 $settings[$rowSetting['NAME']] = $rowSetting['VALUE'];
208 }
209
210 if(empty($settings))
211 {
212 return $defaultSettings;
213 }
214
215 $settings = static::decodeSettings($settings);
216
217 return array_replace_recursive($defaultSettings, $settings);
218 }
219
220 public static function allowedUserBySimpleNotificationSettings(int $userId, string $notifyType): bool
221 {
222 $userSettings = static::createWithUserId($userId);
223 if ($userSettings->getValue('notifyScheme') === 'simple')
224 {
225 $settingName = static::getNotifySettingByType($notifyType);
226 return (bool)$userSettings->getValue($settingName);
227 }
228
229 return true;
230 }
231
232 public static function filterAllowedUsersBySimpleNotificationSettings(array $userList, string $notifyType): array
233 {
234 if (empty($userList))
235 {
236 return $userList;
237 }
238
239 $settingName = static::getNotifySettingByType($notifyType);
240 if ($settingName === '')
241 {
242 return $userList;
243 }
244
245 $encodedSettingName = static::encodeName($settingName);
246 $encodedDefaultSettings = static::encodeSettings(static::getDefaultSettings());
247
248 if (!array_key_exists($encodedSettingName, $encodedDefaultSettings))
249 {
250 return $userList;
251 }
252
253 $filteredUsers = [];
254 if (count($userList) < 1000)
255 {
256 $filteredUsers = static::filterChunk($userList, $settingName);
257 }
258 else
259 {
260 $chunkList = array_chunk($userList, static::CHUNK_LENGTH);
261 foreach ($chunkList as $chunk)
262 {
263 $filteredUsers = array_merge($filteredUsers, static::filterChunk($chunk, $settingName));
264 }
265 }
266
267 return $filteredUsers;
268 }
269
270 protected static function filterChunk(array $userList, string $settingName): array
271 {
272 $notifySchemas = static::getUserNotifySchemas($userList);
273 $filteredUserListWithSimpleScheme = static::filterUsersWithSimpleNotifyScheme($notifySchemas['simple'], $settingName);
274
275 return array_merge($filteredUserListWithSimpleScheme, $notifySchemas['expert']);
276 }
277
278 protected static function getNotifySettingByType(string $notifyType): string
279 {
280 switch ($notifyType)
281 {
283 return 'notifySchemeSendSite';
285 return 'notifySchemeSendEmail';
287 return 'notifySchemeSendXmpp';
289 return 'notifySchemeSendPush';
290 default:
291 return '';
292 }
293 }
294
299 protected static function getUserNotifySchemas(array $userList): array
300 {
301 if (empty($userList))
302 {
303 return [];
304 }
305
306 $default = static::getDefaultSettings();
307 $notifySchemeValue = $default['notifyScheme'];
308 $encodedSettingName = static::encodeName('notifyScheme');
309
310 $query =
311 OptionUserTable::query()
312 ->addSelect('USER_ID')
313 ->addSelect(new ExpressionField('NOTIFY_SCHEMA',"COALESCE(%s, '$notifySchemeValue')", ['OPTION_STATE.VALUE']))
314 ->registerRuntimeField(
315 'USER',
316 new Reference(
317 'USER',
318 UserTable::class,
319 Join::on('this.USER_ID', 'ref.ID'),
320 ['join_type' => Join::TYPE_INNER]
321 )
322 )
323 ->registerRuntimeField(
324 'OPTION_STATE',
325 new Reference(
326 'OPTION_STATE',
327 OptionStateTable::class,
328 Join::on('this.GENERAL_GROUP_ID', 'ref.GROUP_ID')
329 ->where('ref.NAME', $encodedSettingName),
330 ['join_type' => Join::TYPE_LEFT]
331 )
332 )
333 ->whereIn('USER_ID', $userList)
334 ->where('USER.ACTIVE', 'Y')
335 ->where('USER.IS_REAL_USER', 'Y')
336 ;
337 $notifySchemas = [
338 'simple' => [],
339 'expert' => [],
340 ];
341
342 foreach ($query->exec() as $row)
343 {
344 if($row['NOTIFY_SCHEMA'] === 'simple')
345 {
346 $notifySchemas['simple'][] = (int)$row['USER_ID'];
347 }
348 elseif ($row['NOTIFY_SCHEMA'] === 'expert')
349 {
350 $notifySchemas['expert'][] = (int)$row['USER_ID'];
351 }
352 }
353
354 return $notifySchemas;
355 }
356
357 protected static function filterUsersWithSimpleNotifyScheme(array $userList, string $settingName): array
358 {
359 if (empty($userList))
360 {
361 return [];
362 }
363
364 $encodedSettingName = static::encodeName($settingName);
365 $defaultSettingValue = static::getDefaultSettings()[$settingName] ? 'Y' : 'N';
366 $query =
367 OptionUserTable::query()
368 ->addSelect('USER_ID')
369 ->registerRuntimeField(
370 'USER',
371 new Reference(
372 'USER',
373 UserTable::class,
374 Join::on('this.USER_ID', 'ref.ID'),
375 ['join_type' => Join::TYPE_INNER]
376 )
377 )
378 ->registerRuntimeField(
379 'OPTION_STATE',
380 new Reference(
381 'OPTION_STATE',
382 OptionStateTable::class,
383 Join::on('this.GENERAL_GROUP_ID', 'ref.GROUP_ID')
384 ->where('ref.NAME', $encodedSettingName),
385 ['join_type' => Join::TYPE_LEFT]
386 )
387 )
388 ->whereIn('USER_ID', $userList)
389 ->where('USER.ACTIVE', 'Y')
390 ->where('USER.IS_REAL_USER', 'Y')
391 ->whereExpr("COALESCE(%s, '$defaultSettingValue') = 'Y'", ['OPTION_STATE.VALUE'])
392 ;
393
394 $filteredUserList = [];
395 foreach ($query->exec() as $row)
396 {
397 $filteredUserList[] = (int)$row['USER_ID'];
398 }
399
400 return $filteredUserList;
401 }
402
410 public static function getGroupSettings(int $groupId): array
411 {
412 $defaultSettings = self::getDefaultSettings();
413
414 $query =
415 OptionStateTable::query()
416 ->setSelect(['NAME', 'VALUE'])
417 ->where('GROUP_ID', $groupId)
418 ->whereLike('NAME', static::ENTITY.'%')
419 ;
420
421 $settings = [];
422 foreach ($query->exec() as $rowSetting)
423 {
424 $settings[$rowSetting['NAME']] = $rowSetting['VALUE'];
425 }
426
427 if (empty($settings))
428 {
429 return $defaultSettings;
430 }
431
432 $settings = static::decodeSettings($settings);
433
434 return array_replace_recursive($defaultSettings, $settings);
435 }
436
445 public static function updateGroupSettings(int $groupId, array $settings): void
446 {
447 if ($settings === [])
448 {
449 return;
450 }
451
452 $settings = self::checkingValues($settings);
453 $encodedSettings = self::encodeSettings($settings);
454
455 $query =
456 OptionStateTable::query()
457 ->setSelect(['NAME', 'VALUE'])
458 ->where('GROUP_ID', $groupId)
459 ->whereLike('NAME', self::ENTITY.'%')
460 ;
461
462 foreach ($query->exec() as $row)
463 {
464 if (array_key_exists($row['NAME'], $encodedSettings))
465 {
466 if ($row['VALUE'] === $encodedSettings[$row['NAME']])
467 {
468 unset($encodedSettings[$row['NAME']]);
469 continue;
470 }
471 OptionStateTable::update(
472 [
473 'GROUP_ID' => $groupId,
474 'NAME' => $row['NAME']
475 ],
476 ['VALUE' => $encodedSettings[$row['NAME']]]
477 );
478 unset($encodedSettings[$row['NAME']]);
479 }
480 }
481
482 $addedSettings = [];
483 foreach ($encodedSettings as $name => $value)
484 {
485 $addedSettings[] = [
486 'GROUP_ID' => $groupId,
487 'NAME' => $name,
488 'VALUE' => $value
489 ];
490 }
491 if ($addedSettings !== [])
492 {
493 OptionStateTable::addMulti($addedSettings, true);
494 }
495 }
496
505 public static function encodeSettings(array $settings): array
506 {
507 $encodedSettings = [];
508 foreach ($settings as $name => $value)
509 {
510 $encodeName = self::encodeName($name);
511
512 if (mb_strlen($encodeName) > 64 || mb_strlen($value) > 255)
513 {
514 continue;
515 }
516
517 if ($value === true)
518 {
519 $encodedSettings[$encodeName] = 'Y';
520 }
521 elseif ($value === false)
522 {
523 $encodedSettings[$encodeName] = 'N';
524 }
525 else
526 {
527 $encodedSettings[$encodeName] = $value;
528 }
529 }
530
531 return $encodedSettings;
532 }
533
541 public static function decodeSettings(array $rowSettings): array
542 {
543 $decodedSettings = [];
544 foreach ($rowSettings as $name => $value)
545 {
546 $decodedName = self::decodeName($name);
547 if ($value === 'Y')
548 {
549 $decodedSettings[$decodedName] = true;
550 }
551 elseif ($value === 'N')
552 {
553 $decodedSettings[$decodedName] = false;
554 }
555 else
556 {
557 $decodedSettings[$decodedName] = $value;
558 }
559 }
560
561 return $decodedSettings;
562 }
563
571 private static function encodeName(string $name): string
572 {
573 return static::ENTITY . static::SEPARATOR . $name;
574 }
575
583 private static function decodeName(string $setting): string
584 {
585 return str_replace(static::ENTITY . static::SEPARATOR, '', $setting);
586 }
587
595 public static function checkingValues(array $settings): array
596 {
597 $verifiedSettings = [];
598
599 $defaultSettings = self::getDefaultSettings();
600 foreach($settings as $name => $value)
601 {
602 if (!array_key_exists($name , $defaultSettings))
603 {
604 continue;
605 }
606
607 switch ($name)
608 {
609 case 'status':
610 $verifiedSettings[$name] =
611 in_array($value, ['online', 'dnd', 'away'])
612 ? $value
613 : $defaultSettings[$name]
614 ;
615
616 break;
617 case 'panelPositionHorizontal':
618 $verifiedSettings[$name] =
619 in_array($value, ['left', 'center', 'right'])
620 ? $value
621 : $defaultSettings[$name]
622 ;
623
624 break;
625 case 'panelPositionVertical':
626 $verifiedSettings[$name] =
627 in_array($value, ['top', 'bottom'])
628 ? $value
629 : $defaultSettings[$name]
630 ;
631
632 break;
633 case 'notifyScheme':
634 $verifiedSettings[$name] =
635 in_array($value, ['simple', 'expert'])
636 ? $value
637 : $defaultSettings[$name]
638 ;
639
640 break;
641 case 'enableDarkTheme':
642 $verifiedSettings[$name] =
643 in_array($value, ['auto', 'light', 'dark'])
644 ? $value
645 : $defaultSettings[$name]
646 ;
647
648 break;
649 case 'privacyMessage':
650 case 'privacyChat':
651 case 'privacyCall':
652 case 'privacySearch':
653 $verifiedSettings[$name] =
654 in_array($value, [self::PRIVACY_RESULT_ALL, self::PRIVACY_RESULT_CONTACT], true)
655 ? $value
656 : $defaultSettings[$name]
657 ;
658
659 break;
660 case 'privacyProfile':
661 $verifiedSettings[$name] =
662 in_array(
663 $value,
664 [
665 self::PRIVACY_RESULT_ALL,
666 self::PRIVACY_RESULT_CONTACT,
667 self::PRIVACY_RESULT_NOBODY
668 ],
669 true
670 )
671 ? $value
672 : $defaultSettings[$name];
673
674 break;
675 case 'backgroundImage':
676 $verifiedSettings[$name] = $value;
677
678 break;
679 case 'notifySchemeLevel':
680 $verifiedSettings[$name] =
681 in_array($value, ['normal', 'important'])
682 ? $value
683 : $defaultSettings[$name];
684
685 break;
686 case 'trackStatus':
687 $status = explode(',', $value);
688 foreach ($status as $key => $val)
689 {
690 if ($val !== 'all')
691 {
692 $status[$key] = (int)$val;
693 if ($status[$key] === 0)
694 {
695 unset($status[$key]);
696 }
697 }
698 }
699 $verifiedSettings[$name] = implode(',', $status);
700
701 break;
702 case 'callAcceptIncomingVideo':
703 $verifiedSettings[$name] =
704 in_array($value, VideoStrategyType::getList())
705 ? $value
706 : $defaultSettings[$name]
707 ;
708
709 break;
710 case 'sendByEnter': // for legacy
711 $verifiedSettings[$name] = $value === 'Y' || $value === true;
712 break;
713 case 'enableSound': // for legacy
714 $verifiedSettings[$name] = !($value === 'N' || $value === false);
715 break;
716 case 'backgroundImageId':
717 $verifiedSettings[$name] = (int)$value > 0 ? (int)$value : 1;
718 break;
719 case 'chatAlignment':
720 $verifiedSettings[$name] =
721 in_array($value, ['left', 'center'])
722 ? $value
723 : $defaultSettings[$name]
724 ;
725 break;
726 case 'pinnedChatSort':
727 $verifiedSettings[$name] =
728 ($value === 'byDate')
729 ? $value
730 : $defaultSettings[$name]
731 ;
732 break;
733 default:
734 if (array_key_exists($name, $defaultSettings))
735 {
736 $verifiedSettings[$name] = is_bool($value) ? $value : $defaultSettings[$name];
737 }
738
739 break;
740 }
741 }
742
743 return $verifiedSettings;
744 }
745
746}
static createWithUserId(int $userId)
Definition General.php:44
static getNotifySettingByType(string $notifyType)
Definition General.php:278
static filterAllowedUsersBySimpleNotificationSettings(array $userList, string $notifyType)
Definition General.php:232
static updateGroupSettings(int $groupId, array $settings)
Definition General.php:445
static setSettings(int $groupId, array $settings=[], bool $forInitialize=false)
Definition General.php:149
static getGroupSettings(int $groupId)
Definition General.php:410
static getUserSettings(int $userId)
Definition General.php:181
static filterChunk(array $userList, string $settingName)
Definition General.php:270
static allowedUserBySimpleNotificationSettings(int $userId, string $notifyType)
Definition General.php:220
static encodeSettings(array $settings)
Definition General.php:505
static decodeSettings(array $rowSettings)
Definition General.php:541
static filterUsersWithSimpleNotifyScheme(array $userList, string $settingName)
Definition General.php:357
static checkingValues(array $settings)
Definition General.php:595
static getUserNotifySchemas(array $userList)
Definition General.php:299