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

/**
 * Layoutlib class
 */

/**
 * 레이아웃을 관리하는 class 입니다.
 */
class Layoutlib extends CI_Controller
{

    private $css = array();
    private $js = array();

    function __construct()
    {

    }


    /**
     * 관리자페이지 레이아웃관리합니다
     */
    function admin($config = array(), $device_view_type = '')
    {
        $data = array();
        $CI = & get_instance();
        $CI->load->config('ci_admin_menu');
        $admin_page_menu = config_item('admin_page_menu');

		$data['admin_page_menu'] = $admin_page_menu;

        if (uri_string() !== config_item('uri_segment_admin')) {
            list($data['menu_dir1'], $data['menu_dir2']) = explode('/', str_replace(config_item('uri_segment_admin') . '/', '', uri_string()));
            $data['menu_title'] = element('name', element(element('menu_dir2', $data), element('menu', element(element('menu_dir1', $data), element('admin_page_menu', $data)))));
        } else {
            $data['menu_dir1'] = '';
            $data['menu_dir2'] = '';
        }

        $layout_skin_file = element('layout', $config);
        $skin_file = element('skin', $config);

        $data['layout_skin_path'] = 'admin';
        $data['layout_skin_url'] = base_url( VIEW_DIR . element('layout_skin_path', $data));
        $data['index_url'] = admin_url($data['menu_dir1'] . '/' . $data['menu_dir2']);
        $data['layout_skin_file'] = $data['layout_skin_path'] . '/' . $layout_skin_file;
        $data['view_skin_file'] = $data['layout_skin_path'] . '/';
        if ($data['menu_dir1']) {
            $data['view_skin_file'] .= $data['menu_dir1'] . '/';
        }
        if ($data['menu_dir2']) {
            $data['view_skin_file'] .= $data['menu_dir2'] . '/';
        }
        $data['view_skin_path'] = $data['view_skin_file'];
        $data['view_skin_url'] = base_url( VIEW_DIR . $data['view_skin_path']);
        $data['view_skin_file'] .= $skin_file;

        $data['favicon'] = $CI->configlib->item('site_favicon') ? site_url(config_item('uploads_dir') . '/favicon/' . $CI->configlib->item('site_favicon')) : '';

        $data['admin-skin']                        = $CI->userlib->item('admin-skin') ? $CI->userlib->item('admin-skin') : 'skin-blue';
        $data['admin-layout-fixed']                = $CI->userlib->item('admin-layout-fixed') == 'fixed' ? $CI->userlib->item('admin-layout-fixed') : '';
        $data['admin-layout-layout-boxed']         = $CI->userlib->item('admin-layout-layout-boxed') == 'layout-boxed' ? $CI->userlib->item('admin-layout-layout-boxed') : '';
        $data['admin-layout-sidebar-collapse']     = $CI->userlib->item('admin-layout-sidebar-collapse') == 'sidebar-collapse' ? $CI->userlib->item('admin-layout-sidebar-collapse') : '';
        $data['admin-layout-control-sidebar-open'] = $CI->userlib->item('admin-layout-control-sidebar-open') == 'control-sidebar-open' ? $CI->userlib->item('admin-layout-control-sidebar-open') : '';
        $data['admin-layout-sidebarskin']          = $CI->userlib->item('admin-layout-sidebarskin') == 'control-sidebar-light' ? 'control-sidebar-light' : 'control-sidebar-dark';


        $page_name = '';
        if (isset($data['menu_dir1']) && $data['menu_dir1'] ) {
            $page_name = $data['admin_page_menu'][$data['menu_dir1']]['__config']['name'];
        }

        $user_id = (int) $CI->userlib->item('user_id');

        return $data;
    }


    /**
     * 프론트페이지 레이아웃관리합니다
     */
    function front($config = array(), $device_view_type = '')
    {
        $data = array();
        $CI = & get_instance();

        $layout = '_layout';
        $data['layout_skin_path'] = $layout;
        $data['layout_skin_url'] = base_url( VIEW_DIR . $data['layout_skin_path']);
        $layout .= '/';
        if (element('layout', $config)) {
            $layout .= element('layout', $config);
        }
        $data['layout_skin_file'] = $layout;

        $skin = '';
        if (element('path', $config)) {
            $skin .= element('path', $config);
        }
        $data['view_skin_path'] = $skin;
        $data['view_skin_url'] = base_url( VIEW_DIR . $data['view_skin_path']);
        $skin .= '/';
        if (element('skin', $config)) {
            $skin .= element('skin', $config);
        }
        $data['view_skin_file'] = $skin;
        $data['page_title'] = $CI->configlib->item('site_title');

        $data['favicon'] = $CI->configlib->item('site_favicon') ? site_url(config_item('uploads_dir') . '/favicon/' . $CI->configlib->item('site_favicon')) : '';

        $user_id = (int) $CI->userlib->item('user_id');

        return $data;
    }


    /**
     * css를 추가합니다
     */
    function add_css($file = '')
    {
        if (empty($file)) {
            return;
        }
        array_push($this->css, $file);
    }


    /**
     * js를 추가합니다
     */
    function add_js($file = '')
    {
        if (empty($file)) {
            return;
        }
        array_push($this->js, $file);
    }


    /**
     * 추가된 css를 리턴합니다
     */
    function display_css()
    {
        $return = '';
        $_css = $this->css;
        if ($_css) {
            foreach ($_css as $val) {
                $return .= '<link rel="stylesheet" type="text/css" href="' . $val . '" />';
            }
        }
        return $return;
    }


    /**
     * 추가된 js를 리턴합니다
     */
    function display_js()
    {
        $return = '';
        $_js = $this->js;
        if ($_js) {
            foreach ($_js as $val) {
                $return .= '<script type="text/javascript" src="' . $val . '"></script>';
            }
        }
        return $return;
    }
}
