Skip to content

Commit

Permalink
Move the change language operation into a new public controller (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
alextselegidis committed Feb 23, 2022
1 parent 0100d0b commit 1cf4f52
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 47 deletions.
46 changes: 0 additions & 46 deletions application/controllers/Backend_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1513,52 +1513,6 @@ public function ajax_validate_username()
->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.
*
Expand Down
77 changes: 77 additions & 0 deletions application/controllers/Localization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <[email protected]>
* @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));
}
}
2 changes: 1 addition & 1 deletion assets/js/general_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1cf4f52

Please sign in to comment.