Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chatconfigconnectionsrestorer.php
1<?php
2
3namespace Bitrix\Im\Update;
4
18use phpDocumentor\Reflection\Types\This;
19
21{
22 protected static $moduleId = 'im';
23 private const OPTION_NAME = 'chat_config_restore';
24
25 function execute(array &$option)
26 {
27 if (!Loader::includeModule(self::$moduleId))
28 {
29 return false;
30 }
31 $haveEventHandlersRestored = Option::get(self::$moduleId, 'have_event_handlers_restored', 'N');
32
33 if ($haveEventHandlersRestored !== 'Y')
34 {
35 $this->restoreEventHandlers();
36 }
37
38 $params = Option::get(self::$moduleId, self::OPTION_NAME, '');
39 $params = $params !== '' ? @unserialize($params, ['allowed_classes' => false]) : [];
40 $params = is_array($params) ? $params : [];
41
42 if (empty($params))
43 {
44 $params = [
45 'default_group_id' => $this->getDefaultGroupId(),
46 'last_recovered_user' => 0,
47 ];
48 }
49
50 $notRecoveredUserIdList =
51 \Bitrix\Main\UserTable::query()
52 ->addSelect('ID')
53 ->registerRuntimeField(
54 'OPTION_USER',
55 (new \Bitrix\Main\ORM\Fields\Relations\Reference(
56 'OPTION_USER',
57 \Bitrix\Im\Model\OptionUserTable::getEntity(),
58 \Bitrix\Main\ORM\Query\Join::on('this.ID', 'ref.USER_ID')
59 ))->configureJoinType(\Bitrix\Main\ORM\Query\Join::TYPE_LEFT)
60 )
61 ->whereNull('OPTION_USER.USER_ID')
62 ->where('IS_REAL_USER', 'Y')
63 ->where('ID', '>', $params['last_recovered_user'])
64 ->addOrder('ID')
65 ->setLimit(100)
66 ->fetchAll()
67 ;
68 if (empty($notRecoveredUserIdList))
69 {
70 Option::delete(self::$moduleId, ['name' => self::OPTION_NAME]);
71 Option::delete(self::$moduleId, ['name' => 'have_event_handlers_restored']);
72
73 return false;
74 }
75
76 $notRecoveredUserIdList = array_map(static fn($user) => (int)$user['ID'], $notRecoveredUserIdList);
77
78 $userPresetList =
79 OptionGroupTable::query()
80 ->addSelect('ID')
81 ->addSelect('USER_ID')
82 ->whereIn('USER_ID', $notRecoveredUserIdList)
83 ->fetchAll()
84 ;
85
86 $flippedNotRecoveredUserIdList = array_flip($notRecoveredUserIdList);
87 foreach ($userPresetList as $userPreset)
88 {
89 $insertFields = [
90 'USER_ID' => (int)$userPreset['USER_ID'],
91 'GENERAL_GROUP_ID' => (int)$userPreset['ID'],
92 'NOTIFY_GROUP_ID' => (int)$userPreset['ID'],
93 ];
94 $updateFields = [
95 'GENERAL_GROUP_ID' => (int)$userPreset['ID'],
96 'NOTIFY_GROUP_ID' => (int)$userPreset['ID'],
97 ];
98
99 OptionUserTable::merge($insertFields, $updateFields);
100
101 $params['last_recovered_user'] = (int)$userPreset['ID'];
102 unset($flippedNotRecoveredUserIdList[(int)$userPreset['USER_ID']]);
103 }
104
105 foreach (array_flip($flippedNotRecoveredUserIdList) as $userId)
106 {
107 $insertFields = [
108 'USER_ID' => $userId,
109 'GENERAL_GROUP_ID' => $params['default_group_id'],
110 'NOTIFY_GROUP_ID' => $params['default_group_id']
111 ];
112 $updateFields = [
113 'GENERAL_GROUP_ID' => $params['default_group_id'],
114 'NOTIFY_GROUP_ID' => $params['default_group_id'],
115 ];
116
117 OptionUserTable::merge($insertFields, $updateFields);
118
119 $params['last_recovered_user'] = $userId;
120 }
121
122
123 $params = serialize($params);
124 Option::set(self::$moduleId, self::OPTION_NAME, $params);
125
126 return true;
127 }
128
129 private function restoreEventHandlers(): void
130 {
131 $eventHandlerClass = '\\' . EventHandler::class;
132 $eventManager = EventManager::getInstance();
133
134 $this->restoreEventHandler($eventManager, 'OnAfterUserAdd', $eventHandlerClass);
135 $this->restoreEventHandler($eventManager, 'OnAfterUserUpdate', $eventHandlerClass);
136 $this->restoreEventHandler($eventManager, 'OnAfterUserDelete', $eventHandlerClass);
137
138 Option::set(self::$moduleId, 'have_event_handlers_restored', 'Y');
139 }
140
141 private function restoreEventHandler(EventManager $eventManager, string $event, string $eventHandlerClass): void
142 {
143 $eventHandler = $this->findEventHandler($eventManager, $event);
144 if (empty($eventHandler))
145 {
146 $eventManager->registerEventHandler('main', $event, 'im', $eventHandlerClass, $event);
147 }
148 }
149
150 private function findEventHandler(EventManager $eventManager, string $event): array
151 {
152 return array_filter(
153 $eventManager->findEventHandlers('main', $event),
154 static fn($handler) =>
155 ($handler['TO_MODULE_ID'] === 'im')
156 && (mb_strpos($handler['TO_CLASS'], 'Configuration') !== false)
157 );
158 }
159
160 private function getDefaultGroupId(): int
161 {
162 $defaultGroupId =
163 OptionGroupTable::query()
164 ->addSelect('ID')
165 ->where('NAME', Configuration::DEFAULT_PRESET_NAME)
166 ->fetch()
167 ;
168
169 if ($defaultGroupId)
170 {
171 Option::set('im', Configuration::DEFAULT_PRESET_SETTING_NAME, (int)$defaultGroupId['ID']);
172
173 return (int)$defaultGroupId['ID'];
174 }
175
176 return Configuration::createDefaultPreset();
177 }
178}
registerEventHandler($fromModuleId, $eventType, $toModuleId, $toClass='', $toMethod='', $sort=100, $toPath='', $toMethodArg=[])