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

/**
 * Description of site_menu_install
 *
 * @author Igor Ratsun
 */
class site_menu_install  extends MX_Controller {
    
    protected $module_config = array();

    public function __construct() {
        parent::__construct();

        $this->lang->load('site_menu/install');
        // Set acl module name
        $this->module_config['module_acl_name'] = 'site_menu';
        // Set title
        $this->module_config['module_title'] = $this->lang->line('module_name');
        // Set actions
        $this->module_config['module_actions'] = array(
                'edit_user_menu_type' => $this->lang->line('edit_user_menu_type'),
                'edit_user_menu' => $this->lang->line('edit_module')
        );
        // Set menu items
        $this->module_config['module_menu_items'] = array(
            array('type', $this->lang->line('edit_user_menu_type'), '', 'edit_user_menu_type'),
            array('manager', $this->lang->line('edit_module'), '', 'edit_user_menu')
        );
    }
    
    public function install() {
        // Create table
        $query = '
            CREATE TABLE IF NOT EXISTS `' . $this->db->dbprefix . 'site_menu_type` (
              `id_site_menu_type` int(11) NOT NULL AUTO_INCREMENT,
              `name` varchar(255) NOT NULL,
              `active_class` varchar(32) NOT NULL,
              `template` text NOT NULL,
              `active` tinyint(1) NOT NULL DEFAULT \'1\',
              PRIMARY KEY (`id_site_menu_type`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
            
            CREATE TABLE IF NOT EXISTS `' . $this->db->dbprefix . 'site_menu` (
              `id_site_menu` int(11) NOT NULL AUTO_INCREMENT,
              `id_site_menu_type` int(11) NOT NULL,
              `href` varchar(255) NOT NULL,
              `parent_id` int(11) NOT NULL,
              `view_order` tinyint(3) NOT NULL DEFAULT \'0\',
              `active` tinyint(1) NOT NULL DEFAULT \'1\',
              PRIMARY KEY (`id_site_menu`)
            ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
            
            CREATE TABLE IF NOT EXISTS `site_menu_translate` (
                `id_site_menu_translate` int(11) NOT NULL AUTO_INCREMENT,
                `id_site_menu` int(11) NOT NULL,
                `id_lan` tinyint(1) NOT NULL,
                `name` varchar(255) NOT NULL,
                PRIMARY KEY (`id_site_menu_translate`)
            ) 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() {
        $query = 'DROP TABLE `site_menu`; DROP TABLE `site_menu_translate`; DROP TABLE `site_menu_type`;';
        foreach (explode(';', trim($query)) as $table) {
            if (!empty($table)) {
                $this->db->query($table);
            }
        }
        
        $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']);
    }
}

