<?php

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

class mconfig extends \Ciext\Controller\AbstractAdminController {

    function __construct() {
        parent::__construct();
        $this->load->library('ciext/admin_lib');
        $this->lang->load('ciext/mconfig');
        
        // Load main language file
        $this->load->library('gettext_language');
        $this->gettext_language->load(FCPATH . 'application/language/' . $this->config->item('language') . '.po');

        // Unset left menu
        //$this->admin_lib->content['user_left_menu'] = '';
        // Set top menu active
        $this->admin_lib->content['active_menu'] = 2;
    }

    public function index($module = '', $method = '') {
        // Set left menu
        unset($this->admin_lib->content['user_left_menu']);

        $module = (!empty($module) && !empty($method)) ? Modules::run($module . '/' . $module . '_admin/' . $method) : '';

        if (empty($module)) {

            show_404('', FALSE);
        }

        $content = (is_array($module)) ? $module : array('content' => $module);
        return $this->admin_lib->show($content);
    }

    public function installer($module = '', $method = '') {
        // Acceess check
        if (!$this->rt_user->isAllowed('system', 'module_manager')) {
            $this->admin_lib->show_denied();
        }
        
        // Load InstallController class
        if (!class_exists('InstallController')) {
            class_alias('Ciext\Controller\AbstractInstallController', 'InstallController');
        }

        $module = (!empty($module) && !empty($method)) ? Modules::run($module . '/' . $module . '_install/' . $method) : '';
        $content = (is_array($module)) ? $module : array('content' => $module);

        $content['content'] = $this->parser->parse('ciext/mconfig/install_template', array(
            'back_link' => '/' . $this->config->item('admin_page') . '/mconfig/manager',
            'Return to list' => $this->lang->line('Return to list'),
            'content' => $content['content']
                ), TRUE);

        return $this->admin_lib->show($content);
    }

    public function installForce($module = '', $method = '') {
        // Acceess check
        if (!$this->rt_user->isAllowed('system', 'module_manager')) {
            $this->admin_lib->show_denied();
        }
        
        // Load InstallController class
        class_alias('Ciext\Controller\AbstractInstallController', 'InstallController');

        $result = (!empty($module) && !empty($method)) ? Modules::run($module . '/' . $module . '_install/' . $method) : '';
        
        return (!empty($result));
    }

    public function manager() {
        // Acceess check
        if (!$this->rt_user->isAllowed('system', 'module_manager')) {
            $this->admin_lib->show_denied();
        }
        // Set top menu active
        $this->admin_lib->content['active_menu'] = 0;

        $this->ciext->load_bootstrap();
        $this->ciext->load_jquery_ui();
        $this->js_css->add_css(base_url() . $this->config->item('assets_folder') . '/' . $this->config->item('components_folder') . '/ciext/css/modules_install.css');
        $this->js_css->remove_css(base_url() . $this->config->item('assets_folder') . '/' . $this->config->item('components_folder') . '/ciext/css/reset.css');

        $this->load->config('ciext/install');

        $ignore = array_merge(
                array('.', '..', 'index.html', 'alphagallery', 'ciext', 'rtgrid', 'userinfo'), $this->config->item('auto_installing_modules')
        );

        $all_modules = array();
        foreach (array_keys($this->config->item('modules_locations')) as $loc) {
            $tmp = scandir($loc);
            foreach ($tmp as $mod_name) {
                if (file_exists($loc . $mod_name . '/controllers/' . ucfirst($mod_name) . '_install.php')) {
                    $all_modules[] = $mod_name;
                }
            }
            $all_modules = array_unique($all_modules);
        }

        $modules = array_filter($all_modules, function($v) use ($ignore) {
            return (array_search($v, $ignore) === FALSE);
        });

        // Prepare data
        $installed = array();
        if (count($modules) > 0) {
            $installed = $this->db->
                            select('acl_resource_name, acl_resource_alias')->
                            where_in('acl_resource_name', $modules)->
                            get('acl_resource')->result_array();
        }

        foreach ($installed as $v) {
            unset($modules[array_search($v['acl_resource_name'], $modules)]);
        }

        $table = array_merge($installed, array_map(function($v) {
                    return array(
                        'acl_resource_name' => $v,
                        'acl_resource_alias' => $v,
                        'installed' => 0
                    );
                }, $modules));

        // prepare html
        foreach ($table as &$v) {
            $triger = (!isset($v['installed'])) ? 'remove' : 'install';
            $v['installed'] = '<button  class="btn ' . $triger . '" rel="' . $v['acl_resource_name'] . '">' . $this->lang->line('ln_' . $triger) . '</button>';
        }
        unset($v);

        $this->load->library('table');
        $this->table->set_heading($this->lang->line('res_name'), $this->lang->line('res_alias'), '');

        $content = $this->table->generate($table) . '<div id="action_dialog" style="display:none"></div>';
        $this->js_css->parse_js('ciext/mconfig/js/main.js', array(
            'admin_page' => $this->config->item('admin_page'),
            'msg_install' => $this->lang->line('msg_install'),
            'msg_uninstall' => $this->lang->line('msg_uninstall')
        ));

        return $this->admin_lib->show(array('content' => $content));
    }

}

/* End of file mconfig.php */
/* Location: ./application/modules/ciext/mconfig.php */
