<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class inlineeditor_admin extends MX_Controller
{

    public function __construct()
    {
        parent::__construct();
        // Acceess check
        if (!$this->rt_user->isAllowed('inlineeditor', 'edit_inlineeditor')) {
            $this->load->library('ciext/admin_lib');
            $this->admin_lib->show_denied();
        }
        
        // Load language file
        $this->load->library('gettext_language');
        $this->gettext_language->load(__DIR__ . '/../language/' . $this->config->item('language') . '.po');
    }

    public function index()
    {
        $this->load->library('ciext/configurator');
        $cnf = new configurator();
        $cnf->set_resource('inlineeditor');
        $cnf->add_radio('show_editor');
        $cnf->display_as('show_editor', $this->lang->line('Enable editor ?'));

        return array('content' => $cnf->render(), 'title' => $this->lang->line('Inline content editor'));
    }

    public function save()
    {
        $rs = array();
        $this->load->model('inlineeditor/inlineeditor_model');
        $rs['status'] = $this->inlineeditor_model->save($this->input->post('id'), $this->input->post('lang'), $this->input->post('data'));

        header('Content-Type: application/json');
        die(json_encode($rs));
    }

    public function switch_editor()
    {
        $this->load->library('ciext/configurator');
        $cnf = new configurator('inlineeditor');
        $old = $cnf->get_config();
        $old['show_editor'] = (int) $this->input->post('show_editor');
        $cnf->set_config($old);

        redirect($this->input->server('HTTP_REFERER'));
    }
}
