Move the change change language method to the new localization controller (removed from account)

This commit is contained in:
Alex Tselegidis 2022-03-28 08:30:51 +02:00
parent 2f67141d75
commit 8f66376b74
4 changed files with 51 additions and 95 deletions

View file

@ -155,47 +155,4 @@ class Account extends EA_Controller {
json_exception($e);
}
}
/**
* Change system language for current user.
*
* The language setting is stored in session data and retrieved every time the user visits any of the system pages.
*/
public function change_language()
{
try
{
// Check if language exists in the available languages.
$found = FALSE;
foreach (config('available_languages') as $lang)
{
if ($lang == request('language'))
{
$found = TRUE;
break;
}
}
if ( ! $found)
{
throw new Exception('Translations for the given language does not exist (' . request('language') . ').');
}
$language = request('language');
session(['language' => $language]);
config(['language' => $language]);
json_response([
'success' => TRUE
]);
}
catch (Throwable $e)
{
json_exception($e);
}
}
}

View file

@ -14,7 +14,7 @@
/**
* Localization Controller
*
* Contains all the location related methods.
* Contains all the localization related methods.
*
* @package Controllers
*/
@ -26,52 +26,31 @@ class Localization extends EA_Controller {
*
* Notice: This method used to be in the Backend_api.php.
*/
public function ajax_change_language()
public function change_language()
{
try
{
// Check if language exists in the available languages.
$found = FALSE;
$language = request('language');
$language = $this->input->post('language');
if (empty($language))
if ( ! in_array($language, config('available_languages')))
{
throw new Exception('No language provided.');
throw new Exception('Translations for the given language does not exist (' . request('language') . ').');
}
foreach (config('available_languages') as $available_language)
$language = request('language');
session(['language' => $language]);
config(['language' => $language]);
json_response([
'success' => TRUE
]);
}
catch (Throwable $e)
{
if ($available_language === $language)
{
$found = TRUE;
break;
json_exception($e);
}
}
if ( ! $found)
{
throw new Exception('The translations for the provided language do not exist: ' . $language);
}
$this->session->set_userdata('language', $language);
$this->config->set_item('language', $language);
$response = AJAX_SUCCESS;
}
catch (Exception $exception)
{
$this->output->set_status_header(500);
$response = [
'message' => $exception->getMessage(),
'trace' => config('debug') ? $exception->getTrace() : []
];
}
$this->output
->set_content_type('application/json')
->set_output(json_encode($response));
}
}

View file

@ -53,25 +53,8 @@ App.Http.Account = (function () {
return $.post(url, data);
}
/**
* Change language.
*
* @param {String} language
*/
function changeLanguage(language) {
const url = App.Utils.Url.siteUrl('account/change_language');
const data = {
csrf_token: vars('csrf_token'),
language
};
return $.post(url, data);
}
return {
save,
validateUsername,
changeLanguage
validateUsername
};
})();

View file

@ -0,0 +1,37 @@
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Localization HTTP client.
*
* This module implements the account related HTTP requests.
*/
App.Http.Localization = (function () {
/**
* Change language.
*
* @param {String} language
*/
function changeLanguage(language) {
const url = App.Utils.Url.siteUrl('localization/change_language');
const data = {
csrf_token: vars('csrf_token'),
language
};
return $.post(url, data);
}
return {
changeLanguage
};
})();