Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
OwnerService.php
1<?php
3
8
10{
11 protected const DELAY_AFTER_USER_FIRED = 10;
12
13 public static function onAfterUserUpdate(array $fields): void
14 {
15 if (!isset($fields['ACTIVE']) || $fields['ACTIVE'] !== 'N')
16 {
17 return;
18 }
19
20 \CAgent::AddAgent(
21 __CLASS__. "::changeChatsOwnerAfterUserFiredAgent({$fields['ID']});",
22 'im',
23 'N',
24 self::DELAY_AFTER_USER_FIRED,
25 '',
26 'Y',
27 ConvertTimeStamp(time() + \CTimeZone::GetOffset() + self::DELAY_AFTER_USER_FIRED, "FULL")
28 );
29 }
30
31 public static function changeChatsOwnerAfterUserFiredAgent(int $ownerId): string
32 {
33 $ownerChats = ChatTable::getList([
34 'filter' => [
35 'AUTHOR_ID' => $ownerId,
37 ]
38 ]);
39
40 foreach ($ownerChats as $ownerChat)
41 {
42 $chat = Chat\ChatFactory::getInstance()->getChat($ownerChat['ID']);
43
44 $ownerRelation = $chat->getRelations([
45 'FILTER' => [
46 'USER_ID' => $ownerId
47 ],
48 'LIMIT' => 1
49 ]);
50 if ($ownerRelation->getIterator()->current())
51 {
52 $ownerRelation->getIterator()->current()->setManager(false);
53 $ownerRelation->getIterator()->current()->save();
54 }
55
56 $relations = $chat->getRelations([
57 'FILTER' => [
58 '!USER_ID' => $ownerId
59 ]
60 ]);
61 if ($relations->count())
62 {
63 foreach ($relations as $relation)
64 {
65 $user = $relation->getUser();
66 if (
67 $user->isExist()
68 && $user->isActive()
69 && !$user->isBot()
70 && !$user->isExtranet()
71 && !$user->isConnector()
72 )
73 {
74 $chat->setAuthorId($relation->getUserId());
75 $chat->save();
76
77 $relation->setManager(true);
78 $relation->save();
79
80 break;
81 }
82 }
83 }
84 }
85
86 return '';
87 }
88
89 public static function changeChatsOwnerForAllFiredUsersAgent(): string
90 {
91 $firedUsers = UserTable::getList([
92 'select' => [
93 'ID'
94 ],
95 'filter' => [
96 'ACTIVE' => 'N'
97 ]
98 ])->fetchAll();
99
100 foreach ($firedUsers as $key => $user)
101 {
102 \CAgent::AddAgent(
103 __CLASS__. '::changeChatsOwnerAfterUserFiredAgent(' . (int)$user['ID'] . ');',
104 'im',
105 'N',
106 self::DELAY_AFTER_USER_FIRED,
107 '',
108 'Y',
109 ConvertTimeStamp(time() + \CTimeZone::GetOffset() + self::DELAY_AFTER_USER_FIRED * $key, "FULL")
110 );
111 }
112
113 return '';
114 }
115
116 public static function migrateOwnershipOfGeneralChatAgent(): string
117 {
118 $generalChatId = \COption::GetOptionInt('im', 'general_chat_id');
119 if (!$generalChatId)
120 {
121 return '';
122 }
123
124 $oldChat = Chat\ChatFactory::getInstance()->getChatById($generalChatId);
125 if ($oldChat instanceof Chat\NullChat)
126 {
127 return '';
128 }
129
130 $oldChat
131 ->setType(Chat::IM_TYPE_OPEN)
132 ->setEntityType(Chat::ENTITY_TYPE_GENERAL)
133 ->save();
134
135 $generalChat = Chat\ChatFactory::getInstance()->getGeneralChat();
136 if (!$generalChat || $generalChat instanceof Chat\NullChat)
137 {
138 return '';
139 }
140
141 $canPostAll = (\COption::GetOptionString('im', 'allow_send_to_general_chat_all', 'Y') === 'Y');
142 if ($canPostAll)
143 {
144 $generalChat
145 ->setCanPost(Chat::MANAGE_RIGHTS_MEMBER)
146 ->save();
147
148 return '';
149 }
150
151 $chatRights = \COption::GetOptionString('im', 'allow_send_to_general_chat_rights');
152 if (!$chatRights)
153 {
154 return '';
155 }
156
157 $users = UserAccessTable::getList([
158 'select' => [
159 'USER_ID'
160 ],
161 'filter' => [
162 '=ACCESS_CODE' => explode(',', $chatRights)
163 ],
164 'group' => [
165 'USER_ID'
166 ]
167 ])->fetchAll();
168
169 if (!$users)
170 {
171 return '';
172 }
173
174 $userIds = array_column($users, 'USER_ID');
175
176 $relations = $generalChat->getRelations();
177 foreach ($relations as $relation)
178 {
179 if (in_array($relation->getUserId(), $userIds))
180 {
181 $relation->setManager(true);
182 $relation->save();
183 }
184 }
185
186 $generalChat
187 ->setCanPost(Chat::MANAGE_RIGHTS_MANAGERS)
188 ->save();
189
190 return '';
191 }
192}
static onAfterUserUpdate(array $fields)
static changeChatsOwnerAfterUserFiredAgent(int $ownerId)
const MANAGE_RIGHTS_MEMBER
Definition Chat.php:133
const ENTITY_TYPE_GENERAL
Definition Chat.php:91
const IM_TYPE_OPEN
Definition Chat.php:61
const MANAGE_RIGHTS_MANAGERS
Definition Chat.php:135
const IM_TYPE_CHAT
Definition Chat.php:56