<?php

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

class admin_lib {

    public $template = 'ciext/template';
    public $content = array(
        'active_menu' => 1 // Active element of top menu
    );
    private $CI;
    protected $_control_panel_link = '';

    function __construct() {
        $this->CI = & get_instance();

        // Load lang
        $this->CI->load->language('ciext/admin');

        // Acceess check
        if (!$this->CI->rt_user->isAllowed('system', 'view_admin')) {
            $this->show_denied();
        }

        $this->CI->load->library('ciext/ciext');
    }

    public function show($content = array()) {
        // load admin menu
        $this->CI->load->model('ciext/admin_menu_model');

        $assets_folder = base_url() . $this->CI->config->item('assets_folder');

        $this->CI->ciext->load_bootstrap();
        $this->CI->js_css->add_css($assets_folder . '/components/ciext/css/admin_menu.css');
        $this->CI->js_css->add_js($assets_folder . '/components/ciext/js/admin_menu.js');

        $this->CI->load->helper('html');
        $admin_menu = ($this->CI->rt_user->isAllowed('system', 'view_amenu')) ? $this->CI->admin_menu_model->get_menu_items() : '';

        // Create page
        $ciext_assets_dir = base_url() . $this->CI->config->item('assets_folder') . '/' . $this->CI->config->item('components_folder') . '/ciext';
        $this->CI->js_css->add_css($ciext_assets_dir . '/css/reset.css');
        $this->CI->js_css->add_css($ciext_assets_dir . '/css/main.css');

        $data = array_merge(array(
            'user_info' => (isset($this->content['user_info'])) ? $this->content['user_info'] : Modules::run('userinfo/userinfo_admin/init'),
            'lang_key' => array_search($this->CI->config->item('language'), $this->CI->config->item('allowed_language')),
            'top_menu' => $this->get_top_menu(),
            'admin_menu' => $admin_menu,
            'user_left_menu' => (isset($this->content['user_left_menu'])) ? $this->content['user_left_menu'] : Modules::run('user_menu/user_menu/index'),
            // Assets
            'js_list' => $this->CI->js_css->get_js_string(),
            'css_list' => $this->CI->js_css->get_css_string(),
            'title' => $this->CI->admin_menu_model->get_current_page_name()
                ), $this->CI->lang->language);

        $this->CI->parser->parse($this->template, array_merge($data, $content));
    }

    public function show_denied() {
        $back_link = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : base_url();
        $_error = & load_class('Exceptions', 'core');
        $message = '<h6>' . $this->CI->lang->line('msg_access_denied') . '</h6>' .
                '<ul>
                    <li>— <a href="' . $back_link . '">' . $this->CI->lang->line('msg_back') . '</a></li>
                    <li>— <a href="' . base_url($this->CI->config->item('admin_page')) . '/logout">' . $this->CI->lang->line('msg_relogin') . '</a></li>
                </ul>';
        echo $_error->show_error($this->CI->lang->line('msg_head_error'), $message, 'error_503', 503);
        exit;
    }

    public function get_top_menu() {
        $menu = array(
            array(
                'active' => ($this->content['active_menu'] == 1) ? 'active' : '',
                'non-active' => '',
                'href' => '/' . $this->CI->config->item('admin_page'),
                'name' => $this->CI->lang->line('top_menu_1'),
                'id' => ''
            ),
            array(
                'active' => ($this->content['active_menu'] == 2) ? 'active' : '',
                'non-active' => '',
                'href' => $this->get_control_panel_link(),
                'name' => $this->CI->lang->line('top_menu_2'),
                'id' => ''
            ),
            array(
                'active' => ($this->content['active_menu'] == 3) ? 'active' : '',
                'non-active' => (!$this->CI->rt_user->isAllowed('filemanager', 'edit_file')) ? 'non-active' : '',
                'href' => (!$this->CI->rt_user->isAllowed('filemanager', 'edit_file')) ? '' : '/' . $this->CI->config->item('admin_page') . '/mconfig/index/filemanager/manage',
                'name' => $this->CI->lang->line('top_menu_3'),
                'id' => ''
            ),
            array(
                'active' => ($this->content['active_menu'] == 4) ? 'active' : '',
                'non-active' => (!$this->CI->rt_user->isAllowed('seo', 'edit_seo_manage')) ? 'non-active' : '',
                'href' => (!$this->CI->rt_user->isAllowed('seo', 'edit_seo_manage')) ? '' : '/' . $this->CI->config->item('admin_page') . '/mconfig/index/seo/manage',
                'name' => $this->CI->lang->line('top_menu_4'),
                'id' => ''
            ),
            array(
                'active' => ($this->content['active_menu'] == 5) ? 'active' : '',
                'non-active' => (!$this->CI->rt_user->isAllowed('system', 'view_amenu')) ? 'non-active' : '',
                'href' => '',
                'name' => $this->CI->lang->line('top_menu_5'),
                'id' => 'id="amenu-main-btn" onclick="return false;"'
            ),
            array(
                'active' => '',
                'non-active' => '',
                'href' => '/' . $this->CI->config->item('admin_page') . '/logout',
                'name' => '<div class="exit"></div>',
                'id' => ''
            )
        );

        return $menu;
    }

    public function get_control_panel_link() {
        if (empty($this->_control_panel_link)) {
            $this->CI->load->model('user_menu/user_menu_model');
            $match = array();
            preg_match('/href="([\w\W]*?)">/', $this->CI->user_menu_model->get_menu_items(), $match);

            $this->_control_panel_link = (!empty($match[1])) ? $match[1] : '/' . $this->CI->config->item('admin_page') . '/mconfig/index/filemanager/manage';
        }

        return $this->_control_panel_link;
    }

}
