Automatically hide invitation popup

Invitation popup will hide if invitation canceled by operator or
closed by timeout.
This commit is contained in:
Dmitriy Simushev 2013-05-24 10:05:20 +00:00
parent 43d731cf72
commit f7d6971849
8 changed files with 77 additions and 50 deletions

View File

@ -47,7 +47,9 @@ if (get_remote_level($_SERVER['HTTP_USER_AGENT']) == 'old') {
exit;
}
if (verifyparam("act", "/^(invitation)$/", "default") == 'invitation') {
if (verifyparam("act", "/^(invitation)$/", "default") == 'invitation'
&& Settings::get('enabletracking')
) {
// Check if user invited to chat
$invitation_state = invitation_state($_SESSION['visitorid']);

View File

@ -13,5 +13,5 @@ this.loadScript(f));for(var h in b)b.hasOwnProperty(h)&&(h in this.handlersDepen
a.Widget.prototype.loadScript=function(b){var c=this,a=this.doLoadScript(this.requestedScripts[b].url,b);a.onload=function(){c.scriptReady(b)};a.onreadystatechange=function(){("complete"==this.readyState||"loaded"==this.readyState)&&c.scriptReady(b)}};a.Widget.prototype.doLoadScript=function(b,c){var a=document.createElement("script");a.setAttribute("type","text/javascript");a.setAttribute("src",b);a.setAttribute("id",c);document.getElementsByTagName("head")[0].appendChild(a);return a};a.Widget.prototype.scriptReady=
function(b){this.requestedScripts[b].status="ready";for(var a in this.handlers)this.handlers.hasOwnProperty(a)&&this.canRunHandler(a)&&(this.handlers[a](),delete this.handlers[a])};a.Widget.prototype.canRunHandler=function(b){b=this.handlersDependences[b];for(var a=0;a<b.length;a++)if("ready"!=this.requestedScripts[b[a]].status)return!1;return!0};a.Widget.init=function(b){a.Objects.widget=new a.Widget(b);a.Objects.widget.makeRequest()};a.Utils={};a.Utils.createCookie=function(b,a){var d=/([^\.]+\.[^\.]+)$/.exec(document.location.hostname)[1];
document.cookie=""+b+"="+a+"; path=/; "+(d?"domain="+d+";":"")};a.Utils.readCookie=function(b){var a=document.cookie.split("; ");b+="=";for(var d=!1,e=0;e<a.length;e++)if(-1!=a[e].indexOf(b)){d=a[e].substr(b.length);break}return d};a.APIFunctions={};a.APIFunctions.updateUserId=function(b){a.Utils.createCookie(a.Objects.widget.visitorCookieName,b.user.id)};a.APIFunctions.inviteOnResponse=function(b){var a=b.invitation.operator,d=b.invitation.avatar,e=b.invitation.url;b='<div id="mibewinvitationpopup" style="display: none;"><div id="mibewinvitationclose"><a href="javascript:void(0);" onclick="Mibew.Invitation.reject();">&times;</a></div>';
a&&(b+='<h1 onclick="Mibew.Invitation.accept();">'+a+"</h1>");d&&(b+='<img id="mibewinvitationavatar" src="'+d+'" title="'+a+'" alt="'+a+'" onclick="Mibew.Invitation.accept();" />');e&&(b+='<iframe id="mibewinvitationframe" src="'+e+'" onload="Mibew.Invitation.show();"></iframe>');b+='<div style="clear: both;"></div></div>';if(a=document.getElementById("mibewinvitation"))a.innerHTML=b};a.Invitation={};a.Invitation.hide=function(){var a=document.getElementById("mibewinvitationpopup");a&&a.parentNode.removeChild(a)};
a.Invitation.show=function(){var a=document.getElementById("mibewinvitationpopup");a&&(a.style.display="block")};a.Invitation.accept=function(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),a.Invitation.hide())};a.Invitation.reject=function(){a.Objects.widget.sendToServer({invitation_rejected:1});a.Invitation.hide()}})(Mibew);
a&&(b+='<h1 onclick="Mibew.Invitation.accept();">'+a+"</h1>");d&&(b+='<img id="mibewinvitationavatar" src="'+d+'" title="'+a+'" alt="'+a+'" onclick="Mibew.Invitation.accept();" />');e&&(b+='<iframe id="mibewinvitationframe" src="'+e+'" onload="Mibew.Invitation.show();"></iframe>');b+='<div style="clear: both;"></div></div>';if(a=document.getElementById("mibewinvitation"))a.innerHTML=b};a.APIFunctions.inviteOnClose=function(){a.Invitation.hide()};a.Invitation={};a.Invitation.hide=function(){var a=
document.getElementById("mibewinvitationpopup");a&&a.parentNode.removeChild(a)};a.Invitation.show=function(){var a=document.getElementById("mibewinvitationpopup");a&&(a.style.display="block")};a.Invitation.accept=function(){document.getElementById("mibewAgentButton")&&(document.getElementById("mibewAgentButton").onclick(),a.Invitation.hide())};a.Invitation.reject=function(){a.Objects.widget.sendToServer({invitation_rejected:1});a.Invitation.hide()}})(Mibew);

View File

@ -418,6 +418,13 @@ var Mibew = {};
}
}
/**
* Close invitation popup
*/
Mibew.APIFunctions.inviteOnClose = function() {
Mibew.Invitation.hide();
}
/**
* @namespace Holds invitation stuff
*/

View File

@ -859,9 +859,40 @@ Class Thread {
* @param boolean $is_user Boolean TRUE if user initiate thread closing or boolean FALSE otherwise
*/
public function close($is_user) {
$db = Database::getInstance();
// Send message about closing
if ($is_user) {
$this->postMessage(
self::KIND_EVENTS,
getstring2_(
"chat.status.user.left",
array($this->userName),
$this->locale
)
);
} else {
if ($this->state == self::STATE_INVITED) {
$this->postMessage(
self::KIND_FOR_AGENT,
getstring_(
"chat.visitor.invitation.canceled",
$this->locale
)
);
} else {
$this->postMessage(
self::KIND_EVENTS,
getstring2_(
"chat.status.operator.left",
array($this->agentName),
$this->locale
)
);
}
}
// Get messages count
$db = Database::getInstance();
list($message_count) = $db->query(
"SELECT COUNT(*) FROM {chatmessage} WHERE {chatmessage}.threadid = :threadid AND ikind = :kind_user",
array(
@ -880,15 +911,6 @@ Class Thread {
$this->messageCount = $message_count;
$this->save();
}
// Send message about closing
$message = '';
if ($is_user) {
$message = getstring2_("chat.status.user.left", array($this->userName), $this->locale);
} else {
$message = getstring2_("chat.status.operator.left", array($this->agentName), $this->locale);
}
$this->postMessage(self::KIND_EVENTS, $message);
}
/**

View File

@ -59,8 +59,9 @@ function invitation_state($visitor_id) {
function invitation_invite($visitor_id, $operator) {
global $current_locale;
// The visitor already invited
if (invitation_check($visitor_id)) {
// Check if visitor already invited
$invitation_state = invitation_state($visitor_id);
if ($invitation_state['invited']) {
return false;
}
@ -127,35 +128,6 @@ function invitation_invite($visitor_id, $operator) {
return $thread;
}
/**
* Check if visitor already invited
*
* @param int $visitor_id ID of the visitor to check
* @return int|boolean ID of the operator who invite the visitor or boolean
* false if the visitor was not invited.
*/
function invitation_check($visitor_id) {
$db = Database::getInstance();
$result = $db->query(
"SELECT t.agentId AS invitedby " .
"FROM {chatsitevisitor} v, {chatthread} t " .
"WHERE v.threadid = t.threadid " .
"AND v.visitorid = :visitor_id " .
// Check only for new invitations. Refactor this place later
"AND v.lasttime < t.dtmcreated " .
"AND t.istate = :state_invited " .
"AND t.invitationstate = :invitation_wait ",
array(
':visitor_id' => $visitor_id,
':state_invited' => Thread::STATE_INVITED,
':invitation_wait' => Thread::INVITATION_WAIT
),
array('return_rows' => Database::RETURN_ONE_ROW)
);
return empty($result['invitedby']) ? false : $result['invitedby'];
}
/**
* Invitation was accepted by visitor
*

View File

@ -81,6 +81,7 @@ chat.thread.state_wait_for_another_agent=Waiting for operator
chat.visitor.email=E-Mail: {0}
chat.visitor.info=Info: {0}
chat.visitor.invitation.accepted=Visitor accepted invitation from operator {0}
chat.visitor.invitation.canceled=Operator canceled invitation
chat.visitor.invitation.rejected=Visitor rejected invitation
chat.visitor.invitation.sent=Operator {0} invites visitor at {1} page
chat.wait=Thank you for contacting us. An operator will be with you shortly.

View File

@ -81,6 +81,7 @@ chat.thread.state_wait_for_another_agent=
chat.visitor.email=E-Mail: {0}
chat.visitor.info=О Посетителе: {0}
chat.visitor.invitation.accepted=Посетитель принял приглашение от оператора {0}
chat.visitor.invitation.canceled=Îïåðàòîð îòìåíèë ïðèãëàøåíèå
chat.visitor.invitation.rejected=Посетитель отклонил приглашение
chat.visitor.invitation.sent=Оператор {0} пригласил посетителя на странице {1}
chat.wait=Пожалуйста, подождите немного, к Вам присоединится оператор..

View File

@ -33,7 +33,7 @@ if (Settings::get('enabletracking') == '1') {
if (isset($_SESSION['visitorid'])
&& preg_match('/^[0-9]+$/', $_SESSION['visitorid'])) {
// Session started. Track visitor
$invited = invitation_check($_SESSION['visitorid']);
$invitation_state = invitation_state($_SESSION['visitorid']);
$visitorid = track_visitor($_SESSION['visitorid'], $entry, $referer);
$visitor = track_get_visitor_by_id($visitorid);
} else {
@ -42,7 +42,7 @@ if (Settings::get('enabletracking') == '1') {
// Session not started but visitor exist in database.
// Probably third-party cookies disabled by the browser.
// Use tracking by local cookie at target site
$invited = invitation_check($visitor['visitorid']);
$invitation_state = invitation_state($visitor['visitorid']);
$visitorid = track_visitor($visitor['visitorid'], $entry, $referer);
} else {
// Start tracking session
@ -62,21 +62,43 @@ if (Settings::get('enabletracking') == '1') {
$response['data']['user']['id'] = $visitor['userid'];
}
// Check if invitation closed
if (! $invitation_state['invited']
&& ! empty($_SESSION['invitation_threadid'])
) {
$response['handlers'][] = 'inviteOnClose';
$response['dependences']['inviteOnClose'] = array();
unset($_SESSION['invitation_threadid']);
}
// Check if visitor just invited to chat
if ($invited !== FALSE) {
$operator = operator_by_id($invited);
$response['handlers'][] = 'inviteOnResponse';
$response['dependences']['inviteOnResponse'] = array();
$is_invited = $invitation_state['invited']
&& (empty($_SESSION['invitation_threadid'])
? true
: ($_SESSION['invitation_threadid'] != $invitation_state['threadid']));
if ($is_invited) {
// Load invitation thread
$thread = Thread::load($invitation_state['threadid']);
// Get operator info
$operator = operator_by_id($thread->agentId);
$locale = isset($_GET['locale']) ? $_GET['locale'] : '';
$operatorName = ($locale == $home_locale)
? $operator['vclocalename']
: $operator['vccommonname'];
// Show invitation dialog at widget side
$response['handlers'][] = 'inviteOnResponse';
$response['dependences']['inviteOnResponse'] = array();
$response['data']['invitation'] = array(
'operator' => htmlspecialchars($operatorName),
'avatar' => htmlspecialchars($operator['vcavatar']),
'url' => get_app_location(true, is_secure_request())
. '/client.php?act=invitation'
);
$_SESSION['invitation_threadid'] = $thread->id;
}
// Check if visitor reject invitation