From 8f66376b748e4dbee80e6817a79aebd4b0c9a2de Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 28 Mar 2022 08:30:51 +0200 Subject: [PATCH] Move the change change language method to the new localization controller (removed from account) --- application/controllers/Account.php | 43 -------------------- application/controllers/Localization.php | 47 ++++++---------------- assets/js/http/account_http_client.js | 19 +-------- assets/js/http/localization_http_client.js | 37 +++++++++++++++++ 4 files changed, 51 insertions(+), 95 deletions(-) create mode 100644 assets/js/http/localization_http_client.js diff --git a/application/controllers/Account.php b/application/controllers/Account.php index 75fdf25d..fd67780c 100644 --- a/application/controllers/Account.php +++ b/application/controllers/Account.php @@ -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); - } - } } diff --git a/application/controllers/Localization.php b/application/controllers/Localization.php index dcc6ef66..1063a007 100644 --- a/application/controllers/Localization.php +++ b/application/controllers/Localization.php @@ -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) - { - if ($available_language === $language) - { - $found = TRUE; - break; - } - } + $language = request('language'); - if ( ! $found) - { - throw new Exception('The translations for the provided language do not exist: ' . $language); - } + session(['language' => $language]); - $this->session->set_userdata('language', $language); + config(['language' => $language]); - $this->config->set_item('language', $language); - - $response = AJAX_SUCCESS; + json_response([ + 'success' => TRUE + ]); } - catch (Exception $exception) + catch (Throwable $e) { - $this->output->set_status_header(500); - - $response = [ - 'message' => $exception->getMessage(), - 'trace' => config('debug') ? $exception->getTrace() : [] - ]; + json_exception($e); } - - $this->output - ->set_content_type('application/json') - ->set_output(json_encode($response)); } } diff --git a/assets/js/http/account_http_client.js b/assets/js/http/account_http_client.js index 4b10f9b8..dc7284f2 100644 --- a/assets/js/http/account_http_client.js +++ b/assets/js/http/account_http_client.js @@ -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 }; })(); diff --git a/assets/js/http/localization_http_client.js b/assets/js/http/localization_http_client.js new file mode 100644 index 00000000..164d8cd0 --- /dev/null +++ b/assets/js/http/localization_http_client.js @@ -0,0 +1,37 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Online Appointment Scheduler + * + * @package EasyAppointments + * @author A.Tselegidis + * @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 + }; +})();