From 0cdf90e24a1912427d76bc24e2c8146255ba7b24 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Mon, 2 Jun 2014 10:32:51 +0000 Subject: [PATCH] Replace "operator/redirect.php" with a controller --- .../Chat/Operator/RedirectController.php | 187 ++++++++++++++++++ src/mibew/libs/operator.php | 4 +- src/mibew/libs/routing.yml | 6 + src/mibew/operator/redirect.php | 128 ------------ 4 files changed, 195 insertions(+), 130 deletions(-) create mode 100644 src/mibew/libs/classes/Mibew/Controller/Chat/Operator/RedirectController.php delete mode 100644 src/mibew/operator/redirect.php diff --git a/src/mibew/libs/classes/Mibew/Controller/Chat/Operator/RedirectController.php b/src/mibew/libs/classes/Mibew/Controller/Chat/Operator/RedirectController.php new file mode 100644 index 00000000..d436ac8d --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Controller/Chat/Operator/RedirectController.php @@ -0,0 +1,187 @@ +query->get('thread'); + if (!preg_match("/^\d{1,10}$/", $thread_id)) { + throw new BadRequestException('Wrong value of "thread" argument.'); + } + + // Get and validate token + $token = $request->query->get('token'); + if (!preg_match("/^\d{1,10}$/", $token)) { + throw new BadRequestException('Wrong value of "token" argument.'); + } + + $thread = Thread::load($thread_id, $token); + if (!$thread) { + throw new BadRequestException('Wrong thread.'); + } + + $page = array( + 'errors' => array(), + ); + + if ($request->query->has('nextGroup')) { + // The thread was redirected to a group. + $next_id = $request->query->get('nextGroup'); + if (!preg_match("/^\d{1,10}$/", $next_id)) { + throw new BadRequestException('Wrong value of "nextGroup" argument.'); + } + $next_group = group_by_id($next_id); + + if ($next_group) { + $page['message'] = getlocal2( + 'chat.redirected.group.content', + array(get_group_name($next_group)) + ); + if (!$this->redirectToGroup($thread, $next_id)) { + $page['errors'][] = getlocal('chat.redirect.cannot'); + } + } else { + $page['errors'][] = 'Unknown group'; + } + } else { + // The thread was redirected to an operator. + $next_id = $request->query->get('nextAgent'); + if (!preg_match("/^\d{1,10}$/", $next_id)) { + throw new BadRequestException('Wrong value of "nextAgent" argument.'); + } + $next_operator = operator_by_id($next_id); + + if ($next_operator) { + $page['message'] = getlocal2( + 'chat.redirected.content', + array(get_operator_name($next_operator)) + ); + if (!$this->redirectToOperator($thread, $next_id)) { + $page['errors'][] = getlocal('chat.redirect.cannot'); + } + } else { + $page['errors'][] = 'Unknown operator'; + } + } + + $page = array_merge_recursive($page, setup_logo()); + + if (count($page['errors']) > 0) { + return $this->render('error', $page); + } else { + return $this->render('redirected', $page); + } + } + + protected function redirectToGroup(Thread $thread, $group_id) + { + if ($thread->state != Thread::STATE_CHATTING) { + // We can redirect only threads which are in proggress now. + return false; + } + + // Redirect the thread + $thread->state = Thread::STATE_WAITING; + $thread->nextAgent = 0; + $thread->groupId = $group_id; + $thread->agentId = 0; + $thread->agentName = ''; + $thread->save(); + + // Send notification message + $thread->postMessage( + Thread::KIND_EVENTS, + getstring2_( + 'chat.status.operator.redirect', + array(get_operator_name($this->getOperator())), + $thread->locale, + true + ) + ); + + return true; + } + + protected function redirectToOperator(Thread $thread, $operator_id) + { + if ($thread->state != Thread::STATE_CHATTING) { + // We can redirect only threads which are in proggress now. + return false; + } + + // Redirect the thread + $thread->state = Thread::STATE_WAITING; + $thread->nextAgent = $operator_id; + $thread->agentId = 0; + + // Check if the target operator belongs to the current thread's group. + // If not reset the current thread's group. + if ($thread->groupId != 0) { + $db = Database::getInstance(); + list($groups_count) = $db->query( + ("SELECT count(*) AS count " + . "FROM {chatgroupoperator} " + . "WHERE operatorid = ? AND groupid = ?"), + array($operator_id, $thread->groupId), + array( + 'return_rows' => Database::RETURN_ONE_ROW, + 'fetch_type' => Database::FETCH_NUM, + ) + ); + if ($groups_count === 0) { + $thread->groupId = 0; + } + } + + $thread->save(); + + // Send notification message + $thread->postMessage( + Thread::KIND_EVENTS, + getstring2_( + 'chat.status.operator.redirect', + array(get_operator_name($this->getOperator())), + $thread->locale, + true + ) + ); + + return true; + } +} diff --git a/src/mibew/libs/operator.php b/src/mibew/libs/operator.php index 76d0239d..3dd5563a 100644 --- a/src/mibew/libs/operator.php +++ b/src/mibew/libs/operator.php @@ -645,7 +645,7 @@ function setup_redirect_links($threadid, $operator, $token) ? getlocal("char.redirect.operator.online_suff") : getlocal("char.redirect.operator.away_suff")) : ""; - $agent_list .= "
  • " . get_operator_name($agent) . " $status
  • "; @@ -660,7 +660,7 @@ function setup_redirect_links($threadid, $operator, $token) $status = group_is_online($group) ? getlocal("char.redirect.operator.online_suff") : (group_is_away($group) ? getlocal("char.redirect.operator.away_suff") : ""); - $group_list .= "
  • " . get_group_name($group) . " $status
  • "; diff --git a/src/mibew/libs/routing.yml b/src/mibew/libs/routing.yml index 5e37eb87..f193bd8e 100644 --- a/src/mibew/libs/routing.yml +++ b/src/mibew/libs/routing.yml @@ -7,6 +7,12 @@ chat_operator: _controller: Mibew\Controller\Chat\Operator\ChatController::indexAction _access_check: Mibew\AccessControl\Check\LoggedInCheck +chat_operator_redirect: + path: /operator/chat/redirect + defaults: + _controller: Mibew\Controller\Chat\Operator\RedirectController::indexAction + _access_check: Mibew\AccessControl\Check\LoggedInCheck + # Pages that are available for all users button: path: /b diff --git a/src/mibew/operator/redirect.php b/src/mibew/operator/redirect.php deleted file mode 100644 index 1fb6fe52..00000000 --- a/src/mibew/operator/redirect.php +++ /dev/null @@ -1,128 +0,0 @@ - array(), -); - -// Initialize chat style which is currently used in system -$chat_style = new ChatStyle(ChatStyle::getCurrentStyle()); - -if (isset($_GET['nextGroup'])) { - $next_id = verify_param("nextGroup", "/^\d{1,8}$/"); - $next_group = group_by_id($next_id); - - if ($next_group) { - $page['message'] = getlocal2( - "chat.redirected.group.content", - array(get_group_name($next_group)) - ); - if ($thread->state == Thread::STATE_CHATTING) { - $thread->state = Thread::STATE_WAITING; - $thread->nextAgent = 0; - $thread->groupId = $next_id; - $thread->agentId = 0; - $thread->agentName = ''; - $thread->save(); - - $thread->postMessage( - Thread::KIND_EVENTS, - getstring2_( - "chat.status.operator.redirect", - array(get_operator_name($operator)), - $thread->locale, - true - ) - ); - } else { - $page['errors'][] = getlocal("chat.redirect.cannot"); - } - } else { - $page['errors'][] = "Unknown group"; - } -} else { - $next_id = verify_param("nextAgent", "/^\d{1,8}$/"); - $next_operator = operator_by_id($next_id); - - if ($next_operator) { - $page['message'] = getlocal2( - "chat.redirected.content", - array(get_operator_name($next_operator)) - ); - if ($thread->state == Thread::STATE_CHATTING) { - $thread->state = Thread::STATE_WAITING; - $thread->nextAgent = $next_id; - $thread->agentId = 0; - if ($thread->groupId != 0) { - $db = Database::getInstance(); - list($groups_count) = $db->query( - ("SELECT count(*) AS count " - . "FROM {chatgroupoperator} " - . "WHERE operatorid = ? AND groupid = ?"), - array($next_id, $thread->groupId), - array( - 'return_rows' => Database::RETURN_ONE_ROW, - 'fetch_type' => Database::FETCH_NUM, - ) - ); - if ($groups_count === 0) { - $thread->groupId = 0; - } - } - $thread->save(); - $thread->postMessage( - Thread::KIND_EVENTS, - getstring2_( - "chat.status.operator.redirect", - array(get_operator_name($operator)), - $thread->locale, - true - ) - ); - } else { - $page['errors'][] = getlocal("chat.redirect.cannot"); - } - } else { - $page['errors'][] = "Unknown operator"; - } -} - -$page = array_merge_recursive($page, setup_logo()); - -if (count($page['errors']) > 0) { - $chat_style->render('error', $page); -} else { - $chat_style->render('redirected', $page); -}