Example #1
0
 public function Add($arParams)
 {
     global $DB;
     $chatTitle = '';
     if (isset($arParams['TITLE'])) {
         $chatTitle = trim($arParams['TITLE']);
     }
     $chatDescription = '';
     if (isset($arParams['DESCRIPTION'])) {
         $chatDescription = trim($arParams['DESCRIPTION']);
     }
     $userId = array();
     if (isset($arParams['USERS'])) {
         $userId = $arParams['USERS'];
     }
     $callNumber = '';
     if (isset($arParams['CALL_NUMBER'])) {
         $callNumber = $arParams['CALL_NUMBER'];
     }
     $entityType = '';
     if (isset($arParams['ENTITY_TYPE'])) {
         $entityType = $arParams['ENTITY_TYPE'];
     }
     $entityId = '';
     if (isset($arParams['ENTITY_ID'])) {
         $entityId = $arParams['ENTITY_ID'];
     }
     $message = '';
     if (isset($arParams['MESSAGE'])) {
         $message = trim($arParams['MESSAGE']);
     }
     $color = '';
     if (isset($arParams['COLOR']) && IM\Color::isSafeColor($arParams['COLOR'])) {
         $color = $arParams['COLOR'];
     }
     $type = IM_MESSAGE_CHAT;
     if (isset($arParams['TYPE']) && in_array($arParams['TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT)) && CIMMessenger::CheckEnableOpenChat()) {
         if ($this->user_id == 0 || !IM\User::getInstance($this->user_id)->isExtranet()) {
             $type = $arParams['TYPE'];
         }
     }
     $skipUserAdd = false;
     if ($userId === false) {
         $skipUserAdd = true;
     }
     $arUserId = array();
     if (is_array($userId)) {
         $arUserId = \CIMContactList::PrepareUserIds($userId);
     } else {
         if (intval($userId) > 0) {
             $arUserId[intval($userId)] = intval($userId);
         }
     }
     if ($this->user_id > 0) {
         $arUserId[$this->user_id] = $this->user_id;
     }
     if (!$skipUserAdd) {
         if (count($arUserId) < 1) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MIN_USER"), "MIN_USER");
             return false;
         }
         if (false && count($arUserId) > 500) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MAX_USER", array('#COUNT#' => 500)), "MAX_USER");
             return false;
         }
         if (!IsModuleInstalled('intranet') && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed()) {
             global $USER;
             $arFriendUsers = array();
             $dbFriends = CSocNetUserRelations::GetList(array(), array("USER_ID" => $USER->GetID(), "RELATION" => SONET_RELATIONS_FRIEND), false, false, array("ID", "FIRST_USER_ID", "SECOND_USER_ID", "DATE_CREATE", "DATE_UPDATE", "INITIATED_BY"));
             while ($arFriends = $dbFriends->Fetch()) {
                 $friendId = $USER->GetID() == $arFriends["FIRST_USER_ID"] ? $arFriends["SECOND_USER_ID"] : $arFriends["FIRST_USER_ID"];
                 $arFriendUsers[$friendId] = $friendId;
             }
             foreach ($arUserId as $id => $userId) {
                 if ($userId == $USER->GetID()) {
                     continue;
                 }
                 if (!isset($arFriendUsers[$userId]) && CIMSettings::GetPrivacy(CIMSettings::PRIVACY_CHAT, $userId) == CIMSettings::PRIVACY_RESULT_CONTACT) {
                     unset($arUserId[$id]);
                 }
             }
             if (count($arUserId) == 2) {
                 $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MIN_USER_BY_PRIVACY"), "MIN_USER_BY_PRIVACY");
                 return false;
             }
         }
     }
     $arUsers = CIMContactList::GetUserData(array('ID' => array_values($arUserId), 'DEPARTMENT' => 'N', 'USE_CACHE' => 'N'));
     $arUsers = is_array($arUsers['users']) ? $arUsers['users'] : array();
     $arUsersName = array();
     if (strlen($chatDescription) <= 0 && $type == IM_MESSAGE_OPEN) {
         $chatDescription = $message;
     }
     if (strlen($chatDescription) > 0) {
         $chatDescription = CIMMessenger::PrepareMessageForPush(array('MESSAGE' => $chatDescription));
     }
     $chatColorCode = "";
     if (IM\Color::isEnabled()) {
         if ($color) {
             $chatColorCode = $color;
         } else {
             CGlobalCounter::Increment('im_chat_color_id', CGlobalCounter::ALL_SITES, false);
             $chatColorId = CGlobalCounter::GetValue('im_chat_color_id', CGlobalCounter::ALL_SITES);
             $chatColorCode = \Bitrix\Im\Color::getCodeByNumber($chatColorId);
             CGlobalCounter::Increment('im_chat_color_' . $chatColorCode, CGlobalCounter::ALL_SITES, false);
         }
         $chatColorCodeCount = CGlobalCounter::GetValue('im_chat_color_' . $chatColorCode, CGlobalCounter::ALL_SITES);
         if ($chatColorCodeCount == 100) {
             CGlobalCounter::Set('im_chat_color_' . $chatColorCode, 1, CGlobalCounter::ALL_SITES, '', false);
             $chatColorId = 1;
         }
     }
     if ($chatTitle == "") {
         if (IM\Color::isEnabled()) {
             $chatTitle = GetMessage('IM_CHAT_NAME_FORMAT', array('#COLOR#' => \Bitrix\Im\Color::getName($chatColorCode), '#NUMBER#' => $chatColorCodeCount));
         } else {
             foreach ($arUserId as $userId) {
                 $arUsersName[$userId] = htmlspecialcharsback($arUsers[$userId]['name']);
             }
             $chatTitle = implode(', ', $arUsersName);
         }
     }
     $isExtranet = false;
     foreach ($arUsers as $userData) {
         if ($userData['extranet']) {
             $isExtranet = true;
             break;
         }
     }
     $result = IM\ChatTable::add(array("TITLE" => substr($chatTitle, 0, 255), "DESCRIPTION" => $chatDescription, "TYPE" => $type, "COLOR" => $chatColorCode, "AUTHOR_ID" => $this->user_id, "ENTITY_TYPE" => $entityType, "ENTITY_ID" => $entityId, "EXTRANET" => $isExtranet ? 'Y' : 'N', "CALL_NUMBER" => $callNumber));
     $chatId = $result->getId();
     if ($chatId > 0) {
         $params = $result->getData();
         if (intval($params['AVATAR']) > 0) {
             $this->lastAvatarId = $params['AVATAR'];
         }
         $arUsersName = array();
         foreach ($arUserId as $userId) {
             if ($userId != $this->user_id) {
                 $arUsersName[$userId] = htmlspecialcharsback($arUsers[$userId]['name']);
             }
             CIMContactList::SetRecent(array('ENTITY_ID' => $chatId, 'MESSAGE_ID' => 0, 'CHAT_TYPE' => $params['TYPE'], 'USER_ID' => $userId));
             IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => $params['TYPE'], "USER_ID" => $userId, "STATUS" => IM_STATUS_READ));
             if ($params['TYPE'] != IM_MESSAGE_OPEN) {
                 CIMContactList::CleanChatCache($userId);
             }
         }
         if ($params['TYPE'] == IM_MESSAGE_OPEN) {
             CIMContactList::CleanAllChatCache();
         }
         if (count($arUserId) > 1 && !$skipUserAdd) {
             self::AddMessage(array("TO_CHAT_ID" => $chatId, "FROM_USER_ID" => $this->user_id, "MESSAGE" => GetMessage("IM_CHAT_JOIN_" . $arUsers[$this->user_id]['gender'], array('#USER_1_NAME#' => htmlspecialcharsback($arUsers[$this->user_id]['name']), '#USER_2_NAME#' => implode(', ', $arUsersName))), "SYSTEM" => 'Y'));
         }
         $generalChatId = self::GetGeneralChatId();
         if ($params['TYPE'] == IM_MESSAGE_OPEN && $generalChatId > 0) {
             $attach = new CIMMessageParamAttach(null, Bitrix\Im\Color::getColor($chatColorCode));
             $attach->AddChat(array("NAME" => $params['TITLE'], "CHAT_ID" => $chatId));
             $attach->AddMessage($params['DESCRIPTION']);
             if ($this->user_id > 0) {
                 $createText = GetMessage("IM_GENERAL_CREATE_BY_USER", array('#USER_NAME#' => htmlspecialcharsback($arUsers[$this->user_id]['name'])));
             } else {
                 $createText = GetMessage("IM_GENERAL_CREATE");
             }
             $ress = self::AddMessage(array("TO_CHAT_ID" => $generalChatId, "FROM_USER_ID" => $this->user_id, "MESSAGE" => $createText, "SYSTEM" => 'Y', "ATTACH" => $attach));
         }
         if ($message) {
             self::AddMessage(array("TO_CHAT_ID" => $chatId, "FROM_USER_ID" => $this->user_id, "MESSAGE" => $message));
         } else {
             if ($params['TYPE'] == IM_MESSAGE_OPEN && !$skipUserAdd) {
                 if ($this->user_id > 0) {
                     $createText = GetMessage("IM_CHAT_CREATE_OPEN_" . $arUsers[$this->user_id]['gender'], array('#USER_NAME#' => htmlspecialcharsback($arUsers[$this->user_id]['name']), '#CHAT_TITLE#' => $params['TITLE']));
                 } else {
                     $createText = GetMessage("IM_CHAT_CREATE_OPEN", array('#CHAT_TITLE#' => $params['TITLE']));
                 }
                 self::AddMessage(array("TO_CHAT_ID" => $chatId, "FROM_USER_ID" => $this->user_id, "MESSAGE" => $createText, "SYSTEM" => 'Y'));
             }
         }
     } else {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_IM_ERROR_EMPTY_USER_OR_CHAT"), "ERROR_OF_CREATE_CHAT");
         return false;
     }
     return $chatId;
 }
Example #2
0
 function __construct($id = null, $color = null)
 {
     $this->result['ID'] = $id ? $id : time();
     $this->result['BLOCKS'] = array();
     if ($color != self::CHAT) {
         if (!$color || !preg_match('/#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\\b/', $color)) {
             $color = Bitrix\Im\Color::getRandomColor();
         }
         $this->result['COLOR'] = $color;
     }
 }