Add an ability to change locale setting using UI

Fixes #99
This commit is contained in:
Dmitriy Simushev 2015-05-22 11:26:48 +00:00
parent 137d92624e
commit e9baceb039
6 changed files with 307 additions and 2 deletions

View File

@ -351,6 +351,26 @@ locale_disable:
requirements:
locale: "[a-z\-]{2,5}"
locale_edit:
path: /operator/locale/{locale}/edit
defaults:
_controller: Mibew\Controller\Localization\LocaleController::showEditFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
requirements:
locale: "[a-z\-]{2,5}"
methods: [GET]
locale_edit_save:
path: /operator/locale/{locale}/edit
defaults:
_controller: Mibew\Controller\Localization\LocaleController::submitEditFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
requirements:
locale: "[a-z\-]{2,5}"
methods: [POST]
locale_enable:
path: /operator/locale/{locale}/enable
defaults:

View File

@ -55,7 +55,10 @@ abstract class AbstractController extends BaseAbstractController
? $this->generateUrl('translation_export')
: '';
$tabs[getlocal('Locales')] = ($route != 'locales')
$locales = ($route == 'locales'
|| $route == 'locale_edit'
|| $route == 'locale_edit_save');
$tabs[getlocal('Locales')] = !$locales
? $this->generateUrl('locales')
: '';

View File

@ -130,4 +130,127 @@ class LocaleController extends AbstractController
return $this->redirect($this->generateUrl('locales'));
}
/**
* Builds locale edit page.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
* @throws NotFoundException If the locale with specified code is not found
* in the system.
*/
public function showEditFormAction(Request $request)
{
$page = array(
// 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()),
);
$locale = $request->attributes->get('locale');
// Check if locale exists and enabled.
if (!in_array($locale, get_available_locales())) {
throw new NotFoundException();
}
$info = get_locale_info($locale);
$page['formtimelocale'] = $info['time_locale'];
$page['formdateformatfull'] = $info['date_format']['full'];
$page['formdateformatdate'] = $info['date_format']['date'];
$page['formdateformattime'] = $info['date_format']['time'];
// Override fields from the request if it's needed. This case will take
// place when a save handler fails and passes the request to this
// action.
if ($request->isMethod('POST')) {
$page['formtimelocale'] = $request->request->get('timelocale');
$page['formdateformatfull'] = $request->request->get('dateformatfull');
$page['formdateformatdate'] = $request->request->get('dateformatdate');
$page['formdateformattime'] = $request->request->get('dateformattime');
}
$page['stored'] = $request->query->has('stored');
$page['title'] = getlocal('Locale details');
$page['menuid'] = 'translation';
$page['formaction'] = $request->getBaseUrl() . $request->getPathInfo();
$page = array_merge($page, prepare_menu($this->getOperator()));
$page['tabs'] = $this->buildTabs($request);
return $this->render('locale_edit', $page);
}
/**
* Processes submitting of the form which is generated in
* {@link \Mibew\Controller\Localization\LocaleController::showEditFormAction()}
* method.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
* @throws NotFoundException If the locale with specified code is not found
* in the system.
*/
public function submitEditFormAction(Request $request)
{
csrf_check_token($request);
$errors = array();
$locale = $request->attributes->get('locale');
$time_locale = $request->request->get('timelocale');
$date_format_full = $request->request->get('dateformatfull');
$date_format_date = $request->request->get('dateformatdate');
$date_format_time = $request->request->get('dateformattime');
if (!$locale) {
throw new NotFoundException();
}
if (!$time_locale) {
$errors[] = no_field('Time locale');
}
if (!$date_format_full) {
$errors[] = no_field('Date format (full)');
}
if (!$date_format_date) {
$errors[] = no_field('Date format (date)');
}
if (!$date_format_time) {
$errors[] = no_field('Date format (time)');
}
if (count($errors) != 0) {
$request->attributes->set('errors', $errors);
// The form should be rebuild. Invoke appropriate action.
return $this->showEditFormAction($request);
}
$locale_info = get_locale_info($locale);
$locale_info['time_locale'] = $time_locale;
$locale_info['date_format'] = array(
'full' => $date_format_full,
'date' => $date_format_date,
'time' => $date_format_time,
);
// Save the locale
set_locale_info($locale, $locale_info);
// Redirect the user to edit page again to use GET method instead of
// POST.
$redirect_to = $this->generateUrl(
'locale_edit',
array(
'locale' => $locale,
'stored' => true,
)
);
return $this->redirect($redirect_to);
}
}

View File

@ -705,7 +705,7 @@ function get_locales()
*/
function get_locale_info($locale)
{
static $cache = array();
$cache = get_locale_info_cache();
if (!isset($cache[$locale])) {
if (get_maintenance_mode() === false) {
@ -748,6 +748,77 @@ function get_locale_info($locale)
return $cache[$locale];
}
/**
* Updates locale meta info.
*
* @param string $locale Code of the locale to update.
* @param array $info Associative array of locale's info. This array should
* contain the follwing keys:
* - "name"
* - "rtl"
* - "time_locale"
* - "date_format"
* See description of {@link get_locale_info()} function for keys meaning.
* @return boolean True if the info is updated and false otherwise.
* @throws \InvalidArgumentException If the $info array is invalid.
*/
function set_locale_info($locale, $info)
{
// Make sure $info array is correct
$missed_keys = array_diff(
array(
'name',
'rtl',
'time_locale',
'date_format'
),
array_keys($info)
);
if (count($missed_keys) > 0) {
throw new \InvalidArgumentException(sprintf(
'These fields are missed: "%s".',
implode('", "', $missed_keys)
));
}
$success = Database::getInstance()->query(
('UPDATE {locale} SET name = :name, rtl = :rtl, '
. 'time_locale = :time_locale, date_format = :date_format '
. 'WHERE code = :code'),
array(
':code' => $locale,
':name' => $info['name'],
':rtl' => $info['rtl'] ? 1 : 0,
':time_locale' => $info['time_locale'],
':date_format' => serialize($info['date_format'])
)
);
if (!$success) {
return false;
}
// Update the cache.
$cache = get_locale_info_cache();
$cache[$locale] = $info;
return true;
}
/**
* Retrieves locales info cache.
*
* @return array Locales info cached. It's stored as static variable inside the
* function.
*/
function &get_locale_info_cache()
{
static $cache = array();
return $cache;
}
/**
* Loads localized messages for the specified locale.
*

View File

@ -0,0 +1,84 @@
{{#extends "_layout"}}
{{#override "menu"}}{{> _menu}}{{/override}}
{{#override "content"}}
{{l10n "Edit locale settings."}}
<br />
<br />
{{> _errors}}
{{#if stored}}
<div id="form-message">{{l10n "Changes saved"}}</div>
{{/if}}
<form name="localeForm" method="post" action="{{formaction}}">
{{csrfTokenInput}}
<div>
{{> _tabs}}
<div class="form-wrapper">
<div class="form-header">
<div class="form-header-inwards"></div>
</div>
<div class="form-inwards">
<div class="form-fields">
<div class="field">
<label for="time-locale" class="field-label">{{l10n "Time locale"}}<span class="required">*</span></label>
<div class="field-value">
<input id="time-locale" type="text" name="timelocale" size="40" value="{{formtimelocale}}" class="field-input" />
</div>
<label for="time-locale" class="field-description"> &mdash; {{l10n "This value will be passed to PHP's setlocale function to localize words in date/time string"}}</label>
<br clear="all"/>
</div>
<div class="field">
<label for="date-format-full" class="field-label">{{l10n "Date format (full)"}}<span class="required">*</span></label>
<div class="field-value">
<input id="date-format-full" type="text" name="dateformatfull" size="40" value="{{formdateformatfull}}" class="field-input" />
</div>
<label for="date-format-full" class="field-description"> &mdash; {{l10n "This value will be used with PHP's strftime to format date with time."}}</label>
<br clear="all"/>
</div>
<div class="field">
<label for="date-format-date" class="field-label">{{l10n "Date format (date)"}}<span class="required">*</span></label>
<div class="field-value">
<input id="date-format-date" type="text" name="dateformatdate" size="40" value="{{formdateformatdate}}" class="field-input" />
</div>
<label for="date-format-date" class="field-description"> &mdash; {{l10n "This value will be used with PHP's strftime to format date only."}}</label>
<br clear="all"/>
</div>
<div class="field">
<label for="date-format-time" class="field-label">{{l10n "Date format (time)"}}<span class="required">*</span></label>
<div class="field-value">
<input id="date-format-time" type="text" name="dateformattime" size="40" value="{{formdateformattime}}" class="field-input" />
</div>
<label for="date-format-time" class="field-description"> &mdash; {{l10n "This value will be used with PHP's strftime to format time only."}}</label>
<br clear="all"/>
</div>
<div class="form-button">
<input type="submit" name="save" class="submit-button-background save-button" value="{{l10n "Save"}}"/>
</div>
</div>
</div>
<div class="form-footer">
<div class="form-footer-inwards"></div>
</div>
</div>
</div>
<div class="asterisk">
<span class="required">*</span> - {{l10n "mandatory fields"}}
</div>
</form>
{{/override}}
{{/extends}}

View File

@ -55,6 +55,10 @@
{{else}}
<a href="{{csrfProtectedRoute "locale_disable" locale=code}}">{{l10n "disable"}}</a>
{{/if}}
{{#unless isDisabled}}
<a href="{{route "locale_edit" locale=code}}">{{l10n "edit"}}</a>
{{/unless}}
</td>
</tr>
{{/each}}