11use Bitrix\Intranet\Settings\CommunicationSettings;
32 return self::IM_TYPE_OPEN;
37 return self::ENTITY_TYPE_GENERAL;
42 if ($this->getId() ===
null || $this->getId() === 0)
52 $userId ??= $this->getContext()->getUserId();
58 if ($this->getAuthorId() === $userId)
73 return Option::get(
'im', self::DISABLE_GENERAL_CHAT_OPTION,
'N') ===
'N';
78 $cache = static::getCache(self::MANAGERS_CACHE_ID);
80 $cachedManagerList = $cache->getVars();
82 if ($cachedManagerList !==
false)
84 return $cachedManagerList;
87 $managerList = $this->getRelations([
'FILTER' => [
'MANAGER' =>
'Y']])->getUserIds();
89 $cache->startDataCache();
90 $cache->endDataCache($managerList);
92 return $this->getRelations([
'FILTER' => [
'MANAGER' =>
'Y']])->getUserIds();
97 if (self::$wasSearched)
102 $chatId = static::getGeneralChatId();
104 self::$instance = ($chat instanceof
NullChat) ?
null : $chat;
105 self::$wasSearched =
true;
112 if (!static::isEnable())
117 $cache = static::getCache(self::ID_CACHE_ID);
119 $cachedId = $cache->getVars();
121 if ($cachedId !==
false)
123 return $cachedId ?? 0;
126 $result = ChatTable::query()
133 $chatId = $result[
'ID'] ??
null;
134 $cache->startDataCache();
135 $cache->endDataCache($chatId);
142 static::cleanGeneralChatCache(self::MANAGERS_CACHE_ID);
144 return parent::setManagers($managerIds);
154 if (isset(self::$resultFind))
161 $row = ChatTable::query()
162 ->setSelect([
'ID',
'TYPE',
'ENTITY_TYPE',
'ENTITY_ID'])
163 ->where(
'ENTITY_TYPE', self::ENTITY_TYPE_GENERAL)
165 ->setOrder([
'ID' =>
'DESC'])
172 'ID' => (
int)$row[
'ID'],
173 'TYPE' => $row[
'TYPE'],
174 'ENTITY_TYPE' => $row[
'ENTITY_TYPE'],
175 'ENTITY_ID' => $row[
'ENTITY_ID'],
179 self::$resultFind = $result;
189 if ($generalChatResult->hasResult())
191 $generalChat =
new GeneralChat([
'ID' => $generalChatResult->getResult()[
'ID']]);
192 return $result->setResult([
193 'CHAT_ID' => $generalChat->getChatId(),
194 'CHAT' => $generalChat,
198 $installUsers = $this->getUsersForInstall();
201 'TYPE' => self::IM_TYPE_OPEN,
202 'ENTITY_TYPE' => self::ENTITY_TYPE_GENERAL,
207 'USER_COUNT' => count($installUsers),
210 $chat =
new static($params);
211 $chat->setExtranet(
false);
214 if (!$chat->getChatId())
222 if (Loader::includeModule(
'bitrix24'))
224 $adminIds = \CBitrix24::getAllAdminId();
227 foreach ($installUsers as $user)
230 $relation->setChatId($chat->getChatId());
231 $relation->setUserId((
int)$user[
'ID']);
232 $relation->setManager(in_array((
int)$user[
'ID'], $adminIds,
true));
233 $relation->setMessageType(self::IM_TYPE_OPEN);
234 $relation->setStatus(IM_STATUS_READ);
243 'CHAT_ID' => $chat->getChatId(),
249 self::cleanCache($chat->getChatId());
266 if (Loader::includeModule(
'pull'))
268 \CPullStack::AddShared([
270 'command' =>
'generalChatId',
292 if (Loader::includeModule(
'pull'))
294 \CPullStack::AddShared([
296 'command' =>
'generalChatId',
304 static::cleanGeneralChatCache(self::ID_CACHE_ID);
305 static::cleanGeneralChatCache(self::MANAGERS_CACHE_ID);
314 || !self::getGeneralChatId()
315 || !Loader::includeModule(
'intranet')
321 $connection = \Bitrix\Main\Application::getConnection();
326 INNER JOIN b_user_field F ON F.ENTITY_ID = 'USER' AND F.FIELD_NAME = 'UF_DEPARTMENT'
327 INNER JOIN b_utm_user UF ON
329 AND UF.VALUE_ID = U.ID
333 AND U.ID = " . $userId .
"
334 AND F.ENTITY_ID = 'USER'
335 AND F.FIELD_NAME = 'UF_DEPARTMENT'
338 if ($connection->query($sql)->fetch())
346 private function getUsersForInstall(): array
348 $externalUserTypes = \Bitrix\Main\UserTable::getExternalUserTypes();
349 $types = implode(
"', '", $externalUserTypes);
350 if (Loader::includeModule(
'intranet'))
356 INNER JOIN b_user_field F ON F.ENTITY_ID = 'USER' AND F.FIELD_NAME = 'UF_DEPARTMENT'
357 INNER JOIN b_utm_user UF ON
359 AND UF.VALUE_ID = U.ID
363 AND (U.EXTERNAL_AUTH_ID IS NULL OR U.EXTERNAL_AUTH_ID NOT IN ('{$types}') )
364 AND F.ENTITY_ID = 'USER'
365 AND F.FIELD_NAME = 'UF_DEPARTMENT'
375 AND (U.EXTERNAL_AUTH_ID IS NULL OR U.EXTERNAL_AUTH_ID NOT IN ('{$types}') )
379 $connection = \Bitrix\Main\Application::getConnection();
380 return $connection->query($sql)->fetchAll();
386 'MESSAGE_TYPE' => self::IM_TYPE_CHAT,
387 'TO_CHAT_ID' => $this->getChatId(),
393 'COMPONENT_ID' =>
'ChatCreationMessage',
403 return (
bool)\COption::GetOptionString(
"im",
"general_chat_message_join");
405 return (
bool)\COption::GetOptionString(
"im",
"general_chat_message_leave");
413 $result[
'generalChatCanPostList'] = self::getCanPostList();
414 $result[
'generalChatCanPost'] = $this->getCanPost();
415 $result[
'generalChatShowManagersList'] = self::MANAGE_RIGHTS_MANAGERS;
416 $managerIds = $this->getRelations([
421 $managers = array_map(
function ($managerId) {
422 return 'U' . $managerId;
424 Loader::includeModule(
'intranet');
425 if (method_exists(
'\Bitrix\Intranet\Settings\CommunicationSettings',
'processOldAccessCodes'))
427 $result[
'generalChatManagersList'] = CommunicationSettings::processOldAccessCodes($managers);
431 $result[
'generalChatManagersList'] = \IntranetConfigsComponent::processOldAccessCodes($managers);
439 $accessCodes = parent::getAccessCodesForDiskFolder();
440 $departmentCode = \CIMDisk::GetTopDepartmentCode();
444 $driver = \Bitrix\Disk\Driver::getInstance();
445 $rightsManager = $driver->getRightsManager();
447 'ACCESS_CODE' => $departmentCode,
448 'TASK_ID' => $rightsManager->getTaskIdByName($rightsManager::TASK_READ)
463 return $generalChat->deleteChat();
468 if ($this->getContext()->getUserId() > 0)
470 parent::sendMessageUsersAdd($usersToAdd, $skipRecent);
475 if (!self::getAutoMessageStatus(self::GENERAL_MESSAGE_TYPE_JOIN))
481 foreach ($usersToAdd as $userId)
483 $userCodes[] =
"[USER={$userId}][/USER]";
485 $userCodesString = implode(
', ', $userCodes);
487 if (count($usersToAdd) > 1)
489 $messageText =
Loc::getMessage(
"IM_CHAT_GENERAL_JOIN_PLURAL", [
'#USER_NAME#' => $userCodesString]);
494 $genderModifier = $user->getGender() ===
'F' ?
'_F' :
'';
495 $messageText =
Loc::getMessage(
'IM_CHAT_GENERAL_JOIN' . $genderModifier, [
'#USER_NAME#' => $userCodesString]);
498 \CIMChat::AddMessage([
499 "TO_CHAT_ID" => $this->getId(),
500 "MESSAGE" => $messageText,
501 "FROM_USER_ID" => $this->getContext(),
503 "RECENT_ADD" => $skipRecent ?
'N' :
'Y',
505 "CODE" =>
'CHAT_JOIN',
506 "NOTIFY" => $this->getEntityType() === self::ENTITY_TYPE_LINE?
'Y':
'N',
509 "SKIP_USER_CHECK" =>
'Y',
515 if (!self::getAutoMessageStatus(self::GENERAL_MESSAGE_TYPE_LEAVE))
520 parent::sendMessageUserDelete($userId, $skipRecent);
527 return Loc::getMessage(
"IM_CHAT_GENERAL_LEAVE_{$user->getGender()}", Array(
'#USER_NAME#' => htmlspecialcharsback($user->getName())));
530 private static function getCache(
string $cacheId):
Cache
533 $cacheTTL = 18144000;
534 $cacheDir = static::getCacheDir();
535 $cache->initCache($cacheTTL, $cacheId, $cacheDir);
540 private static function getCacheDir(): string
542 return '/bx/imc/general_chat';
getRightsForIntranetConfig()
sendMessageUsersAdd(array $usersToAdd, bool $skipRecent=false)
static cleanGeneralChatCache(string $cacheId)
add(array $params, ?Context $context=null)
const GENERAL_MESSAGE_TYPE_JOIN
sendBanner(?int $authorId=null)
static getGeneralChatId()
static getAutoMessageStatus(string $type)
static find(array $params=[], ?Context $context=null)
getAccessCodesForDiskFolder()
const DISABLE_GENERAL_CHAT_OPTION
const GENERAL_MESSAGE_TYPE_LEAVE
sendMessageUserDelete(int $userId, bool $skipRecent=false)
canJoinGeneralChat(int $userId)
setManagers(array $managerIds)
getMessageUserDeleteText(int $userId)
hasPostAccess(?int $userId=null)
static linkGeneralChat(?int $chatId=null)
static deleteGeneralChat()
static unlinkGeneralChat()
static Result $resultFind
const MANAGE_RIGHTS_MEMBER
const ENTITY_TYPE_GENERAL
const MANAGE_RIGHTS_OWNER
static getMessage($code, $replace=null, $language=null)