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

include_once(APPPATH.'core/web/cm_exceptions.php');
class cm_controller extends CI_Controller {
	function __construct() {
        parent::__construct();
		$this->load->library('session');
		$this->load->library('user_agent');
        $this->load->helper('crossmap/tool_helper');
		$this->load->helper('crossmap/exception_helper');

	}

	function _remap($method, $params = array()) {


        $this->load->helper('cookie');
        $session_id = get_cookie('crossmap_save_id');
        # session
        $SES_MEMBER_ID  = $this->session->userdata('C_Member_Id');
        if($session_id != '' && $SES_MEMBER_ID=='') {
            # model - session 가져오기
            $this->load->model('web/m_ci_sessions');
            $session_result    = $this->m_ci_sessions->ci_sessions_select($session_id);
            session_decode($session_result['data']);
        }
        
        if($SES_MEMBER_ID!='') {
            $expire_date = date("Y-m-d H:i:s", strtotime("+10 minutes"));
            
            # model - 중복로그인 체크용
            $req_data = array();
            $req_data['member_id']      = $SES_MEMBER_ID;
            $req_data['member_ip']      = $_SERVER['REMOTE_ADDR'];
            $req_data['expire_date']    = $expire_date;
            $this->load->model('web/m_member_login');
            $this->m_member_login->member_login_member_id_member_ip_update($req_data);
        }
        
        
	    # model - 사이트 정보
        $this->load->model('web/m_site_info');
        $site_info    = $this->m_site_info->site_info_select(1);

        $controller     = $this->uri->segment(2);


		// Method가 있으면 해당 함수 실행
		if (method_exists($this, 'fn_'.$method)) {
            $data = array();
            $data['Skin_Content']           = call_user_func_array(array($this, 'fn_'.$method), $params);

            $arr_content    = array();
            $content_idx    = $this->uri->segment(4);
            if($method=='news_view') {
                $this->load->model('web/m_news');
                $arr_content = $this->m_news->news_select_idx($content_idx);
            } else if($method=='column_view') {
                $this->load->model('web/m_column');
                $arr_content = $this->m_column->column_select($content_idx);
            } else if($method=='life_view') {
                $this->load->model('web/m_life');
                $arr_content = $this->m_life->life_select_idx($content_idx);
            }

            $return_data = array();
            $return_data['site_info']   = $site_info;
            $return_data['arr_content'] = $arr_content;

            $skin_header_url    = 'skin/web/_header';
            $skin_footer_url    = 'skin/web/_footer';
            $skin_body_url      = 'skin/web/_body';
            if($controller =='mypage' || $controller =='cs' || $controller =='rules') {
                $skin_header_url    = 'skin/web/mypage/_header';
                $data['Skin_Left'] = $this->load->view('skin/web/mypage/_left', $return_data, true);
                $skin_body_url = 'skin/web/mypage/_body';

            } else {
                if($method == 'channel_recommend' || ($controller == 'life' && ($method == 'category' || $method=='tag'))) { // 추천채널
                    $skin_header_url = 'skin/web/life/_header_simple';
                    $skin_body_url = 'skin/web/life/_body';

                } else if($method == 'life_view' || $method == 'life_comment' ||  $method == 'channel' ||  $method == 'editor') {
                    $skin_header_url = '';
                    $skin_body_url = 'skin/web/life/_body';
                }
            }

            if($controller =='maptoon') {
                    $skin_header_url = 'skin/web/maptoon/_header_simple';
                    $skin_body_url = 'skin/web/maptoon/_body';
                    $skin_footer_url = 'skin/web/maptoon/_footer';
            }

            $data['Skin_Header'] = '';
            if ($skin_header_url!='')   $data['Skin_Header']    = $this->load->view($skin_header_url, $return_data, TRUE);
            $data['Skin_Footer']    = $this->load->view($skin_footer_url, $return_data, TRUE);
            $this->load->view($skin_body_url, $data);

		} else if (method_exists($this, 'fn_ajax_'.$method))  {                 // Ajax페이지는 Skin 필요없음
			call_user_func_array(array($this, 'fn_ajax_'.$method), $params);
        } else if (method_exists($this, 'fn_nonskin_'.$method))  {
            call_user_func_array(array($this, 'fn_nonskin_'.$method), $params);

		} else {                                                                // 없으면 잘못된 URL
			show_404();
		}
	}
}

