From e01a75faea6d5c738781fa1d43cde4edef463b15 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 29 May 2013 14:18:21 +0000 Subject: [PATCH] Add an ability to start chat with specified operator --- src/messenger/webim/client.php | 21 ++++- src/messenger/webim/install/dbinfo.php | 4 +- src/messenger/webim/install/dbperform.php | 4 + src/messenger/webim/libs/chat.php | 27 ++++++- .../webim/libs/classes/thread_processor.php | 1 + src/messenger/webim/libs/getcode.php | 21 ++++- src/messenger/webim/libs/operator.php | 77 ++++++++++++++----- src/messenger/webim/locales/en/properties | 7 ++ src/messenger/webim/locales/ru/properties | 7 ++ src/messenger/webim/operator/getcode.php | 12 ++- src/messenger/webim/operator/operator.php | 11 ++- src/messenger/webim/view/agent.php | 8 ++ src/messenger/webim/view/gen_button.php | 20 +++-- 13 files changed, 181 insertions(+), 39 deletions(-) diff --git a/src/messenger/webim/client.php b/src/messenger/webim/client.php index d8831db1..8ec20acf 100644 --- a/src/messenger/webim/client.php +++ b/src/messenger/webim/client.php @@ -92,6 +92,14 @@ if( !isset($_GET['token']) || !isset($_GET['thread']) ) { } } + // Get operator code + $operator_code = empty($_GET['operator_code']) + ? '' + : $_GET['operator_code']; + if (! preg_match("/^[A-z0-9_]+$/", $operator_code)) { + $operator_code = false; + } + // Get visitor info $visitor = visitor_from_request(); $info = getgetparam('info'); @@ -132,8 +140,17 @@ if( !isset($_GET['token']) || !isset($_GET['thread']) ) { $visitor_is_invited = false; } + // Get operator info + $requested_operator = false; + if ($operator_code) { + $requested_operator = operator_by_code($operator_code); + } + // Check if survey should be displayed - if(Settings::get('enablepresurvey') == '1' && !$visitor_is_invited) { + if(Settings::get('enablepresurvey') == '1' + && !$visitor_is_invited + && !$requested_operator + ) { // Display prechat survey $page = array_merge_recursive( setup_logo($group), @@ -145,7 +162,7 @@ if( !isset($_GET['token']) || !isset($_GET['thread']) ) { } // Start chat thread - $thread = chat_start_for_user($groupid, $visitor['id'], $visitor['name'], $referrer, $info); + $thread = chat_start_for_user($groupid, $requested_operator, $visitor['id'], $visitor['name'], $referrer, $info); } $threadid = $thread->id; $token = $thread->lastToken; diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php index e75b0b85..8d5d6e70 100644 --- a/src/messenger/webim/install/dbinfo.php +++ b/src/messenger/webim/install/dbinfo.php @@ -128,6 +128,8 @@ $dbtables = array( "iperm" => "int DEFAULT 0", /* Do not grant all privileges by default */ "dtmrestore" => "int NOT NULL DEFAULT 0", "vcrestoretoken" => "varchar(64)", + // Use to start chat with specified operator + "code" => "varchar(64) DEFAULT ''" ), "${mysqlprefix}chatoperatorstatistics" => array( @@ -249,7 +251,7 @@ $dbtables_can_update = array( "${mysqlprefix}requestbuffer" => array("requestid", "requestkey", "request"), "${mysqlprefix}chatmessage" => array("agentId"), "${mysqlprefix}indexedchatmessage" => array(), - "${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"), + "${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken", "code"), "${mysqlprefix}chatoperatorstatistics" => array("sentinvitations", "acceptedinvitations", "rejectedinvitations", "ignoredinvitations"), "${mysqlprefix}chatban" => array(), "${mysqlprefix}chatgroup" => array("vcemail", "iweight", "parent", "vctitle", "vcchattitle", "vclogo", "vchosturl"), diff --git a/src/messenger/webim/install/dbperform.php b/src/messenger/webim/install/dbperform.php index 96dcfe00..db3a4338 100644 --- a/src/messenger/webim/install/dbperform.php +++ b/src/messenger/webim/install/dbperform.php @@ -184,6 +184,10 @@ if ($act == "silentcreateall") { runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD vcrestoretoken varchar(64)", $link); } + if (in_array("${mysqlprefix}chatoperator.code", $absent_columns)) { + runsql("ALTER TABLE ${mysqlprefix}chatoperator ADD code varchar(64) DEFAULT ''", $link); + } + if (in_array("${mysqlprefix}chatoperatorstatistics.sentinvitations", $absent_columns)) { runsql("ALTER TABLE ${mysqlprefix}chatoperatorstatistics ADD sentinvitations int NOT NULL DEFAULT 0", $link); } diff --git a/src/messenger/webim/libs/chat.php b/src/messenger/webim/libs/chat.php index e4629cae..095df339 100644 --- a/src/messenger/webim/libs/chat.php +++ b/src/messenger/webim/libs/chat.php @@ -625,12 +625,13 @@ function get_remote_host() * * @global string $current_locale Current locale code * @param int $group_id Id of group related to thread + * @param array $requested_operator Array of requested operator info * @param string $visitor_id Id of the visitor * @param string $visitor_name Name of the visitor * @param string $referrer Page user came from * @param string $info User info */ -function chat_start_for_user($group_id, $visitor_id, $visitor_name, $referrer, $info) { +function chat_start_for_user($group_id, $requested_operator, $visitor_id, $visitor_name, $referrer, $info) { global $current_locale; // Get user info @@ -651,6 +652,14 @@ function chat_start_for_user($group_id, $visitor_id, $visitor_name, $referrer, $ } } + // Get info about requested operator + $requested_operator_online = false; + if ($requested_operator) { + $requested_operator_online = is_operator_online( + $requested_operator['operatorid'] + ); + } + // Get thread object if ($is_invited) { // Get thread from invitation @@ -663,6 +672,9 @@ function chat_start_for_user($group_id, $visitor_id, $visitor_name, $referrer, $ // Create thread $thread = Thread::create(); $thread->state = Thread::STATE_LOADING; + if ($requested_operator && $requested_operator_online) { + $thread->nextAgent = $requested_operator['operatorid']; + } } // Update thread fields @@ -695,8 +707,17 @@ function chat_start_for_user($group_id, $visitor_id, $visitor_name, $referrer, $ getstring2('chat.came.from',array($referrer)) ); } - - $thread->postMessage(Thread::KIND_INFO, getstring('chat.wait')); + if ($requested_operator && !$requested_operator_online) { + $thread->postMessage( + Thread::KIND_INFO, + getstring2( + 'chat.requested_operator.offline', + array(get_operator_name($requested_operator)) + ) + ); + } else { + $thread->postMessage(Thread::KIND_INFO, getstring('chat.wait')); + } } // TODO: May be move sending this message somewhere else? diff --git a/src/messenger/webim/libs/classes/thread_processor.php b/src/messenger/webim/libs/classes/thread_processor.php index a97d85ff..51a67b6e 100644 --- a/src/messenger/webim/libs/classes/thread_processor.php +++ b/src/messenger/webim/libs/classes/thread_processor.php @@ -479,6 +479,7 @@ class ThreadProcessor extends ClientSideProcessor { // Initialize dialog $thread = chat_start_for_user( $group_id, + false, $visitor['id'], $visitor['name'], $referrer, diff --git a/src/messenger/webim/libs/getcode.php b/src/messenger/webim/libs/getcode.php index 412b9e68..e4f2a48b 100644 --- a/src/messenger/webim/libs/getcode.php +++ b/src/messenger/webim/libs/getcode.php @@ -15,7 +15,7 @@ * limitations under the License. */ -function generate_button($title, $locale, $style, $invitationstyle, $group, $inner, $showhost, $forcesecure, $modsecurity) +function generate_button($title, $locale, $style, $invitationstyle, $group, $inner, $showhost, $forcesecure, $modsecurity, $operator_code) { global $visitorcookie; $app_location = get_app_location($showhost, $forcesecure); @@ -29,8 +29,23 @@ function generate_button($title, $locale, $style, $invitationstyle, $group, $inn $modsecfix = $modsecurity ? ".replace('http://','').replace('https://','')" : ""; $jslink = append_query("'" . $link, "url='+escape(document.location.href$modsecfix)+'&referrer='+escape(document.referrer$modsecfix)"); + $popup_options = "toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1"; + + // Generate operator code field + if ($operator_code) { + $form_on_submit = "if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 " . + "&& window.event.preventDefault) window.event.preventDefault();" . + "this.newWindow = window.open({$jslink} + '&operator_code=' + document.getElementById('mibewOperatorCodeField').value, 'webim', '{$popup_options}');" . + "this.newWindow.focus();this.newWindow.opener=window;return false;"; + $temp = '
' . + '' . + '
'; + return "" . $temp . ""; + } + + // Generate button $temp = get_popup($link, "$jslink", - $inner, $title, "webim", "toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1"); + $inner, $title, "webim", $popup_options); if (Settings::get('enabletracking')) { $widget_data = array(); @@ -55,7 +70,7 @@ function generate_button($title, $locale, $style, $invitationstyle, $group, $inn $widget_data['visitorCookieName'] = $visitorcookie; // Build additional button code - $temp = preg_replace('/^(' . '