From 177c3f22a7a34a935bae7bb81126e823df6860bf Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 26 Jan 2012 16:00:30 +0000 Subject: [PATCH] Cleaned up code, related to canned messages --- src/messenger/webim/libs/canned.php | 93 +++++++++++++++++++++ src/messenger/webim/libs/chat.php | 33 ++------ src/messenger/webim/operator/agent.php | 1 + src/messenger/webim/operator/canned.php | 33 +------- src/messenger/webim/operator/cannededit.php | 35 +------- 5 files changed, 106 insertions(+), 89 deletions(-) create mode 100644 src/messenger/webim/libs/canned.php diff --git a/src/messenger/webim/libs/canned.php b/src/messenger/webim/libs/canned.php new file mode 100644 index 00000000..197fd069 --- /dev/null +++ b/src/messenger/webim/libs/canned.php @@ -0,0 +1,93 @@ + '', 'vcvalue' => $answer); + } + if (count($result) > 0) { + $updatequery = "insert into ${mysqlprefix}chatresponses (vcvalue,locale,groupid) values "; + for ($i = 0; $i < count($result); $i++) { + if ($i > 0) { + $updatequery .= ", "; + } + $updatequery .= "('" . db_escape_string($result[$i]['vcvalue'], $link) . "','$locale', NULL)"; + } + perform_query($updatequery, $link); + $query = "select id, vcvalue from ${mysqlprefix}chatresponses " . + "where locale = '" . $locale . "' AND (groupid is NULL OR groupid = 0) order by vcvalue"; + $result = select_multi_assoc($query, $link); + } + close_connection($link); + return $result; +} + +function load_canned_message($key) +{ + global $mysqlprefix; + $link = connect(); + $result = select_one_row("select vcvalue from ${mysqlprefix}chatresponses where id = $key", $link); + close_connection($link); + return $result ? $result['vcvalue'] : null; +} + +function save_canned_message($key, $message) +{ + global $mysqlprefix; + $link = connect(); + perform_query("update ${mysqlprefix}chatresponses set vcvalue = '" . db_escape_string($message, $link) . "' " . + "where id = $key", $link); + close_connection($link); +} + +function add_canned_message($locale, $groupid, $message) +{ + global $mysqlprefix; + $link = connect(); + perform_query("insert into ${mysqlprefix}chatresponses (locale,groupid,vcvalue) values ('$locale'," . + ($groupid ? "$groupid, " : "null, ") . + "'" . db_escape_string($message, $link) . "')", $link); + close_connection($link); +} + +?> \ No newline at end of file diff --git a/src/messenger/webim/libs/chat.php b/src/messenger/webim/libs/chat.php index 57b802fa..44f310ba 100644 --- a/src/messenger/webim/libs/chat.php +++ b/src/messenger/webim/libs/chat.php @@ -399,31 +399,6 @@ function setup_chatview_for_user($thread, $level) $page['frequency'] = $settings['updatefrequency_chat']; } -function load_canned_messages($locale, $groupid) -{ - global $mysqlprefix; - $link = connect(); - $result = select_multi_assoc( - "select vcvalue from ${mysqlprefix}chatresponses where locale = '$locale' " . - "AND (groupid is NULL OR groupid = 0) order by vcvalue", $link); - if (count($result) == 0) { - foreach (explode("\n", getstring_('chat.predefined_answers', $locale)) as $answer) { - $result[] = array('vcvalue' => $answer); - } - } - if ($groupid) { - $result2 = select_multi_assoc( - "select vcvalue from ${mysqlprefix}chatresponses where locale = '$locale' " . - "AND groupid = $groupid order by vcvalue", $link); - foreach ($result as $r) { - $result2[] = $r; - } - $result = $result2; - } - close_connection($link); - return $result; -} - function setup_chatview_for_operator($thread, $operator) { global $page, $webimroot, $company_logo_link, $company_name, $settings; @@ -462,7 +437,13 @@ function setup_chatview_for_operator($thread, $operator) close_connection($link); } $predefinedres = ""; - $canned_messages = load_canned_messages($thread['locale'], $thread['groupid']); + $canned_messages = load_canned_messages($thread['locale'], 0); + if ($thread['groupid']) { + $canned_messages = array_merge( + load_canned_messages($thread['locale'], $thread['groupid']), + $canned_messages + ); + }; foreach ($canned_messages as $answer) { $predefinedres .= ""; } diff --git a/src/messenger/webim/operator/agent.php b/src/messenger/webim/operator/agent.php index a48d4d8a..a7cf7a39 100644 --- a/src/messenger/webim/operator/agent.php +++ b/src/messenger/webim/operator/agent.php @@ -19,6 +19,7 @@ * Evgeny Gryaznov - initial API and implementation */ +require_once('../libs/canned.php'); require_once('../libs/common.php'); require_once('../libs/chat.php'); require_once('../libs/groups.php'); diff --git a/src/messenger/webim/operator/canned.php b/src/messenger/webim/operator/canned.php index 49b1a503..a050a937 100644 --- a/src/messenger/webim/operator/canned.php +++ b/src/messenger/webim/operator/canned.php @@ -19,6 +19,7 @@ * Evgeny Gryaznov - initial API and implementation */ +require_once('../libs/canned.php'); require_once('../libs/common.php'); require_once('../libs/operator.php'); require_once('../libs/settings.php'); @@ -33,38 +34,6 @@ loadsettings(); $errors = array(); $page = array(); -function load_canned_messages($locale, $groupid) -{ - global $mysqlprefix; - $link = connect(); - $query = "select id, vcvalue from ${mysqlprefix}chatresponses " . - "where locale = '" . $locale . "' AND (" . - ($groupid - ? "groupid = $groupid" - : "groupid is NULL OR groupid = 0") . - ") order by vcvalue"; - - $result = select_multi_assoc($query, $link); - if (!$groupid && count($result) == 0) { - foreach (explode("\n", getstring_('chat.predefined_answers', $locale)) as $answer) { - $result[] = array('id' => '', 'vcvalue' => $answer); - } - if (count($result) > 0) { - $updatequery = "insert into ${mysqlprefix}chatresponses (vcvalue,locale,groupid) values "; - for ($i = 0; $i < count($result); $i++) { - if ($i > 0) { - $updatequery .= ", "; - } - $updatequery .= "('" . db_escape_string($result[$i]['vcvalue'], $link) . "','$locale', NULL)"; - } - perform_query($updatequery, $link); - $result = select_multi_assoc($query, $link); - } - } - close_connection($link); - return $result; -} - # locales $all_locales = get_available_locales(); diff --git a/src/messenger/webim/operator/cannededit.php b/src/messenger/webim/operator/cannededit.php index e8d1049d..c3d33cf2 100644 --- a/src/messenger/webim/operator/cannededit.php +++ b/src/messenger/webim/operator/cannededit.php @@ -19,38 +19,11 @@ * Evgeny Gryaznov - initial API and implementation */ +require_once('../libs/canned.php'); require_once('../libs/common.php'); require_once('../libs/operator.php'); require_once('../libs/pagination.php'); -function load_message($key) -{ - global $mysqlprefix; - $link = connect(); - $result = select_one_row("select vcvalue from ${mysqlprefix}chatresponses where id = $key", $link); - close_connection($link); - return $result ? $result['vcvalue'] : null; -} - -function save_message($key, $message) -{ - global $mysqlprefix; - $link = connect(); - perform_query("update ${mysqlprefix}chatresponses set vcvalue = '" . db_escape_string($message, $link) . "' " . - "where id = $key", $link); - close_connection($link); -} - -function add_message($locale, $groupid, $message) -{ - global $mysqlprefix; - $link = connect(); - perform_query("insert into ${mysqlprefix}chatresponses (locale,groupid,vcvalue) values ('$locale'," . - ($groupid ? "$groupid, " : "null, ") . - "'" . db_escape_string($message, $link) . "')", $link); - close_connection($link); -} - $operator = check_login(); loadsettings(); @@ -60,7 +33,7 @@ $errors = array(); $page = array(); if ($stringid) { - $message = load_message($stringid); + $message = load_canned_message($stringid); if (!$message) { $errors[] = getlocal("cannededit.no_such"); $stringid = ""; @@ -80,9 +53,9 @@ if (isset($_POST['message'])) { if (count($errors) == 0) { if ($stringid) { - save_message($stringid, $message); + save_canned_message($stringid, $message); } else { - add_message($page['locale'], $page['groupid'], $message); + add_canned_message($page['locale'], $page['groupid'], $message); } $page['saved'] = true; prepare_menu($operator, false);