diff --git a/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php b/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php new file mode 100644 index 00000000..3d90fd94 --- /dev/null +++ b/src/mibew/libs/classes/Mibew/Controller/CannedMessageController.php @@ -0,0 +1,139 @@ +attributes->get('_operator'); + $page = array( + 'errors' => array(), + ); + + // Get selected locale, if any. + $all_locales = get_available_locales(); + $locales_with_label = array(); + foreach ($all_locales as $id) { + $locales_with_label[] = array( + 'id' => $id, + 'name' => getlocal_($id, 'names') + ); + } + $page['locales'] = $locales_with_label; + + $lang = $request->query->get('lang'); + $correct_locale = $lang + && preg_match("/^[\w-]{2,5}$/", $lang) + && in_array($lang, $all_locales); + if (!$correct_locale) { + $lang = in_array(CURRENT_LOCALE, $all_locales) + ? CURRENT_LOCALE + : $all_locales[0]; + } + + // Get selected group ID, if any. + $group_id = $request->query->get('group'); + if ($group_id && preg_match("/^\d{0,8}$/", $group_id)) { + $group = group_by_id($group_id); + if (!$group) { + $page['errors'][] = getlocal('page.group.no_such'); + $group_id = false; + } + } else { + $group_id = false; + } + + $all_groups = in_isolation($operator) + ? get_all_groups_for_operator($operator) + : get_all_groups(); + $page['groups'] = array(); + $page['groups'][] = array( + 'groupid' => '', + 'vclocalname' => getlocal('page.gen_button.default_group'), + 'level' => 0, + ); + foreach ($all_groups as $g) { + $page['groups'][] = $g; + } + + // Get messages and setup pagination + $canned_messages = load_canned_messages($lang, $group_id); + foreach ($canned_messages as &$message) { + $message['vctitle'] = $message['vctitle']; + $message['vcvalue'] = $message['vcvalue']; + } + unset($message); + + $pagination = setup_pagination($canned_messages); + $page['pagination'] = $pagination['info']; + $page['pagination.items'] = $pagination['items']; + + // Buil form values + $page['formlang'] = $lang; + $page['formgroup'] = $group_id; + + // Set other needed page values and render the response + $page['title'] = getlocal('canned.title'); + $page['menuid'] = 'canned'; + $page = array_merge($page, prepare_menu($operator)); + + return $this->render('canned_message', $page); + } + + /** + * Generate content for "canned_message_delete" route. + * + * @param Request $request + * @return string Rendered page content + */ + public function deleteAction(Request $request) + { + // Check for CSRF attack + csrf_check_token($request); + + // Remove message from the database. + $db = Database::getInstance(); + + $key = (int)$request->attributes->get('message_id'); + $db->query("DELETE FROM {chatresponses} WHERE id = ?", array($key)); + + // Redirect user to canned messages list. Use only "lang" and "group" + // get params for the target URL. + $parameters = array_intersect_key( + $request->query->all(), + array_flip(array('lang', 'group')) + ); + + return $this->redirect($this->generateUrl('canned_message', $parameters)); + } +} diff --git a/src/mibew/libs/routing.yml b/src/mibew/libs/routing.yml index 275ba675..3bc888f5 100644 --- a/src/mibew/libs/routing.yml +++ b/src/mibew/libs/routing.yml @@ -12,6 +12,20 @@ widget_gateway: defaults: { _controller: Mibew\Controller\WidgetController::indexAction } # Operators' pages +canned_message: + path: /operator/canned-message + defaults: + _controller: Mibew\Controller\CannedMessageController::indexAction + _access_check: Mibew\AccessControl\Check\LoggedInCheck + +canned_message_delete: + path: /operator/canned-message/{message_id}/delete + defaults: + _controller: Mibew\Controller\CannedMessageController::deleteAction + _access_check: Mibew\AccessControl\Check\LoggedInCheck + requirements: + message_id: \d+ + history: path: /operator/history defaults: diff --git a/src/mibew/operator/canned.php b/src/mibew/operator/canned.php deleted file mode 100644 index 9f29fed4..00000000 --- a/src/mibew/operator/canned.php +++ /dev/null @@ -1,111 +0,0 @@ - array(), -); - -# locales - -$all_locales = get_available_locales(); -$locales_with_label = array(); -foreach ($all_locales as $id) { - $locales_with_label[] = array('id' => $id, 'name' => getlocal_($id, "names")); -} -$page['locales'] = $locales_with_label; - -$lang = verify_param("lang", "/^[\w-]{2,5}$/", ""); -if (!$lang || !in_array($lang, $all_locales)) { - $lang = in_array(CURRENT_LOCALE, $all_locales) ? CURRENT_LOCALE : $all_locales[0]; -} - -# groups - -$group_id = verify_param("group", "/^\d{0,8}$/", ""); -if ($group_id) { - $group = group_by_id($group_id); - if (!$group) { - $page['errors'][] = getlocal("page.group.no_such"); - $group_id = ""; - } -} - -$all_groups = in_isolation($operator) ? get_all_groups_for_operator($operator) : get_all_groups(); -$page['groups'] = array(); -$page['groups'][] = array( - 'groupid' => '', - 'vclocalname' => getlocal("page.gen_button.default_group"), - 'level' => 0, -); -foreach ($all_groups as $g) { - $page['groups'][] = $g; -} - -# delete - -if (isset($_GET['act']) && $_GET['act'] == 'delete') { - $key = isset($_GET['key']) ? $_GET['key'] : ""; - - if (!preg_match("/^\d+$/", $key)) { - $page['errors'][] = "Wrong key"; - } - - if (count($page['errors']) == 0) { - $db = Database::getInstance(); - $db->query("DELETE FROM {chatresponses} WHERE id = ?", array($key)); - $redirect_to = MIBEW_WEB_ROOT . "/operator/canned.php?lang=" - . urlencode($lang) . "&group=" . intval($group_id); - header("Location: " . $redirect_to); - exit; - } -} - -// Get messages and setup pagination - -$canned_messages = load_canned_messages($lang, $group_id); -foreach ($canned_messages as &$message) { - $message['vctitle'] = $message['vctitle']; - $message['vcvalue'] = $message['vcvalue']; -} -unset($message); - -$pagination = setup_pagination($canned_messages); -$page['pagination'] = $pagination['info']; -$page['pagination.items'] = $pagination['items']; - -# form values - -$page['formlang'] = $lang; -$page['formgroup'] = $group_id; -$page['title'] = getlocal("canned.title"); -$page['menuid'] = "canned"; - -$page = array_merge($page, prepare_menu($operator)); - -$page_style = new PageStyle(PageStyle::getCurrentStyle()); -$page_style->render('canned', $page); diff --git a/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars b/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars index 31471b2f..59c1b615 100644 --- a/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars +++ b/src/mibew/styles/pages/default/templates_src/server_side/_menu.handlebars @@ -26,7 +26,7 @@
  • {{l10n "right.administration"}}