Replace "operator/translate.php" with a controller

This commit is contained in:
Dmitriy Simushev 2014-05-23 12:56:39 +00:00
parent ae92fe1514
commit 8969b71613
6 changed files with 278 additions and 166 deletions

View File

@ -0,0 +1,244 @@
<?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.
*/
namespace Mibew\Controller;
use Symfony\Component\HttpFoundation\Request;
/**
* Contains actions for all translation functionality.
*/
class TranslationController extends AbstractController
{
/**
* Generates list of all localization constants in the system.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function indexAction(Request $request)
{
$operator = $request->attributes->get('_operator');
$source = $request->query->get('source');
if (!preg_match("/^[\w-]{2,5}$/", $source)) {
$source = DEFAULT_LOCALE;
}
$target = $request->query->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
}
$lang1 = load_messages($source);
$lang2 = load_messages($target);
$page = array(
'lang1' => $source,
'lang2' => $target,
'title1' => isset($lang1['localeid']) ? $lang1['localeid'] : $source,
'title2' => isset($lang2['localeid']) ? $lang2['localeid'] : $target,
'errors' => array(),
);
// Load list of all available locales.
$locales_list = array();
$all_locales = get_available_locales();
foreach ($all_locales as $loc) {
$locales_list[] = array("id" => $loc, "name" => getlocal_("localeid", $loc));
}
// Extract needed localization constants.
$show = $request->query->get('show');
if (!in_array($show, array('all', 's1', 's2', 's3'))) {
$show = 'all';
}
$result = array();
$all_keys = array_keys($lang1);
if ($show == 's1') {
$all_keys = array_intersect($all_keys, locale_load_id_list('level1'));
} elseif ($show == 's2') {
$all_keys = array_intersect($all_keys, locale_load_id_list('level2'));
} elseif ($show == 's3') {
$all_keys = array_diff($all_keys, locale_load_id_list('level1'), locale_load_id_list('level2'));
}
// Prepare localization constants to display.
foreach ($all_keys as $key) {
$t_source = htmlspecialchars($lang1[$key]);
if (isset($lang2[$key])) {
$value = htmlspecialchars($lang2[$key]);
} else {
$value = "<font color=\"#c13030\"><b>absent</b></font>";
}
$result[] = array(
'id' => $key,
'l1' => $t_source,
'l2' => $value,
);
}
// Sort localization constants in the specified order.
$order = $request->query->get('sort');
if (!in_array($order, array('id', 'l1'))) {
$order = 'id';
}
if ($order == 'id') {
usort(
$result,
function ($a, $b) {
return strcmp($a['id'], $b['id']);
}
);
} elseif ($order == 'l1') {
usort(
$result,
function ($a, $b) {
return strcmp($a['l1'], $b['l1']);
}
);
}
$pagination = setup_pagination($result, 100);
$page['pagination'] = $pagination['info'];
$page['pagination.items'] = $pagination['items'];
$page['formtarget'] = $target;
$page['formsource'] = $source;
$page['availableLocales'] = $locales_list;
$page['availableOrders'] = array(
array('id' => 'id', 'name' => getlocal('translate.sort.key')),
array('id' => 'l1', 'name' => getlocal('translate.sort.lang')),
);
$page['formsort'] = $order;
$page['showOptions'] = array(
array('id' => 'all', 'name' => getlocal('translate.show.all')),
array('id' => 's1', 'name' => getlocal('translate.show.forvisitor')),
array('id' => 's2', 'name' => getlocal('translate.show.foroperator')),
array('id' => 's3', 'name' => getlocal('translate.show.foradmin')),
);
$page['formshow'] = $show;
$page['title'] = getlocal('page.translate.title');
$page['menuid'] = 'translation';
$page = array_merge($page, prepare_menu($operator));
return $this->render('translations', $page);
}
/**
* Builds a page with form for edit localization constant.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function showEditFormAction(Request $request)
{
set_csrf_token();
$operator = $request->attributes->get('_operator');
$string_id = $request->attributes->get('string_id');
$source = $request->query->get('source');
if (!preg_match("/^[\w-]{2,5}$/", $source)) {
$source = DEFAULT_LOCALE;
}
$target = $request->query->has('target')
? $request->query->get('target')
: $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
}
$lang1 = load_messages($source);
$lang2 = load_messages($target);
$page = array(
'title1' => isset($lang1['localeid']) ? $lang1['localeid'] : $source,
'title2' => isset($lang2['localeid']) ? $lang2['localeid'] : $target,
// Use errors list stored in the request. We need to do so to have
// an ability to pass the request from the "submitEditForm" action.
'errors' => $request->attributes->get('errors', array()),
);
$translation = isset($lang2[$string_id]) ? $lang2[$string_id] : '';
// Override translation value from the request if needed.
if ($request->request->has('translation')) {
$translation = $request->request->get('translation');
}
$page['saved'] = false;
$page['key'] = $string_id;
$page['target'] = $target;
$page['formoriginal'] = isset($lang1[$string_id]) ? $lang1[$string_id] : '<b><unknown></b>';
$page['formtranslation'] = $translation;
$page['title'] = getlocal('page.translate.title');
$page = array_merge(
$page,
prepare_menu($operator, false)
);
return $this->render('translation_edit', $page);
}
/**
* Processes submitting of the form which is generated in
* {@link \Mibew\Controller\TranslateController::showEditFormAction()}
* method.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function submitEditFormAction(Request $request)
{
csrf_check_token($request);
$operator = $request->attributes->get('_operator');
$string_id = $request->attributes->get('string_id');
$errors = array();
$target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
}
$translation = $request->request->get('translation');
if (!$translation) {
$errors[] = no_field("form.field.translation");
}
if (count($errors) != 0) {
$request->attributes->set('errors', $errors);
// The form should be rebuild. Invoke appropriate action.
return $this->showEditFormAction($request);
}
save_message($target, $string_id, $translation);
$page['saved'] = true;
$page['title'] = getlocal("page.translate.title");
$page = array_merge(
$page,
prepare_menu($operator, false)
);
return $this->render('translation_edit', $page);
}
}

View File

@ -323,6 +323,34 @@ statistics:
requirements:
type: by-date|by-operator|by-page
## Translation
translation_edit:
path: /operator/translation/{string_id}/edit
defaults:
_controller: Mibew\Controller\TranslationController::showEditFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
requirements:
string_id: "[_\.\w]+"
methods: [GET]
translation_edit_save:
path: /operator/translation/{string_id}/edit
defaults:
_controller: Mibew\Controller\TranslationController::submitEditFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
requirements:
string_id: "[_\.\w]+"
methods: [POST]
translations:
path: /operator/translation
defaults:
_controller: Mibew\Controller\TranslationController::indexAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
## Updates
updates:
path: /operator/updates

View File

@ -1,159 +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();
force_password($operator);
csrf_check_token();
$source = verify_param("source", "/^[\w-]{2,5}$/", DEFAULT_LOCALE);
$target = verify_param("target", "/^[\w-]{2,5}$/", CURRENT_LOCALE);
$string_id = verify_param("key", "/^[_\.\w]+$/", "");
$lang1 = load_messages($source);
$lang2 = load_messages($target);
$page = array(
'lang1' => $source,
'lang2' => $target,
'title1' => (isset($lang1["localeid"]) ? $lang1["localeid"] : $source),
'title2' => (isset($lang2["localeid"]) ? $lang2["localeid"] : $target),
'errors' => array(),
);
$page_style = new PageStyle(PageStyle::getCurrentStyle());
if ($string_id) {
$translation = isset($lang2[$string_id]) ? $lang2[$string_id] : "";
if (isset($_POST['translation'])) {
$translation = get_param('translation');
if (!$translation) {
$page['errors'][] = no_field("form.field.translation");
}
if (count($page['errors']) == 0) {
save_message($target, $string_id, $translation);
$page['saved'] = true;
$page['title'] = getlocal("page.translate.title");
$page = array_merge(
$page,
prepare_menu($operator, false)
);
$page_style->render('translate', $page);
exit;
}
}
$page['saved'] = false;
$page['key'] = $string_id;
$page['target'] = $target;
$page['formoriginal'] = isset($lang1[$string_id]) ? $lang1[$string_id] : "<b><unknown></b>";
$page['formtranslation'] = $translation;
$page['title'] = getlocal("page.translate.title");
$page = array_merge(
$page,
prepare_menu($operator, false)
);
$page_style->render('translate', $page);
exit;
}
$locales_list = array();
$all_locales = get_available_locales();
foreach ($all_locales as $loc) {
$locales_list[] = array("id" => $loc, "name" => getlocal_("localeid", $loc));
}
$show = verify_param("show", "/^(all|s1|s2|s3)$/", "all");
$result = array();
$all_keys = array_keys($lang1);
if ($show == 's1') {
$all_keys = array_intersect($all_keys, locale_load_id_list('level1'));
} elseif ($show == 's2') {
$all_keys = array_intersect($all_keys, locale_load_id_list('level2'));
} elseif ($show == 's3') {
$all_keys = array_diff($all_keys, locale_load_id_list('level1'), locale_load_id_list('level2'));
}
foreach ($all_keys as $key) {
$t_source = htmlspecialchars($lang1[$key]);
if (isset($lang2[$key])) {
$value = htmlspecialchars($lang2[$key]);
} else {
$value = "<font color=\"#c13030\"><b>absent</b></font>";
}
$result[] = array(
'id' => $key,
'l1' => $t_source,
'l2' => $value,
'idToPage' => $key,
'l1ToPage' => $t_source,
'l2ToPage' => $value,
);
}
$order = verify_param("sort", "/^(id|l1)$/", "id");
if ($order == 'id') {
usort(
$result,
function ($a, $b) {
return strcmp($a['id'], $b['id']);
}
);
} elseif ($order == 'l1') {
usort(
$result,
function ($a, $b) {
return strcmp($a['l1'], $b['l1']);
}
);
}
$pagination = setup_pagination($result, 100);
$page['pagination'] = $pagination['info'];
$page['pagination.items'] = $pagination['items'];
$page['formtarget'] = $target;
$page['formsource'] = $source;
$page['availableLocales'] = $locales_list;
$page['availableOrders'] = array(
array("id" => "id", "name" => getlocal("translate.sort.key")),
array("id" => "l1", "name" => getlocal("translate.sort.lang")),
);
$page['formsort'] = $order;
$page['showOptions'] = array(
array("id" => "all", "name" => getlocal("translate.show.all")),
array("id" => "s1", "name" => getlocal("translate.show.forvisitor")),
array("id" => "s2", "name" => getlocal("translate.show.foroperator")),
array("id" => "s3", "name" => getlocal("translate.show.foradmin")),
);
$page['formshow'] = $show;
$page['title'] = getlocal("page.translate.title");
$page['menuid'] = "translate";
$page = array_merge($page, prepare_menu($operator));
$page_style->render('translate_list', $page);

View File

@ -32,7 +32,7 @@
<li{{#ifEqual menuid "operators"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/operator">{{l10n "leftMenu.client_agents"}}</a></li>
<li{{#ifEqual menuid "groups"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/group">{{l10n "menu.groups"}}</a></li>
<li{{#ifEqual menuid "settings"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/settings.php">{{l10n "leftMenu.client_settings"}}</a></li>
<li{{#ifEqual menuid "translate"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/translate.php">{{l10n "menu.translate"}}</a></li>
<li{{#ifEqual menuid "translation"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/translation">{{l10n "menu.translate"}}</a></li>
<li{{#ifEqual menuid "updates"}} class="active"{{/ifEqual}}><a href="{{mibewRoot}}/operator/updates">{{l10n "menu.updates"}}</a></li>
{{/if}}
{{#if currentopid}}

View File

@ -17,9 +17,8 @@
{{> _errors}}
<form name="translateForm" method="post" action="{{mibewRoot}}/operator/translate.php">
<form name="translateForm" method="post" action="{{mibewRoot}}/operator/translation/{{key}}/edit">
{{csrfTokenInput}}
<input type="hidden" name="key" value="{{key}}"/>
<input type="hidden" name="target" value="{{target}}"/>
<div class="mform">

View File

@ -7,7 +7,7 @@
<br />
<br />
<form name="translateForm" method="get" action="{{mibewRoot}}/operator/translate.php">
<form name="translateForm" method="get" action="{{mibewRoot}}/operator/translation">
<div class="mform">
<div class="formtop">
@ -79,13 +79,13 @@
{{#each [pagination.items]}}
<tr>
<td>
<a href="{{../mibewRoot}}/operator/translate.php?source={{../lang1}}&amp;target={{../lang2}}&amp;key={{id}}" target="_blank" onclick="this.newWindow = window.open('{{../mibewRoot}}/operator/translate.php?source={{../lang1}}&amp;target={{../lang2}}&amp;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;">{{idToPage}}</a>
<a href="{{../mibewRoot}}/operator/translation/{{id}}/edit?source={{../lang1}}&amp;target={{../lang2}}" target="_blank" onclick="this.newWindow = window.open('{{../mibewRoot}}/operator/translation/{{id}}/edit?source={{../lang1}}&amp;target={{../lang2}}', '', '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;">{{id}}</a>
</td>
<td>
{{{l1ToPage}}}
{{{l1}}}
</td>
<td>
{{{l2ToPage}}}
{{{l2}}}
</td>
</tr>
{{else}}