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); 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 * Localization Controller
* *
* Contains all the location related methods. * Contains all the localization related methods.
* *
* @package Controllers * @package Controllers
*/ */
@ -26,52 +26,31 @@ class Localization extends EA_Controller {
* *
* Notice: This method used to be in the Backend_api.php. * Notice: This method used to be in the Backend_api.php.
*/ */
public function ajax_change_language() public function change_language()
{ {
try try
{ {
// Check if language exists in the available languages. // Check if language exists in the available languages.
$found = FALSE; $language = request('language');
$language = $this->input->post('language'); if ( ! in_array($language, config('available_languages')))
if (empty($language))
{ {
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');
{
if ($available_language === $language)
{
$found = TRUE;
break;
}
}
if ( ! $found) session(['language' => $language]);
{
throw new Exception('The translations for the provided language do not exist: ' . $language);
}
$this->session->set_userdata('language', $language); config(['language' => $language]);
$this->config->set_item('language', $language); json_response([
'success' => TRUE
$response = AJAX_SUCCESS; ]);
} }
catch (Exception $exception) catch (Throwable $e)
{ {
$this->output->set_status_header(500); json_exception($e);
$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); 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 { return {
save, save,
validateUsername, validateUsername
changeLanguage
}; };
})(); })();

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
};
})();