<?php defined('BASEPATH') OR exit('No direct script access allowed');
header("Content-Type: text/html; charset=UTF-8");
/**
 * 인클루드 class
 *
 * cm_controller: 부모 Class(CI_Controller 확장한 custome class)
 */
include_once(APPPATH.'core/m/cm_controller.php');
class news extends cm_Controller {


	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 뷰
	# CREATE  :	2018-04-16
	#---------------------------------------------------------------------------
	function fn_news_view() {
		# Request
		$idx                   = $this->uri->segment(4);

		$SES_MEMBER_ID          = $this->session->userdata('C_Member_Id');
		$content_type           = '1';



		$bookmark_count = 0;
		$arr_review = array();
		if($SES_MEMBER_ID !='') {
			# 게시물 열람 불가 체크
			$req_data = array();
			$req_data['member_id']  = $SES_MEMBER_ID;
			$req_data['limit_type'] = '2';

			# model
			$this->load->model('web/m_blind_limit');
			$arr_blind = $this->m_blind_limit->blind_limit_where_member_id_limit_type_select($req_data);
			if(count($arr_blind) > 0) {
				$return_data['arr_blind'] = $arr_blind;
				$return_data['url'] = '/web/news/';

				# view
				return $this->load->view('/web/comm/blind_view_alert', $return_data, true);
			}


			# model - bookmark
			$req_data = array();
			$req_data['member_id']      = $SES_MEMBER_ID;
			$req_data['content_type']   = $content_type;
			$req_data['content_idx']    = $idx;

			$this->load->model('web/m_bookmark');
			$bookmark_count   = $this->m_bookmark->bookmark_member_id_content_idx_count($req_data);

			# model - review
			$req_data = array();
			$req_data['member_id']      = $SES_MEMBER_ID;
			$req_data['content_idx']    = $idx;

			$this->load->model('web/m_review_log');
			$arr_review   = $this->m_review_log->review_log_where_member_id_content_idx_select($req_data);

		}

		# hit count update
		$req_data = array();
		$req_data['content_type']   = $content_type;
		$req_data['content_idx']    = $idx;
		$req_data['member_id']      = $SES_MEMBER_ID;

		$this->load->library('comm');
		$this->comm->fn_hit_count_update($req_data);



		# model - news view
		$this->load->model('web/m_news');
		$result         = $this->m_news->news_join_news_section_press_select($idx);


		# 연관글
		$req_data = array();
		$req_data['content_type']       = $content_type;
		$req_data['content_idx']        = $idx;
		$this->load->model('web/m_relation_article');
		$arr_relation_news  = $this->m_relation_article->relation_article_join_news_list($req_data);


		# 영적기상도
		$this->load->model('web/m_weather');
		$arr_weather = $this->m_weather->weather_join_relation_article_join_news_where_news_idx_select($idx);


		# view data
		$return_data = array();
		$return_data['idx']                     = $idx;
		$return_data['arr_relation_news']       = $arr_relation_news;
		$return_data['arr_weather']             = $arr_weather;
		$return_data['arr_review']              = $arr_review;
		$return_data['bookmark_count']          = $bookmark_count;
		$return_data['result']                  = $result;


		# view
		return $this->load->view('/m/news/news_view', $return_data, true);


	}

	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 컬럼 메인
	# CREATE  :	2018-04-28
	#---------------------------------------------------------------------------
	function fn_column() {

		# 칼럼
		$req_data = array();
		$req_data['sort_column']        = 'reg_date';
		$req_data['offset']             = 0;
		$req_data['limit']              = 6;

		# model - list
		$this->load->model('web/m_column');
		$result         = $this->m_column->column_list($req_data);
		$total_count    = $this->m_column->column_total_count();


		# 칼럼리스트
		$req_data = array();
		$req_data['member_group_idx']   = 7;
		$req_data['sort_column']        = 'display_sort';
		$this->load->model('web/m_partner');
		$arr_columnist  = $this->m_partner->partner_all_list($req_data);


		# 테마
		# 7: 헤드라인칼럼
		$theme_types = array('7', '15');
		$this->load->model('web/m_theme');
		$req_data = array();
		$req_data['theme_types'] = $theme_types;
		$arr_theme = $this->m_theme->theme_join_vw_content_where_in_theme_type_list($req_data);

		$arr_recomm_life        = array();
		$arr_headline_column    = array();
		foreach ($arr_theme as $val) {
			if($val['theme_type'] == 7)     $arr_headline_column[$val['rank']]  = array('rank'=>$val['rank'], 'title' => $val['title'], 'column_name'=> $val['column_name'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx'], 'partner_idx' => $val['partner_idx'], 'content' => $val['content']);
			if($val['theme_type'] == 15)    $arr_recomm_life[$val['rank']]      = array('rank'=>$val['rank'], 'title' => $val['title'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx']);
		}


		# view data
		$return_data = array();
		$return_data['list']                        = $result;
		$return_data['total_count']                 = $total_count;
		$return_data['arr_columnist']               = $arr_columnist;
		$return_data['arr_headline_column']         = $arr_headline_column;
		$return_data['arr_recomm_life']             = $arr_recomm_life;

		# view
		return $this->load->view('/m/news/column', $return_data, true);


	}




	#---------------------------------------------------------------------------
	# PURPOSE :	column list more
	# CREATE  :	2018-04-28
	#---------------------------------------------------------------------------
	function fn_ajax_column_list_more() {
		# request
		$page                   = trim($this->input->get_post('page', TRUE));

		# paging 설정 -- 2page 부터 시작이고 1페이지는 6개였음.
		$limit  = 10;
		$offset = ($page-2) * $limit;
		$offset = $offset + 6;

		# 검색
		$req_data = array();
		$req_data['sort_column']        = 'reg_date';
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;

		# model - list
		$this->load->model('web/m_column');
		$arr_column     = $this->m_column->column_list($req_data);
		$total_count    = $this->m_column->column_total_count();


		$more_list = 'N';
		if(($total_count-($page*$limit)) > 0){
			$more_list = 'Y';
		}

		# view data
		$return_data = array();
		$return_data['result_code']         = '0000';
		$return_data['total_count']         = $total_count;
		$return_data['list']                = $arr_column;
		$return_data['more_list']           = $more_list;

		# view
		return $this->load->view('/m/news/column_list_more', $return_data);

	}






	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 컬럼 뷰
	# CREATE  :	2018-04-28
	#---------------------------------------------------------------------------
	function fn_column_view() {
		# Request
		$idx                   = $this->uri->segment(4);

		$SES_MEMBER_ID         = $this->session->userdata('C_Member_Id');
		$content_type          = '2';


		$bookmark_count = 0;
		if($SES_MEMBER_ID !='') {
			# 게시물 열람 불가 체크
			$req_data = array();
			$req_data['member_id']  = $SES_MEMBER_ID;
			$req_data['limit_type'] = '2';

			# model
			$this->load->model('web/m_blind_limit');
			$arr_blind = $this->m_blind_limit->blind_limit_where_member_id_limit_type_select($req_data);
			if(count($arr_blind) > 0) {
				$return_data['arr_blind'] = $arr_blind;
				$return_data['url'] = '/web/news/column';

				# view
				return $this->load->view('/web/comm/blind_view_alert', $return_data, true);
			}


			# bookmark
			$req_data = array();
			$req_data['member_id']      = $SES_MEMBER_ID;
			$req_data['content_type']   = $content_type;
			$req_data['content_idx']    = $idx;

			$this->load->model('web/m_bookmark');
			$bookmark_count   = $this->m_bookmark->bookmark_member_id_content_idx_count($req_data);
		}

		# hit count update
		$req_data = array();
		$req_data['content_type']   = $content_type;
		$req_data['content_idx']    = $idx;
		$req_data['member_id']      = $SES_MEMBER_ID;

		$this->load->library('comm');
		$this->comm->fn_hit_count_update($req_data);


		# model - column view
		$this->load->model('web/m_column');
		$result         = $this->m_column->column_select($idx);

		# 연관글
		$req_data = array();
		$req_data['content_type']       = $content_type;
		$req_data['content_idx']        = $idx;
		$this->load->model('web/m_relation_article');
		$arr_relation_column  = $this->m_relation_article->relation_article_join_column_list($req_data);


		# 영적기상도
		$this->load->model('web/m_weather');
		$arr_weather = $this->m_weather->weather_join_relation_article_join_news_where_news_idx_select($idx);

		# view data
		$return_data = array();
		$return_data['idx']                     = $idx;
		$return_data['arr_relation_column']     = $arr_relation_column;
		$return_data['arr_weather']             = $arr_weather;
		$return_data['bookmark_count']          = $bookmark_count;
		$return_data['result']                  = $result;


		# view
		return $this->load->view('/m/news/column_view', $return_data, true);


	}


	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 프린트
	# CREATE  :	2018-04-16
	#---------------------------------------------------------------------------
	function fn_nonskin_news_print() {

		# view
		return $this->load->view('/m/news/news_print', '');


	}


	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 칼럼니스트 메인
	# CREATE  :	2018-04-28
	#---------------------------------------------------------------------------
	function fn_columnist() {
		# Request
		$page           = trim($this->input->get_post('page', TRUE));
		$search_sort    = trim($this->input->get_post('search_sort', TRUE));
		$partner_idx    = $this->uri->segment(4);
		$SES_MEMBER_ID  = $this->session->userdata('C_Member_Id');
		$content_type   = '2';

		# default setting
		if($search_sort == '')  $search_sort = '1';
		if($page=='')           $page = '1';

		# sort
		switch ($search_sort) {
			case '1': $sort_column = 'display_date'; break;
			case '2': $sort_column = 'popular_count'; break;
			default : $sort_column = 'display_date'; break;
		}

		$column_delivery_count = 0;
		if($SES_MEMBER_ID != '') {
			# 배달여부
			$req_data = array();
			$req_data['content_type']   = $content_type;
			$req_data['main_idx']       = $partner_idx;
			$req_data['member_id']      = $SES_MEMBER_ID;
			$this->load->model('web/m_delivery');
			$arr_column_delivery = $this->m_delivery->delivery_where_member_id_main_idx_select($req_data);
			$column_delivery_count = count($arr_column_delivery);

		}


		# 칼럼정보
		# model - list
		$this->load->model('web/m_partner');
		$arr_columnist  = $this->m_partner->partner_select($partner_idx);
		if(count($arr_columnist)==0) {
			gf_alert("잘못된 접근입니다.", '/');
			exit;
		}


		# paging 설정
		$limit  = 10;
		$offset = ($page-1) * $limit;

		# 칼럼 리스트
		$req_data = array();
		$req_data['sort_column']        = $sort_column;
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;
		$req_data['search_type']        = 'partner_idx';
		$req_data['search_key']         = $partner_idx;

		# model - list
		$this->load->model('web/m_column');
		$result         = $this->m_column->column_list($req_data);
		$total_count    = $this->m_column->column_list_total_count($req_data);

		# page
		$max_page   = ceil($total_count / $limit);
		$pagination =  gf_paging($limit, $page, $max_page, '/web/news/columnist/'.$partner_idx.'?search_sort='.$search_sort.'&page=');



		# view data
		$return_data = array();
		$return_data['list']                        = $result;
		$return_data['total_count']                 = $total_count;
		$return_data['search_sort']                 = $search_sort;
		$return_data['pagination']                  = $pagination;
		$return_data['arr_columnist']               = $arr_columnist;
		$return_data['column_delivery_count']       = $column_delivery_count;


		# view
		return $this->load->view('/m/news/columnist', $return_data, true);


	}



	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 랭킹
	# CREATE  :	2018-04-28
	#---------------------------------------------------------------------------
	function fn_ranking() {
		# Request
		$page           = trim($this->input->get_post('page', TRUE));
		$ranking_type   = trim($this->input->get_post('ranking_type', TRUE));
		$content_type   = 1;

		# default setting
		if($ranking_type == '')     $ranking_type = '1';
		if($page=='')               $page = '1';


		# paging 설정
		$limit  = 10;
		$offset = ($page-1) * $limit;

		$req_data = array();
		$req_data['ranking_type']   = $ranking_type;
		$req_data['content_type']   = $content_type;
		$req_data['offset']         = $offset;
		$req_data['limit']          = $limit;
		$this->load->model('web/m_ranking');
		$result                 = $this->m_ranking->ranking_join_news_list($req_data);
		$total_count            = $this->m_ranking->ranking_join_news_total_count($req_data);


		# page
		$max_page   = ceil($total_count / $limit);
		$pagination =  gf_paging($limit, $page, $max_page, '/web/news/ranking?ranking_type='.$ranking_type.'&page=');


		# news setting
		$this->load->model('web/m_contents_setting');
		$arr_news_setting   = $this->m_contents_setting->contents_setting_select(1);


		# view data
		$return_data = array();
		$return_data['total_count']                 = $total_count;
		$return_data['list']                        = $result;
		$return_data['pagination']	                = $pagination;
		$return_data['ranking_type']	            = $ranking_type;
		$return_data['arr_news_setting']	        = $arr_news_setting;

		# view
		return $this->load->view('/m/news/ranking', $return_data, true);


	}


	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 카테고리
	# CREATE  :	2018-04-28
	#---------------------------------------------------------------------------
	function fn_category() {
		# Request
		$page           = trim($this->input->get_post('page', TRUE));
		$search_sort    = trim($this->input->get_post('search_sort', TRUE));
		$category_code  = $this->uri->segment(4);

		$this->load->helper('url');

		if($category_code=='') {
			redirect(WWW_CROSSMAP.'/web/news/');
			exit;
		}

		# default setting
		if($search_sort == '')  $search_sort = '1';
		if($page=='')           $page = '1';

		# sort
		switch ($search_sort) {
			case '1': $sort_column = 'display_date'; break;
			case '2': $sort_column = 'popular_count'; break;
			default : $sort_column = 'display_date'; break;
		}

		# paging 설정
		$limit  = 10;
		$offset = ($page-1) * $limit;

		# 뉴스
		$req_data = array();
		$req_data['sort_column']        = $sort_column;
		$req_data['offset']             = $offset;
		$req_data['limit']              = $limit;
		$req_data['search_type']        = 'category_code';
		$req_data['search_keyword']     = $category_code;


		# model - list
		$this->load->model('web/m_news');
		$result         = $this->m_news->news_list($req_data);
		$total_count    = $this->m_news->news_list_total_count($req_data);


		$category_count = strlen($category_code);
		$category_code1 = substr($category_code, 0, 2);
		$category_code2 = ''; $category_code3 = '';
		if($category_count == 6)    $category_code3 = substr($category_code, 0, 6);
		if($category_count == 4 || $category_count == 6)    $category_code2 = substr($category_code, 0, 4);


		# 카테고리
		$this->load->model('web/m_news_section');
		$arr_category   = $this->m_news_section->news_section_select($category_code);
		$arr_category1   = $this->m_news_section->news_section_select($category_code1);

		$arr_category2 = array();
		if($category_code2 != '')   $arr_category2   = $this->m_news_section->news_section_select($category_code2);

		$arr_category3 = array();
		if($category_code3 != '')   $arr_category3   = $this->m_news_section->news_section_select($category_code3);


		# page
		$max_page   = ceil($total_count / $limit);
		$pagination =  gf_paging($limit, $page, $max_page, '/m/news/category/'.$category_code.'?search_sort='.$search_sort.'&page=');



		# view data
		$return_data = array();
		$return_data['total_count']                 = $total_count;
		$return_data['list']                        = $result;
		$return_data['pagination']	                = $pagination;
		$return_data['search_sort']	                = $search_sort;
		$return_data['category_code']	            = $category_code;
		$return_data['arr_category']	            = $arr_category;
		$return_data['arr_category1']	            = $arr_category1;
		$return_data['arr_category2']	            = $arr_category2;
		$return_data['arr_category3']	            = $arr_category3;

		# view
		return $this->load->view('/m/news/category', $return_data, true);


	}


	#---------------------------------------------------------------------------
	# PURPOSE : 뉴스 댓글
	# CREATE  :	2018-04-29
	#---------------------------------------------------------------------------
	function fn_news_comment() {
		# Request
		$idx                   = $this->uri->segment(4);

		# model - news view
		$this->load->model('web/m_news');
		$result         = $this->m_news->news_join_news_section_press_select($idx);


		# 영적기상도
		$this->load->model('web/m_weather');
		$arr_weather = $this->m_weather->weather_join_relation_article_join_news_where_news_idx_select($idx);



		# view data
		$return_data = array();
		$return_data['idx']                     = $idx;
		$return_data['arr_weather']             = $arr_weather;
		$return_data['result']                  = $result;


		# view
		return $this->load->view('/m/news/news_comment', $return_data, true);


	}


	#---------------------------------------------------------------------------
	# PURPOSE : 칼럼 댓글
	# CREATE  :	2018-05-14
	#---------------------------------------------------------------------------
	function fn_column_comment() {
		# Request
		$idx                   = $this->uri->segment(4);

		# model - column view
		$this->load->model('web/m_column');
		$result         = $this->m_column->column_select($idx);

		# 영적기상도
		$this->load->model('web/m_weather');
		$arr_weather = $this->m_weather->weather_join_relation_article_join_news_where_news_idx_select($idx);



		# view data
		$return_data = array();
		$return_data['idx']                     = $idx;
		$return_data['arr_weather']             = $arr_weather;
		$return_data['result']                  = $result;


		# view
		return $this->load->view('/m/news/column_comment', $return_data, true);


	}


	function fn_ajax_news_more(){

						$ci = $this;
						$ci->db->from("news");
		$ci->db->select("*, `news`.idx as content_idx");
		$ci->db->join('press', 'press.idx = news.press_idx');
						$ci->db->where("news.is_del","N");
						$ci->db->where("news.is_display","Y");

						$ci->db->order_by("news.reg_date","desc");
						$offset = $_GET['page'] * 6;
						$ci->db->limit(6,$offset);
						$res = $ci->db->get();
						$result = $res->result_array();
//						echo $ci->db->last_query();
//						exit();
						# news setting
						$this->load->model('web/m_contents_setting');
						$arr_news_setting   = $this->m_contents_setting->contents_setting_select(1);

						foreach($result as $data):
						?>


						<div class="news_list_cont">
							<div class="art-img2<?=($data['video_url']) ? " plays_b" : ""; ?>"><a href="/m/news/news_view/<?=$data['idx']?>"><img src="<?=$data['main_img']?>" width="160"></a></div>
							<div class="art-info2">
								<h2 class="art-title">
									<a href="/m/news/news_view/<?=$data['idx']?>"><?=$data['title']?></a>
								</h2>
								<p class="art-info"><?
									$a = time();
									$b = strtotime($result[0]['update_date']);
									echo floor(($a-$b)/3600);

									?>시간 전
									 <? if($arr_news_setting['etc_char1']=='Y') {?>
										 |
										 <a href="<?=$data['homepage']?>"><?=$data['press_name']?></a>
									<?}?>
									<span class="feedback fr">
										<img src="/img/mobile/img_comments.gif"> <?=$data['comment_count']?>+</span>
								</p>
							</div>
						</div>
						<?endforeach;
						exit();

	}
}

?>
