Skip to content
This repository has been archived by the owner on Nov 25, 2022. It is now read-only.

Commit

Permalink
Moved languages actions from Ajax to Languages controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkader committed May 28, 2018
1 parent 5ee1618 commit 2dcb1ab
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 280 deletions.
99 changes: 42 additions & 57 deletions content/common/js/language.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,71 @@
"use strict";

// Prepare globals.
var csk = window.csk = window.csk || {};
var csk = window.csk = window.csk || {};
csk.i18n = csk.i18n || {};
csk.i18n.language = csk.i18n.language || {};
csk.languages = csk.languages || {};
csk.i18n.languages = csk.i18n.languages || {};

/**
* Language Object.
* Handles all operations done on language module.
* @since 1.3.0
* Skeleton Languages.
* @since 2.1.1
*/
csk.language = {
csk.languages.proceed = function(el, action) {
var $this = $(el),
href = $this.data("endpoint"),
row = $this.closest("tr"),
id = row.attr("id") || undefined,
name = row.data("name") || 'this',
action = action || -1;

// Enable a language.
enable: function (el) {
return csk.language._do(el, "enable");
},

// Disable a language.
disable: function (el) {
return csk.language._do(el, "disable");
},

// Make default.
make_default: function (el) {
return csk.language._do(el, "default");
},
/** If no URL is provided, nothing to do... */
if (typeof href === "undefined" || !href.length) {
return false;
}

// Actions handler.
_do: function (el, action) {
var $this = $(el),
href = $this.data("endpoint"),
row = $this.closest("tr"),
lang = row.data("lang"),
id = row.attr("id"),
action = action || -1;
/** Add opacity to siblings */
row.siblings("tr").addClass("op-2");

// No URL provided? Nothing to do...
if (typeof href === "undefined" || !href.length || action <= 0) {
return false;
/** We define the confirmation message. */
var message = csk.i18n.languages[action] || undefined;
if (typeof message === "undefined") {
message = csk.i18n.default[action] || undefined;
if (typeof message === "undefined") {
message = "Are you sure you to " + action + " %s?";
}
}

csk.ui.confirm(csk.i18n.language[action], function () {
csk.ajax.request(href, {
type: "POST",
data: {action: action + "-language_" + lang},
complete: function (jqXHR, textStatus) {
if (textStatus !== "success") {
return;
}

// Refresh the page is we disable the current language.
if (action === "disable" && lang === csk.config.lang.folder) {
location.reload();
return;
}

// Simply reload the UI.
csk.ui.reload();
}
});
});
/** We add the id to the URL if defined. */
if (typeof id !== "undefined" && id.length) {
href = href + "#" + id;
}

/** Display confirmation message. */
csk.ui.confirm($.sprintf(message, name), function () {
window.location.href = href;
}, function () {
/** Make sure to remove opacity class from siblings. */
row.siblings("tr").removeClass("op-2");
});
};

$(document).ready(function () {
// Enable language.
/** Enable language. */
$(document).on("click", ".language-enable", function (e) {
e.preventDefault();
return csk.language.enable(this);
return csk.languages.proceed(this, "enable");
});

// Disable language.
/** Disable language. */
$(document).on("click", ".language-disable", function (e) {
e.preventDefault();
return csk.language.disable(this);
return csk.languages.proceed(this, "disable");
});

// Make default.
/** Make default. */
$(document).on("click", ".language-default", function (e) {
e.preventDefault();
return csk.language.make_default(this);
return csk.languages.proceed(this, "make_default");
});
});

Expand Down
2 changes: 1 addition & 1 deletion content/common/js/language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 1 addition & 176 deletions skeleton/controllers/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,7 @@ class Ajax extends AJAX_Controller {
* Array of available contexts.
* @var array
*/
private $_targets = array(
'languages',
'modules',
'plugins',
'reports',
'themes',
'users',
);
private $_targets = array('reports');

/**
* __constructr
Expand All @@ -82,11 +75,7 @@ public function __construct()
parent::__construct();

// Add safe reports.
$this->safe_admin_methods[] = '_languages';
$this->safe_admin_methods[] = '_plugins';
$this->safe_admin_methods[] = '_reports';
$this->safe_admin_methods[] = '_themes';
$this->safe_admin_methods[] = '_users';
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -121,170 +110,6 @@ public function index($target, $action = null, $id = 0)

// ------------------------------------------------------------------------

/**
* _languages
*
* Method for interacting with languages.
*
* @author Kader Bouyakoub
* @link https://goo.gl/wGXHO9
* @since 2.0.0
*
* @access public
* @param string $action The action to perform.
* @param string $name The plugin's folder name;
* @return AJAX_Controller::response()
*/
public function _languages($action = null, $name = null)
{
$this->load->language('csk_languages');
$actions = array('enable', 'disable', 'make_default');

/**
* Here are conditions in order to proceed:
* 1. The action is provided and available.
* 2. The language name is provided and available.
* 3. The action passes nonce check.
*/
if ((null === $action OR ! in_array($action, $actions))
OR (null === $name OR ! array_key_exists($name, $this->lang->languages()))
OR true !== $this->check_nonce())
{
$this->response->header = self::HTTP_NOT_ACCEPTABLE;
$this->response->message = __('CSK_ERROR_NONCE_URL');
return;
}

// Make sure to lower the name.
ctype_lower($name) OR $name = strtolower($name);

// We cannot touch "English" language.
if ('english' === $name && 'make_default' !== $action)
{
$this->response->header = self::HTTP_NOT_ACCEPTABLE;
$this->response->message = __('CSK_LANGUAGES_ERROR_ENGLISH_REQUIRED');
return;
}

// Get database languages for later use.
$languages = $this->config->item('languages');
$languages OR $languages = array();

switch ($action)
{
// Enabling a language.
case 'enable':

// Already enabled? Nothing to do..
if (in_array($name, $languages))
{
$this->response->header = self::HTTP_NOT_MODIFIED;
$this->response->message = __('CSK_LANGUAGES_ALREADY_ENABLE');
return;
}

// Add language to languages array.
$languages[] = $name;
asort($languages);
$languages = array_values($languages);

// Successfully updated?
if (false !== $this->kbcore->options->set_item('languages', $languages))
{
// TODO: Log the activity.
log_activity($this->c_user->id, 'Enabled language: '.$name);

$this->response->header = self::HTTP_OK;
$this->response->message = __('CSK_LANGUAGES_SUCCESS_ENABLE');
return;
}

$this->response->message = __('CSK_LANGUAGES_ERROR_ENABLE');

break;

// Disabling a language.
case 'disable':

// Already disabled? Nothing to do..
if ( ! in_array($name, $languages))
{
$this->response->header = self::HTTP_NOT_MODIFIED;
$this->response->message = __('CSK_LANGUAGES_ALREADY_DISABLE');
return;
}

// Remove language from languages array.
$languages[] = $name;
foreach ($languages as $i => $lang)
{
if ($lang === $name)
{
unset($languages[$i]);
}
}
asort($languages);
$languages = array_values($languages);

// Successfully updated?
if (false !== $this->kbcore->options->set_item('languages', $languages))
{
/**
* If the language is the site's default language, we make
* sure to set English as the default one.
*/
if ($name === $this->kbcore->options->item('language'))
{
$this->kbcore->options->set_item('language', 'english');
}

// TODO: Log the activity.
log_activity($this->c_user->id, 'Disabled language: '.$name);

$this->response->header = self::HTTP_OK;
$this->response->message = __('CSK_LANGUAGES_SUCCESS_DISABLE');
return;
}

$this->response->message = __('CSK_LANGUAGES_ERROR_DISABLE');

break;

// Making language default.
case 'make_default':

// If the language is not enabled, we make sure to enable it first.
if ( ! in_array($name, $languages))
{
$languages[] = $name;
asort($languages);
if (false === $this->kbcore->options->set_item('languages', $languages))
{
$this->response->header = self::HTTP_CONFLICT;
$this->response->message = __('CSK_LANGUAGES_ERROR_DEFAULT');
return;
}
}

// Successfully changed?
if (false !== $this->kbcore->options->set_item('language', $name))
{
// TODO: Log the activity.
log_activity($this->c_user->id, 'Set default language: '.$name);

$this->response->header = self::HTTP_OK;
$this->response->message = __('CSK_LANGUAGES_SUCCESS_DEFAULT');
return;
}

$this->response->message = __('CSK_LANGUAGES_ERROR_DEFAULT');

break;
}
}

// ------------------------------------------------------------------------

/**
* _reports
*
Expand Down
Loading

0 comments on commit 2dcb1ab

Please sign in to comment.