<?php defined('BASEPATH') OR exit('No direct script access allowed');
header("Content-Type: text/html; charset=UTF-8");
/**
 * PURPOSE         Controller에서 공통 함수 호출을 위해 Class 확장
 */

include_once(APPPATH . 'core/m/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');

		$config = Array(
			'protocol' => 'smtp',
			'smtp_host' => 'ssl://smtp.googlemail.com',
			'smtp_port' => 465,
			'smtp_user' => CROSSMAP_EMAIL_ID,
			'smtp_pass' => CROSSMAP_EMAIL_PASSWORD,
			'mailtype'  => 'html',
			'charset'   => 'utf-8'
		);


		$this->load->library('email',$config);
		$this->email->set_newline("\r\n");



		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);
		}

		$this->load->model('web/m_site_info');
		$site_info = $this->m_site_info->site_info_select(1);


		// 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);


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

			$skin_header_url = 'skin/m/_header';
			$skin_footer_url = 'skin/m/_footer';
			$skin_body_url = 'skin/m/_body';


			$body_class = "news";

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

			if($controller == 'search' || $controller == 'life' || $controller == 'cmtv' || $controller == '' || $controller == 'rules' || $controller == 'main2'){

				$skin_header_url = 'skin/m/news/_header';
				$skin_body_url = 'skin/m/news/_body';
				$skin_footer_url = 'skin/m/news/_footer';

				if($method == 'image_view')
				{
					$skin_footer_url = 'skin/m/_null';
					$skin_header_url = 'skin/m/_null';
					$skin_body_url = 'skin/m/_body';
					$body_class = "photos";

				}

				if($controller == 'rules'){
					$body_class = "mypage";
//					$skin_footer_url = "skin/m/_null";
				}

				if($controller == ''){
					$skin_header_url =  'skin/m/_null';
				}

			}
			else if($controller == 'news'){
				$skin_header_url = 'skin/m/news/_header';
				$skin_body_url = 'skin/m/news/_body';
				$skin_footer_url = 'skin/m/news/_footer';
			
			}else{
				switch ($method) {



					case "news_view":
						$skin_header_url = 'skin/m/news/_header';
						$skin_body_url = 'skin/m/news/_body';
						$skin_footer_url = 'skin/m/news/_footer';
						break;
					case "login":
					case "join":
					case "join_1":
					case "join_2":
						$body_class = "mypage";
						$skin_body_url = 'skin/m/_body';
						$skin_footer_url = 'skin/m/_null';
						break;

					case "notice":
					case "faq":
					case "mtm":
					case "faq_view":
					case "life_view":
					case "qna":
					case "qna_view":
					case "qna_add":
					case "bookmark":
					case "delivery":
					case "bookmark":
					case "req_password":
					case "member_edit":
					case "user_comment":
						$body_class = "mypage";
						$skin_header_url = 'skin/m/news/_header';
						$skin_body_url = 'skin/m/_body';
						break;
					case "find_id":
					case "find_pw":
					case "find_id_with_email":
					case "find_pw_with_email":
					case "secession":
					case "image_view":
					case "change_pw":
					case "change_pw2":

						$body_class = "mypage";
						$skin_footer_url = 'skin/m/_null';
						$skin_header_url = 'skin/m/_null';
						$skin_body_url = 'skin/m/_body';

						break;
					default:
						$skin_header_url = 'skin/m/_null';
						$skin_footer_url = 'skin/m/_null';
						break;
				}
			}
//			var_dump($skin_header_url);
			$data['body_class'] = $body_class;
			$data['Skin_Header'] = $this->load->view($skin_header_url, $return_data, TRUE);
			$data['Skin_Footer'] = $this->load->view($skin_footer_url, $return_data, TRUE);


//            var_dump($data);
			$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();
		}
	}
}

