<?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();
        
        // Set admin panel language
        if ($this->CI->load->is_loaded('session') && isset($this->CI->rt_user) && !empty($this->CI->rt_user->id_user)) {
            $id_user = $this->CI->rt_user->id_user;
            $allowed_lang = $this->CI->config->item('allowed_language');
            $short_name = $this->CI->db
                    ->select('lan.short_name')
                    ->join('lan', 'lan.id_lan=acl_user.id_lan', 'left')
                    ->where('lan.active', TRUE)
                    ->where('acl_user.id_acl_user', $id_user)
                    ->get('acl_user')->row_array();
            if (!empty($short_name['short_name']) && isset($allowed_lang[$short_name['short_name']])) {
                $this->CI->config->set_item('language_short', $short_name['short_name']);
                $this->CI->config->set_item('language', $allowed_lang[$short_name['short_name']]);
            }
        }

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

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

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

    public function show($content = array())
    {
        // Load main language file
        $this->CI->load->library('gettext_language');
        $this->CI->gettext_language->load(FCPATH . 'application/language/' . $this->CI->config->item('language') . '.po');
        
        // Create page
        if (!isset($this->content['user_left_menu'])) {
            // load admin menu
            $this->CI->load->model('ciext/admin_menu_model');
            $menu = $this->CI->admin_menu_model->get_menu_items();
        } else {
            $menu = $this->content['user_left_menu'];
        }

        $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(),
            'menu' => $menu,
                ), $this->CI->lang->language);

        $data['admin_footer'] = $this->CI->config->item('admin_footer');

        $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' => '',
                '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('ciext/admin_menu_model');
            $this->_control_panel_link = '/' . $this->CI->config->item('admin_page') . '/' . $this->CI->admin_menu_model->getFirstElementPage();
        }

        return $this->_control_panel_link;
    }
    
}
