<?php

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

class logger_install extends MX_Controller
{

    protected $module_config = array();

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

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

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

        // 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('View'), '', 'view_logger')
        );
    }

    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 was 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']);
            }
            // Drop table
            if ($this->input->post('remove_tables')) {
                $this->db->query('DROP TABLE IF EXISTS `logger`;');
            }

            return array('content' => $this->lang->line('Module was 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 .= sprintf($checkbox, 'remove_tables', '', $this->lang->line('Drop module tables'));
        $content .= form_submit('uninstall', $this->lang->line('Uninstall'), 'style="margin-top:20px" class="btn btn-danger"');

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

}
