Beispiel #1
0
 public function Add($arParams)
 {
     global $DB;
     $chatTitle = '';
     if (isset($arParams['TITLE'])) {
         $chatTitle = trim($arParams['TITLE']);
     }
     $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'];
     }
     $type = IM_MESSAGE_CHAT;
     if (isset($arParams['TYPE']) && in_array($arParams['TYPE'], array(IM_MESSAGE_OPEN, IM_MESSAGE_CHAT))) {
         $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);
         }
     }
     $arUserId[$this->user_id] = $this->user_id;
     if (!$skipUserAdd) {
         if (count($arUserId) <= 2) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MIN_USER"), "MIN_USER");
             return false;
         }
         if (count($arUserId) > 300) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MAX_USER", array('#COUNT#' => 300)), "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 = $arUsers['users'];
     $arUsersName = array();
     if ($chatTitle == "") {
         if (IM\Color::isEnabled()) {
             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;
             }
             $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), "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));
             CIMContactList::CleanChatCache($userId);
         }
         if (!$skipUserAdd) {
             $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)));
             self::AddMessage(array("TO_CHAT_ID" => $chatId, "FROM_USER_ID" => $this->user_id, "MESSAGE" => $message, "SYSTEM" => 'Y'));
         }
     } else {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_IM_ERROR_EMPTY_USER_OR_CHAT"), "ERROR_OF_CREATE_CHAT");
         return false;
     }
     return $chatId;
 }
 public static function GetUserColor($id, $gender)
 {
     $code = IM\Color::getCodeByNumber($id);
     if ($gender == 'M') {
         $replaceColor = IM\Color::getReplaceColors();
         if (isset($replaceColor[$code])) {
             $code = $replaceColor[$code];
         }
     }
     return IM\Color::getColor($code);
 }