<?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/admin/cm_controller.php');

class column extends cm_Controller {


    #---------------------------------------------------------------------------
    # PURPOSE :	칼럼관리 > 기능별 칼럼관리
    # CREATE  :	2018-03-07
    #---------------------------------------------------------------------------
	function fn_column_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_click1          = trim($this->input->get_post('search_click1', TRUE));
        $search_click2          = trim($this->input->get_post('search_click2', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));

        # 칼럼리스트일 경우 자기 글만 볼수 있게 처리
        $search_write_id = '';
        $admin_type = $this->session->userdata('Adm_Type');
        if($admin_type == '2') {
            $search_write_id = $this->session->userdata('Adm_Id');
        }


        if($sort=='')   $sort ='1';
        switch ($sort) {
            case '1': $sort_column = 'reg_date'; break;
            case '2': $sort_column = 'hit_count'; break;
            case '3': $sort_column = 'like_count'; break;
            case '4': $sort_column = 'comment_count'; break;
            default : $sort_column = 'reg_date'; break;
        }

        # 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_write_id']        = $search_write_id;
        $req_data['search_click1']          = $search_click1;
        $req_data['search_click2']          = $search_click2;
        $req_data['sort_column']            = $sort_column;

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

	    # model - list
        $this->load->model('admin/column/m_column');
        $total_count        = $this->m_column->column_list_total_count($req_data);
        $result             = $this->m_column->column_list($req_data);
        $new_count          = $this->m_column->column_new_count($req_data);
        $arr_display_count  = $this->m_column->column_is_display_group_by_list($req_data);


        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/column/column_list/3' ;
        $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['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_click1']           = $search_click1;
        $return_data['search_click2']           = $search_click2;
        $return_data['sort']                    = $sort;
        $return_data['list']                    = $result;
        $return_data['new_count']               = $new_count;
        $return_data['arr_display_count']       = $arr_display_count;

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

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	칼럼관리  > 등록/수정
    # CREATE  :	2018-03-07
    #---------------------------------------------------------------------------
	function fn_column_reg() {
        # request
        $mode                   = trim($this->input->get_post('mode', TRUE));
        $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_click1          = trim($this->input->get_post('search_click1', TRUE));
        $search_click2          = trim($this->input->get_post('search_click2', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));
        $idx                    = trim($this->input->get_post('idx', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 컬럼은 2

        if($mode == '')     $mode = 'add';


        # 칼럼리스트일 경우 자기 글만 볼수 있게 처리
        $search_write_id = '';
        $admin_type = $this->session->userdata('Adm_Type');
        if($admin_type == '2') {
            $search_write_id = $this->session->userdata('Adm_Id');
        }

        # 칼럼명리스트
        $req_data = array();
        $req_data['member_group_idx']   = DEF_MEMBER_COLUMNIST_CODE;    // 칼럼회원코드는 7
        $req_data['search_write_id']    = $search_write_id;
        $this->load->model('admin/member/m_partner');
        $arr_columnlist   = $this->m_partner->partner_list_where_member_group_idx($req_data);

        $return_data = array();
        $arr_theme  = array();
        $arr_section = array();
        $arr_relation_article = array();
	    if($mode == 'edit') {

            $req_data = array();
            $req_data['content_idx']    = $idx;
            $req_data['content_type']   = $content_type;

            # 테마리스트
            $this->load->model('admin/comm/m_theme');
            $arr_theme   = $this->m_theme->theme_content_idx_select($req_data);

            # etc_section
            $this->load->model('admin/etc_section/m_etc_section');
            $arr_section   = $this->m_etc_section->etc_section_content_idx_select($req_data);

            # 연관기사
            $this->load->model('admin/column/m_column');
            $arr_relation_article   = $this->m_column->relation_article_join_column_list($req_data);

	        # model
            $req_data = array();
            $req_data['tb']     = 'column';
            $req_data['idx']    = $idx;
            $this->load->model('admin/comm/m_crud');
            $result = $this->m_crud->select($req_data);
            $return_data['result'] = $result[0];

        }

        $return_data['mode']                    = $mode;
        $return_data['page']                    = $page;
        $return_data['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_click1']           = $search_click1;
        $return_data['search_click2']           = $search_click2;
        $return_data['sort']                    = $sort;
        $return_data['idx']                     = $idx;
        $return_data['arr_theme']               = $arr_theme;
        $return_data['arr_section']             = $arr_section;
        $return_data['arr_relation_article']    = $arr_relation_article;
        $return_data['arr_columnlist']          = $arr_columnlist;


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

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	칼럼관리 > 칼럼관리 - 저장
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_reg_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_click1          = trim($this->input->get_post('search_click1', TRUE));
        $search_click2          = trim($this->input->get_post('search_click2', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));
        $mode                   = trim($this->input->get_post('mode', TRUE));

        $idx                    = trim($this->input->get_post('idx', TRUE));
        $column_idx             = trim($this->input->get_post('column_idx', TRUE));
        $title                  = trim($this->input->get_post('title', TRUE));
        $tag                    = trim($this->input->get_post('tag', TRUE));
        $content                = trim($this->input->get_post('content', FALSE));
        $hit_count              = trim($this->input->get_post('hit_count', TRUE));
        $share_count            = trim($this->input->get_post('share_count', TRUE));
        $like_count             = trim($this->input->get_post('like_count', TRUE));
        $is_auto_setting        = trim($this->input->get_post('is_auto_setting', TRUE));
        $relation_idxs          = $this->input->get_post('relation_idxs', TRUE);
        $section_type1          = trim($this->input->get_post('section_type1', TRUE));
        $section_type2          = trim($this->input->get_post('section_type2', TRUE));
        $section_type3          = trim($this->input->get_post('section_type3', TRUE));
        $section_type4          = trim($this->input->get_post('section_type4', TRUE));
        $theme_type7            = trim($this->input->get_post('theme_type7', TRUE));
        $theme_type8            = trim($this->input->get_post('theme_type8', TRUE));
        $theme_type9            = trim($this->input->get_post('theme_type9', TRUE));
        $theme_type10           = trim($this->input->get_post('theme_type10', TRUE));
        $display_date           = trim($this->input->get_post('display_date', TRUE));
        $display_hour           = trim($this->input->get_post('display_hour', TRUE));
        $display_min            = trim($this->input->get_post('display_min', TRUE));
        $display_sec            = trim($this->input->get_post('display_sec', TRUE));
        $is_del_img             = trim($this->input->get_post('is_del_img', TRUE));
        $org_main_img           = trim($this->input->get_post('org_main_img', TRUE));
        $video_url              = trim($this->input->get_post('video_url', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 칼럼코드 2

        $video_playtime = '';
        if($video_url!='') {
            $this->load->library('comm_admin');
            $video_playtime = $this->comm_admin->fn_youtube_playtime($video_url);
            $video_url = 'https://www.youtube.com/embed/'.$video_url;
        }


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

        # 이미지업로드
        if($_FILES['main_img']['name']!='') {
             # image upload
            $upload_path = './files/column/'.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('main_img')) {
                $error = array('error' => $this->upload->display_errors());
                gf_alert("이미지 업로드에 실패했습니다.", 'back');
                exit;
            }
            $arr_upload_data = $this->upload->data();
            $filename = str_replace($_SERVER['DOCUMENT_ROOT'], '', $arr_upload_data['full_path']);

        } else {
            $filename = $org_main_img;
        }


        # 컬럼명
        $req_data = array();
        $req_data['idx']    = $column_idx;
        $req_data['tb']     = 'partner';
        # model
        $this->load->model('admin/comm/m_crud');
        $result = $this->m_crud->select($req_data);
        $column_name = '';
        $write_id = '';
        if(count($result) > 0){
            $write_id       = $result[0]['partner_id'];
            $column_name    = $result[0]['partner_name'];
        }

        # 예약시간
        $now_date       = date("Y-m-d H:i:s");
        if($display_date=='' || $display_date=='0000-00-00') {
            $display_date   = $now_date;
        } else {
            $display_hour   = sprintf('%02d',$display_hour);
            $display_min    = sprintf('%02d',$display_min);
            $display_sec    = sprintf('%02d',$display_sec);
            $display_date   = $display_date.' '.$display_hour.':'.$display_min.':'.$display_sec;
        }

        $is_display = 'Y';
        if($display_date > $now_date) {
            $is_display = 'N';
        }

        # tag
        $tag = str_replace(' ','', $tag);
        $tag = str_replace(',',' ', $tag);

        # update/insert data
        $req_data = array();
        $req_data['title']          = $title;
        $req_data['main_img']       = $filename;
        $req_data['partner_idx']    = $column_idx;
        $req_data['column_name']    = $column_name;
        $req_data['write_id']       = $write_id;
        $req_data['write_name']     = $column_name;
        $req_data['content']        = $content;
        $req_data['is_auto_setting']= $is_auto_setting;
        $req_data['tag']            = $tag;

        $req_data['hit_count']      = $hit_count;
        $req_data['share_count']    = $share_count;
        $req_data['like_count']     = $like_count;

        $req_data['is_display']     = $is_display;
        $req_data['display_date']   = $display_date;
        $req_data['video_url']      = $video_url;
        $req_data['video_playtime'] = $video_playtime;

        if($mode=='add') {

            # model
            $this->load->model('admin/column/m_column');
            $idx = $this->m_column->column_insert($req_data);
            if(!$idx) {
                gf_alert("칼럼등록에 실패했습니다.", 'back');
                exit;
            }
            $url = '/admin/column/column_list/3';



        } else if($mode=='edit') {
            $req_data['idx']     = $idx;

            # model
            $this->load->model('admin/column/m_column');
            $result = $this->m_column->column_update($req_data);

            if(!$result) {
                gf_alert("칼럼수정에 실패했습니다.", 'back');
                exit;
            }
            $url = '/admin/column/column_list/3/'.$page.'?search_type='.$search_type.'&search_keyword='.$search_keyword.'&sort='.$sort.'&search_click1='.$search_click1.'&search_click2='.$search_click2;

        }

        # tag
        $req_data = array();
        $req_data['content_type']   = $content_type;
        $req_data['content_idx']    = $idx;

        # model - tag 삭제
        $this->load->model('admin/comm/m_tag');
        $this->m_tag->tag_delete($req_data);

        $arr_tag = explode(" ", $tag);
        $arr_tag = array_filter(array_map('trim', $arr_tag));
        foreach ($arr_tag as $val) {
            $req_data['tag']            = $val;

            # model
            $tag_count = $this->m_tag->tag_select_where_tag_count($req_data);
            if($tag_count==0) { // insert
                $result = $this->m_tag->tag_insert($req_data);

            }
        }



        # 관련뉴스
        $req_data = array();
        $req_data['content_type']   = $content_type;
        $req_data['content_idx']    = $idx;

        # model - 관련뉴스 삭제
        $this->load->model('admin/comm/m_relation_article');
        $this->m_relation_article->relation_article_delete($req_data);


        if($is_auto_setting == 'Y') {
            $relation_idxs = array();

            $string = preg_replace("/[#\&\+\-%@=\/\\\:;,\.'\"\^`~\_|\!\?\*$#<>()\[\]\{\}]/i", "", $title);
            $tag = $tag.' '.$string;

            $req_data['tag']    = $tag;

            # model - 관련뉴스
            $this->load->model('admin/column/m_column');
            $result = $this->m_column->column_tag_select($req_data);
            if(count($result) > 0) {
                foreach ($result as $val) {
                    $relation_idxs[] = $val['idx'];
                }
            }
        }

        if(count($relation_idxs) > 0) {
            $req_data = array();
            $req_data['content_type']   = $content_type;
            $req_data['content_idx']    = $idx;
            $req_data['relation_idxs']  = $relation_idxs;

            # model - 수동등록
            $result = $this->m_relation_article->relation_article_insert($req_data);
            if(!$result) {
                gf_alert("관련칼럼등록에 실패했습니다.", 'back');
                exit;
            }

        }

        # 테마 등록
        for($i=7; $i<=10; $i++) {
            $rank = ${"theme_type".$i};
            if($rank != '') {
                $req_data = array();
                $req_data['theme_type']     = $i;
                $req_data['content_type']   = $content_type;
                $req_data['rank']           = $rank;
                $req_data['content_idx']    = $idx;
                $req_data['filename']       = $filename;
                $req_data['title']          = $title;
                $req_data['is_display']     = $is_display;

                $this->load->library('comm_admin');
                $result = $this->comm_admin->fn_theme_reg($req_data);

                if (!$result) {
                    gf_alert("테마등록에 실패했습니다.", 'back');
                    exit;
                }

            }
        }

        # 기타섹션 등록
        for($i=1; $i<=4; $i++) {
            $rank = ${"section_type".$i};
            if($rank != '') {
                $req_data = array();
                $req_data['etc_section_type']   = $i;
                $req_data['content_type']       = $content_type;
                $req_data['rank']               = $rank;
                $req_data['content_idx']        = $idx;
                $req_data['filename']           = $filename;
                $req_data['title']              = $title;
                $req_data['is_display']         = $is_display;

                $this->load->library('comm_admin');
                $result = $this->comm_admin->fn_etc_section_reg($req_data);

                if (!$result) {
                    gf_alert("기타섹션등록에 실패했습니다.", 'back');
                    exit;
                }
            }
        }

        gf_alert("저장되었습니다.", $url);
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	삭제
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_reg_del_proc() {
        # request
        $idx                    = trim($this->input->get_post('idx', TRUE));

        # model
        $this->load->model('admin/comm/m_crud');

        // 이미지 삭제
        $req_data = array();
        $req_data['idx']    = $idx;
        $req_data['tb']     = 'column';
        $result1 = $this->m_crud->select($req_data);
        if(count($result1) > 0){
            if($result1[0]['main_img']!=''){
                unlink('.'.$result1[0]['main_img']);
            }
        }


        $req_data = array();
        $req_data['tb']     = 'column';
        $req_data['data'][] = $idx;

        # model - update
        $this->m_crud->is_del_update($req_data);

        gf_alert("삭제되었습니다.", '/admin/column/column_list/3');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	삭제 (다중)
    # CREATE  :	2018-03-05
    #---------------------------------------------------------------------------
	function fn_column_reg_arr_del_proc() {
        # request
        $arr_idx    = $this->input->get_post('is_del', TRUE);   // 삭제할 idx


        # model
        $this->load->model('admin/comm/m_crud');

        $req_data = array();
        $req_data['tb']     = 'column';
        $req_data['data']   = $arr_idx;

        // 이미지 삭제
        $result1 = $this->m_crud->select_in_idx($req_data);
        if(count($result1) > 0){
            foreach ($result1 as $val) {
                if($val['main_img']!=''){
                    unlink('.'.$val['main_img']);
                }
            }
        }

        # model - update
        $this->m_crud->is_del_update($req_data);

        gf_alert("삭제되었습니다.", '/admin/column/column_list/3');

    }


    #---------------------------------------------------------------------------
    # PURPOSE : 상태,노출일 변경
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_ajax_column_is_display_update() {
        # request
        $idx            = trim($this->input->get_post('idx', TRUE));
        $is_display     = trim($this->input->get_post('is_display', TRUE));
        $content_type   = DEF_COLUMN_CODE; // 칼럼코드 2


        $req_data = array();
        $req_data['content_type']   = $content_type;
        $req_data['content_idx']    = $idx;
        $req_data['search_type']    = 'is_display';
        $req_data['search_key']     = 'N';

        // 테마
        $this->load->library('comm_admin');
        $this->comm_admin->fn_theme_change_is_display($req_data);

        // 기타테마
        $this->load->library('comm_admin');
        $this->comm_admin->fn_etc_section_change_is_display($req_data);


        $req_data = array();
        $req_data['idx']            = $idx;
        $req_data['is_display']     = $is_display;


        # model
        $this->load->model('admin/column/m_column');
        $result = $this->m_column->column_is_display_update($req_data);
        if(!$result){
            $return_data['result'] = array('result_code' => '9999');
        } else {
            $return_data['result'] = array('result_code' => '0000');
        }

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

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	테마별킬럼관리
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_theme_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_theme_type      = trim($this->input->get_post('search_theme_type', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 칼럼코드 2
        $search_click1          = trim($this->input->get_post('search_click1', TRUE));
        $search_click2          = trim($this->input->get_post('search_click2', TRUE));

        if($sort=='')   $sort ='1';
        switch ($sort) {
            case '1': $sort_column = 'a.reg_date'; break;
            case '2': $sort_column = 'b.hit_count'; break;
            case '3': $sort_column = 'b.like_count'; break;
            case '4': $sort_column = 'b.comment_count'; break;
            case '5': $sort_column = 'b.share_count'; break;
            default : $sort_column = 'a.reg_date'; break;
        }

        # 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_theme_type']      = $search_theme_type;
        $req_data['sort_column']            = $sort_column;
        $req_data['content_type']           = $content_type;
        $req_data['search_click1']          = $search_click1;
        $req_data['search_click2']          = $search_click2;

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

	    # model - list
        $this->load->model('admin/column/m_column');
        $total_count        = $this->m_column->theme_join_column_list_total_count($req_data);
        $result             = $this->m_column->theme_join_column_list($req_data);
        $new_count          = $this->m_column->theme_join_column_new_count($req_data);
        $arr_display_count  = $this->m_column->theme_join_column_is_display_group_by_list($req_data);



        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/column/column_theme_list/3' ;
        $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['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_theme_type']       = $search_theme_type;
        $return_data['sort']                    = $sort;
        $return_data['list']                    = $result;
        $return_data['arr_display_count']       = $arr_display_count;
        $return_data['search_click1']           = $search_click1;
        $return_data['search_click2']           = $search_click2;
        $return_data['new_count']               = $new_count;

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

    }


    #---------------------------------------------------------------------------
    # PURPOSE : 테마별 관리 등록/수정
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_theme_list_reg() {
        # request
        $mode                   = trim($this->input->get_post('mode', TRUE));
        $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_theme_type      = trim($this->input->get_post('search_theme_type', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));
        $idx                    = trim($this->input->get_post('idx', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 칼럼코드 2

        if($mode == '')             $mode = 'add';

        if($search_theme_type == '')   $search_theme_type = '7';

        $req_data = array();
        $req_data['content_type']   = $content_type;
        $req_data['theme_type']     = $search_theme_type;

        # 테마리스트
        $this->load->model('admin/column/m_column');
        $arr_theme   = $this->m_column->theme_join_column_type_list($req_data);

        $return_data = array();
        $return_data['mode']                    = $mode;
        $return_data['page']                    = $page;
        $return_data['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_theme_type']       = $search_theme_type;
        $return_data['sort']                    = $sort;
        $return_data['content_type']            = $content_type;
        $return_data['idx']                     = $idx;
        $return_data['arr_theme']               = $arr_theme;

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

    }





    #---------------------------------------------------------------------------
    # PURPOSE :	테마별 관리 - 저장
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_theme_list_reg_proc() {
        # request
        $theme_type             = trim($this->input->get_post('theme_type', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 칼럼코드 2

        include_once ("./static_data/code.php");
        $arr_req_data = array();
        for($i=1;$i<=$def_arr_column_theme[$theme_type]['count'];$i++){
            ${"content_idx".$i}    = trim($this->input->get_post('content_idx'.$i, TRUE));
            ${"head_title".$i}     = trim($this->input->get_post('head_title'.$i, TRUE));
            ${"title".$i}          = trim($this->input->get_post('title'.$i, TRUE));
            ${"sub_content".$i}    = trim($this->input->get_post('sub_content'.$i, TRUE));
            ${"img".$i}            = trim($this->input->get_post('img'.$i, TRUE));
            ${"org_img".$i}        = trim($this->input->get_post('org_img'.$i, TRUE));

            if(${"title".$i}!=''){
                # 이미지업로드
                if($_FILES['theme_img'.$i]['name']!='') {
                     # image upload
                    $upload_path = './files/theme/'.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('theme_img'.$i)) {
                        $error = array('error' => $this->upload->display_errors());
                        gf_alert("이미지 업로드에 실패했습니다.", 'back');
                        exit;
                    }
                    $arr_upload_data = $this->upload->data();
                    $filename = str_replace($_SERVER['DOCUMENT_ROOT'], '', $arr_upload_data['full_path']);
                    $is_file_copy = 'N';
                } else {
                    if(${"img".$i}=='') {
                        $filename = ${"org_img".$i};
                        $is_file_copy = 'N';
                    } else {
                        $filename = ${"img".$i};
                        $is_file_copy = 'Y';
                    }
                }

                $req_data['theme_type']     = $theme_type;
                $req_data['content_type']   = $content_type;
                $req_data['rank']           = $i;
                $req_data['content_idx']    = ${"content_idx".$i};
                $req_data['filename']       = $filename;
                $req_data['is_file_copy']   = $is_file_copy;
                $req_data['is_display']     = 'Y';
                $req_data['title']          = ${"title".$i};
                $req_data['sub_content']    = ${"sub_content".$i};

                $this->load->library('comm_admin');
                $result = $this->comm_admin->fn_theme_reg($req_data);

                if (!$result) {
                    gf_alert("테마등록에 실패했습니다.", 'back');
                    exit;
                }


            }

        }

        gf_alert("저장되었습니다.", "/admin/column/column_theme_list/3");


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	테마별관리 수정
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_theme_edit() {
        # 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_theme_type      = trim($this->input->get_post('search_theme_type', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));
        $idx                    = trim($this->input->get_post('idx', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 칼럼코드 2


        # model
        $this->load->model('admin/column/m_column');
        $result   = $this->m_column->theme_join_column_select($idx);

        $return_data = array();
        $return_data['page']                    = $page;
        $return_data['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_theme_type']       = $search_theme_type;
        $return_data['sort']                    = $sort;
        $return_data['idx']                     = $idx;
        $return_data['result']                  = $result[0];
        $return_data['content_type']            = $content_type;
        # view
        return $this->load->view('/admin/column/column_theme_edit', $return_data, true);

    }



    #---------------------------------------------------------------------------
    # PURPOSE :	테마별 뉴스관리 - 수정
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
	function fn_column_theme_edit_proc() {
        # request
        $content_idx        = trim($this->input->get_post('content_idx', TRUE));
        $head_title         = trim($this->input->get_post('head_title', TRUE));
        $title              = trim($this->input->get_post('title', TRUE));
        $sub_content        = trim($this->input->get_post('sub_content', TRUE));
        $img                = trim($this->input->get_post('img', TRUE));
        $org_img            = trim($this->input->get_post('org_img', TRUE));
        $idx                = trim($this->input->get_post('idx', TRUE));

        $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_theme_type      = trim($this->input->get_post('search_theme_type', TRUE));
        $sort                   = trim($this->input->get_post('sort', TRUE));



        # 이미지업로드
        if($_FILES['theme_img']['name']!='') {
             # image upload
            $upload_path = './files/theme/'.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('theme_img')) {
                $error = array('error' => $this->upload->display_errors());
                gf_alert("이미지 업로드에 실패했습니다.", 'back');
                exit;
            }
            $arr_upload_data = $this->upload->data();
            $filename = str_replace($_SERVER['DOCUMENT_ROOT'], '', $arr_upload_data['full_path']);

        } else {
            if($img!='') {
                # image upload
                $upload_path = './files/theme/'.date("Y")."/".date("m");
                if (!is_dir($upload_path)) {
                    mkdir($upload_path, 0777, TRUE);
                }

                $arr_new_file =  explode('/', $img);
                copy('.'.$img, $upload_path.'/'.$arr_new_file[5]);
                $filename = '/files/theme/'.date("Y").'/'.date("m").'/'.$arr_new_file[5];

            } else {
                $filename = $org_img;
            }
        }

        $req_data['content_idx']    = $content_idx;
        $req_data['head_title']     = $head_title;
        $req_data['title']          = $title;
        $req_data['sub_content']    = $sub_content;
        $req_data['theme_img']      = $filename;
        $req_data['idx']            = $idx;
        $this->load->model('admin/comm/m_theme');
        $this->m_theme->theme_where_idx_update($req_data);



        gf_alert("저장되었습니다.", '/admin/column/column_theme_list/2/'.$page.'?search_type='.$search_type.'&search_keyword='.$search_keyword.'&sort='.$sort.'&search_theme_type='.$search_theme_type);


    }






    #---------------------------------------------------------------------------
    # PURPOSE :	테마별관리 - 삭제
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_theme_reg_del_proc() {
        # request
        $idx                    = trim($this->input->get_post('idx', TRUE));

        # model
        $this->load->model('admin/comm/m_theme');

        // 이미지 삭제
        $result1 = $this->m_theme->theme_select($idx);
        if(count($result1) > 0){
            if($result1[0]['theme_img']!=''){
                unlink('.'.$result1[0]['theme_img']);
            }
        }

        $req_date[] = $idx;

        # model - update
        $this->m_theme->theme_is_del_update($req_date);

        gf_alert("삭제되었습니다.", '/admin/column/column_theme_list/3');

    }

    #---------------------------------------------------------------------------
    # PURPOSE : 테마별관리 삭제 (다중)
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_theme_reg_arr_del_proc() {
        # request
        $arr_idx    = $this->input->get_post('is_del', TRUE);   // 삭제할 idx


        # model
        $this->load->model('admin/comm/m_theme');

        // 이미지 삭제
        $result1 = $this->m_theme->theme_select_in_idx($arr_idx);
        if(count($result1) > 0){
            foreach ($result1 as $val) {
                if($val['theme_img']!=''){
                    unlink('.'.$val['theme_img']);
                }
            }
        }

        # model - update
        $this->m_theme->theme_is_del_update($arr_idx);

        gf_alert("삭제되었습니다.", '/admin/column/column_theme_list/3');

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	댓글관리
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_comment_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_comment_type    = trim($this->input->get_post('search_comment_type', TRUE));
        $search_theme_type      = trim($this->input->get_post('search_theme_type', TRUE));
        $search_today           = trim($this->input->get_post('search_today', TRUE));
        $search_today_best      = trim($this->input->get_post('search_today_best', 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_comment_type']    = $search_comment_type;

        $req_data['offset']                 = $offset;
        $req_data['limit']                  = $limit;
        $req_data['search_theme_type']      = $search_theme_type;
        $req_data['content_type']           = DEF_COLUMN_CODE;  // 칼럼코드 2
        $req_data['search_today']           = $search_today;
        $req_data['search_today_best']      = $search_today_best;

	    # model - 카테고리 list
        $this->load->model('admin/column/m_column_comment');
        $total_count    = $this->m_column_comment->column_comment_list_total_count($req_data);
        $result         = $this->m_column_comment->column_comment_list($req_data);
        $new_count      = $this->m_column_comment->column_comment_new_count($req_data);
        $best_count     = $this->m_column_comment->column_comment_is_best_count($req_data);
        $best_new_count = $this->m_column_comment->column_comment_is_best_new_count($req_data);

        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/column/column_comment_list/3' ;
        $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['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['search_comment_type']     = $search_comment_type;
        $return_data['search_theme_type']       = $search_theme_type;
        $return_data['list']                    = $result;
        $return_data['new_count']               = $new_count;
        $return_data['best_count']              = $best_count;
        $return_data['best_new_count']          = $best_new_count;
        $return_data['search_today']            = $search_today;
        $return_data['search_today_best']       = $search_today_best;

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

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	칼럼댓글 삭제
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function fn_column_comment_del_proc() {
        # request
        $idx                    = trim($this->input->get_post('idx', TRUE));


        $comment = '관리자에 의해 삭제된 댓글입니다.';


        # 검색
        $req_data = array();
        $req_data['idx']            = $idx;
        $req_data['comment_status'] = '6';  // 관리자삭제
        $req_data['comment']        = $comment;

        # model
        $this->load->model('admin/column/m_column_comment');
        $this->m_column_comment->column_comment_is_del_update($req_data);

        gf_alert("삭제되었습니다.", '/admin/column/column_comment_list/3');

    }


    #---------------------------------------------------------------------------
    # PURPOSE :	태그관리
    # CREATE  :	2018-03-09
    #---------------------------------------------------------------------------
	function fn_column_tag_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));
        $sort                   = trim($this->input->get_post('sort', TRUE));
        $content_type           = DEF_COLUMN_CODE; // 칼럼코드 2

        if($sort=='')   $sort ='1';
        switch ($sort) {
            case '1': $sort_column = 'reg_date'; break;
            case '2': $sort_column = 'hit_count'; break;
            default : $sort_column = 'reg_date'; break;
        }

        # 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['sort_column']            = $sort_column;
        $req_data['content_type']           = $content_type;

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

	    # model - list
        $this->load->model('admin/comm/m_tag');
        $total_count    = $this->m_tag->tag_list_total_count($req_data);
        $result         = $this->m_tag->tag_list($req_data);




        # pagination
        $req_data = array();
        $req_data['total_count']    = $total_count;
        $req_data['limit']          = $limit;
        $req_data['base_url']       = '/admin/column/column_tag_list/3' ;
        $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['search_type']             = $search_type;
        $return_data['search_keyword']          = $search_keyword;
        $return_data['sort']                    = $sort;
        $return_data['list']                    = $result;


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

    }




}

?>