<?php

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

class inlineeditor_install extends MX_Controller
{

    const AUTOLOAD_CONFIG_FILE = 'application/config/autoload.php';

    protected $module_config = array();

    public function __construct()
    {
        // Set acl module name
        $this->module_config['module_acl_name'] = 'inlineeditor';

        // Set title
        $this->module_config['module_title'] = $this->lang->line('Inline content editor');

        // Set actions
        $this->module_config['module_actions'] = array(
            // 'acl_action' => 'description'
            'edit_inlineeditor' => $this->lang->line('Configuration')
        );

        // Set menu items
        $this->module_config['module_menu_items'] = array(
            //    array('method in admin controller', 'title in menu', 'path to the image. empty - default image', 'acl_action')
            array('index', $this->lang->line('Configuration'), '', 'edit_inlineeditor')
        );
    }

    public function install()
    {
        $this->load->library('ciext/installer');
        $this->installer->install($this->module_config['module_acl_name'], $this->module_config['module_title'], $this->module_config['module_actions'], $this->module_config['module_menu_items']);

        // Add autoload
        if (file_exists(self::AUTOLOAD_CONFIG_FILE) && is_writable(self::AUTOLOAD_CONFIG_FILE)) {
            $old = file_get_contents(self::AUTOLOAD_CONFIG_FILE);
            if (!strpos($old, 'inlineeditor/inlineeditor_autoload')) {
                file_put_contents(self::AUTOLOAD_CONFIG_FILE, str_replace('ciext/RT_user', 'ciext/RT_user\', \'inlineeditor/inlineeditor_autoload', $old));
            }
        }


        return array('content' => $this->lang->line('Inline editor installed'), 'title' => $this->module_config['module_title']);
    }

    public function uninstall()
    {
        if (!empty($_POST['uninstall'])) {
            // Uninstall acl
            if ($this->input->post('remove_acl')) {
                $this->load->library('ciext/installer');
                $this->installer->uninstall($this->module_config['module_acl_name']);
            }

            // Remove autoload
            if (file_exists(self::AUTOLOAD_CONFIG_FILE) && is_writable(self::AUTOLOAD_CONFIG_FILE)) {
                $old = file_get_contents(self::AUTOLOAD_CONFIG_FILE);
                file_put_contents(self::AUTOLOAD_CONFIG_FILE, str_replace(', \'inlineeditor/inlineeditor_autoload\'', '', $old));
            }

            return array('content' => $this->lang->line('Inline editor removed'), 'title' => $this->module_config['module_title']);
        }

        // Render uninstall form
        $this->load->helper('form');
        $checkbox = '<label class="checkbox"><input type="checkbox" name="%s" %s/> %s</label>';
        $content = form_open('', array('class' => 'form-horizontal'));
        $content .= sprintf($checkbox, 'remove_acl', 'checked="checked" ', $this->lang->line('Remove module from ACL'));
        $content .= form_submit('uninstall', $this->lang->line('Remove'), 'style="margin-top:20px" class="btn btn-danger"');

        return array('content' => $content, 'title' => $this->module_config['module_title']);
    }

}
