1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
General.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\Configuration;
4
5use Bitrix\Im\Call\VideoStrategyType;
6use Bitrix\Im\Model\OptionStateTable;
7use Bitrix\Im\Model\OptionUserTable;
8use Bitrix\Im\V2\Application\Features;
9use Bitrix\Main\ArgumentException;
10use Bitrix\Main\Config\Option;
11use Bitrix\Main\ObjectPropertyException;
12use Bitrix\Main\ORM\Fields\ExpressionField;
13use Bitrix\Main\ORM\Fields\Relations\Reference;
14use Bitrix\Main\ORM\Query\Join;
15use Bitrix\Main\SystemException;
16use Bitrix\Main\UserTable;
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 }
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::multiplyInsertWithoutDuplicate($rows);
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
435 }
436
438 {
440 $redefinedSettings = self::getRedefinedSettings();
441
442 return array_replace($settings, $redefinedSettings);
443 }
444
446 {
447 $result = [];
448 $defaultSettings = self::getDefaultSettings();
449
450 foreach ($defaultSettings as $name => $value)
451 {
452 $result[$name] = $settings[$name] ?? $value;
453 }
454
455 return $result;
456 }
457
458 protected static function getRedefinedSettings(): array
459 {
460 $result = [];
461
463 {
464 $result['openDesktopFromPanel'] = false;
465 }
466
467 return $result;
468 }
469
478 public static function updateGroupSettings(int $groupId, array $settings): void
479 {
480 if ($settings === [])
481 {
482 return;
483 }
484
486 $encodedSettings = self::encodeSettings($settings);
487
488 $query =
489 OptionStateTable::query()
490 ->setSelect(['NAME', 'VALUE'])
491 ->where('GROUP_ID', $groupId)
492 ->whereLike('NAME', self::ENTITY.'%')
493 ;
494
495 foreach ($query->exec() as $row)
496 {
497 if (array_key_exists($row['NAME'], $encodedSettings))
498 {
499 if ($row['VALUE'] === $encodedSettings[$row['NAME']])
500 {
501 unset($encodedSettings[$row['NAME']]);
502 continue;
503 }
504 OptionStateTable::update(
505 [
506 'GROUP_ID' => $groupId,
507 'NAME' => $row['NAME']
508 ],
509 ['VALUE' => $encodedSettings[$row['NAME']]]
510 );
511 unset($encodedSettings[$row['NAME']]);
512 }
513 }
514
515 $addedSettings = [];
516 foreach ($encodedSettings as $name => $value)
517 {
518 $addedSettings[] = [
519 'GROUP_ID' => $groupId,
520 'NAME' => $name,
521 'VALUE' => $value
522 ];
523 }
524 if ($addedSettings !== [])
525 {
526 OptionStateTable::addMulti($addedSettings, true);
527 }
528 }
529
538 public static function encodeSettings(array $settings): array
539 {
540 $encodedSettings = [];
541 foreach ($settings as $name => $value)
542 {
543 $encodeName = self::encodeName($name);
544
545 if (mb_strlen($encodeName) > 64 || mb_strlen($value) > 255)
546 {
547 continue;
548 }
549
550 if ($value === true)
551 {
552 $encodedSettings[$encodeName] = 'Y';
553 }
554 elseif ($value === false)
555 {
556 $encodedSettings[$encodeName] = 'N';
557 }
558 else
559 {
560 $encodedSettings[$encodeName] = $value;
561 }
562 }
563
564 return $encodedSettings;
565 }
566
574 public static function decodeSettings(array $rowSettings): array
575 {
576 $decodedSettings = [];
577 foreach ($rowSettings as $name => $value)
578 {
579 $decodedName = self::decodeName($name);
580 if ($value === 'Y')
581 {
582 $decodedSettings[$decodedName] = true;
583 }
584 elseif ($value === 'N')
585 {
586 $decodedSettings[$decodedName] = false;
587 }
588 else
589 {
590 $decodedSettings[$decodedName] = $value;
591 }
592
593 if ($decodedName === 'backgroundImageId')
594 {
595 $decodedSettings[$decodedName] = (int)$value;
596 }
597 }
598
599 return $decodedSettings;
600 }
601
609 private static function encodeName(string $name): string
610 {
611 return static::ENTITY . static::SEPARATOR . $name;
612 }
613
621 private static function decodeName(string $setting): string
622 {
623 return str_replace(static::ENTITY . static::SEPARATOR, '', $setting);
624 }
625
633 public static function checkingValues(array $settings): array
634 {
635 $verifiedSettings = [];
636
637 $defaultSettings = self::getDefaultSettings();
638 foreach($settings as $name => $value)
639 {
640 if (!array_key_exists($name , $defaultSettings))
641 {
642 continue;
643 }
644
645 switch ($name)
646 {
647 case 'status':
648 $verifiedSettings[$name] =
649 in_array($value, ['online', 'dnd', 'away'])
650 ? $value
651 : $defaultSettings[$name]
652 ;
653
654 break;
655 case 'panelPositionHorizontal':
656 $verifiedSettings[$name] =
657 in_array($value, ['left', 'center', 'right'])
658 ? $value
659 : $defaultSettings[$name]
660 ;
661
662 break;
663 case 'panelPositionVertical':
664 $verifiedSettings[$name] =
665 in_array($value, ['top', 'bottom'])
666 ? $value
667 : $defaultSettings[$name]
668 ;
669
670 break;
671 case 'notifyScheme':
672 $verifiedSettings[$name] =
673 in_array($value, ['simple', 'expert'])
674 ? $value
675 : $defaultSettings[$name]
676 ;
677
678 break;
679 case 'enableDarkTheme':
680 $verifiedSettings[$name] =
681 in_array($value, ['auto', 'light', 'dark'])
682 ? $value
683 : $defaultSettings[$name]
684 ;
685
686 break;
687 case 'privacyMessage':
688 case 'privacyChat':
689 case 'privacyCall':
690 case 'privacySearch':
691 $verifiedSettings[$name] =
692 in_array($value, [self::PRIVACY_RESULT_ALL, self::PRIVACY_RESULT_CONTACT], true)
693 ? $value
694 : $defaultSettings[$name]
695 ;
696
697 break;
698 case 'privacyProfile':
699 $verifiedSettings[$name] =
700 in_array(
701 $value,
702 [
703 self::PRIVACY_RESULT_ALL,
704 self::PRIVACY_RESULT_CONTACT,
705 self::PRIVACY_RESULT_NOBODY
706 ],
707 true
708 )
709 ? $value
710 : $defaultSettings[$name];
711
712 break;
713 case 'backgroundImage':
714 $verifiedSettings[$name] = $value;
715
716 break;
717 case 'notifySchemeLevel':
718 $verifiedSettings[$name] =
719 in_array($value, ['normal', 'important'])
720 ? $value
721 : $defaultSettings[$name];
722
723 break;
724 case 'trackStatus':
725 $status = explode(',', $value);
726 foreach ($status as $key => $val)
727 {
728 if ($val !== 'all')
729 {
730 $status[$key] = (int)$val;
731 if ($status[$key] === 0)
732 {
733 unset($status[$key]);
734 }
735 }
736 }
737 $verifiedSettings[$name] = implode(',', $status);
738
739 break;
740 case 'callAcceptIncomingVideo':
741 $verifiedSettings[$name] =
742 in_array($value, VideoStrategyType::getList())
743 ? $value
744 : $defaultSettings[$name]
745 ;
746
747 break;
748 case 'sendByEnter': // for legacy
749 $verifiedSettings[$name] = $value === 'Y' || $value === true;
750 break;
751 case 'enableSound': // for legacy
752 $verifiedSettings[$name] = !($value === 'N' || $value === false);
753 break;
754 case 'backgroundImageId':
755 $verifiedSettings[$name] = (int)$value > 0 ? (int)$value : 1;
756 break;
757 case 'chatAlignment':
758 $verifiedSettings[$name] =
759 in_array($value, ['left', 'center'])
760 ? $value
761 : $defaultSettings[$name]
762 ;
763 break;
764 case 'pinnedChatSort':
765 $verifiedSettings[$name] =
766 ($value === 'byDate')
767 ? $value
768 : $defaultSettings[$name]
769 ;
770 break;
771 default:
772 if (array_key_exists($name, $defaultSettings))
773 {
774 $verifiedSettings[$name] = is_bool($value) ? $value : $defaultSettings[$name];
775 }
776
777 break;
778 }
779 }
780
781 return $verifiedSettings;
782 }
783
784}
static getUserPresetFromCache(int $userId)
Определения Configuration.php:917
static createWithUserId(int $userId)
Определения General.php:44
static getNotifySettingByType(string $notifyType)
Определения General.php:278
static filterAllowedUsersBySimpleNotificationSettings(array $userList, string $notifyType)
Определения General.php:232
const PRIVACY_RESULT_ALL
Определения General.php:23
static updateGroupSettings(int $groupId, array $settings)
Определения General.php:478
const PRIVACY_RESULT_CONTACT
Определения General.php:24
getValue($settingName)
Определения General.php:63
static setSettings(int $groupId, array $settings=[], bool $forInitialize=false)
Определения General.php:149
static getDefaultSettings()
Определения General.php:95
static getGroupSettings(int $groupId)
Определения General.php:410
static $instanceList
Определения General.php:34
static getUserSettings(int $userId)
Определения General.php:181
static filterChunk(array $userList, string $settingName)
Определения General.php:270
const PRIVACY_RESULT_NOBODY
Определения General.php:25
static allowedUserBySimpleNotificationSettings(int $userId, string $notifyType)
Определения General.php:220
static encodeSettings(array $settings)
Определения General.php:538
static filterGroupSettingsByDefault(array $settings)
Определения General.php:445
static getRedefinedSettings()
Определения General.php:458
static decodeSettings(array $rowSettings)
Определения General.php:574
static prepareRawGroupSettings(array $settings)
Определения General.php:437
static filterUsersWithSimpleNotifyScheme(array $userList, string $settingName)
Определения General.php:357
static checkingValues(array $settings)
Определения General.php:633
setUserId(int $userId)
Определения General.php:68
static getUserNotifySchemas(array $userList)
Определения General.php:299
const ENTITY
Определения General.php:21
static isDesktopRedirectAvailable()
Определения Features.php:150
$userList
Определения discount_coupon_list.php:276
</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
$query
Определения get_search.php:11
$status
Определения session.php:10
IsModuleInstalled($module_id)
Определения tools.php:5301
$name
Определения menu_edit.php:35
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$instance
Определения ps_b24_final.php:14
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$val
Определения options.php:1793
$rows
Определения options.php:264