Replace "operator/cannededit.php" with a controller

This commit is contained in:
Dmitriy Simushev 2014-05-20 09:22:44 +00:00
parent 2630786cc1
commit 5ef2495ee3
5 changed files with 206 additions and 99 deletions

View File

@ -51,26 +51,21 @@ class CannedMessageController extends AbstractController
} }
$page['locales'] = $locales_with_label; $page['locales'] = $locales_with_label;
$lang = $request->query->get('lang'); $lang = $this->extractLocale($request);
$correct_locale = $lang if (!$lang) {
&& preg_match("/^[\w-]{2,5}$/", $lang)
&& in_array($lang, $all_locales);
if (!$correct_locale) {
$lang = in_array(CURRENT_LOCALE, $all_locales) $lang = in_array(CURRENT_LOCALE, $all_locales)
? CURRENT_LOCALE ? CURRENT_LOCALE
: $all_locales[0]; : $all_locales[0];
} }
// Get selected group ID, if any. // Get selected group ID, if any.
$group_id = $request->query->get('group'); $group_id = $this->extractGroupId($request);
if ($group_id && preg_match("/^\d{0,8}$/", $group_id)) { if ($group_id) {
$group = group_by_id($group_id); $group = group_by_id($group_id);
if (!$group) { if (!$group) {
$page['errors'][] = getlocal('page.group.no_such'); $page['errors'][] = getlocal('page.group.no_such');
$group_id = false; $group_id = false;
} }
} else {
$group_id = false;
} }
$all_groups = in_isolation($operator) $all_groups = in_isolation($operator)
@ -136,4 +131,161 @@ class CannedMessageController extends AbstractController
return $this->redirect($this->generateUrl('canned_message', $parameters)); return $this->redirect($this->generateUrl('canned_message', $parameters));
} }
/**
* Handles "canned_message_add" and "canned_message_edit" routes.
*
* Builds a page with form for add/edit canned message.
*
* @param Request $request
* @return string Rendered page content
*/
public function showEditFormAction(Request $request)
{
set_csrf_token();
$operator = $request->attributes->get('_operator');
$message_id = $request->attributes->get('message_id', false);
$page = array(
// Use errors list stored in the request. We need to do so to have
// an ability to pass the request from the "save" action.
'errors' => $request->attributes->get('errors', array()),
);
if ($message_id) {
// Load existing message
$canned_message = load_canned_message($message_id);
if (!$canned_message) {
$page['errors'][] = getlocal('cannededit.no_such');
$message_id = false;
} else {
$title = $canned_message['vctitle'];
$message = $canned_message['vcvalue'];
}
} else {
// Create new message
$message = '';
$title = '';
$page['locale'] = $this->extractLocale($request);
$page['groupid'] = $this->extractGroupId($request);
}
// Override message's fields from the request if it's needed. This
// case will take place when save handler fails.
if ($request->request->has('title')) {
$title = $request->request->get('title');
}
if ($request->request->has('message')) {
$message = $request->request->get('message');
}
$page['saved'] = false;
$page['key'] = $message_id;
$page['formtitle'] = $title;
$page['formmessage'] = $message;
$page['formaction'] = $request->getBaseUrl() . $request->getPathInfo();
$page['title'] = empty($message_id)
? getlocal('cannednew.title')
: getlocal('cannededit.title');
$page = array_merge($page, prepare_menu($operator, false));
return $this->render('canned_message_edit', $page);
}
/**
* Handles "canned_message_add_save" and "canned_message_edit_save" routes.
*
* The action processes submitting of the forms which are generated in
* "canned_message_add" and "canned_message_edit" routes (see
* {@link \Mibew\Controller\CannedMessageController::showEditForm()} for
* details).
*
* @param Request $request
* @return string Rendered page content
*/
public function saveAction(Request $request)
{
csrf_check_token($request);
$operator = $request->attributes->get('_operator');
$message_id = $request->request->get('key');
$errors = array();
$title = $request->request->get('title');
if (!$title) {
$errors[] = no_field("form.field.title");
}
$message = $request->request->get('message');
if (!$message) {
$errors[] = no_field("form.field.message");
}
if (count($errors) != 0) {
$request->attributes->set('errors', $errors);
// The form should be rebuild. Invoke appropriate action.
return $this->showEditFormAction($request);
}
if ($message_id) {
save_canned_message($message_id, $title, $message);
} else {
$locale = $this->extractLocale($request);
$group_id = $this->extractGroupId($request);
add_canned_message($locale, $group_id, $title, $message);
}
$page['saved'] = true;
$page = array_merge($page, prepare_menu($operator, false));
return $this->render('canned_message_edit', $page);
}
/**
* Extracts locale code from the request.
*
* @param Request $request
* @return string|boolean Locale code or boolean false if the code cannot be
* extracted.
*/
protected function extractLocale(Request $request)
{
$lang = $request->isMethod('POST')
? $request->request->get('lang')
: $request->query->get('lang');
$all_locales = get_available_locales();
$correct_locale = !empty($lang)
&& preg_match("/^[\w-]{2,5}$/", $lang)
&& in_array($lang, $all_locales);
if (!$correct_locale) {
return false;
}
return $lang;
}
/**
* Extracts group ID from the request.
*
* @param Request $request
* @return string|boolean Group ID or boolean false if the ID cannot be
* extracted.
*/
protected function extractGroupId(Request $request)
{
$group_id = $request->isMethod('POST')
? $request->request->get('group')
: $request->query->get('group');
if (!$group_id) {
return false;
}
if (!preg_match("/^\d{0,10}$/", $group_id)) {
return false;
}
return $group_id;
}
} }

View File

@ -12,20 +12,55 @@ widget_gateway:
defaults: { _controller: Mibew\Controller\WidgetController::indexAction } defaults: { _controller: Mibew\Controller\WidgetController::indexAction }
# Operators' pages # Operators' pages
## Canned messages
canned_message: canned_message:
path: /operator/canned-message path: /operator/canned-message
defaults: defaults:
_controller: Mibew\Controller\CannedMessageController::indexAction _controller: Mibew\Controller\CannedMessageController::indexAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
canned_message_add:
path: /operator/canned-message/add
defaults:
_controller: Mibew\Controller\CannedMessageController::showEditFormAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck
methods: [GET]
canned_message_add_save:
path: /operator/canned-message/add
defaults:
_controller: Mibew\Controller\CannedMessageController::saveAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck
methods: [POST]
canned_message_delete: canned_message_delete:
path: /operator/canned-message/{message_id}/delete path: /operator/canned-message/{message_id}/delete
defaults: defaults:
_controller: Mibew\Controller\CannedMessageController::deleteAction _controller: Mibew\Controller\CannedMessageController::deleteAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
requirements: requirements:
message_id: \d+ message_id: \d{0,10}
canned_message_edit:
path: /operator/canned-message/{message_id}/edit
defaults:
_controller: Mibew\Controller\CannedMessageController::showEditFormAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck
requirements:
message_id: \d{0,10}
methods: [GET]
canned_message_edit_save:
path: /operator/canned-message/{message_id}/edit
defaults:
_controller: Mibew\Controller\CannedMessageController::saveAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck
requirements:
message_id: \d{0,10}
methods: [POST]
## History
history: history:
path: /operator/history path: /operator/history
defaults: defaults:
@ -54,6 +89,7 @@ history_user_track:
_controller: Mibew\Controller\HistoryController::userTrackAction _controller: Mibew\Controller\HistoryController::userTrackAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
## Password recovery
password_recovery: password_recovery:
path: /operator/password-recovery path: /operator/password-recovery
defaults: defaults:
@ -64,6 +100,7 @@ password_recovery_reset:
defaults: defaults:
_controller: Mibew\Controller\PasswordRecoveryController::resetAction _controller: Mibew\Controller\PasswordRecoveryController::resetAction
## Statistics
statistics: statistics:
path: /operator/statistics/{type} path: /operator/statistics/{type}
defaults: defaults:
@ -73,12 +110,14 @@ statistics:
requirements: requirements:
type: by-date|by-operator|by-page type: by-date|by-operator|by-page
## Updates
updates: updates:
path: /operator/updates path: /operator/updates
defaults: defaults:
_controller: Mibew\Controller\UpdatesController::indexAction _controller: Mibew\Controller\UpdatesController::indexAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck _access_check: Mibew\AccessControl\Check\LoggedInCheck
## Users (visitors avaiting page)
users: users:
path: /operator/users path: /operator/users
defaults: defaults:

View File

@ -1,84 +0,0 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Import namespaces and classes of the core
use Mibew\Style\PageStyle;
// Initialize libraries
require_once(dirname(dirname(__FILE__)) . '/libs/init.php');
$operator = check_login();
csrf_check_token();
$string_id = verify_param("key", "/^\d{0,9}$/", "");
$page = array(
'errors' => array(),
);
$page_style = new PageStyle(PageStyle::getCurrentStyle());
if ($string_id) {
$canned_message = load_canned_message($string_id);
if (!$canned_message) {
$page['errors'][] = getlocal("cannededit.no_such");
$string_id = "";
} else {
$title = $canned_message['vctitle'];
$message = $canned_message['vcvalue'];
}
} else {
$message = '';
$title = '';
$page['locale'] = verify_param("lang", "/^[\w-]{2,5}$/", "");
$page['groupid'] = "";
$page['groupid'] = verify_param("group", "/^\d{0,8}$/");
}
if (isset($_POST['message']) && isset($_POST['title'])) {
$title = get_param('title');
if (!$title) {
$page['errors'][] = no_field("form.field.title");
}
$message = get_param('message');
if (!$message) {
$page['errors'][] = no_field("form.field.message");
}
if (count($page['errors']) == 0) {
if ($string_id) {
save_canned_message($string_id, $title, $message);
} else {
add_canned_message($page['locale'], $page['groupid'], $title, $message);
}
$page['saved'] = true;
$page = array_merge($page, prepare_menu($operator, false));
$page_style->render('canned_edit', $page);
exit;
}
}
$page['saved'] = false;
$page['key'] = $string_id;
$page['formtitle'] = $title;
$page['formmessage'] = $message;
$page['title'] = empty($string_id) ? getlocal("cannednew.title") : getlocal("cannededit.title");
$page = array_merge($page, prepare_menu($operator, false));
$page_style->render('canned_edit', $page);

View File

@ -46,8 +46,8 @@
<div class="tabletool"> <div class="tabletool">
<img src="{{stylePath}}/images/buttons/createban.gif" border="0" alt=""/> <img src="{{stylePath}}/images/buttons/createban.gif" border="0" alt=""/>
<a href="{{mibewRoot}}/operator/cannededit.php?lang={{formlang}}&amp;group={{formgroup}}" target="_blank" <a href="{{mibewRoot}}/operator/canned-message/add?lang={{formlang}}&amp;group={{formgroup}}" target="_blank"
onclick="this.newWindow = window.open('{{mibewRoot}}/operator/cannededit.php?lang={{formlang}}&amp;group={{formgroup}}', '', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=640,height=480,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;"> onclick="this.newWindow = window.open('{{mibewRoot}}/operator/canned-message/add?lang={{formlang}}&amp;group={{formgroup}}', '', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=640,height=480,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;">
{{l10n "canned.add"}} {{l10n "canned.add"}}
</a> </a>
</div> </div>
@ -72,8 +72,8 @@
{{#replace "\n" "<br/>"}}{{vcvalue}}{{/replace}} {{#replace "\n" "<br/>"}}{{vcvalue}}{{/replace}}
</td> </td>
<td> <td>
<a href="{{../mibewRoot}}/operator/cannededit.php?key={{id}}" target="_blank" <a href="{{../mibewRoot}}/operator/canned-message/{{id}}/edit" target="_blank"
onclick="this.newWindow = window.open('{{../mibewRoot}}/operator/cannededit.php?key={{id}}', '', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=640,height=480,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;">{{l10n "canned.actions.edit"}}</a>, onclick="this.newWindow = window.open('{{../mibewRoot}}/operator/canned-message/{{id}}/edit', '', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=640,height=480,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;">{{l10n "canned.actions.edit"}}</a>,
<a href="{{../mibewRoot}}/operator/canned-message/{{id}}/delete?lang={{../formlang}}&amp;group={{../formgroup}}{{csrfTokenInUrl}}">{{l10n "canned.actions.del"}}</a> <a href="{{../mibewRoot}}/operator/canned-message/{{id}}/delete?lang={{../formlang}}&amp;group={{../formgroup}}{{csrfTokenInUrl}}">{{l10n "canned.actions.del"}}</a>
</td> </td>
</tr> </tr>

View File

@ -20,7 +20,7 @@
{{> _errors}} {{> _errors}}
<form name="cannedForm" method="post" action="{{mibewRoot}}/operator/cannededit.php"> <form name="cannedForm" method="post" action="{{formaction}}">
{{csrfTokenInput}} {{csrfTokenInput}}
<input type="hidden" name="key" value="{{key}}"/> <input type="hidden" name="key" value="{{key}}"/>