<?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/web/cm_controller.php');
class search extends cm_Controller {



    #---------------------------------------------------------------------------
    # PURPOSE : 검색메인
    # CREATE  :	2018-04-04
    #---------------------------------------------------------------------------
	function fn_index() {
	    # Request
        $search_keyword    = trim($this->input->get_post('search_keyword', TRUE));


        $search_keyword_tmp     = urldecode($search_keyword);
        $search_keyword_tmp     = addslashes($search_keyword_tmp);

        # 연관검색어
        $req_data = array();
        $req_data['keyword']    = $search_keyword_tmp;
        $req_data['offset']     = 0;
        $req_data['limit']      = 10;
        $this->load->model('web/m_search_keyword_relation');
        $arr_search_keyword_relation    = $this->m_search_keyword_relation->search_keyword_relation_list($req_data);


        $search_keyword_txt = gf_search_keyword_fulltext_string($search_keyword_tmp);


        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_txt;
        $req_data['offset']                 = 0;
        $req_data['limit']                  = 3;
        $req_data['sort_column']            = 'display_date';

        # 라이프
        $this->load->model('web/m_life');
        $arr_life         = $this->m_life->life_list($req_data);

        # 뉴스
        $this->load->model('web/m_news');
        $arr_news         = $this->m_news->news_list($req_data);

        # 칼럼
        $this->load->model('web/m_column');
        $arr_column        = $this->m_column->column_list($req_data);
        

        # 뉴스 - 관련 뉴스
        $this->load->model('web/m_relation_article');
        $arr_news_relation_article = array();
        foreach($arr_news as $val) {
            $req_data = array();
            $req_data['content_type']       = '1';
            $req_data['content_idx']        = $val['idx'];
            $arr_news_relation_article[$val['idx']] = $this->m_relation_article->relation_article_join_news_list($req_data);
        }

        $search_keyword_txt     = addslashes($search_keyword);


        # 동영상
        $req_data = array();
        $req_data['search_type']            = 'main_search_and_video_url';
        $req_data['search_keyword']         = $search_keyword_tmp;
        $req_data['offset']                 = 0;
        $req_data['limit']                  = 5;
        $req_data['sort_column']            = 'display_date';

        $arr_life_video     = $this->m_life->life_list($req_data);
        $arr_news_video     = $this->m_news->news_list($req_data);
        $arr_column_video   = $this->m_column->column_list($req_data);
        $arr_video = array_merge($arr_life_video, $arr_news_video, $arr_column_video);
        $arr_display_date = array();
        foreach ($arr_video as $key=>$val) {
            $arr_display_date[$key]     = $val['display_date'];
        }
        array_multisort($arr_display_date, SORT_DESC, $arr_video);


        # 이미지
        $arr_img = array_merge($arr_life, $arr_news, $arr_column);
        $arr_display_date = array();
        foreach ($arr_img as $key=>$val) {
            $arr_display_date[$key]     = $val['display_date'];
        }
        array_multisort($arr_display_date, SORT_DESC, $arr_img);

        $total_count = count($arr_img);




        $return_data = array();
        $return_data['arr_life']                    = $arr_life;
        $return_data['arr_news']                    = $arr_news;
        $return_data['arr_column']                  = $arr_column;
        $return_data['search_keyword']              = $search_keyword;
        $return_data['arr_search_keyword_relation'] = $arr_search_keyword_relation;
        $return_data['arr_img']                     = $arr_img;
        $return_data['arr_video']                   = $arr_video;
        $return_data['total_count']                 = $total_count;
        $return_data['arr_news_relation_article']   = $arr_news_relation_article;


        // $this->load->library('remotesearch');
        // $return_data['wikiResult'] = $this->remotesearch->wikipedia($return_data['search_keyword']);
        $return_data['wikiResult'] = [];

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

    #---------------------------------------------------------------------------
    # PURPOSE :	검색어 찾기
    # CREATE  :	2018-05-02
    #---------------------------------------------------------------------------
	function fn_ajax_search_keyword() {
	    # request
        $keyword    = trim($this->input->get_post('keyword', TRUE));

        # select
        $req_data = array();
        $req_data['search_type']        = 'keyword';
        $req_data['search_keyword']     = $keyword;
        $req_data['offset']             = 0;
        $req_data['limit']              = 15;
        $this->load->model('web/m_search_keyword');
        $result_tmp = $this->m_search_keyword->search_keyword_list($req_data);

        $result = array();
        foreach ($result_tmp as $key=>$val) {
            $result[$key]['keyword'] = str_replace("'", "\\'", $val['keyword']);

        }

        if(count($result) > 0) {
            $return_data['result'] = array('code' => '0000', 'data' => $result);
        } else {
            $return_data['result'] = array('code' => '9999');
        }


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



    }


    #---------------------------------------------------------------------------
    # PURPOSE :	검색어 찾기
    # CREATE  :	2018-05-02
    #---------------------------------------------------------------------------
	function fn_ajax_search_keyword_reg() {
	    # request
        $keyword        = trim($this->input->get_post('keyword', TRUE));
        $SES_MEMBER_ID  = $this->session->userdata('C_Member_Id');

        $this->load->library('comm');
        $this->load->model('web/m_search_keyword');
        $this->load->model('web/m_search_keyword_relation');

        $arr_keyword = explode(" ", $keyword);
        if(count($arr_keyword) > 0) {
            array_unshift($arr_keyword, $keyword);
            foreach ($arr_keyword as $key =>$val) {

                # 금칙어
                $forbidden_word_count = $this->comm->fn_forbidden_word($val);
                if($forbidden_word_count > 0) {
                    throw new Cm_Exception('json', '9999', '검색할수 없는 단어가 있습니다.');
                    exit;
                }

                # 검색키워드 넣기
                $req_data = array();
                $req_data['keyword']    = $val;
                $result = $this->m_search_keyword->search_keyword_where_keyword_select($req_data);
                if($result > 0) {
                    $this->m_search_keyword->search_keyword_search_count_update($result['idx']);

                } else {
                    $req_data = array();
                    $req_data['keyword'] = $val;
                    $this->m_search_keyword->search_keyword_insert($req_data);
                }

                # 연관검색어
                if($key > 1) {
                    $req_data = array();
                    $req_data['keyword']            = $arr_keyword[1];
                    $req_data['relation_keyword']   = $val;

                    $result1 = $this->m_search_keyword_relation->search_keyword_relation_where_keyword_relation_keyword_select($req_data);
                    if($result1 > 0) {
                        $this->m_search_keyword_relation->search_keyword_relation_search_count_update($result1['idx']);

                    } else {
                        $req_data = array();
                        $req_data['keyword']            = $arr_keyword[1];
                        $req_data['relation_keyword']   = $val;
                        $this->m_search_keyword_relation->search_keyword_relation_keyword_insert($req_data);
                    }


                }

            }
        }

        # log insert
        $req_data = array();
        $req_data['member_id']  = $SES_MEMBER_ID;
        $req_data['keyword']    = $keyword;
        $this->load->model('web/m_search_keyword_log');
        $this->m_search_keyword_log->search_keyword_log_insert($req_data);


        $return_data['result'] = array('code' => '0000');

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



    }


    #---------------------------------------------------------------------------
    # PURPOSE : 이미지
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function fn_image() {
	    # Request
        $search_keyword    = trim($this->input->get_post('search_keyword', TRUE));

        $search_keyword_tmp     = urldecode($search_keyword);
        $search_keyword_tmp     = addslashes($search_keyword_tmp);

        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_tmp;
        $req_data['offset']                 = 0;
        $req_data['limit']                  = 100;

        $this->load->model('web/m_vw_content');
        $total_count    = $this->m_vw_content->vw_content_list_total_count($req_data);

        $return_data = array();
        $return_data['search_keyword']              = $search_keyword;
        $return_data['total_count']                 = $total_count;
        $return_data['limit']                       = 100;



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




    #---------------------------------------------------------------------------
    # PURPOSE : 이미지 더가져오기
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function fn_ajax_image_list_more() {
	    # request
        $page           = trim($this->input->get_post('page', TRUE));
        $search_keyword = trim($this->input->get_post('search_keyword', TRUE));

        $search_keyword_tmp     = urldecode($search_keyword);
        $search_keyword_tmp     = addslashes($search_keyword_tmp);


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

        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_tmp;
        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;




        $this->load->model('web/m_vw_content');
        $list           = $this->m_vw_content->vw_content_list($req_data);
        $total_count    = $this->m_vw_content->vw_content_list_total_count($req_data);

        $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']                = $list;
        $return_data['more_list']           = $more_list;
        $return_data['limit']               = $limit;
        $return_data['page']                = $page;
        $return_data['view_count']          = $offset;
        $return_data['search_keyword_tmp']  = $search_keyword_tmp;





        return $this->load->view('/web/search/image_list_more', $return_data);



    }



    #---------------------------------------------------------------------------
    # PURPOSE :	이미지, 동영상 신고
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function fn_ajax_spam_proc() {
        $spam_type          = trim($this->input->get_post('spam_type', TRUE));
        $spam_list          = $this->input->get_post('spam_list', TRUE);
        $SES_MEMBER_ID      = $this->session->userdata('C_Member_Id');

        if($spam_type=='')          throw new Cm_Exception('json', '9999', '신고할 내용이 없습니다.');
        if(count($spam_list)==0)    throw new Cm_Exception('json', '9999', '신고할 내용이 없습니다.');
        if($SES_MEMBER_ID == '')    throw new Cm_Exception('json', '9999', '로그인을 하셔야 가능합니다.');

        $this->load->model('web/m_spam_media_log');

        foreach ($spam_list as $val) {
            $arr_content = explode("_", $val);
            $content_type   = $arr_content[0];
            $content_idx    = $arr_content[1];
            if($content_type=='')       throw new Cm_Exception('json', '9999', '신고할 내용이 없습니다.');
            if($content_idx=='')        throw new Cm_Exception('json', '9999', '신고할 내용이 없습니다.');

            # insert
            $req_data = array();
            $req_data['member_id']      = $SES_MEMBER_ID;
            $req_data['spam_type']      = $spam_type;
            $req_data['content_type']   = $content_type;
            $req_data['content_idx']    = $content_idx;
            $this->m_spam_media_log->spam_media_log_insert($req_data);

        }

        $return_data['result'] = array('code' => '0000');

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



    }


    #---------------------------------------------------------------------------
    # PURPOSE : 라이프, 칼럼
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function fn_list() {
	    # Request
        $search_keyword = trim($this->input->get_post('search_keyword', TRUE));
        $content_type   = trim($this->input->get_post('content_type', TRUE));
        $page           = trim($this->input->get_post('page', TRUE));

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

        $search_keyword_tmp     = urldecode($search_keyword);
        $search_keyword_tmp     = addslashes($search_keyword_tmp);


        $search_keyword_txt = gf_search_keyword_fulltext_string($search_keyword_tmp);

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

        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_txt;
        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;
        $req_data['sort_column']            = 'display_date';

        if($content_type == '3') {
            # 라이프
            $this->load->model('web/m_life');
            $list           = $this->m_life->life_list($req_data);
            $total_count    = $this->m_life->life_list_total_count($req_data);
            $page_title = '라이프';

        } else if($content_type == '2') {
            # 칼럼
            $this->load->model('web/m_column');
            $list           = $this->m_column->column_list($req_data);
            $total_count    = $this->m_column->column_list_total_count($req_data);
            $page_title = '칼럼';

        }

        # page
        $max_page   = ceil($total_count / $limit);
        $pagination =  gf_paging($limit, $page, $max_page, '/web/search/list?search_keyword='.urlencode($search_keyword).'&content_type='.$content_type.'&page=');


        $start_row  = $offset+1;
        $end_row    = $limit*$page;

        if($end_row >= $total_count)    $end_row = $total_count;


        $return_data = array();
        $return_data['search_keyword']              = $search_keyword;
        $return_data['list']                        = $list;
        $return_data['total_count']                 = $total_count;
        $return_data['page']                        = $page;
        $return_data['start_row']                   = $start_row;
        $return_data['end_row']                     = $end_row;
        $return_data['pagination']                  = $pagination;
        $return_data['page_title']                  = $page_title;
        $return_data['content_type']                = $content_type;

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


    #---------------------------------------------------------------------------
    # PURPOSE : 영상
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function fn_video() {
	    # Request
        $search_keyword = trim($this->input->get_post('search_keyword', TRUE));
        $page           = trim($this->input->get_post('page', TRUE));

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


        $search_keyword_tmp     = urldecode($search_keyword);
        $search_keyword_tmp     = addslashes($search_keyword_tmp);


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

        $req_data = array();
        $req_data['search_type']            = 'main_search_and_video_url';
        $req_data['search_keyword']         = $search_keyword_tmp;
        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;

        $this->load->model('web/m_vw_content');
        $list           = $this->m_vw_content->vw_content_list($req_data);
        $total_count    = $this->m_vw_content->vw_content_list_total_count($req_data);




        # page
        $max_page   = ceil($total_count / $limit);
        $pagination =  gf_paging($limit, $page, $max_page, '/web/search/video?search_keyword='.urlencode($search_keyword).'&page=');


        $start_row  = $offset+1;
        $end_row    = $limit*$page;

        if($end_row >= $total_count)    $end_row = $total_count;

        $return_data = array();
        $return_data['search_keyword']              = $search_keyword;
        $return_data['list']                        = $list;
        $return_data['total_count']                 = $total_count;
        $return_data['page']                        = $page;
        $return_data['start_row']                   = $start_row;
        $return_data['end_row']                     = $end_row;
        $return_data['pagination']                  = $pagination;



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



    #---------------------------------------------------------------------------
    # PURPOSE : 뉴스
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function fn_news() {
	    # Request
        $search_keyword = trim($this->input->get_post('search_keyword', TRUE));
        $page           = trim($this->input->get_post('page', TRUE));
        $search_sort    = trim($this->input->get_post('search_sort', TRUE));
        $content_idx    = trim($this->input->get_post('content_idx', TRUE));

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

        # sort
        switch ($search_sort) {
            case '1': $sort_column = 'display_date'; $sort_type = 'desc'; $sorting = SORT_DESC; break;
            case '2': $sort_column = 'display_date'; $sort_type = 'asc';  $sorting = SORT_ASC; break;
            default : $sort_column = 'display_date'; $sort_type = 'desc'; $sorting = SORT_DESC; break;
        }

        $search_keyword_tmp     = urldecode($search_keyword);
        $search_keyword_tmp     = addslashes($search_keyword_tmp);


        $search_keyword_txt = gf_search_keyword_fulltext_string($search_keyword_tmp);

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

        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_txt;
        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;
        $req_data['sort_column']            = $sort_column;
        $req_data['sort_type']              = $sort_type;

        $news_type = '';
        $arr_news_relation_article = array();
        if($content_idx == '') {
            # 뉴스
            $this->load->model('web/m_news');
            $list           = $this->m_news->news_list($req_data);
            $total_count    = $this->m_news->news_list_total_count($req_data);

        } else {
            # 뉴스
            $this->load->model('web/m_news');
            $list[] = $this->m_news->news_select($content_idx);
        }

        # 뉴스 - 관련 뉴스
        $this->load->model('web/m_relation_article');
        foreach($list as $val) {
            $req_data = array();
            $req_data['content_type']       = '1';
            $req_data['content_idx']        = $val['idx'];
            $arr_news_relation_article[$val['idx']] = $this->m_relation_article->relation_article_join_news_list($req_data);
        }

        if($content_idx != '') {
            $list = array_merge($list, $arr_news_relation_article[$content_idx]);
            $total_count = count($list);
            $news_type='relation';
            $arr_news_relation_article = array();

            $arr_display_date = array();
            foreach ($list as $key=>$val) {
                $arr_display_date[$key]     = $val['display_date'];
            }
            array_multisort($arr_display_date, $sorting, $list);

        }

        # page
        $max_page   = ceil($total_count / $limit);
        $pagination =  gf_paging($limit, $page, $max_page, '/web/search/news?search_keyword='.urlencode($search_keyword).'&content_idx='.$content_idx.'&search_sort='.$search_sort.'&page=');


        $start_row  = $offset+1;
        $end_row    = $limit*$page;

        if($end_row >= $total_count)    $end_row = $total_count;


        $return_data = array();
        $return_data['search_keyword']              = $search_keyword;
        $return_data['list']                        = $list;
        $return_data['total_count']                 = $total_count;
        $return_data['page']                        = $page;
        $return_data['start_row']                   = $start_row;
        $return_data['end_row']                     = $end_row;
        $return_data['pagination']                  = $pagination;
        $return_data['search_sort']                 = $search_sort;
        $return_data['arr_news_relation_article']   = $arr_news_relation_article;
        $return_data['news_type']                   = $news_type;
        $return_data['content_idx']                 = $content_idx;


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

    #---------------------------------------------------------------------------
    # PURPOSE : 이미지 뷰
    # CREATE  :	2018-05-04
    #---------------------------------------------------------------------------
	function fn_ajax_image_view() {
	    # request
        $content_type   = trim($this->input->get_post('content_type', TRUE));
        $content_idx    = trim($this->input->get_post('content_idx', TRUE));
        $row_num        = trim($this->input->get_post('row_num', TRUE));
        $search_keyword = trim($this->input->get_post('search_keyword', TRUE));

        $search_keyword_tmp     = addslashes($search_keyword);



        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_tmp;
        $req_data['row_num']                = $row_num;

        $this->load->model('web/m_vw_content');
        $arr_next   = $this->m_vw_content->vw_content_next_select($req_data);
        $arr_prev   = $this->m_vw_content->vw_content_prev_select($req_data);

        if($content_type == '1') {
            $this->load->model('web/m_news');
            $result = $this->m_news->news_select($content_idx);

        } else if($content_type == '2') {
            $this->load->model('web/m_column');
            $result = $this->m_column->column_select($content_idx);

        } else if($content_type == '3') {
            $this->load->model('web/m_life');
            $result = $this->m_life->life_select($content_idx);
        }

        
        $req_data = array();
        $req_data['search_type']            = 'main_search';
        $req_data['search_keyword']         = $search_keyword_tmp;
        $req_data['offset']                 = 0;
        $req_data['limit']                  = 8;
        $req_data['idx']                    = $content_idx;
        $this->load->model('web/m_vw_content');
        $arr_relation_img   = $this->m_vw_content->vw_content_where_not_idx_list($req_data);



        # view data
        $return_data = array();
        $return_data['result_code']         = '0000';
        $return_data['result']              = $result;
        $return_data['arr_relation_img']    = $arr_relation_img;
        $return_data['content_type']        = $content_type;
        $return_data['arr_next']            = $arr_next;
        $return_data['arr_prev']            = $arr_prev;
        $return_data['search_keyword']      = $search_keyword;

        # view
        return $this->load->view('/web/search/image_view', $return_data);


    }










}

?>
