Fixed bug with placeholders and sql functions

This commit is contained in:
Dmitriy Simushev 2012-07-24 16:00:58 +00:00
parent 63ab503077
commit f54acb3ae5
4 changed files with 85 additions and 46 deletions

View File

@ -511,29 +511,27 @@ function setup_chatview_for_operator($thread, $operator)
$page['frequency'] = Settings::get('updatefrequency_chat'); $page['frequency'] = Settings::get('updatefrequency_chat');
} }
function update_thread_access($threadid, $params) /**
{ * Pings the chat thread. Updates 'lastpinguser' (or 'lastpingagent') to current timestamp.
$db = Database::getInstance(); * Sends system messages when operator or user was seen more than $connection_timeout seconds ago
$clause = ""; *
$values = array(); * @global int $kind_for_agent
foreach ($params as $k => $v) { * @global int $state_queue
if (strlen($clause) > 0) * @global int $state_loading
$clause .= ", "; * @global int $state_chatting
$clause .= $k . "=?"; * @global int $state_waiting
$values[] = $v; * @global int $kind_conn
} * @global int $connection_timeout
$values[] = $threadid; * @param array $thread Thread's array
* @param boolean $isuser true for user and false for operator
$db->query( * @param boolean $istyping true if user (or agent) is typing
"update {chatthread} set {$clause} where threadid = ?", */
$values
);
}
function ping_thread($thread, $isuser, $istyping) function ping_thread($thread, $isuser, $istyping)
{ {
global $kind_for_agent, $state_queue, $state_loading, $state_chatting, $state_waiting, $kind_conn, $connection_timeout; global $kind_for_agent, $state_queue, $state_loading, $state_chatting, $state_waiting, $kind_conn, $connection_timeout;
$db = Database::getInstance();
$params = array(($isuser ? "lastpinguser" : "lastpingagent") => "CURRENT_TIMESTAMP", $params = array(($isuser ? "lastpinguser" : "lastpingagent") => "CURRENT_TIMESTAMP",
($isuser ? "userTyping" : "agentTyping") => ($istyping ? "1" : "0")); ($isuser ? "userTyping" : "agentTyping") => ($istyping ? "1" : "0"));
@ -562,19 +560,48 @@ function ping_thread($thread, $isuser, $istyping)
} }
} }
update_thread_access($thread['threadid'], $params); $clause = "";
$values = array();
foreach ($params as $k => $v) {
if (strlen($clause) > 0) {
$clause .= ", ";
}
if (($k == 'lastpinguser' || $k == 'lastpingagent') && $v == 'CURRENT_TIMESTAMP') {
$clause .= $k . " = CURRENT_TIMESTAMP";
}else{
$clause .= $k . "=?";
$values[] = $v;
}
}
$values[] = $thread['threadid'];
$db->query(
"update {chatthread} set {$clause} where threadid = ?",
$values
);
} }
function commit_thread($threadid, $params) function commit_thread($threadid, $params)
{ {
$db = Database::getInstance(); $db = Database::getInstance();
$timestamp_allowed_for = array(
'dtmcreated',
'dtmchatstarted',
'dtmmodified',
'lastpinguser',
'lastpingagent'
);
$query = "update {chatthread} t " . $query = "update {chatthread} t " .
"set lrevision = ?, dtmmodified = CURRENT_TIMESTAMP"; "set lrevision = ?, dtmmodified = CURRENT_TIMESTAMP";
$values = array(next_revision()); $values = array(next_revision());
foreach ($params as $k => $v) { foreach ($params as $k => $v) {
$query .= ", " . $k . "=?"; if (in_array($k, $timestamp_allowed_for) && strcasecmp($v,'CURRENT_TIMESTAMP')) {
$values[] = $v; $query .= ", " . $k . " = CURRENT_TIMESTAMP";
} else {
$query .= ", " . $k . "=?";
$values[] = $v;
}
} }
$query .= " where threadid = ?"; $query .= " where threadid = ?";
$values[] = $threadid; $values[] = $threadid;
@ -598,12 +625,21 @@ function close_thread($thread, $isuser)
{ {
global $state_closed, $kind_events; global $state_closed, $kind_events;
$db = Database::getInstance();
list($message_count) = $db->query(
"SELECT COUNT(*) FROM {chatmessage} WHERE {chatmessage}.threadid = ? AND ikind = 1",
array($thread['threadid']),
array(
'return_rows' => Database::RETURN_ONE_ROW,
'fetch_type' => Database::FETCH_NUM
)
);
if ($thread['istate'] != $state_closed) { if ($thread['istate'] != $state_closed) {
commit_thread( commit_thread(
$thread['threadid'], $thread['threadid'],
array( array(
'istate' => $state_closed, 'istate' => $state_closed,
'messageCount' => "(SELECT COUNT(*) FROM {chatmessage} WHERE {chatmessage}.threadid = t.threadid AND ikind = 1)" 'messageCount' => $message_count
) )
); );
} }

View File

@ -183,21 +183,25 @@ function operator_is_disabled($operator)
function update_operator($operatorid, $login, $email, $password, $localename, $commonname) function update_operator($operatorid, $login, $email, $password, $localename, $commonname)
{ {
$db = Database::getInstance(); $db = Database::getInstance();
$values = array(
':login' => $login,
':localname' => $localename,
':commonname' => $commonname,
':email' => $email,
':jabbername' => '',
':operatorid' => $operatorid
);
if ($password) {
$values[':password'] = md5($password);
}
$db->query( $db->query(
"update {chatoperator} set vclogin = :login, " . "update {chatoperator} set vclogin = :login, " .
($password ? " vcpassword=:password, " : "") . ($password ? " vcpassword=:password, " : "") .
"vclocalename = :localname, vccommonname = :commonname, " . "vclocalename = :localname, vccommonname = :commonname, " .
"vcemail = :email, vcjabbername= :jabbername " . "vcemail = :email, vcjabbername= :jabbername " .
"where operatorid = :operatorid", "where operatorid = :operatorid",
array( $values
':login' => $login,
':password' => $password,
':localname' => $localename,
':commonname' => $commonname,
':email' => $email,
':jabbername' => '',
':operatorid' => $operatorid
)
); );
} }
@ -544,6 +548,7 @@ function get_groups_($checkaway, $operator, $order = NULL)
$orderby = "iweight, vclocalname"; $orderby = "iweight, vclocalname";
} }
$values = array();
$query = "select {chatgroup}.groupid as groupid, {chatgroup}.parent as parent, vclocalname, vclocaldescription, iweight" . $query = "select {chatgroup}.groupid as groupid, {chatgroup}.parent as parent, vclocalname, vclocaldescription, iweight" .
", (SELECT count(*) from {chatgroupoperator} where {chatgroup}.groupid = " . ", (SELECT count(*) from {chatgroupoperator} where {chatgroup}.groupid = " .
"{chatgroupoperator}.groupid) as inumofagents" . "{chatgroupoperator}.groupid) as inumofagents" .
@ -558,17 +563,17 @@ function get_groups_($checkaway, $operator, $order = NULL)
"and {chatgroupoperator}.operatorid = {chatoperator}.operatorid) as ilastseenaway" "and {chatgroupoperator}.operatorid = {chatoperator}.operatorid) as ilastseenaway"
: "" : ""
) . ) .
" from {chatgroup} " . " from {chatgroup} ";
($operator if ($operator) {
? ", (select distinct parent from {chatgroup}, {chatgroupoperator} " . $query .= ", (select distinct parent from {chatgroup}, {chatgroupoperator} " .
"where {chatgroup}.groupid = {chatgroupoperator}.groupid and {chatgroupoperator}.operatorid = ?) i " . "where {chatgroup}.groupid = {chatgroupoperator}.groupid and {chatgroupoperator}.operatorid = ?) i " .
"where {chatgroup}.groupid = i.parent or {chatgroup}.parent = i.parent " "where {chatgroup}.groupid = i.parent or {chatgroup}.parent = i.parent ";
: "" $values[] = $operator['operatorid'];
) . }
" order by " . $orderby; $query .= " order by " . $orderby;
$groups = $db->query( $groups = $db->query(
$query, $query,
array($operator['operatorid']), $values,
array('return_rows' => Database::RETURN_ALL_ROWS) array('return_rows' => Database::RETURN_ALL_ROWS)
); );
return get_sorted_child_groups_($groups); return get_sorted_child_groups_($groups);

View File

@ -48,9 +48,7 @@ if ($query !== false) {
$page['groupName'] = $groupName; $page['groupName'] = $groupName;
$values = array( $values = array(
':query' => "%{$escapedQuery}%", ':query' => "%{$escapedQuery}%"
':kind_user' => $kind_user,
':kind_agent' => $kind_agent
); );
$searchConditions = array(); $searchConditions = array();
@ -58,6 +56,8 @@ if ($query !== false) {
$searchConditions[] = "({chatmessage}.tmessage LIKE :query" . $searchConditions[] = "({chatmessage}.tmessage LIKE :query" .
($searchInSystemMessages?'':" AND ({chatmessage}.ikind = :kind_user OR {chatmessage}.ikind = :kind_agent)") . ($searchInSystemMessages?'':" AND ({chatmessage}.ikind = :kind_user OR {chatmessage}.ikind = :kind_agent)") .
")"; ")";
$values[':kind_user'] = $kind_user;
$values[':kind_agent'] = $kind_agent;
} }
if ($searchType == 'operator' || $searchType == 'all') { if ($searchType == 'operator' || $searchType == 'all') {
$searchConditions[] = "({chatthread}.agentName LIKE :query)"; $searchConditions[] = "({chatthread}.agentName LIKE :query)";

View File

@ -135,9 +135,7 @@ function print_pending_threads($groupids, $since)
"ORDER BY threadid"; "ORDER BY threadid";
$rows = $db->query( $rows = $db->query(
$query, $query,
array( array(':since' => $since),
':since' => $since
),
array('return_rows' => Database::RETURN_ALL_ROWS) array('return_rows' => Database::RETURN_ALL_ROWS)
); );