コード例 #1
0
ファイル: im_chat.php プロジェクト: andy-profi/bxApiDocs
 public function AddUser($chatId, $userId)
 {
     global $DB;
     $chatId = intval($chatId);
     if ($chatId <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_CHAT_ID"), "EMPTY_CHAT_ID");
         return false;
     }
     $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 (count($arUserId) <= 0) {
         $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_ID"), "EMPTY_USER_ID");
         return false;
     }
     if ($this->user_id > 0 && !IsModuleInstalled('intranet') && CModule::IncludeModule('socialnetwork') && CSocNetUser::IsFriendsAllowed()) {
         $arFriendUsers = array();
         $dbFriends = CSocNetUserRelations::GetList(array(), array("USER_ID" => $this->user_id, "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 = $this->user_id == $arFriends["FIRST_USER_ID"] ? $arFriends["SECOND_USER_ID"] : $arFriends["FIRST_USER_ID"];
             $arFriendUsers[$friendId] = $friendId;
         }
         foreach ($arUserId as $id => $userId) {
             if ($userId == $this->user_id) {
                 continue;
             }
             if (!isset($arFriendUsers[$userId]) && CIMSettings::GetPrivacy(CIMSettings::PRIVACY_CHAT, $userId) == CIMSettings::PRIVACY_RESULT_CONTACT) {
                 unset($arUserId[$id]);
             }
         }
         if (count($arUserId) <= 0) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_ID_BY_PRIVACY"), "EMPTY_USER_ID_BY_PRIVACY");
             return false;
         }
     }
     $strSql = "\n\t\t\tSELECT\n\t\t\t\tR.CHAT_ID, C.TITLE CHAT_TITLE, C.AUTHOR_ID CHAT_AUTHOR_ID, C.EXTRANET CHAT_EXTRANET, C.TYPE CHAT_TYPE\n\t\t\tFROM b_im_relation R\n\t\t\tLEFT JOIN b_im_chat C ON R.CHAT_ID = C.ID\n\t\t\tWHERE\n\t\t\t\t" . ($this->user_id > 0 ? "R.USER_ID = " . $this->user_id . " AND " : "") . "\n\t\t\t\tR.MESSAGE_TYPE IN ('" . IM_MESSAGE_OPEN . "','" . IM_MESSAGE_CHAT . "')\n\t\t\t\tAND R.CHAT_ID = " . $chatId . "\n\t\t";
     $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
     if ($arRes = $dbRes->Fetch()) {
         $extranetFlag = $arRes["CHAT_EXTRANET"] == "" ? "" : ($arRes["CHAT_EXTRANET"] == "Y" ? true : false);
         $chatTitle = $arRes['CHAT_TITLE'];
         $chatAuthorId = intval($arRes['CHAT_AUTHOR_ID']);
         $chatType = intval($arRes['CHAT_TYPE']);
         $arRelation = self::GetRelationById($chatId);
         $arExistUser = array();
         foreach ($arRelation as $relation) {
             $arExistUser[] = $relation['USER_ID'];
         }
         if (count($arRelation) + count($arUserId) > 500) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_MAX_USER", array('#COUNT#' => 500)), "MAX_USER");
             return false;
         }
         $arUserId = array_diff($arUserId, $arExistUser);
         if (empty($arUserId)) {
             $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_NOTHING_TO_ADD"), "NOTHING_TO_ADD");
             return false;
         }
         $arUserSelect = $arUserId;
         if ($this->user_id > 0) {
             $arUserSelect[] = $this->user_id;
         }
         $arUsers = CIMContactList::GetUserData(array('ID' => array_values($arUserSelect), 'DEPARTMENT' => 'N', 'USE_CACHE' => 'N'));
         $arUsers = $arUsers['users'];
         if ($extranetFlag !== true) {
             $isExtranet = false;
             foreach ($arUsers as $userData) {
                 if ($userData['extranet']) {
                     $isExtranet = true;
                     break;
                 }
             }
             if ($isExtranet || $extranetFlag === "") {
                 IM\ChatTable::update($chatId, array('EXTRANET' => $isExtranet ? "Y" : "N"));
             }
             $extranetFlag = $isExtranet;
         }
         $arUsersName = array();
         foreach ($arUserId as $userId) {
             $arUsersName[] = htmlspecialcharsback($arUsers[$userId]['name']);
         }
         if ($this->user_id > 0) {
             $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)));
         } else {
             if (count($arUsersName) > 1) {
                 $message = GetMessage("IM_CHAT_SELF_JOIN", array('#USERS_NAME#' => implode(', ', $arUsersName)));
             } else {
                 $arUserList = array_values($arUserId);
                 $message = GetMessage("IM_CHAT_SELF_JOIN_" . $arUsers[$arUserList[0]]['gender'], array('#USER_NAME#' => implode(', ', $arUsersName)));
             }
         }
         $fileMaxId = CIMDisk::GetMaxFileId($chatId);
         $maxId = 0;
         $strSql = "SELECT MAX(ID) ID FROM b_im_message WHERE CHAT_ID = " . $chatId . " GROUP BY CHAT_ID";
         $dbRes = $DB->Query($strSql, false, "File: " . __FILE__ . "<br>Line: " . __LINE__);
         if ($arMax = $dbRes->Fetch()) {
             $maxId = $arMax['ID'];
         }
         $update = array();
         $publicPullWatch = false;
         if ($chatType == IM_MESSAGE_OPEN && CModule::IncludeModule("pull")) {
             $publicPullWatch = true;
         }
         foreach ($arUserId as $userId) {
             if ($publicPullWatch) {
                 CPullWatch::Delete($userId, 'IM_PUBLIC_' . $chatId);
             }
             CIMContactList::SetRecent(array('ENTITY_ID' => $chatId, 'MESSAGE_ID' => $maxId, 'CHAT_TYPE' => $arRes['CHAT_TYPE'], 'USER_ID' => $userId));
             $hideHistory = CIMSettings::GetStartChatMessage() == CIMSettings::START_MESSAGE_LAST && $arRes['CHAT_TYPE'] == IM_MESSAGE_CHAT;
             if ($arRes['CHAT_TYPE'] != IM_MESSAGE_PRIVATE && $arUsers[$userId]['extranet']) {
                 $hideHistory = true;
             }
             $orm = IM\RelationTable::add(array("CHAT_ID" => $chatId, "MESSAGE_TYPE" => $arRes['CHAT_TYPE'], "USER_ID" => $userId, "START_ID" => $hideHistory ? $maxId + 1 : 0, "LAST_ID" => $maxId, "LAST_SEND_ID" => $maxId, "LAST_FILE_ID" => $hideHistory ? $fileMaxId : 0));
             $update[] = $orm->getId();
             CIMContactList::CleanChatCache($userId);
         }
         CIMDisk::ChangeFolderMembers($chatId, $arUserId);
         if (CModule::IncludeModule("pull")) {
             foreach ($arRelation as $ar) {
                 CPullStack::AddByUser($ar['USER_ID'], array('module_id' => 'im', 'command' => 'chatUserAdd', 'params' => array('chatId' => $chatId, 'chatTitle' => $chatTitle, 'chatOwner' => $chatAuthorId, 'chatExtranet' => $extranetFlag, 'users' => $arUsers, 'newUsers' => $arUserId)));
             }
         }
         $lastId = self::AddMessage(array("TO_CHAT_ID" => $chatId, "MESSAGE" => $message, "FROM_USER_ID" => $this->user_id, "SYSTEM" => 'Y'));
         if (IsModuleInstalled('replica')) {
             if ($lastId && CIMSettings::GetStartChatMessage() == CIMSettings::START_MESSAGE_LAST && $arRes['CHAT_TYPE'] == IM_MESSAGE_CHAT) {
                 foreach ($update as $relId) {
                     IM\RelationTable::update($relId, array('START_ID' => $lastId));
                 }
             }
         }
         return true;
     }
     $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_AUTHORIZE_ERROR"), "AUTHORIZE_ERROR");
     return false;
 }