<?php 

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

class skeleton_install extends MX_Controller {

    protected $module_config = array();

    public function __construct() {
        $this->lang->load('skeleton/install');
        // Set acl module name
        $this->module_config['module_acl_name'] = 'skeleton';
        // Set title
        $this->module_config['module_title'] = $this->lang->line('module_name');
        // Set actions
        $this->module_config['module_actions'] = $this->lang->line('module_actions');
        // Set menu items
        $this->module_config['module_menu_items'] = $this->lang->line('module_menu_items');
    }
    
    public function install() {
        // Create table
        $file = dirname(__DIR__) . '/data/install.sql';
        if (file_exists($file)) {
            $sql = file_get_contents($file);
            foreach (explode(';', trim($sql)) as $table) {
                if (!empty($table)) {
                    $this->db->query($table);
                }
            }
        }

        $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']);

        return array('content' => $this->lang->line('module_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']);
            }

            return array('content' => $this->lang->line('module_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('ln_remove'), 'style="margin-top:20px" class="btn btn-danger"');

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