1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
GeneralChannel.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
5use Bitrix\HumanResources\Repository\StructureRepository;
6use Bitrix\Im\Model\ChatTable;
7use Bitrix\Im\V2\Chat;
8use Bitrix\Im\V2\Entity\User\User;
9use Bitrix\Im\V2\Integration\HumanResources\Structure;
10use Bitrix\Im\V2\Relation\AddUsersConfig;
11use Bitrix\Im\V2\Relation\Reason;
12use Bitrix\Im\V2\Result;
13use Bitrix\Im\V2\Service\Context;
14use Bitrix\Intranet\Settings\CommunicationSettings;
15use Bitrix\Main\Application;
16use Bitrix\Main\Data\Cache;
17use Bitrix\Main\Loader;
18use Bitrix\Main\Localization\Loc;
19use CAllSite;
20
22{
23 public const ID_CACHE_ID = 'general_channel_id';
24
25 private const MESSAGE_COMPONENT_START = 'GeneralChannelCreationMessage';
26
27 protected static ?self $instance = null;
28 protected static bool $wasSearched = false;
29 protected static int $idStaticCache;
30
31 protected function getDefaultEntityType(): string
32 {
33 return self::ENTITY_TYPE_GENERAL_CHANNEL;
34 }
35
36 public function getDefaultManageMessages(): string
37 {
38 return self::MANAGE_RIGHTS_MEMBER;
39 }
40
41 public static function get(): ?GeneralChannel
42 {
43 if (self::$wasSearched)
44 {
45 return self::$instance;
46 }
47
48 $chatId = self::getGeneralChannelId();
49 $chat = Chat::getInstance($chatId);
50 self::$instance = ($chat instanceof NullChat) ? null : $chat;
51 self::$wasSearched = true;
52
53 return self::$instance;
54 }
55
56 public static function getGeneralChannelId(): ?int
57 {
58 if (isset(self::$idStaticCache))
59 {
60 return self::$idStaticCache;
61 }
62
63 $cache = static::getCache(self::ID_CACHE_ID);
64
65 $cachedId = $cache->getVars();
66
67 if ($cachedId !== false)
68 {
69 self::$idStaticCache = $cachedId ?? 0;
70
71 return self::$idStaticCache;
72 }
73
74 $result = ChatTable::query()
75 ->setSelect(['ID'])
76 ->where('TYPE', self::IM_TYPE_OPEN_CHANNEL)
77 ->where('ENTITY_TYPE', self::ENTITY_TYPE_GENERAL_CHANNEL)
78 ->fetch() ?: []
79 ;
80
81 self::$idStaticCache = $result['ID'] ?? 0;
82 $cache->startDataCache();
83 $cache->endDataCache(self::$idStaticCache);
84
85 return self::$idStaticCache;
86 }
87
88 protected function getGeneralChannelIdWithoutCache(): ?int
89 {
90 $result = ChatTable::query()
91 ->setSelect(['ID'])
92 ->where('ENTITY_TYPE', self::ENTITY_TYPE_GENERAL_CHANNEL)
93 ->setLimit(1)
94 ->fetch()
95 ;
96
97 if ($result)
98 {
99 return (int)$result['ID'];
100 }
101
102 return null;
103 }
104
105 public function add(array $params, ?Context $context = null): Result
106 {
107 $result = new Result;
108
109 $generalChannel = Chat::getInstance($this->getGeneralChannelIdWithoutCache());
110 if ($generalChannel instanceof self)
111 {
112 return $result->setResult([
113 'CHAT_ID' => $generalChannel->getChatId(),
114 'CHAT' => $generalChannel,
115 ]);
116 }
117
118 $portalLanguage = self::getPortalLanguage();
119
120 $params = [
121 'TYPE' => self::IM_TYPE_OPEN_CHANNEL,
122 'ENTITY_TYPE' => self::ENTITY_TYPE_GENERAL_CHANNEL,
123 'COLOR' => 'AZURE',
124 'TITLE' => Loc::getMessage('IM_CHAT_GENERAL_CHANNEL_TITLE', null, $portalLanguage),
125 'DESCRIPTION' => Loc::getMessage('IM_CHAT_GENERAL_CHANNEL_DESCRIPTION', null, $portalLanguage),
126 'AUTHOR_ID' => User::getFirstAdmin(),
127 'MEMBER_ENTITIES' => [['department', $this->getCompanyStructureId()]],
128 ];
129
130 $result = parent::add($params);
131 self::cleanGeneralChannelCache(self::ID_CACHE_ID);
132
133 return $result;
134 }
135
136 private static function getPortalLanguage(): ?string
137 {
138 $defSite = CAllSite::GetDefSite();
139 if ($defSite === false)
140 {
141 return null;
142 }
143
144 $portalData = CAllSite::GetByID($defSite)->Fetch();
145 if ($portalData)
146 {
147 $languageId = $portalData['LANGUAGE_ID'];
148 if ($languageId !== '')
149 {
150 return $languageId;
151 }
152 }
153
154 return null;
155 }
156
157 public function addUsers(array $userIds, AddUsersConfig $config = new AddUsersConfig()): self
158 {
159 $managerIds = [];
160 foreach ($userIds as $userId)
161 {
163
164 if ($user->isAdmin())
165 {
166 $managerIds[] = $user->getId();
167 }
168 }
169
170 $generalChat = GeneralChat::get();
171 if (isset($generalChat))
172 {
173 foreach ($generalChat->getManagerList() as $manager)
174 {
175 if (in_array((int)$manager, $userIds, true))
176 {
177 $managerIds[] = $manager;
178 }
179 }
180 }
181
182 $managerIds = array_unique($managerIds);
183 $config = $config->setManagerIds($managerIds);
184
185 return parent::addUsers($userIds, $config);
186 }
187
188 protected function sendMessageUsersAdd(array $usersToAdd, AddUsersConfig $config): void
189 {
190 return;
191 }
192
193 protected function getCompanyStructureId(): ?string
194 {
195 if (!\Bitrix\Main\Loader::includeModule('humanresources'))
196 {
197 return null;
198 }
199
200 $structure = (new StructureRepository())->getByXmlId(\Bitrix\HumanResources\Item\Structure::DEFAULT_STRUCTURE_XML_ID);
201 if (!isset($structure))
202 {
203 return null;
204 }
205
206 $rootNode = (new \Bitrix\HumanResources\Repository\NodeRepository())->getRootNodeByStructureId($structure->id);
207 if (!isset($rootNode))
208 {
209 return null;
210 }
211
212 $accessCode = $rootNode->accessCode;
213 preg_match('/D(\d+)/', $accessCode ?? '', $matches);
214
215 return $matches[1] ?? null;
216 }
217
218 protected function sendBanner(?int $authorId = null): void
219 {
221 'MESSAGE_TYPE' => self::IM_TYPE_OPEN_CHANNEL,
222 'TO_CHAT_ID' => $this->getChatId(),
223 'FROM_USER_ID' => 0,
224 'MESSAGE' => Loc::getMessage('IM_CHAT_GENERAL_CHANNEL_CREATE_WELCOME', null, self::getPortalLanguage()),
225 'SYSTEM' => 'Y',
226 'PUSH' => 'N',
227 'PARAMS' => [
228 'COMPONENT_ID' => self::MESSAGE_COMPONENT_START,
229 'NOTIFY' => 'N',
230 ],
231 'SKIP_COUNTER_INCREMENTS' => 'Y',
232 ]);
233 }
234
235 protected function sendGreetingMessage(?int $authorId = null)
236 {
237 return;
238 }
239
240 protected function sendDescriptionMessage(?int $authorId = null): void
241 {
242 return;
243 }
244
245 protected function needToSendGreetingMessages(): bool
246 {
247 return true;
248 }
249
250 public static function deleteGeneralChannel(): Result
251 {
252 $chat = self::get();
253 if (!isset($chat))
254 {
255 return (new Result())->addError(new ChatError(ChatError::NOT_FOUND));
256 }
257
258 $structureNodes = Structure::splitEntities([['department', $chat->getCompanyStructureId()]]);
259 $chat->unlinkStructureNodes($structureNodes[1] ?? []);
260
261 $result = $chat->deleteChat();
262 self::cleanGeneralChannelCache(self::ID_CACHE_ID);
263
264 return $result;
265 }
266
267 public static function installGeneralChannel(): void
268 {
269 (new self())->add([]);
270 }
271
272 public static function installAgent(): string
273 {
274 if (self::get() !== null)
275 {
276 return '';
277 }
278
279 if (!Structure::isSyncAvailable())
280 {
281 return "\\" . __METHOD__ . '();';
282 }
283
285
286 return '';
287 }
288
289 public static function changeLangAgent(): string
290 {
291 if (!Loader::includeModule('im'))
292 {
293 return '';
294 }
295
297
299 if ($chatId > 0)
300 {
301 $portalLanguage = self::getPortalLanguage();
302
303 ChatTable::update($chatId, [
304 'TITLE' => Loc::getMessage('IM_CHAT_GENERAL_CHANNEL_TITLE', null, $portalLanguage),
305 'DESCRIPTION' => Loc::getMessage('IM_CHAT_GENERAL_CHANNEL_DESCRIPTION', null, $portalLanguage),
306 ]);
307 }
308
309 return '';
310 }
311
313 {
314 $result['generalChannelCanPostList'] = self::getCanPostList();
315 $result['generalChannelCanPost'] = $this->getManageMessages();
316 $result['generalChannelShowManagersList'] = self::MANAGE_RIGHTS_MANAGERS;
317 $managerIds = $this->getRelationFacade()->getManagerOnly()->getUserIds();
318 $managers = array_map(function ($managerId) {
319 return 'U' . $managerId;
320 }, $managerIds);
321
322 if (!Loader::includeModule('intranet'))
323 {
324 return $result;
325 }
326
327 if (method_exists('\Bitrix\Intranet\Settings\CommunicationSettings', 'processOldAccessCodes'))
328 {
329 $result['generalChannelManagersList'] = CommunicationSettings::processOldAccessCodes($managers);
330 }
331 else
332 {
333 $result['generalChannelManagersList'] = \IntranetConfigsComponent::processOldAccessCodes($managers);
334 }
335
336 return $result;
337 }
338
339 private static function getCache(string $cacheId): Cache
340 {
341 $cache = Application::getInstance()->getCache();
342 $cacheTTL = 18144000;
343 $cacheDir = static::getCacheDir();
344 $cache->initCache($cacheTTL, $cacheId, $cacheDir);
345
346 return $cache;
347 }
348
349 private static function getCacheDir(): string
350 {
351 return '/bx/imc/general_channel';
352 }
353
354 public static function cleanGeneralChannelCache(string $cacheId): void
355 {
356 Application::getInstance()->getCache()->clean($cacheId, static::getCacheDir());
357 }
358}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
const NOT_FOUND
Определения ChatError.php:20
static self $instance
Определения GeneralChannel.php:27
sendMessageUsersAdd(array $usersToAdd, AddUsersConfig $config)
Определения GeneralChannel.php:188
add(array $params, ?Context $context=null)
Определения GeneralChannel.php:105
static changeLangAgent()
Определения GeneralChannel.php:289
static cleanGeneralChannelCache(string $cacheId)
Определения GeneralChannel.php:354
sendBanner(?int $authorId=null)
Определения GeneralChannel.php:218
static bool $wasSearched
Определения GeneralChannel.php:28
addUsers(array $userIds, AddUsersConfig $config=new AddUsersConfig())
Определения GeneralChannel.php:157
static getGeneralChannelId()
Определения GeneralChannel.php:56
sendDescriptionMessage(?int $authorId=null)
Определения GeneralChannel.php:240
sendGreetingMessage(?int $authorId=null)
Определения GeneralChannel.php:235
static installGeneralChannel()
Определения GeneralChannel.php:267
static deleteGeneralChannel()
Определения GeneralChannel.php:250
static int $idStaticCache
Определения GeneralChannel.php:29
static get()
Определения GeneralChat.php:108
static getFirstAdmin()
Определения User.php:717
static getInstance()
Определения application.php:98
Определения result.php:20
static getInstance()
Определения servicelocator.php:33
static Add($arFields)
Определения im_message.php:28
getCache(string $moduleID)
Определения options.php:26
</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
$context
Определения csv_new_setup.php:223
Определения culture.php:9
$user
Определения mysql_to_pgsql.php:33
$manager
Определения office365push.php:39
$config
Определения quickway.php:69
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$matches
Определения index.php:22