<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 인클루드 class
 *
 * cm_controller: 부모 Class(CI_Controller 확장한 custome class)
 */

include_once (APPPATH.'core/admin/cm_controller.php');

class setting extends cm_controller {

	#---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 기본정보
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_site_reg() {

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_info_select();
        if($return_data == '') {
            $return_data['homepage_title'] = '';
            $return_data['admin_logo'] = '';
            $return_data['footer_script'] = '';
            $return_data['webmaster_name'] = '';
            $return_data['webmaster_email'] = '';
        }

        # view
        return $this->load->view('/admin/setting/setting_site_reg', $return_data, true);

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 기본정보 저장
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_site_reg_proc() {

	    $homepage_title     = trim($this->input->get_post('homepage_title', TRUE));     // 홈페이지제목
        $footer_script      = trim($this->input->get_post('footer_script', FALSE));      // 하단스크립트
        $webmaster_name     = trim($this->input->get_post('webmaster_name', TRUE));     // 웹마스터이름
        $webmaster_email    = trim($this->input->get_post('webmaster_email', TRUE));    // 웹마스터이메일
        $is_del_admin_logo  = trim($this->input->get_post('is_del_admin_log', TRUE));   // 관리자페이지로고 - 삭제여부

        # valid check
        $this->load->library('form_validation');
        $this->form_validation->set_rules('homepage_title', '홈페이지 제목', 'required');
        $this->form_validation->set_rules('webmaster_name', '웹마스터 이름', 'required');
        $this->form_validation->set_rules('webmaster_email', '웹마스터 이메일주소', 'required');

        if($this->form_validation->run() == false){
            gf_alert("정보를 정확히 입력해주세요.", '/admin/main');
            exit;
        }

        $footer_script = htmlspecialchars($footer_script);

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_info_select();
        $org_filename = '';
        if($return_data != '') {
            $org_filename   = $return_data['admin_logo'];
            $footer_content = $return_data['footer_content'];
        }

        # 이미지 삭제
        if($is_del_admin_logo=='Y') {
            unlink('.'.$org_filename);
            $org_filename = '';
        }

        if($_FILES['admin_logo']['name']!='') {
             # image upload
            $upload_path = './files/setting/'.date("Y")."/".date("m");
            if (!is_dir($upload_path)) {
                mkdir($upload_path, 0777, TRUE);
            }
            $config['upload_path']      = $upload_path;
            $config['allowed_types']    = 'gif|jpg|png';
            $config['encrypt_name']     = TRUE;

            $this->load->library('upload');
            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('admin_logo')) {
                $error = array('error' => $this->upload->display_errors());
                gf_alert("이미지 업로드에 실패했습니다.", '/admin/setting/setting_site_reg/1');
                exit;
            }
            $arr_upload_data = $this->upload->data();
            $filename = str_replace($_SERVER['DOCUMENT_ROOT'], '', $arr_upload_data['full_path']);

        } else {
            $filename = $org_filename;
        }

        $req_data['homepage_title']     = $homepage_title;
        $req_data['admin_logo']         = $filename;
        $req_data['footer_script']      = $footer_script;
        $req_data['webmaster_name']     = $webmaster_name;
        $req_data['webmaster_email']    = $webmaster_email;

        if($return_data != '') {    // 수정
            $idx          = $return_data['idx'];
            $req_data['idx']   = $idx;

            # model - update
            $this->load->model('admin/setting/m_setting_site');
            $this->m_setting_site->site_info_update($req_data);

        } else {    // 등록
            # model - update
            $this->load->model('admin/setting/m_setting_site');
            $this->m_setting_site->site_info_insert($req_data);

        }

        gf_alert("저장되었습니다.", '/admin/setting/setting_site_reg/1');

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 접근기능
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_site_access_reg() {

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_access_ip_select();

        $arr_return_data['access_type_1'] = '';
        $arr_return_data['access_type_2'] = '';
        $arr_return_data['access_type_3'] = '';
        if($return_data != '') {
            foreach($return_data as $val) {
                $key = 'access_type_'.$val['access_type'];
                $arr_return_data[$key] .= $val['ip']."\r\n";;
            }
        }

        # view
        return $this->load->view('/admin/setting/setting_site_access_reg', $arr_return_data, true);

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 접근기능 저장
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function fn_setting_site_access_reg_proc() {

	    $access_type_1  = trim($this->input->get_post('access_type_1', TRUE));  // 관리자 페이지 접근가능 IP
	    $access_type_2  = trim($this->input->get_post('access_type_2', TRUE));  // 사이트 접근불가 IP
	    $access_type_3  = trim($this->input->get_post('access_type_3', TRUE));  // 사이트 접근가능 IP

        $arr_access_type_1 = explode("\r\n", $access_type_1);
        $arr_access_type_1 = array_values(array_filter(array_map('trim',$arr_access_type_1)));

        $arr_access_type_2 = explode("\r\n", $access_type_2);
        $arr_access_type_2 = array_values(array_filter(array_map('trim',$arr_access_type_2)));

        $arr_access_type_3 = explode("\r\n", $access_type_3);
        $arr_access_type_3 = array_values(array_filter(array_map('trim',$arr_access_type_3)));

        $req_data['access_type_1'] = $arr_access_type_1;
        $req_data['access_type_2'] = $arr_access_type_2;
        $req_data['access_type_3'] = $arr_access_type_3;

        # model - 접근기능 삭제
        $this->load->model('admin/setting/m_setting_site');
        $this->m_setting_site->site_access_ip_delete();

        # model - 금칙접근기능어 입력
        $this->load->model('admin/setting/m_setting_site');
        $this->m_setting_site->site_access_ip_insert($req_data);

        gf_alert("저장되었습니다.", '/admin/setting/setting_site_access_reg/1');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 하단푸터관리
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_footer_reg() {

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_info_select();
        if($return_data == '') {
            $return_data['footer_content'] = '';
        }

        # view
        return $this->load->view('/admin/setting/setting_footer_reg', $return_data, true);

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 하단푸터관리 저장
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_footer_reg_proc() {

	    $footer_content    = trim($this->input->get_post('footer_content', TRUE));     // 사이트 하단 정보입력

        # valid check
        $this->load->library('form_validation');
        $this->form_validation->set_rules('footer_content', '사이트 하단 정보입력', 'required');

        if($this->form_validation->run() == false){
            gf_alert("정보를 정확히 입력해주세요.", '/admin/main');
            exit;
        }

        $footer_content = htmlspecialchars($footer_content);


        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_info_select();

        $req_data['footer_content']     = $footer_content;

        if($return_data != '') {    // 수정
            $idx          = $return_data['idx'];
            $req_data['idx']   = $idx;

            # model - update
            $this->load->model('admin/setting/m_setting_site');
            $this->m_setting_site->site_info_footer_content_update($req_data);

        } else {    // 등록
            # model - update
            $this->load->model('admin/setting/m_setting_site');
            $this->m_setting_site->site_info_insert($req_data);

        }

        gf_alert("저장되었습니다.", '/admin/setting/setting_footer_reg/1');

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 금칙어설정
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_forbidden_word_reg() {

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->forbidden_word_select();
        if($return_data == '') {
            $return_data['forbidden_word'] = '';
        } else {
            $forbidden_word = '';
            foreach($return_data as $val) {
                if($forbidden_word!='')     $forbidden_word.=",";
                $forbidden_word.= $val['word'];
            }
            $return_data['forbidden_word'] = $forbidden_word;

        }

        # view
        return $this->load->view('/admin/setting/setting_forbidden_word_reg', $return_data, true);

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 금칙어설정 저장
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function fn_setting_forbidden_word_reg_proc() {

	    $forbidden_word    = trim($this->input->get_post('forbidden_word', TRUE));     // 단어 필터링

        # valid check
        $this->load->library('form_validation');
        $this->form_validation->set_rules('forbidden_word', '단어 필터링', 'required');

        if($this->form_validation->run() == false){
            gf_alert("정보를 정확히 입력해주세요.", '/admin/main');
            exit;
        }

        $forbidden_word = str_replace(" ", "", $forbidden_word);
        $arr_forbidden_word = explode(",", $forbidden_word);
        $req_data = array_values(array_filter(array_map('trim',$arr_forbidden_word)));

        # model - 금칙어 삭제
        $this->load->model('admin/setting/m_setting_site');
        $this->m_setting_site->forbidden_word_delete();

        # model - 금칙어 입력
        $this->load->model('admin/setting/m_setting_site');
        $this->m_setting_site->forbidden_word_insert($req_data);



        gf_alert("저장되었습니다.", '/admin/setting/setting_forbidden_word_reg/1');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 이용약관개인정보 취급방침
    # CREATE  :	2018-02-19
    #---------------------------------------------------------------------------
	function fn_setting_policy_reg() {

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_info_select();
        if($return_data == '') {
            $return_data['service'] = '';
            $return_data['privacy'] = '';
            $return_data['privacy_agreement'] = '';
        }

        # view
        return $this->load->view('/admin/setting/setting_policy_reg', $return_data, true);


    }

    #---------------------------------------------------------------------------
    # PURPOSE :	사이트정보설정 - 이용약관개인정보 취급방침 저장
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function fn_setting_policy_reg_proc() {

	    $service            = trim($this->input->get_post('service', TRUE));            // 이용약관
	    $privacy            = trim($this->input->get_post('privacy', TRUE));            // 개인정보 취급방침
	    $privacy_agreement  = trim($this->input->get_post('privacy_agreement', TRUE));  // 개인정보 수집 이용동의

        # valid check
        $this->load->library('form_validation');
        $this->form_validation->set_rules('service', '이용약관', 'required');
        $this->form_validation->set_rules('privacy', '개인정보 취급방침', 'required');
        $this->form_validation->set_rules('privacy_agreement', '개인정보 수집 이용동의', 'required');

        if($this->form_validation->run() == false){
            gf_alert("정보를 정확히 입력해주세요.", '/admin/main');
            exit;
        }

        $service            = htmlspecialchars($service);
        $privacy            = htmlspecialchars($privacy);
        $privacy_agreement  = htmlspecialchars($privacy_agreement);

        # model
        $this->load->model('admin/setting/m_setting_site');
        $return_data    = $this->m_setting_site->site_info_select();

        $req_data['service']            = $service;
        $req_data['privacy']            = $privacy;
        $req_data['privacy_agreement']  = $privacy_agreement;

        if($return_data != '') {    // 수정
            $idx          = $return_data['idx'];
            $req_data['idx']   = $idx;

            # model - update
            $this->load->model('admin/setting/m_setting_site');
            $this->m_setting_site->site_info_policy_update($req_data);

        } else {    // 등록
            # model - update
            $this->load->model('admin/setting/m_setting_site');
            $this->m_setting_site->site_info_insert($req_data);

        }

        gf_alert("저장되었습니다.", '/admin/setting/setting_policy_reg/1');

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실검환경설정
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_setting_reg() {

	    $idx = 1;

	    # model
        $this->load->model('admin/setting/m_realtime_keyword');
        $result    = $this->m_realtime_keyword->realtime_keyword_setting_select($idx);

        if(count($result) == 0) {
            gf_alert("잘못된 접근입니다.", '/admin/main');
            exit;
        }
        $return_data = $result[0];

        # view
        return $this->load->view('/admin/setting/realtime_keyword_setting_reg', $return_data, true);


    }



    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실검환경설정 저장
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_setting_reg_proc() {
        # request
        $base_hour              = trim($this->input->get_post('base_hour', TRUE));
        $delete_hour            = trim($this->input->get_post('delete_hour', TRUE));
        $is_no_result_keyword   = trim($this->input->get_post('is_no_result_keyword', TRUE));
        $idx = 1;

        # update data
        $req_data['idx']                    = $idx;
        $req_data['base_hour']              = $base_hour;
        $req_data['delete_hour']            = $delete_hour;
        $req_data['is_no_result_keyword']   = $is_no_result_keyword;

        # model - update
        $this->load->model('admin/setting/m_realtime_keyword');
        $this->m_realtime_keyword->realtime_keyword_setting_update($req_data);

        gf_alert('수정되었습니다.', '/admin/setting/realtime_keyword_setting_reg/1');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실시간검색어
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_list() {
        # model - 카테고리 list
        $this->load->model('admin/setting/m_realtime_keyword');
        $result              = $this->m_realtime_keyword->realtime_keyword_list();

        $return_data['list'] = array();
        if(count($result) > 0) {
            $return_data['list'] = $result;
        }

        # view
        return $this->load->view('/admin/setting/realtime_keyword_list', $return_data, true);

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실시간검색어 - 등록
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_add_proc() {
        # request
        $keyword        = trim($this->input->get_post('keyword', TRUE));
        $search_count   = trim($this->input->get_post('search_count', TRUE));

        # valid check
        $this->load->library('form_validation');
        $this->form_validation->set_rules('keyword', '', 'required');
        $this->form_validation->set_rules('search_count', '', 'required');
        if($this->form_validation->run() == false){
            gf_alert("잘못된 접근입니다.", '/admin/main');
            exit;
        }

        # model
        $this->load->model('admin/setting/m_realtime_keyword');
        $all_list_result    = $this->m_realtime_keyword->realtime_keyword_list();

        $all_list_result[]  = array('search_count'=>$search_count, 'keyword'=>$keyword, 'reg_date'=>date("Y-m-d H:i:s"));

//        echo '<pre>';
//        print_r($all_list_result);
//        echo '<pre>';
//        exit;



        $arr_search_count = array(); $arr_reg_date= array();
        foreach ($all_list_result as $key => $row) {
            $arr_search_count[$key]  = $row['search_count'];
            $arr_reg_date[$key]  = $row['reg_date'];
        }
        array_multisort($arr_search_count, SORT_DESC, $arr_reg_date, SORT_DESC, $all_list_result);



        $i=1;
        foreach ($all_list_result as $val) {
            if(isset($val['idx'])==false) {
                $insert_data = array();
                $insert_data['rank']            = $i;
                $insert_data['keyword']         = $val['keyword'];
                $insert_data['search_count']    = $val['search_count'];
                $this->m_realtime_keyword->realtime_keyword_insert($insert_data);
            } else {
                $update_data = array();
                $update_data[0]['rank']         = $i;
                $update_data[0]['last_rank']    = $val['rank'];
                $update_data[0]['idx']          = $val['idx'];
                $this->m_realtime_keyword->realtime_keyword_update($update_data);
            }

            $i++;

        }


        $msg = '저장되었습니다.';
        gf_alert($msg, '/admin/setting/realtime_keyword_list/1');

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실시간검색어 - 삭제
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_del_proc() {
        # request
        $arr_realtime_keyword_idx   = $this->input->get_post('is_del', TRUE);   // 삭제할 idx
        $search_year                = trim($this->input->get_post('search_year', TRUE));
        $search_month               = trim($this->input->get_post('search_month', TRUE));
        $search_day                 = trim($this->input->get_post('search_day', TRUE));

        # model - update
        $this->load->model('admin/setting/m_realtime_keyword');
        $this->m_realtime_keyword->realtime_keyword_is_del_update($arr_realtime_keyword_idx);

        gf_alert('삭제되었습니다.', '/admin/setting/realtime_keyword_list/1?search_year='.$search_year.'&search_month='.$search_month.'&search_day='.$search_day);

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실시간검색어 - 수정
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_edit_proc() {
        # request
        $arr_realtime_keyword_idx                   = $this->input->get_post('idx', TRUE);   //  idx
        $arr_realtime_keyword_edit_keyword          = $this->input->get_post('edit_keyword', TRUE);
        $arr_realtime_keyword_edit_search_count     = $this->input->get_post('edit_search_count', TRUE);

        # 랭킹계산
        $arr_realtime_keyword = array();
        foreach ($arr_realtime_keyword_idx as $key=>$val) {
            $arr_realtime_keyword[] = array('idx'=>$val, 'keyword' => $arr_realtime_keyword_edit_keyword[$key], 'search_count' =>$arr_realtime_keyword_edit_search_count[$key]);
        }
        array_multisort($arr_realtime_keyword_edit_search_count, SORT_DESC, $arr_realtime_keyword);

        # model - 카테고리 list
        $this->load->model('admin/setting/m_realtime_keyword');
        $list_result    = $this->m_realtime_keyword->realtime_keyword_list();

        $i = 1;
        foreach ($arr_realtime_keyword as $key=>$val1) {
            $arr_realtime_keyword[$key]['rank'] = $i;
            foreach($list_result as $val2) {
                if($val1['keyword'] == $val2['keyword']) {
                    $arr_realtime_keyword[$key]['last_rank'] = $val2['rank'];
                }
            }
            $i++;
        }

        # model - update
        $this->m_realtime_keyword->realtime_keyword_update($arr_realtime_keyword);

        gf_alert('수정되었습니다.', '/admin/setting/realtime_keyword_list/1');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실시간검색어 log
    # CREATE  :	2018-02-28
    #---------------------------------------------------------------------------
	function fn_realtime_keyword_log_list() {
        # request
	    $page           = $this->uri->segment(5);
        $start_date     = trim($this->input->get_post('start_date', TRUE));
        $end_date       = trim($this->input->get_post('end_date', TRUE));

        if($start_date=='')     $start_date = date("Y-m-d");
        if($end_date=='')       $end_date   = date("Y-m-d");

        # paging 설정
        if($page == '') $page = 1;
        $limit  = PAGE_SIZE;
        $offset = ($page-1) * $limit;


        # 검색
        $req_data['start_date']     = $start_date;
        $req_data['end_date']       = $end_date;
        $req_data['offset']         = $offset;
        $req_data['limit']          = $limit;

	    # model - 카테고리 list
        $this->load->model('admin/setting/m_realtime_keyword');
        $result             = $this->m_realtime_keyword->realtime_keyword_log_list($req_data);
        $total_count        = $this->m_realtime_keyword->realtime_keyword_log_list_total_count($req_data);

        $return_data['list'] = array();
        if(count($result) > 0) {
            $return_data['list'] = $result;
        }

        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/setting/realtime_keyword_log_list/1' ;
        $req_data['suffix']         = '?'.$_SERVER['QUERY_STRING'];

        $this->load->library('comm_admin');
        $pagination = $this->comm_admin->fn_pagination($req_data);




        # view data
		$return_data['pagination']	        = $pagination;
        $return_data['page']                = $page;
        $return_data['limit']               = $limit;
        $return_data['total_count']         = $total_count;
        $return_data['list']                = $result;
        $return_data['start_date']          = $start_date;
        $return_data['end_date']            = $end_date;

        # view
        return $this->load->view('/admin/setting/realtime_keyword_log_list', $return_data, true);

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	검색설정 - 실시간검색어 - excel download
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_doc_realtime_keyword_excel_download_proc() {
        # request
        $start_date     = trim($this->input->get_post('start_date', TRUE));
        $end_date       = trim($this->input->get_post('end_date', TRUE));

        # valid check
        $this->load->library('form_validation');
        $this->form_validation->set_rules('start_date', '', 'required');
        $this->form_validation->set_rules('end_date', '', 'required');
        if($this->form_validation->run() == false){
            gf_alert("정보를 정확히 입력해주세요.", '/admin/main');
            exit;
        }

        # 검색
        $req_data['start_date']     = $start_date;
        $req_data['end_date']       = $end_date;

        # model - 카테고리 list
        $this->load->model('admin/setting/m_realtime_keyword');
        $result              = $this->m_realtime_keyword->realtime_keyword_log_all_list($req_data);

        # excel download
        $this->load->library('excel');


        $this->excel->setActiveSheetIndex(0);
        $this->excel->getActiveSheet()->setTitle('Sheet1');

        # cell 헤더 설정
        $this->excel->getActiveSheet()->setCellValue('A1', '순위');
        $this->excel->getActiveSheet()->setCellValue('B1', '순위변동');
        $this->excel->getActiveSheet()->setCellValue('C1', '날짜');
        $this->excel->getActiveSheet()->setCellValue('D1', '검색어');
        $this->excel->getActiveSheet()->setCellValue('E1', '검색횟수');
        $this->excel->getActiveSheet()->getStyle('A1:E1')->getFont()->setSize(10);
        $this->excel->getActiveSheet()->getStyle('A1:E1')->getFont()->setBold(true);

        # cell 데이터 출력
        $i=2;
        foreach($result as $val) {
            $last_rank 		= $val['last_rank'];
			$rank		    = $val['rank'];
			$rank_diff 		= $last_rank - $rank;

			$rank_diff_txt = '';
			if($last_rank == 0) {
				$rank_diff_txt = 'new';
			} else {
				if($rank_diff == 0) {
					$rank_diff_txt = '-';
				} else if($rank_diff > 0) {
					$rank_diff_txt = "▲ ".$rank_diff;
				} else {
					$rank_diff_txt = "▼ ".abs($rank_diff);
				}
			}

            $this->excel->getActiveSheet()->setCellValue('A'.$i, $val['rank']);
            $this->excel->getActiveSheet()->setCellValue('B'.$i, $rank_diff_txt);
            $this->excel->getActiveSheet()->setCellValue('C'.$i, $val['reg_date']);
            $this->excel->getActiveSheet()->setCellValue('D'.$i, $val['keyword']);
            $this->excel->getActiveSheet()->setCellValue('E'.$i, $val['search_count']);
            $i++;
        }

        # 파일로 내보낸다.
        $filename = 'realtime_keyword_list_'.date('Ymd').'.xls';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header("Content-Type: text/html; charset=UTF-8");
        header('Cache-Control: max-age=0');

        # Excel5 포맷(excel 2003 .XLS file)으로 저장한다.
        # 두 번째 매개변수를 'Excel2007'로 바꾸면 Excel 2007 .XLSX 포맷으로 저장한다.
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');

        # 이용자가 다운로드하여 컴퓨터 HD에 저장하도록 강제한다.
        $objWriter->save('php://output');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	댓글설정 - 수정
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_setting_comment_reg() {
	    # request
        $setting_type    = trim($this->input->get_post('setting_type', TRUE));

        if($setting_type=='')   $setting_type = '3';

        $req_data['setting_type']   = $setting_type;

	    # model
        $this->load->model('admin/setting/m_comment_setting');
        $result    = $this->m_comment_setting->comment_setting_select($req_data);

        if(count($result) == 0) {
            gf_alert("잘못된 접근입니다.", '/admin/main');
            exit;
        }

        $return_data = $result[0];
        $return_data['setting_type']= $setting_type;

        # view
        return $this->load->view('/admin/setting/setting_comment_reg', $return_data, true);

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	댓글설정 - 수정 실행
    # CREATE  :	2018-02-23
    #---------------------------------------------------------------------------
	function fn_setting_comment_reg_proc() {
        # request
        $setting_type               = trim($this->input->get_post('setting_type', TRUE));
        $best_comment_like_count    = trim($this->input->get_post('best_comment_like_count', TRUE));
        $max_best_comment_count     = trim($this->input->get_post('max_best_comment_count', TRUE));
        $block_call_count           = trim($this->input->get_post('block_call_count', TRUE));
        $block_call_type            = trim($this->input->get_post('block_call_type', TRUE));
        $block_blind_conent         = trim($this->input->get_post('block_blind_conent', TRUE));
        $block_del_conent           = trim($this->input->get_post('block_del_conent', TRUE));
        $self_del_contnet           = trim($this->input->get_post('self_del_contnet', TRUE));

        # update data
        $req_data['setting_type']               = $setting_type;
        $req_data['best_comment_like_count']    = $best_comment_like_count;
        $req_data['max_best_comment_count']     = $max_best_comment_count;
        $req_data['block_call_count']           = $block_call_count;
        $req_data['block_call_type']            = $block_call_type;
        $req_data['block_blind_conent']         = $block_blind_conent;
        $req_data['block_del_conent']           = $block_del_conent;
        $req_data['self_del_contnet']           = $self_del_contnet;

        # model - update
        $this->load->model('admin/setting/m_comment_setting');
        $this->m_comment_setting->comment_setting_update($req_data);

        gf_alert('수정되었습니다.', '/admin/setting/setting_comment_reg/1?setting_type='.$setting_type);

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	신고관리 - 블라인드리스트
    # CREATE  :	2018-02-20
    #---------------------------------------------------------------------------
	function fn_blind_list() {
        # request
	    $page                   = $this->uri->segment(5);
        $search_type            = trim($this->input->get_post('search_type', TRUE));
        $search_keyword         = trim($this->input->get_post('search_keyword', TRUE));
        $search_content_type    = trim($this->input->get_post('search_content_type', TRUE));
        $search_spam_reason     = trim($this->input->get_post('search_spam_reason', TRUE));
        $search_status          = trim($this->input->get_post('search_status', TRUE));

        # paging 설정
        if($page == '') $page = 1;
        $limit  = PAGE_SIZE;
        $offset = ($page-1) * $limit;


        # 검색
        $req_data = array();
        $req_data['search_type']            = $search_type;
        $req_data['search_keyword']         = addslashes($search_keyword);
        $req_data['search_content_type']    = $search_content_type;
        $req_data['search_spam_reason']     = $search_spam_reason;
        $req_data['search_status']          = $search_status;

        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;

	    # model - 카테고리 list
        $this->load->model('admin/setting/m_blind');
        $total_count        = $this->m_blind->blind_list_total_count($req_data);
        $self_del_count     = $this->m_blind->blind_list_join_comment_status_count(2);
        $status_list        = $this->m_blind->blind_list_status_group_by_count($req_data);
        $result             = $this->m_blind->blind_list($req_data);




        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/setting/blind_list/1' ;
        $req_data['suffix']         = '?'.$_SERVER['QUERY_STRING'];

        $this->load->library('comm_admin');
        $pagination = $this->comm_admin->fn_pagination($req_data);



        # view data
		$return_data['pagination']	            = $pagination;
        $return_data['page']                    = $page;
        $return_data['limit']                   = $limit;
        $return_data['total_count']             = $total_count;
        $return_data['self_del_count']          = $self_del_count;
        $return_data['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_content_type']     = $search_content_type;
        $return_data['search_spam_reason']      = $search_spam_reason;
        $return_data['search_status']           = $search_status;
        $return_data['list']                    = $result;
        $return_data['status_list']             = $status_list;

        # view
        return $this->load->view('/admin/setting/blind_list', $return_data, true);




    }


    #---------------------------------------------------------------------------
    # PURPOSE :	블라인드 상태 변경
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function fn_blind_status_proc() {
        # request
        $idx                    = trim($this->input->get_post('idx', TRUE));
        $blind_status           = trim($this->input->get_post('status', TRUE));

        # update data
        $req_data['idx']                    = $idx;
        $req_data['blind_status']           = $blind_status;

        # model
        $this->load->model('admin/setting/m_blind');
        $return_data    = $this->m_blind->blind_update($req_data);
        if(!$return_data) {
            gf_alert("저장에 실패했습니다.", '/admin/setting/blind_list/1');
            exit;
        }


        if($blind_status==0) {  // 블라인드 해지
            $result = $this->m_blind->blind_select($idx);
            if (!$result) {
                gf_alert("저장에 실패했습니다.", '/admin/setting/blind_list/1');
                exit;
            }
            $comment_idx = $result['comment_idx'];


            # update data
            $req_data['idx'] = $comment_idx;
            $req_data['comment_status'] = 1;

            # model
            $this->load->model('admin/comm/m_comment');
            $this->m_comment->comment_blind_stop_update($req_data);


        }



        gf_alert("저장되었습니다.", '/admin/setting/blind_list/1');

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	블라인드 처리된 글보기
    # CREATE  :	2018-03-28
    #---------------------------------------------------------------------------
	function fn_blind_view() {
	    # request
        $page                   = trim($this->input->get_post('page', TRUE));
        $mode                   = trim($this->input->get_post('mode', TRUE));
        $idx                    = trim($this->input->get_post('idx', TRUE));
        $search_type            = trim($this->input->get_post('search_type', TRUE));
        $search_keyword         = trim($this->input->get_post('search_keyword', TRUE));
        $search_content_type    = trim($this->input->get_post('search_content_type', TRUE));
        $search_spam_reason     = trim($this->input->get_post('search_spam_reason', TRUE));
        $search_status          = trim($this->input->get_post('search_status', TRUE));
        $search_reason_type     = trim($this->input->get_post('search_reason_type', TRUE));

        # model
        $this->load->model('admin/setting/m_blind');
        $result    = $this->m_blind->blind_join_comment_select($idx);

        if(count($result)==0) {
            gf_alert("잘못된저장입니다.", '/admin/setting/blind_list/1');
            exit;
        }

        $comment_idx = $result[0]['comment_idx'];


        # 검색
        $req_data = array();
        $req_data['reason_type']            = $search_reason_type;
        $req_data['comment_idx']            = $comment_idx;

        $this->load->model('admin/comm/m_spam');
        $spam_list          = $this->m_spam->spam_where_comment_idx_list($req_data);
        $reason_type_list   = $this->m_spam->spam_where_comment_idx_reason_type_group_by_count($req_data);

        $return_data = array();
        $return_data['page']                    = $page;
        $return_data['mode']                    = $mode;
        $return_data['idx']                     = $idx;
        $return_data['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_content_type']     = $search_content_type;
        $return_data['search_spam_reason']      = $search_spam_reason;
        $return_data['search_status']           = $search_status;
        $return_data['result']                  = $result[0];
        $return_data['spam_list']               = $spam_list;
        $return_data['total_count']             = count($spam_list);
        $return_data['reason_type_list']        = $reason_type_list;

        # view
        return $this->load->view('/admin/setting/blind_view', $return_data, true);

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	신고관리 - 블라인드제재리스트
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function fn_blind_limit_list() {
        # request
	    $page                   = $this->uri->segment(5);
        $search_type            = trim($this->input->get_post('search_type', TRUE));
        $search_keyword         = trim($this->input->get_post('search_keyword', TRUE));
        $search_limit_type      = trim($this->input->get_post('search_limit_type', TRUE));
        $search_status          = trim($this->input->get_post('search_status', TRUE));
        $search_is_today        = trim($this->input->get_post('search_is_today', TRUE));


        # paging 설정
        if($page == '') $page = 1;
        $limit  = PAGE_SIZE;
        $offset = ($page-1) * $limit;


        # 검색
        $req_data = array();
        $req_data['search_type']            = $search_type;
        $req_data['search_keyword']         = addslashes($search_keyword);
        $req_data['search_limit_type']      = $search_limit_type;
        $req_data['search_status']          = $search_status;
        $req_data['search_is_today']        = $search_is_today;

        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;

	    # model - 카테고리 list
        $this->load->model('admin/setting/m_blind_limit');
        $total_count    = $this->m_blind_limit->blind_limit_list_total_count($req_data);
        $new_count      = $this->m_blind_limit->blind_limit_new_count($req_data);
        $status_list    = $this->m_blind_limit->blind_limit_status_group_by_count($req_data);
        $result         = $this->m_blind_limit->blind_limit_list($req_data);


        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/setting/blind_limit_list/1' ;
        $req_data['suffix']         = '?'.$_SERVER['QUERY_STRING'];

        $this->load->library('comm_admin');
        $pagination = $this->comm_admin->fn_pagination($req_data);



        # view data
		$return_data['pagination']	            = $pagination;
        $return_data['page']                    = $page;
        $return_data['limit']                   = $limit;
        $return_data['total_count']             = $total_count;
        $return_data['new_count']               = $new_count;
        $return_data['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_limit_type']       = $search_limit_type;
        $return_data['search_status']           = $search_status;
        $return_data['search_is_today']         = $search_is_today;
        $return_data['list']                    = $result;
        $return_data['status_list']             = $status_list;

        # view
        return $this->load->view('/admin/setting/blind_limit_list', $return_data, true);




    }


    #---------------------------------------------------------------------------
    # PURPOSE : 신고관리 - 블라인드제재상세
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function fn_ajax_blind_limit_detail() {
        # request
        $idx             = trim($this->input->get_post('idx', TRUE));

        # model
        $this->load->model('admin/setting/m_blind_limit');
        $result = $this->m_blind_limit->blind_limit_select($idx);

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

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

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	신고관리 - 블라인드제재 - 저장
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function fn_blind_limit_edit_proc() {
        # request
        $page                   = trim($this->input->get_post('page', TRUE));
        $search_type            = trim($this->input->get_post('search_type', TRUE));
        $search_keyword         = trim($this->input->get_post('search_keyword', TRUE));
        $search_limit_type      = trim($this->input->get_post('search_limit_type', TRUE));
        $search_status          = trim($this->input->get_post('search_status', TRUE));

        $idx                    = trim($this->input->get_post('idx', TRUE));
        $limit_day              = trim($this->input->get_post('limit_day', TRUE));
        $limit_type             = trim($this->input->get_post('limit_type', TRUE));
        $limit_status           = trim($this->input->get_post('limit_status', TRUE));
        $start_date             = trim($this->input->get_post('start_date', TRUE));
        $blind_setting_idx      = trim($this->input->get_post('blind_setting_idx', TRUE));
        $blind_id               = trim($this->input->get_post('blind_id', TRUE));

        $end_date   = date("Y-m-d H:i:s", strtotime("+".$limit_day." days"));

        if($limit_type == '3' || $limit_type == '4')    $limit_status = '2';
        
        if($limit_status == '2') {
            $end_date = date("Y-m-d H:i:s");
        }

        $req_data = array();
        $req_data['idx']            = $idx;
        $req_data['limit_day']      = $limit_day;
        $req_data['limit_type']     = $limit_type;
        $req_data['limit_status']   = $limit_status;
        $req_data['start_date']     = $start_date;
        $req_data['end_date']       = $end_date;

        # model
        $this->load->model('admin/setting/m_blind_limit');
        $result = $this->m_blind_limit->blind_limit_update($req_data);
        if(!$result) {
            gf_alert("제재수정에 실패했습니다.", 'back');
            exit;
        }

        $this->load->model('admin/member/m_member');
        $arr_member    = $this->m_member->member_where_member_id_select($blind_id);
        $member_email = '';
        if(isset($arr_member['member_email'])==true) {
            $member_email   = $arr_member['member_email'];
        }

        $this->load->model('admin/setting/m_blind_setting');
        $arr_blind_setting = $this->m_blind_setting->blind_setting_select($blind_setting_idx);

        $req_data = array();
        $req_data['limit_content']          = $arr_blind_setting['limit_content'];
        $req_data['limit_count']            = '';
        $req_data['member_email']           = $member_email;
        $req_data['member_id']              = $blind_id;
        $req_data['limit_type']             = $limit_type;
        $req_data['end_date']               = $end_date;

        $this->load->library('comm_admin');
        $this->comm_admin->fn_blind_limit_proc($req_data);


        $url = '/admin/setting/blind_limit_list/1/'.$page.'?search_type='.$search_type.'&search_keyword='.$search_keyword.'&search_limit_type='.$search_limit_type.'&search_status='.$search_status;
        gf_alert("저장되었습니다.", $url);
    }




    #---------------------------------------------------------------------------
    # PURPOSE :	신고관리 - 블라인드제재 - 삭제
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function fn_blind_limit_del_proc() {
        # request
        $idx                    = trim($this->input->get_post('idx', TRUE));

        # model
        $this->load->model('admin/setting/m_blind_limit');
        $this->m_blind_limit->blind_limit_is_del_update($idx);

        gf_alert("삭제되었습니다.", '/admin/setting/blind_limit_list/1');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	신고관리 - 블라인드 셋팅
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function fn_blind_setting_reg() {

	    # model
        $this->load->model('admin/setting/m_blind_setting');
        $result    = $this->m_blind_setting->blind_setting_list();
        if(count($result) == 0) {
            gf_alert("셋팅데이터가 없습니다.", '/admin/main');
            exit;
        }

        $return_data['list']                    = $result;

        # view
        return $this->load->view('/admin/setting/blind_setting_reg', $return_data, true);


    }

    #---------------------------------------------------------------------------
    # PURPOSE :	신고관리 - 블라인드 셋팅 저장
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function fn_blind_setting_reg_proc() {

	    $req_data = array();
	    for($i=1;$i<=5;$i++) {
	        $limit_count    = trim($this->input->get_post('limit_count'.$i, TRUE));
	        $limit_type     = trim($this->input->get_post('limit_type'.$i, TRUE));
	        $limit_day      = trim($this->input->get_post('limit_day'.$i, TRUE));
	        $limit_content  = trim($this->input->get_post('limit_content'.$i, TRUE));

            $limit_status = '1';
            if($limit_type == '0' || $limit_type == '3' || $limit_type == '4' ) {
                $limit_status = '2';
            }

            $req_data[] = array('idx' => $i
                              , 'limit_count' => $limit_count
                              , 'limit_type' => $limit_type
                              , 'limit_status' => $limit_status
                              , 'limit_day' => $limit_day
                              , 'limit_content' => $limit_content);

        }

        # model - update
        $this->load->model('admin/setting/m_blind_setting');
        $this->m_blind_setting->blind_setting_update($req_data);


        gf_alert("저장되었습니다.", '/admin/setting/blind_setting_reg/1');

    }




}

?>