<?php

namespace Ciext\Controller;

abstract class AbstractInstallController extends \Ciext\Controller\AbstractAdminController
{

    protected $module_config = array();

    /**
     * 
     * @param array $options - list of options
     * Ex. array(
     *      'shemaFile' => dirname(__DIR__) . '/data/install.sql',
     * )
     * @return array
     */
    public function install($options = array())
    {
        // Create table
        $file = !empty($options['shemaFile']) ? $options['shemaFile'] : '';
        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->module_image = $this->module_config['module_image'];
        $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']);
        
        // Upgrade module if needed
        $this->load->module('ciext/upgrade');
        $this->upgrade->upgrade($this->module_config['module_acl_name']);

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

    /**
     * Uninstall
     * @param array $options - list of options
     * Ex. array(
     *      'dropTables' => array('myTable')
     * )
     * @return array
     */
    public function uninstall($options = array())
    {
        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') && !empty($options['dropTables'])) {
                foreach ($options['dropTables'] as $table) {
                    $this->db->query('DROP TABLE IF EXISTS `' . $table . '`;');
                }
            }

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

}
