<?php

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

/**
 * Description of callme_install
 *
 * @author Igor Ratsun
 */
class callme_install extends MX_Controller
{

    protected $module_config = array();

    public function __construct()
    {
        $folder_name = 'callme';
        $this->lang->load($folder_name . '/install');
        // Set acl module name
        $this->module_config['module_acl_name'] = $folder_name;
        // Set title
        $this->module_config['module_title'] = $this->lang->line('module_name');
        // Set module image
        $this->module_config['module_image'] = 'fa-commenting-o';
        // 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
        $query = '
            CREATE TABLE IF NOT EXISTS `callme` (
                `id_callme` int(11) NOT NULL AUTO_INCREMENT,
                `name` varchar(32) NOT NULL,
                `text` varchar(512) NOT NULL,
                `phone` varchar(16) NOT NULL,
                `email` varchar(128) NOT NULL,
                `date_add` date NOT NULL,
                `worked_out` tinyint(1) NOT NULL DEFAULT \'0\',
                PRIMARY KEY (`id_callme`)
              ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
        ';
        foreach (explode(';', trim($query)) 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']);
            }
            // Drop table
            if ($this->input->post('remove_tables')) {
                $this->db->query('DROP TABLE `callme`;');
            }

            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 .= sprintf($checkbox, 'remove_tables', '', $this->lang->line('Drop module tables'));
        $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']);
    }

}
