public static function SetSetting($type, $value, $userId = false) { if (!in_array($type, array(self::SETTINGS, self::NOTIFY))) { return false; } global $USER, $USER_FIELD_MANAGER; $userId = intval($userId); if ($userId == 0) { $userId = $USER->GetId(); } $arSettings = CUserOptions::GetOption('IM', $type, array(), $userId); foreach ($value as $key => $val) { $arSettings[$key] = $val; } if (isset($value[self::STATUS])) { CIMStatus::Set($userId, array('STATUS' => $value[self::STATUS])); } $arDefault = self::GetDefaultSettings($type); foreach ($arSettings as $key => $val) { if (isset($arDefault[$key]) && $arDefault[$key] == $val) { if ($key == self::PRIVACY_SEARCH) { $USER_FIELD_MANAGER->Update("USER", $userId, array('UF_IM_SEARCH' => '')); } unset($value[$key]); } } CUserOptions::SetOption('IM', $type, $arSettings, false, $userId); if (isset($value[self::PRIVACY_SEARCH])) { $USER_FIELD_MANAGER->Update("USER", $userId, array('UF_IM_SEARCH' => $value[self::PRIVACY_SEARCH])); } self::ClearCache($userId); return true; }
public static function SetColor($userId, $color) { CIMStatus::Set($userId, array('COLOR' => $color)); if (defined("BX_COMP_MANAGED_CACHE")) { global $CACHE_MANAGER; $CACHE_MANAGER->ClearByTag('IM_CONTACT_LIST'); } }
public static function SetMobile($userId, $result = true) { $date = null; if ($result) { $date = new Bitrix\Main\Type\DateTime(); } CIMStatus::Set($userId, array('MOBILE_LAST_DATE' => $date)); }
public static function SetIdle($userId, $result) { $date = null; if ($result) { $date = new Bitrix\Main\Type\DateTime(); $date->add('-10 MINUTE'); } CIMStatus::Set($userId, array('IDLE' => $date)); }
public function GetLastMessage($toChatId, $fromUserId = false, $loadExtraData = false, $bTimeZone = true, $limit = true) { global $DB; $fromUserId = IntVal($fromUserId); if ($fromUserId <= 0) { $fromUserId = $this->user_id; } $toChatId = IntVal($toChatId); if ($toChatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_CHAT_ID"), "ERROR_TO_CHAT_ID"); return false; } $orm = IM\ChatTable::getById($toChatId); if (!($chatData = $orm->fetch())) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_CHAT_NOT_EXISTS"), "ERROR_CHAT_NOT_EXISTS"); return false; } if ($limit) { $dbType = strtolower($DB->type); if ($dbType == "mysql") { $sqlLimit = " AND M.DATE_CREATE > DATE_SUB(NOW(), INTERVAL 30 DAY)"; } else { if ($dbType == "mssql") { $sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())"; } else { if ($dbType == "oracle") { $sqlLimit = " AND M.DATE_CREATE > SYSDATE-30"; } } } } $limitById = ''; $ar = \CIMChat::GetRelationById($toChatId, $fromUserId); if ($ar && $ar['START_ID'] > 0) { $limitById = 'AND M.ID >= ' . intval($ar['START_ID']); } if (!$bTimeZone) { CTimeZone::Disable(); } if ($chatData['TYPE'] == IM_MESSAGE_OPEN) { $strSql = "\n\t\t\t\tSELECT\n\t\t\t\t\tM.ID,\n\t\t\t\t\tM.CHAT_ID,\n\t\t\t\t\tM.MESSAGE,\n\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\tM.AUTHOR_ID,\n\t\t\t\t\tC.TYPE CHAT_TYPE,\n\t\t\t\t\tR.USER_ID RID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_chat C ON C.ID = M.CHAT_ID AND C.TYPE = '" . IM_MESSAGE_OPEN . "'\n\t\t\t\tLEFT JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID AND R.USER_ID = " . $fromUserId . "\n\t\t\t\tWHERE\n\t\t\t\t\tM.CHAT_ID = " . $toChatId . "\n\t\t\t\t\t" . $limitById . "\n\t\t\t\t\t#LIMIT#\n\t\t\t\tORDER BY M.DATE_CREATE DESC\n\t\t\t"; } else { $strSql = "\n\t\t\t\tSELECT\n\t\t\t\t\tM.ID,\n\t\t\t\t\tM.CHAT_ID,\n\t\t\t\t\tM.MESSAGE,\n\t\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\t\tM.AUTHOR_ID,\n\t\t\t\t\tC.TYPE CHAT_TYPE,\n\t\t\t\t\tR1.USER_ID RID\n\t\t\t\tFROM b_im_message M\n\t\t\t\tINNER JOIN b_im_relation R1 ON M.CHAT_ID = R1.CHAT_ID\n\t\t\t\tINNER JOIN b_im_chat C ON C.ID = M.CHAT_ID AND C.TYPE = '" . IM_MESSAGE_CHAT . "'\n\t\t\t\tWHERE\n\t\t\t\t\tR1.CHAT_ID = " . $toChatId . " AND R1.USER_ID = " . $fromUserId . "\n\t\t\t\t\t" . $limitById . "\n\t\t\t\t\t#LIMIT#\n\t\t\t\tORDER BY M.DATE_CREATE DESC\n\t\t\t"; } $strSql = $DB->TopSql($strSql, 20); if (!$bTimeZone) { CTimeZone::Enable(); } if ($limit) { $dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if (!$dbRes->SelectedRowsCount()) { $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } else { $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } CIMStatus::Set($fromUserId, array('IDLE' => null)); $chatType = IM_MESSAGE_CHAT; $chatRelationUserId = 0; $arMessages = array(); $arMessageId = array(); $arUsersMessage = array(); $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink ? "N" : "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink ? "N" : "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); while ($arRes = $dbRes->Fetch()) { $chatType = $arRes['CHAT_TYPE']; $chatRelationUserId = intval($arRes['RID']); $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE']))); $arMessageId[] = $arRes['ID']; $arUsersMessage[$arRes['CHAT_ID']][] = $arRes['ID']; } if ($chatType == IM_MESSAGE_OPEN && $chatRelationUserId <= 0) { if (IM\User::getInstance($fromUserId)->isExtranet()) { $arMessages = array(); $arMessageId = array(); $arUsersMessage = array(); $loadExtraData = false; } else { if (CModule::IncludeModule('pull')) { CPullWatch::Add($fromUserId, 'IM_PUBLIC_' . $toChatId, true); } } } $params = CIMMessageParam::Get($arMessageId); $arFiles = array(); foreach ($params as $messageId => $param) { $arMessages[$messageId]['params'] = $param; if (isset($param['FILE_ID'])) { foreach ($param['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } } $arChatFiles = CIMDisk::GetFiles($toChatId, $arFiles); $arResult = array('chatId' => $toChatId, 'message' => $arMessages, 'usersMessage' => $arUsersMessage, 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'files' => $arChatFiles); if (is_array($loadExtraData) || is_bool($loadExtraData) && $loadExtraData == true) { $bDepartment = true; if (is_array($loadExtraData) && $loadExtraData['DEPARTMENT'] == 'N') { $bDepartment = false; } $arChat = self::GetChatData(array('ID' => $toChatId, 'USE_CACHE' => 'N')); if ($arChat['chat'][$toChatId]['messageType'] == IM_MESSAGE_OPEN || in_array($fromUserId, $arChat['userInChat'][$toChatId])) { $arResult['userInChat'] = $arChat['userInChat']; $arResult['userChatBlockStatus'] = $arChat['userChatBlockStatus']; $ar = CIMContactList::GetUserData(array('ID' => $arChat['userInChat'][$toChatId], 'DEPARTMENT' => $bDepartment ? 'Y' : 'N', 'USE_CACHE' => 'N')); $arResult['users'] = $ar['users']; $arResult['userInGroup'] = $ar['userInGroup']; $arResult['woUserInGroup'] = $ar['woUserInGroup']; if ($arChat['chat'][$toChatId]['extranet'] === "") { $isExtranet = false; foreach ($ar['users'] as $userData) { if ($userData['extranet']) { $isExtranet = true; break; } } IM\ChatTable::update($toChatId, array('EXTRANET' => $isExtranet ? "Y" : "N")); $arChat['chat'][$toChatId]['extranet'] = $isExtranet; } $arResult['chat'] = $arChat['chat']; } } return $arResult; }
function GetLastMessage($toUserId, $fromUserId = false, $loadUserData = false, $bTimeZone = true, $limit = true) { global $DB; $fromUserId = IntVal($fromUserId); if ($fromUserId <= 0) $fromUserId = $this->user_id; $toUserId = IntVal($toUserId); if ($toUserId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_USER_ID"), "ERROR_TO_USER_ID"); return false; } $chatId = 0; $startId = 0; $arMessages = Array(); $arUsersMessage = Array(); $arMessageId = Array(); if (!$bTimeZone) CTimeZone::Disable(); $strSql =" SELECT R1.CHAT_ID, R1.START_ID, R2.LAST_ID, ".$DB->DatetimeToTimestampFunction('R2.LAST_READ')." LAST_READ FROM b_im_relation R1 INNER JOIN b_im_relation R2 on R2.CHAT_ID = R1.CHAT_ID WHERE R1.USER_ID = ".$fromUserId." AND R1.MESSAGE_TYPE = '".IM_MESSAGE_PRIVATE."' AND R2.USER_ID = ".$toUserId." "; if (!$bTimeZone) CTimeZone::Enable(); $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__); if ($arRes = $dbRes->Fetch()) { $chatId = intval($arRes['CHAT_ID']); $startId = intval($arRes['START_ID']); $lastId = intval($arRes['LAST_ID']); $lastRead = intval($arRes['LAST_READ']); } if ($chatId > 0) { if ($limit) { $dbType = strtolower($DB->type); if ($dbType== "mysql") $sqlLimit = " AND M.DATE_CREATE > DATE_SUB(NOW(), INTERVAL 30 DAY)"; else if ($dbType == "mssql") $sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())"; else if ($dbType == "oracle") $sqlLimit = " AND M.DATE_CREATE > SYSDATE-30"; } if (!$bTimeZone) CTimeZone::Disable(); $strSql =" SELECT M.ID, M.CHAT_ID, M.MESSAGE, ".$DB->DatetimeToTimestampFunction('M.DATE_CREATE')." DATE_CREATE, M.AUTHOR_ID, M.NOTIFY_EVENT FROM b_im_message M WHERE M.CHAT_ID = ".$chatId." #LIMIT# ORDER BY DATE_CREATE DESC, ID DESC "; $strSql = $DB->TopSql($strSql, 20); if (!$bTimeZone) CTimeZone::Enable(); if ($limit) { $dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql), false, "File: ".__FILE__."<br>Line: ".__LINE__); } else { $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: ".__FILE__."<br>Line: ".__LINE__); } CIMStatus::Set($fromUserId, Array('IDLE' => null)); $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink? "N": "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink? "N": "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); while ($arRes = $dbRes->Fetch()) { if ($arRes['ID'] < $startId) continue; if ($fromUserId == $arRes['AUTHOR_ID']) { $arRes['TO_USER_ID'] = $toUserId; $arRes['FROM_USER_ID'] = $fromUserId; $convId = $arRes['TO_USER_ID']; } else { $arRes['TO_USER_ID'] = $fromUserId; $arRes['FROM_USER_ID'] = $toUserId; $convId = $arRes['FROM_USER_ID']; } $arMessages[$arRes['ID']] = Array( 'id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['FROM_USER_ID'], 'recipientId' => $arRes['TO_USER_ID'], 'system' => $arRes['NOTIFY_EVENT'] == 'private'? 'N': 'Y', 'date' => $arRes['DATE_CREATE'], 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE'])) ); $arMessageId[] = $arRes['ID']; $arUsersMessage[$convId][] = $arRes['ID']; } } $params = CIMMessageParam::Get($arMessageId); $arFiles = Array(); foreach ($params as $messageId => $param) { $arMessages[$messageId]['params'] = $param; if (isset($param['FILE_ID'])) { foreach ($param['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } } $arChatFiles = CIMDisk::GetFiles($chatId, $arFiles); $arResult = Array( 'chatId' => $chatId, 'message' => $arMessages, 'usersMessage' => $arUsersMessage, 'users' => Array(), 'userInGroup' => Array(), 'woUserInGroup' => Array(), 'files' => $arChatFiles ); if ($lastRead > 0) { $arResult['readedList'][$toUserId] = Array( 'messageId' => $lastId, 'date' => $lastRead, ); } if (is_array($loadUserData) || is_bool($loadUserData) && $loadUserData == true) { $bDepartment = true; if (is_array($loadUserData) && $loadUserData['DEPARTMENT'] == 'N') $bDepartment = false; $ar = CIMContactList::GetUserData(array( 'ID' => Array($fromUserId, $toUserId), 'DEPARTMENT' => ($bDepartment? 'Y': 'N'), 'USE_CACHE' => 'N', 'PHONES' => IsModuleInstalled('voximplant')? 'Y': 'N' ) ); $arResult['users'] = $ar['users']; $arResult['userInGroup'] = $ar['userInGroup']; $arResult['woUserInGroup'] = $ar['woUserInGroup']; $arResult['phones'] = $ar['phones']; } return $arResult; }
function GetLastMessage($toChatId, $fromUserId = false, $loadExtraData = false, $bTimeZone = true, $limit = true) { global $DB; $fromUserId = IntVal($fromUserId); if ($fromUserId <= 0) { $fromUserId = $this->user_id; } $toChatId = IntVal($toChatId); if ($toChatId <= 0) { $GLOBALS["APPLICATION"]->ThrowException(GetMessage("IM_ERROR_EMPTY_CHAT_ID"), "ERROR_TO_CHAT_ID"); return false; } if ($limit) { $dbType = strtolower($DB->type); if ($dbType == "mysql") { $sqlLimit = " AND M.DATE_CREATE > DATE_SUB(NOW(), INTERVAL 30 DAY)"; } else { if ($dbType == "mssql") { $sqlLimit = " AND M.DATE_CREATE > dateadd(day, -30, getdate())"; } else { if ($dbType == "oracle") { $sqlLimit = " AND M.DATE_CREATE > SYSDATE-30"; } } } } if (!$bTimeZone) { CTimeZone::Disable(); } $strSql = "\n\t\t\tSELECT\n\t\t\t\tM.ID,\n\t\t\t\tM.CHAT_ID,\n\t\t\t\tM.MESSAGE,\n\t\t\t\t" . $DB->DatetimeToTimestampFunction('M.DATE_CREATE') . " DATE_CREATE,\n\t\t\t\tM.AUTHOR_ID\n\t\t\tFROM b_im_message M\n\t\t\tINNER JOIN b_im_relation R1 ON R1.START_ID <= M.ID AND M.CHAT_ID = R1.CHAT_ID\n\t\t\tWHERE R1.CHAT_ID = " . $toChatId . " AND R1.USER_ID = " . $fromUserId . " #LIMIT#\n\t\t\tORDER BY M.DATE_CREATE DESC\n\t\t"; $strSql = $DB->TopSql($strSql, 20); if (!$bTimeZone) { CTimeZone::Enable(); } if ($limit) { $dbRes = $DB->Query(str_replace("#LIMIT#", $sqlLimit, $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); if (!$dbRes->SelectedRowsCount()) { $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } } else { $dbRes = $DB->Query(str_replace("#LIMIT#", "", $strSql), false, "File: " . __FILE__ . "<br>Line: " . __LINE__); } CIMStatus::Set($fromUserId, array('IDLE' => null)); $arMessages = array(); $arMessageId = array(); $arUsersMessage = array(); $CCTP = new CTextParser(); $CCTP->MaxStringLen = 200; $CCTP->allow = array("HTML" => "N", "ANCHOR" => $this->bHideLink ? "N" : "Y", "BIU" => "Y", "IMG" => "N", "QUOTE" => "N", "CODE" => "N", "FONT" => "N", "LIST" => "N", "SMILES" => $this->bHideLink ? "N" : "Y", "NL2BR" => "Y", "VIDEO" => "N", "TABLE" => "N", "CUT_ANCHOR" => "N", "ALIGN" => "N"); while ($arRes = $dbRes->Fetch()) { $arMessages[$arRes['ID']] = array('id' => $arRes['ID'], 'chatId' => $arRes['CHAT_ID'], 'senderId' => $arRes['AUTHOR_ID'], 'recipientId' => $arRes['CHAT_ID'], 'date' => $arRes['DATE_CREATE'], 'text' => $CCTP->convertText(htmlspecialcharsbx($arRes['MESSAGE']))); $arMessageId[] = $arRes['ID']; $arUsersMessage[$arRes['CHAT_ID']][] = $arRes['ID']; } $params = CIMMessageParam::Get($arMessageId); $arFiles = array(); foreach ($params as $messageId => $param) { $arMessages[$messageId]['params'] = $param; if (isset($param['FILE_ID'])) { foreach ($param['FILE_ID'] as $fileId) { $arFiles[$fileId] = $fileId; } } } $arChatFiles = CIMDisk::GetFiles($toChatId, $arFiles); $arResult = array('chatId' => $toChatId, 'message' => $arMessages, 'usersMessage' => $arUsersMessage, 'users' => array(), 'userInGroup' => array(), 'woUserInGroup' => array(), 'files' => $arChatFiles); if (is_array($loadExtraData) || is_bool($loadExtraData) && $loadExtraData == true) { $bDepartment = true; if (is_array($loadExtraData) && $loadExtraData['DEPARTMENT'] == 'N') { $bDepartment = false; } $arChat = self::GetChatData(array('ID' => $toChatId, 'USE_CACHE' => 'N')); if (in_array($fromUserId, $arChat['userInChat'][$toChatId])) { $arResult['chat'] = $arChat['chat']; $arResult['userInChat'] = $arChat['userInChat']; $arResult['userChatBlockStatus'] = $arChat['userChatBlockStatus']; $ar = CIMContactList::GetUserData(array('ID' => $arChat['userInChat'][$toChatId], 'DEPARTMENT' => $bDepartment ? 'Y' : 'N', 'USE_CACHE' => 'N')); $arResult['users'] = $ar['users']; $arResult['userInGroup'] = $ar['userInGroup']; $arResult['woUserInGroup'] = $ar['woUserInGroup']; } } return $arResult; }