From 3dd57021e2beb268368939f0a50a33ffbd94c958 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Wed, 23 Feb 2022 11:09:24 +0100 Subject: [PATCH] Move the change language operation into a new public controller (#1210) --- application/controllers/Backend_api.php | 46 -------------- application/controllers/Localization.php | 77 ++++++++++++++++++++++++ assets/js/general_functions.js | 2 +- 3 files changed, 78 insertions(+), 47 deletions(-) create mode 100644 application/controllers/Localization.php diff --git a/application/controllers/Backend_api.php b/application/controllers/Backend_api.php index 4abc641c..c2f4b793 100755 --- a/application/controllers/Backend_api.php +++ b/application/controllers/Backend_api.php @@ -1513,52 +1513,6 @@ class Backend_api extends EA_Controller { ->set_output(json_encode($response)); } - /** - * 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 ajax_change_language() - { - try - { - // Check if language exists in the available languages. - $found = FALSE; - - foreach (config('available_languages') as $lang) - { - if ($lang == $this->input->post('language')) - { - $found = TRUE; - break; - } - } - - if ( ! $found) - { - throw new Exception('Translations for the given language does not exist (' . $this->input->post('language') . ').'); - } - - $this->session->set_userdata('language', $this->input->post('language')); - $this->config->set_item('language', $this->input->post('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)); - } - /** * This method will return a list of the available google calendars. * diff --git a/application/controllers/Localization.php b/application/controllers/Localization.php new file mode 100644 index 00000000..dcc6ef66 --- /dev/null +++ b/application/controllers/Localization.php @@ -0,0 +1,77 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.4.3 + * ---------------------------------------------------------------------------- */ + +/** + * Localization Controller + * + * Contains all the location related methods. + * + * @package Controllers + */ +class Localization extends EA_Controller { + /** + * 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. + * + * Notice: This method used to be in the Backend_api.php. + */ + public function ajax_change_language() + { + try + { + // Check if language exists in the available languages. + $found = FALSE; + + $language = $this->input->post('language'); + + if (empty($language)) + { + throw new Exception('No language provided.'); + } + + foreach (config('available_languages') as $available_language) + { + if ($available_language === $language) + { + $found = TRUE; + break; + } + } + + 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)); + } +} diff --git a/assets/js/general_functions.js b/assets/js/general_functions.js index 235120d1..a98ba73a 100755 --- a/assets/js/general_functions.js +++ b/assets/js/general_functions.js @@ -287,7 +287,7 @@ window.GeneralFunctions = window.GeneralFunctions || {}; $(document).on('click', 'li.language', function () { // Change language with ajax call and refresh page. - var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_change_language'; + var url = GlobalVariables.baseUrl + '/index.php/localization/ajax_change_language'; var data = { csrfToken: GlobalVariables.csrfToken,