diff --git a/lhc_web/modules/lhchat/chatwidget.php b/lhc_web/modules/lhchat/chatwidget.php index 685b4d492b..d7b1af3005 100644 --- a/lhc_web/modules/lhchat/chatwidget.php +++ b/lhc_web/modules/lhchat/chatwidget.php @@ -313,113 +313,124 @@ $chat->nick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Visitor'); } - // Store chat - $chat->saveThis(); - - // Assign chat to user - if ( erLhcoreClassModelChatConfig::fetch('track_online_visitors')->current_value == 1 ) { - // To track online users - $userInstance = erLhcoreClassModelChatOnlineUser::handleRequest(array('check_message_operator' => true, 'message_seen_timeout' => erLhcoreClassModelChatConfig::fetch('message_seen_timeout')->current_value, 'vid' => $Params['user_parameters_unordered']['vid'])); - - if ($userInstance !== false) { - $userInstance->chat_id = $chat->id; - $userInstance->dep_id = $chat->dep_id; - $userInstance->message_seen = 1; - $userInstance->message_seen_ts = time(); - $userInstance->saveThis(); - - $chat->online_user_id = $userInstance->id; - $chat->saveThis(); - - if ( erLhcoreClassModelChatConfig::fetch('track_footprint')->current_value == 1) { - erLhcoreClassModelChatOnlineUserFootprint::assignChatToPageviews($userInstance); - } - } - } - - $messageInitial = false; - - // Store message if required - if (isset($startDataFields['message_visible_in_page_widget']) && $startDataFields['message_visible_in_page_widget'] == true) { - if ( $inputData->question != '' && $inputData->key_up_started == false) { - // Store question as message - $msg = new erLhcoreClassModelmsg(); - $msg->msg = trim($inputData->question); - $msg->chat_id = $chat->id; - $msg->user_id = 0; - $msg->time = time(); - erLhcoreClassChat::getSession()->save($msg); - - $messageInitial = $msg; - - $chat->unanswered_chat = 1; - $chat->last_msg_id = $msg->id; - $chat->saveThis(); - } - } - - // Auto responder does not make sense in this mode - if ($inputData->key_up_started == false) { - // Auto responder - $responder = erLhAbstractModelAutoResponder::processAutoResponder($chat); + try { + $db = ezcDbInstance::get(); + $db->beginTransaction(); + + // Store chat + $chat->saveThis(); + + // Assign chat to user + if ( erLhcoreClassModelChatConfig::fetch('track_online_visitors')->current_value == 1 ) { + // To track online users + $userInstance = erLhcoreClassModelChatOnlineUser::handleRequest(array('check_message_operator' => true, 'message_seen_timeout' => erLhcoreClassModelChatConfig::fetch('message_seen_timeout')->current_value, 'vid' => $Params['user_parameters_unordered']['vid'])); - if ($responder instanceof erLhAbstractModelAutoResponder) { - $beforeAutoResponderErrors = array(); - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.before_auto_responder_triggered',array('chat' => & $chat, 'errors' => & $beforeAutoResponderErrors)); - - if (empty($beforeAutoResponderErrors)) { - $chat->wait_timeout = $responder->wait_timeout; - $chat->timeout_message = $responder->timeout_message; - $chat->wait_timeout_send = 1 - $responder->repeat_number; - $chat->wait_timeout_repeat = $responder->repeat_number; - - if ($responder->wait_message != '') { - $msg = new erLhcoreClassModelmsg(); - $msg->msg = trim($responder->wait_message); - $msg->chat_id = $chat->id; - $msg->name_support = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Live Support'); - $msg->user_id = -2; - $msg->time = time() + 5; - erLhcoreClassChat::getSession()->save($msg); - - if ($chat->last_msg_id < $msg->id) { - $chat->last_msg_id = $msg->id; - } - } - - $chat->saveThis(); - - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.auto_responder_triggered', array('chat' => & $chat)); - } else { - $msg = new erLhcoreClassModelmsg(); - $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/adminchat','Auto responder got error').': '.implode('; ', $beforeAutoResponderErrors); - $msg->chat_id = $chat->id; - $msg->user_id = -1; - $msg->time = time(); - - if ($chat->last_msg_id < $msg->id) { - $chat->last_msg_id = $msg->id; - } - - erLhcoreClassChat::getSession()->save($msg); - } + if ($userInstance !== false) { + $userInstance->chat_id = $chat->id; + $userInstance->dep_id = $chat->dep_id; + $userInstance->message_seen = 1; + $userInstance->message_seen_ts = time(); + $userInstance->saveThis(); + + $chat->online_user_id = $userInstance->id; + $chat->saveThis(); + + if ( erLhcoreClassModelChatConfig::fetch('track_footprint')->current_value == 1) { + erLhcoreClassModelChatOnlineUserFootprint::assignChatToPageviews($userInstance); + } + } + } + + $messageInitial = false; + + // Store message if required + if (isset($startDataFields['message_visible_in_page_widget']) && $startDataFields['message_visible_in_page_widget'] == true) { + if ( $inputData->question != '' && $inputData->key_up_started == false) { + // Store question as message + $msg = new erLhcoreClassModelmsg(); + $msg->msg = trim($inputData->question); + $msg->chat_id = $chat->id; + $msg->user_id = 0; + $msg->time = time(); + erLhcoreClassChat::getSession()->save($msg); + + $messageInitial = $msg; + + $chat->unanswered_chat = 1; + $chat->last_msg_id = $msg->id; + $chat->saveThis(); + } } - } else { - $chat->status_sub = erLhcoreClassModelChat::STATUS_SUB_START_ON_KEY_UP; - $chat->saveThis(); + + // Auto responder does not make sense in this mode + if ($inputData->key_up_started == false) { + // Auto responder + $responder = erLhAbstractModelAutoResponder::processAutoResponder($chat); + + if ($responder instanceof erLhAbstractModelAutoResponder) { + $beforeAutoResponderErrors = array(); + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.before_auto_responder_triggered',array('chat' => & $chat, 'errors' => & $beforeAutoResponderErrors)); + + if (empty($beforeAutoResponderErrors)) { + $chat->wait_timeout = $responder->wait_timeout; + $chat->timeout_message = $responder->timeout_message; + $chat->wait_timeout_send = 1 - $responder->repeat_number; + $chat->wait_timeout_repeat = $responder->repeat_number; + + if ($responder->wait_message != '') { + $msg = new erLhcoreClassModelmsg(); + $msg->msg = trim($responder->wait_message); + $msg->chat_id = $chat->id; + $msg->name_support = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Live Support'); + $msg->user_id = -2; + $msg->time = time() + 5; + erLhcoreClassChat::getSession()->save($msg); + + if ($chat->last_msg_id < $msg->id) { + $chat->last_msg_id = $msg->id; + } + } + + $chat->saveThis(); + + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.auto_responder_triggered', array('chat' => & $chat)); + } else { + $msg = new erLhcoreClassModelmsg(); + $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/adminchat','Auto responder got error').': '.implode('; ', $beforeAutoResponderErrors); + $msg->chat_id = $chat->id; + $msg->user_id = -1; + $msg->time = time(); + + if ($chat->last_msg_id < $msg->id) { + $chat->last_msg_id = $msg->id; + } + + erLhcoreClassChat::getSession()->save($msg); + } + } + } else { + $chat->status_sub = erLhcoreClassModelChat::STATUS_SUB_START_ON_KEY_UP; + $chat->saveThis(); + } + + erLhcoreClassChat::updateDepartmentStats($chat->department); + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.chat_started',array('chat' => & $chat, 'msg' => $messageInitial)); + + // Paid chat settings + if (isset($paidChatSettings)) { + erLhcoreClassChatPaid::processPaidChatWorkflow(array( + 'chat' => $chat, + 'paid_chat_params' => $paidChatSettings, + )); + } + + $db->commit(); + + } catch (Exception $e) { + $db->rollback(); + throw $e; } - erLhcoreClassChat::updateDepartmentStats($chat->department); - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.chat_started',array('chat' => & $chat, 'msg' => $messageInitial)); - - // Paid chat settings - if (isset($paidChatSettings)) { - erLhcoreClassChatPaid::processPaidChatWorkflow(array( - 'chat' => $chat, - 'paid_chat_params' => $paidChatSettings, - )); - } - // Redirect user $Result = erLhcoreClassModule::reRun(erLhcoreClassDesign::baseurlRerun('chat/chatwidgetchat') . '/' . $chat->id . '/' . $chat->hash . $modeAppend . $modeAppendTheme . '/(cstarted)/online_chat_started_cb'); return true; diff --git a/lhc_web/modules/lhchat/refreshcustomfields.php b/lhc_web/modules/lhchat/refreshcustomfields.php index f228c39737..127e947aea 100644 --- a/lhc_web/modules/lhchat/refreshcustomfields.php +++ b/lhc_web/modules/lhchat/refreshcustomfields.php @@ -35,18 +35,20 @@ if ( (($checkHash == true && $chat !== false && $chat->hash == $hash) || $checkHash == false) && ( is_object($vid) || ($chat !== false && $chat->status == erLhcoreClassModelChat::STATUS_PENDING_CHAT || $chat->status == erLhcoreClassModelChat::STATUS_ACTIVE_CHAT))) { - erLhcoreClassChatValidator::validateCustomFieldsRefresh($chat); - - $chat->user_typing = time(); - $chat->user_typing_txt = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/refreshcustomfields','Data refreshed'); - $chat->operation_admin .= "lhinst.updateVoteStatus(".$chat->id.");"; - $chat->saveThis(); - - // Force operators to check for new messages - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.data_changed_chat', array( - 'chat' => & $chat - )); - + if ($chat instanceof erLhcoreClassModelChat) + { + erLhcoreClassChatValidator::validateCustomFieldsRefresh($chat); + + $chat->user_typing = time(); + $chat->user_typing_txt = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/refreshcustomfields','Data refreshed'); + $chat->operation_admin .= "lhinst.updateVoteStatus(".$chat->id.");"; + $chat->saveThis(); + + // Force operators to check for new messages + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.data_changed_chat', array( + 'chat' => & $chat + )); + } echo json_encode(array('stored' => 'true')); exit; } diff --git a/lhc_web/modules/lhchat/startchat.php b/lhc_web/modules/lhchat/startchat.php index 60e7cb2e7e..6ef19c5ae5 100644 --- a/lhc_web/modules/lhchat/startchat.php +++ b/lhc_web/modules/lhchat/startchat.php @@ -271,105 +271,116 @@ $chat->nick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Visitor'); } - // Store chat - $chat->saveThis(); - - // Assign chat to user - if ( erLhcoreClassModelChatConfig::fetch('track_online_visitors')->current_value == 1 && (string)$Params['user_parameters_unordered']['vid'] != '') { - // To track online users - $userInstance = erLhcoreClassModelChatOnlineUser::handleRequest(array('message_seen_timeout' => erLhcoreClassModelChatConfig::fetch('message_seen_timeout')->current_value, 'check_message_operator' => true, 'vid' => (string)$Params['user_parameters_unordered']['vid'])); - - if ($userInstance !== false) { - $userInstance->chat_id = $chat->id; - $userInstance->dep_id = $chat->dep_id; - $userInstance->message_seen = 1; - $userInstance->message_seen_ts = time(); - $userInstance->saveThis(); - - $chat->online_user_id = $userInstance->id; - - if ( erLhcoreClassModelChatConfig::fetch('track_footprint')->current_value == 1) { - erLhcoreClassModelChatOnlineUserFootprint::assignChatToPageviews($userInstance); - } - } - } - - $messageInitial = false; + try { + $db = ezcDbInstance::get(); + $db->beginTransaction(); - // Store message if required - if (isset($startDataFields['message_visible_in_popup']) && $startDataFields['message_visible_in_popup'] == true) { - if ( $inputData->question != '' ) { - // Store question as message - $msg = new erLhcoreClassModelmsg(); - $msg->msg = trim($inputData->question); - $msg->chat_id = $chat->id; - $msg->user_id = 0; - $msg->time = time(); - erLhcoreClassChat::getSession()->save($msg); - - $messageInitial = $msg; - - $chat->unanswered_chat = 1; - $chat->last_msg_id = $msg->id; - $chat->saveThis(); - } - } - - // Auto responder - $responder = erLhAbstractModelAutoResponder::processAutoResponder($chat); - - if ($responder instanceof erLhAbstractModelAutoResponder) { - $beforeAutoResponderErrors = array(); - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.before_auto_responder_triggered', array('chat' => & $chat, 'errors' => & $beforeAutoResponderErrors)); - - if (empty($beforeAutoResponderErrors)) { - $chat->wait_timeout = $responder->wait_timeout; - $chat->timeout_message = $responder->timeout_message; - $chat->wait_timeout_send = 1 - $responder->repeat_number; - $chat->wait_timeout_repeat = $responder->repeat_number; - - if ($responder->wait_message != '') { - $msg = new erLhcoreClassModelmsg(); - $msg->msg = trim($responder->wait_message); - $msg->chat_id = $chat->id; - $msg->name_support = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Live Support'); - $msg->user_id = -2; - $msg->time = time() + 5; - erLhcoreClassChat::getSession()->save($msg); - - if ($chat->last_msg_id < $msg->id) { - $chat->last_msg_id = $msg->id; - } - } - - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.auto_responder_triggered', array('chat' => & $chat)); - - $chat->saveThis(); - } else { - $msg = new erLhcoreClassModelmsg(); - $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/adminchat','Auto responder got error').': '.implode('; ', $beforeAutoResponderErrors); - $msg->chat_id = $chat->id; - $msg->user_id = -1; - $msg->time = time(); - - if ($chat->last_msg_id < $msg->id) { - $chat->last_msg_id = $msg->id; - } - - erLhcoreClassChat::getSession()->save($msg); - } - } - - erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.chat_started',array('chat' => & $chat, 'msg' => $messageInitial)); - - erLhcoreClassChat::updateDepartmentStats($chat->department); - - // Paid chat settings - if (isset($paidChatSettings)) { - erLhcoreClassChatPaid::processPaidChatWorkflow(array( - 'chat' => $chat, - 'paid_chat_params' => $paidChatSettings, - )); + // Store chat + $chat->saveThis(); + + // Assign chat to user + if ( erLhcoreClassModelChatConfig::fetch('track_online_visitors')->current_value == 1 && (string)$Params['user_parameters_unordered']['vid'] != '') { + // To track online users + $userInstance = erLhcoreClassModelChatOnlineUser::handleRequest(array('message_seen_timeout' => erLhcoreClassModelChatConfig::fetch('message_seen_timeout')->current_value, 'check_message_operator' => true, 'vid' => (string)$Params['user_parameters_unordered']['vid'])); + + if ($userInstance !== false) { + $userInstance->chat_id = $chat->id; + $userInstance->dep_id = $chat->dep_id; + $userInstance->message_seen = 1; + $userInstance->message_seen_ts = time(); + $userInstance->saveThis(); + + $chat->online_user_id = $userInstance->id; + + if ( erLhcoreClassModelChatConfig::fetch('track_footprint')->current_value == 1) { + erLhcoreClassModelChatOnlineUserFootprint::assignChatToPageviews($userInstance); + } + } + } + + $messageInitial = false; + + // Store message if required + if (isset($startDataFields['message_visible_in_popup']) && $startDataFields['message_visible_in_popup'] == true) { + if ( $inputData->question != '' ) { + // Store question as message + $msg = new erLhcoreClassModelmsg(); + $msg->msg = trim($inputData->question); + $msg->chat_id = $chat->id; + $msg->user_id = 0; + $msg->time = time(); + erLhcoreClassChat::getSession()->save($msg); + + $messageInitial = $msg; + + $chat->unanswered_chat = 1; + $chat->last_msg_id = $msg->id; + $chat->saveThis(); + } + } + + // Auto responder + $responder = erLhAbstractModelAutoResponder::processAutoResponder($chat); + + if ($responder instanceof erLhAbstractModelAutoResponder) { + $beforeAutoResponderErrors = array(); + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.before_auto_responder_triggered', array('chat' => & $chat, 'errors' => & $beforeAutoResponderErrors)); + + if (empty($beforeAutoResponderErrors)) { + $chat->wait_timeout = $responder->wait_timeout; + $chat->timeout_message = $responder->timeout_message; + $chat->wait_timeout_send = 1 - $responder->repeat_number; + $chat->wait_timeout_repeat = $responder->repeat_number; + + if ($responder->wait_message != '') { + $msg = new erLhcoreClassModelmsg(); + $msg->msg = trim($responder->wait_message); + $msg->chat_id = $chat->id; + $msg->name_support = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Live Support'); + $msg->user_id = -2; + $msg->time = time() + 5; + erLhcoreClassChat::getSession()->save($msg); + + if ($chat->last_msg_id < $msg->id) { + $chat->last_msg_id = $msg->id; + } + } + + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.auto_responder_triggered', array('chat' => & $chat)); + + $chat->saveThis(); + } else { + $msg = new erLhcoreClassModelmsg(); + $msg->msg = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/adminchat','Auto responder got error').': '.implode('; ', $beforeAutoResponderErrors); + $msg->chat_id = $chat->id; + $msg->user_id = -1; + $msg->time = time(); + + if ($chat->last_msg_id < $msg->id) { + $chat->last_msg_id = $msg->id; + } + + erLhcoreClassChat::getSession()->save($msg); + } + } + + erLhcoreClassChatEventDispatcher::getInstance()->dispatch('chat.chat_started',array('chat' => & $chat, 'msg' => $messageInitial)); + + erLhcoreClassChat::updateDepartmentStats($chat->department); + + // Paid chat settings + if (isset($paidChatSettings)) { + erLhcoreClassChatPaid::processPaidChatWorkflow(array( + 'chat' => $chat, + 'paid_chat_params' => $paidChatSettings, + )); + } + + $db->commit(); + + } catch (Exception $e) { + $db->rollback(); + throw $e; } // Redirect user