Exemplo n.º 1
0
         if (isset($statusCommand['info'])) {
             $tpl = erLhcoreClassTemplate::getInstance('lhchat/lists/assistance_message.tpl.php');
             $tpl->set('msg', array('msg' => $statusCommand['info'], 'time' => time()));
             $returnBody = $tpl->fetch();
         }
     }
 }
 if ($ignoreMessage == false) {
     $msg = new erLhcoreClassModelmsg();
     $msg->msg = $msgText;
     $msg->chat_id = $Params['user_parameters']['chat_id'];
     $msg->user_id = $messageUserId;
     $msg->time = time();
     $msg->name_support = $userData->name_support;
     if ($messageUserId != -1 && $Chat->chat_locale != '' && $Chat->chat_locale_to != '') {
         erLhcoreClassTranslate::translateChatMsgOperator($Chat, $msg);
     }
     erLhcoreClassChat::getSession()->save($msg);
     // Set last message ID
     if ($Chat->last_msg_id < $msg->id) {
         $stmt = $db->prepare('UPDATE lh_chat SET status = :status, user_status = :user_status, last_msg_id = :last_msg_id WHERE id = :id');
         $stmt->bindValue(':id', $Chat->id, PDO::PARAM_INT);
         $stmt->bindValue(':last_msg_id', $msg->id, PDO::PARAM_INT);
         if ($userData->invisible_mode == 0 && $messageUserId > 0) {
             // Change status only if it's not internal command
             if ($Chat->status == erLhcoreClassModelChat::STATUS_PENDING_CHAT) {
                 $Chat->status = erLhcoreClassModelChat::STATUS_ACTIVE_CHAT;
             }
         }
         // Chat can be reopened only if user did not ended chat explictly
         if ($Chat->user_status == erLhcoreClassModelChat::USER_STATUS_CLOSED_CHAT && $Chat->status_sub != erLhcoreClassModelChat::STATUS_SUB_USER_CLOSED_CHAT) {
 /**
  * Executed in erLhcoreClassExtensionXmppserviceHandler::handleMessageFromOperator send message to user from operator.
  * if provided message is a command to operator is send command response
  *
  * @param erLhcoreClassModelChat $chat            
  *
  * @param erLhcoreClassModelXMPPAccount $xmppUser            
  *
  * @param string $body            
  *
  * @throws Exception
  */
 public static function sendMessageToChat(erLhcoreClassModelChat $chat, erLhcoreClassModelXMPPAccount $xmppUser, $body)
 {
     $db = ezcDbInstance::get();
     $db->beginTransaction();
     try {
         $user = $xmppUser->user;
         $messageUserId = $user->id;
         $ignoreMessage = false;
         // Predefine
         $statusCommand = array('processed' => false, 'process_status' => '');
         if (strpos(trim($body), '!') === 0) {
             $statusCommand = erLhcoreClassChatCommand::processCommand(array('no_ui_update' => true, 'msg' => $body, 'chat' => &$chat));
             if ($statusCommand['processed'] === true) {
                 $messageUserId = -1;
                 // Message was processed set as internal message
                 $body = '[b]' . $userData->name_support . '[/b]: ' . $body . ' ' . ($statusCommand['process_status'] != '' ? '|| ' . $statusCommand['process_status'] : '');
             }
             if (isset($statusCommand['ignore']) && $statusCommand['ignore'] == true) {
                 $ignoreMessage = true;
             }
             if (isset($statusCommand['info'])) {
                 $xmppService = erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionXmppservice');
                 $xmppService->sendMessageToOperatorAsUserByChat(array('xmpp_account_operator' => $xmppUser, 'chat' => $chat, 'msg' => '[[System Assistant]] ' . $statusCommand['info']));
             }
         }
         if ($ignoreMessage == false) {
             $msg = new erLhcoreClassModelmsg();
             $msg->msg = $body;
             $msg->chat_id = $chat->id;
             $msg->user_id = $messageUserId;
             $msg->time = time();
             $msg->name_support = $user->name_support;
             if ($messageUserId > 0 && $chat->chat_locale != '' && $chat->chat_locale_to != '') {
                 erLhcoreClassTranslate::translateChatMsgOperator($chat, $msg);
             }
             erLhcoreClassChat::getSession()->save($msg);
             // Set last message ID
             if ($chat->last_msg_id < $msg->id) {
                 $userChange = '';
                 // Assign operator if chat does not have one
                 if ($chat->user_id == 0) {
                     $userChange = ',user_id = :user_id';
                 }
                 $stmt = $db->prepare("UPDATE lh_chat SET status = :status, user_status = :user_status, last_msg_id = :last_msg_id{$userChange} WHERE id = :id");
                 $stmt->bindValue(':id', $chat->id, PDO::PARAM_INT);
                 $stmt->bindValue(':last_msg_id', $msg->id, PDO::PARAM_INT);
                 $changeStatus = false;
                 if ($user->invisible_mode == 0) {
                     if ($chat->status == erLhcoreClassModelChat::STATUS_PENDING_CHAT) {
                         $chat->status = erLhcoreClassModelChat::STATUS_ACTIVE_CHAT;
                         $changeStatus = true;
                     }
                 }
                 if ($chat->user_status == erLhcoreClassModelChat::USER_STATUS_CLOSED_CHAT) {
                     $chat->user_status = erLhcoreClassModelChat::USER_STATUS_PENDING_REOPEN;
                     if (($onlineuser = $chat->online_user) !== false) {
                         $onlineuser->reopen_chat = 1;
                         $onlineuser->saveThis();
                     }
                 }
                 $stmt->bindValue(':user_status', $chat->user_status, PDO::PARAM_INT);
                 $stmt->bindValue(':status', $chat->status, PDO::PARAM_INT);
                 if ($userChange != '') {
                     $stmt->bindValue(':user_id', $msg->user_id, PDO::PARAM_INT);
                 }
                 $stmt->execute();
             }
             // If chat status changes update statistic
             if ($changeStatus == true) {
                 if ($chat->department !== false) {
                     erLhcoreClassChat::updateDepartmentStats($chat->department);
                 }
                 erLhcoreClassChat::updateActiveChats($chat->user_id);
             }
         }
         $db->commit();
         // Inform operator about command status
         if ($statusCommand['processed'] == true && $statusCommand['process_status'] != '') {
             $xmppService = erLhcoreClassModule::getExtensionInstance('erLhcoreClassExtensionXmppservice');
             $xmppService->sendMessageToOperatorAsUserByChat(array('xmpp_account_operator' => $xmppUser, 'chat' => $chat, 'msg' => '[[System Assistant]] ' . $statusCommand['process_status']));
         }
         // For nodejs plugin
         erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.desktop_client_admin_msg', array('msg' => &$msg, 'chat' => &$chat));
         // For general listeners
         erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.web_add_msg_admin', array('msg' => &$msg, 'chat' => &$chat));
     } catch (Exception $e) {
         $db->rollback();
         throw $e;
     }
 }