From 372bae4203f36c106d1d03b14a5c35a0e847034c Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 18 Feb 2013 14:50:07 +0000 Subject: [PATCH] Build Chat JavaScript application options mostly not in template --- src/messenger/webim/client.php | 2 + src/messenger/webim/libs/chat.php | 138 ++++++++++++------ src/messenger/webim/operator/agent.php | 2 + .../styles/dialogs/default/templates/chat.tpl | 35 +---- 4 files changed, 99 insertions(+), 78 deletions(-) diff --git a/src/messenger/webim/client.php b/src/messenger/webim/client.php index ea4a9c4d..1647f9f1 100644 --- a/src/messenger/webim/client.php +++ b/src/messenger/webim/client.php @@ -188,6 +188,8 @@ if( $pparam == "mailthread" ) { $page['additional_css'] = get_additional_css('client_chat_window'); $page['additional_js'] = get_additional_js('client_chat_window'); $page['js_plugin_options'] = get_js_plugin_options('client_chat_window'); + // Build js application options + $page['chatModule'] = json_encode($page['chat']); // Expand page expand("styles/dialogs", getchatstyle(), "chat.tpl"); } else if( $level == "old" ) { diff --git a/src/messenger/webim/libs/chat.php b/src/messenger/webim/libs/chat.php index 6ffbdae5..ad3be149 100644 --- a/src/messenger/webim/libs/chat.php +++ b/src/messenger/webim/libs/chat.php @@ -293,9 +293,20 @@ function setup_chatview(Thread $thread) { $group = array(); } + // Create some empty arrays + $data['chat'] = array( + 'messageForm' => array(), + 'links' => array(), + 'windowsParams' => array(), + 'layoutData' => array() + ); + // Set thread params - $data['chat.thread.id'] = $thread->id; - $data['chat.thread.token'] = $thread->lastToken; + $data['chat']['thread'] = array( + 'id' => $thread->id, + 'token' => $thread->lastToken + ); + $data['chat.title'] = topage( empty($group['vcchattitle']) ? Settings::get('chattitle') @@ -311,12 +322,12 @@ function setup_chatview(Thread $thread) { // Set enter key shortcut if (Settings::get('sendmessagekey') == 'enter') { $data['send_shortcut'] = "Enter"; - $data['ignorectrl'] = 1; + $data['chat']['messageForm']['ignoreCtrl'] = true; } else { $data['send_shortcut'] = is_mac_opera() ? "⌘-Enter" : "Ctrl-Enter"; - $data['ignorectrl'] = 0; + $data['chat']['messageForm']['ignoreCtrl'] = false; } // Set some browser info @@ -328,11 +339,13 @@ function setup_chatview(Thread $thread) { // Load dialogs style options $style_config = get_dialogs_style_config(getchatstyle()); - $data['chatStyles.mailWindowParams'] = $style_config['mail']['window_params']; + $data['chat']['windowsParams']['mail'] + = $style_config['mail']['window_params']; // Load core style options $style_config = get_core_style_config(); - $data['coreStyles.historyWindowParams'] = $style_config['history']['window_params']; + $data['chat']['windowsParams']['history'] + = $style_config['history']['window_params']; return $data; } @@ -351,22 +364,32 @@ function setup_chatview_for_user(Thread $thread, $level) { $data = setup_chatview($thread); - // Set user info - $data['agent'] = false; - $data['user'] = true; - $data['canpost'] = true; $data['level'] = $level; - $data['chat.user.name'] = htmlspecialchars(topage($thread->userName)); - $data['canChangeName'] = Settings::get('usercanchangename') == "1"; + + // Set user info + $data['chat']['layoutData']['user'] = true; + $data['chat']['user'] = array( + 'name' => htmlspecialchars(topage($thread->userName)), + 'canChangeName' => (bool)(Settings::get('usercanchangename') == "1"), + 'defaultName' => (bool)(getstring("chat.default.username") + != $thread->userName), + 'canPost' => true, + 'isAgent' => false + ); $params = "thread=" . $thread->id . "&token=" . $thread->lastToken; // Set link to send mail page - $data['mailLink'] = "$webimroot/client.php?" . $params . "&level=$level&act=mailthread"; + $data['chat']['links']['mail'] = "$webimroot/client.php?" + . $params + . "&level=$level&act=mailthread"; // Set SSL link if (Settings::get('enablessl') == "1" && !is_secure_request()) { - $data['sslLink'] = get_app_location(true, true) . "/client.php?" . $params . "&level=$level"; + $data['chat']['links']['ssl'] = get_app_location(true, true) + . "/client.php?" + . $params + . "&level=$level"; } return $data; @@ -388,53 +411,80 @@ function setup_chatview_for_operator(Thread $thread, $operator) { $data = setup_chatview($thread); // Set operator info - $data['agent'] = true; - $data['user'] = false; - $data['canpost'] = $thread->agentId == $operator['operatorid']; - $data['chat.user.name'] = htmlspecialchars(topage(get_user_name($thread->userName, $thread->remote, $thread->userId))); + $data['chat']['layoutData']['user'] = false; + $data['chat']['user'] = array( + 'name' => htmlspecialchars( + topage( + get_user_name( + $thread->userName, + $thread->remote, + $thread->userId + ) + ) + ), + 'canPost' => (bool)($thread->agentId == $operator['operatorid']), + 'isAgent' => true + ); // Set SSL link if (Settings::get('enablessl') == "1" && !is_secure_request()) { - $data['sslLink'] = get_app_location(true, true) . "/operator/agent.php?thread=" . $thread->id . "&token=" . $thread->lastToken; + $data['chat']['links']['ssl'] = get_app_location(true, true) + . "/operator/agent.php?thread=" + . $thread->id + . "&token=" + . $thread->lastToken; } // Set history window params - $data['historyParams'] = array("userid" => (string)$thread->userId); - $data['historyParamsLink'] = add_params( + $history_link_params = array("userid" => (string)$thread->userId); + $data['chat']['links']['history'] = add_params( $webimroot . "/operator/userhistory.php", - $data['historyParams'] + $history_link_params ); // Set tracking params if (Settings::get('enabletracking')) { $visitor = track_get_visitor_by_threadid($thread->id); - $data['trackedParams'] = array("visitor" => "" . $visitor['visitorid']); - $data['trackedParamsLink'] = add_params($webimroot . "/operator/tracked.php", $data['trackedParams']); - } - - // Get predefined answers - $canned_messages = load_canned_messages($thread->locale, 0); - if ($thread->groupId) { - $canned_messages = array_merge( - load_canned_messages($thread->locale, $thread->groupId), - $canned_messages - ); - }; - - $predefined_answers = array(); - foreach ($canned_messages as $answer) { - $predefined_answers[] = array( - 'short' => htmlspecialchars( - topage($answer['vctitle']?$answer['vctitle']:cutstring($answer['vcvalue'], 97, '...')) - ), - 'full' => myiconv($webim_encoding, getoutputenc(), $answer['vcvalue']) + $tracked_link_params = array("visitor" => "" . $visitor['visitorid']); + $data['chat']['links']['tracked'] = add_params( + $webimroot . "/operator/tracked.php", + $tracked_link_params ); } - $data['predefinedAnswers'] = json_encode($predefined_answers); + // Check if agent can post messages + if ($thread->agentId == $operator['operatorid']) { + // Get predefined answers + $canned_messages = load_canned_messages($thread->locale, 0); + if ($thread->groupId) { + $canned_messages = array_merge( + load_canned_messages($thread->locale, $thread->groupId), + $canned_messages + ); + }; + + $predefined_answers = array(); + foreach ($canned_messages as $answer) { + $predefined_answers[] = array( + 'short' => htmlspecialchars( + topage($answer['vctitle'] + ? $answer['vctitle'] + : cutstring($answer['vcvalue'], 97, '...')) + ), + 'full' => myiconv( + $webim_encoding, + getoutputenc(), + $answer['vcvalue'] + ) + ); + } + $data['chat']['messageForm']['predefinedAnswers'] = $predefined_answers; + } // Set link to user redirection page $params = "thread=" . $thread->id . "&token=" . $thread->lastToken; - $data['redirectLink'] = "$webimroot/operator/agent.php?" . $params . "&act=redirect"; + $data['chat']['links']['redirect'] = "$webimroot/operator/agent.php?" + . $params + . "&act=redirect"; $data['namePostfix'] = ""; diff --git a/src/messenger/webim/operator/agent.php b/src/messenger/webim/operator/agent.php index 6ccd3b54..e92bc5a2 100644 --- a/src/messenger/webim/operator/agent.php +++ b/src/messenger/webim/operator/agent.php @@ -132,6 +132,8 @@ if ($pparam == "redirect") { $page['additional_css'] = get_additional_css('agent_chat_window'); $page['additional_js'] = get_additional_js('agent_chat_window'); $page['js_plugin_options'] = get_js_plugin_options('agent_chat_window'); + // Build js application options + $page['chatModule'] = json_encode($page['chat']); // Expand page expand("../styles/dialogs", getchatstyle(), "chat.tpl"); } diff --git a/src/messenger/webim/styles/dialogs/default/templates/chat.tpl b/src/messenger/webim/styles/dialogs/default/templates/chat.tpl index c3de9a4d..a84a81c7 100644 --- a/src/messenger/webim/styles/dialogs/default/templates/chat.tpl +++ b/src/messenger/webim/styles/dialogs/default/templates/chat.tpl @@ -63,40 +63,7 @@ webimRoot: '${webimroot}', tplRoot: '${tplroot}' }, - chatModule: { - user: { - ${if:user} - name: "${page:chat.user.name}", - canChangeName: ${if:canChangeName}true${else:canChangeName}false${endif:canChangeName}, - defaultName: ("${page:chat.user.name}" == "${msg:chat.default.username}"), - ${endif:user} - canPost: ${if:canpost}true${else:canpost}false${endif:canpost}, - isAgent: ${if:agent}true${else:agent}false${endif:agent} - }, - thread: { - id:${page:chat.thread.id}, - token:${page:chat.thread.token} - }, - messageForm: { - ${if:agent}${if:canpost} - predefinedAnswers: ${page:predefinedAnswers}, - ${endif:canpost}${endif:agent} - ignoreCtrl:${if:ignorectrl}true${else:ignorectrl}false${endif:ignorectrl} - }, - links: { - mail: "${page:mailLink}", - redirect: "${page:redirectLink}", - history: "${page:historyParamsLink}", - ssl: "${page:sslLink}" - }, - windowsParams: { - mail: "${page:chatStyles.mailWindowParams}", - history: "${page:coreStyles.historyWindowParams}" - }, - layoutData: { - user: ${if:user}true${else:user}false${endif:user} - } - }, + chatModule: ${page:chatModule}, plugins: ${page:js_plugin_options} }); });