Skip to content

Commit

Permalink
Add transactions for chat start and check if chat object exists
Browse files Browse the repository at this point in the history
  • Loading branch information
remdex committed May 17, 2017
1 parent 4a90e21 commit f913db4
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 213 deletions.
217 changes: 114 additions & 103 deletions lhc_web/modules/lhchat/chatwidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 14 additions & 12 deletions lhc_web/modules/lhchat/refreshcustomfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit f913db4

Please sign in to comment.