<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Comm_admin
{


    #---------------------------------------------------------------------------
    # PURPOSE : 테마 등록
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_theme_reg($req_data){
        # request
        $theme_type     = $req_data['theme_type'];
        $content_type   = $req_data['content_type'];
        $rank           = $req_data['rank'];
        $content_idx    = $req_data['content_idx'];
        $filename       = $req_data['filename'];
        $title          = $req_data['title'];
        $is_display     = $req_data['is_display'];

        $head_title = '';
        if(isset($req_data['head_title']) == true)      $head_title     = $req_data['head_title'];

        $sub_content = '';
        if(isset($req_data['sub_content']) == true)     $sub_content     = $req_data['sub_content'];

        $is_file_copy = '';
        if(isset($req_data['is_file_copy']) == true)    $is_file_copy     = $req_data['is_file_copy'];

        $sub_title = '';
        if(isset($req_data['sub_title']) == true)       $sub_title        = $req_data['sub_title'];


        $ci=& get_instance();

        if($filename!='' && $is_file_copy!='N') {
            # image upload
            $upload_path = './files/theme/' . date("Y") . "/" . date("m");
            if (!is_dir($upload_path)) {
                mkdir($upload_path, 0777, true);
            }
            $arr_new_file = explode('/', $filename);
            copy('.' . $filename, $upload_path . '/' . $theme_type .$rank. '_' . $arr_new_file[5]);
            $filename = '/files/theme/' . date("Y") . '/' . date("m") . '/' . $theme_type .$rank. '_' . $arr_new_file[5];
        }


        $req_data1 = array();
        $req_data1['theme_type']    = $theme_type;
        $req_data1['content_type']  = $content_type;
        $req_data1['rank']          = $rank;
        $req_data1['content_idx']   = $content_idx;

        # model
        $ci->load->model('admin/comm/m_theme');
        $result = $ci->m_theme->theme_rank_content_idx_select($req_data1);

        $arr_theme = array('theme_type' => $theme_type, 'content_type' => $content_type, 'rank' => $rank, 'title' => $title, 'head_title' => $head_title, 'sub_title' => $sub_title,'sub_content' => $sub_content, 'theme_img' => $filename, 'content_idx' => $content_idx, 'is_display' => $is_display);
        if(count($result)==0) {
            $req_data2 = array();
            $req_data2['theme_type']     = $theme_type;
            $req_data2['content_type']   = $content_type;
            $req_data2['rank']           = $rank;

            $rank_count = $ci->m_theme->theme_rank_count($req_data);
            if ($rank_count > 0 && $is_display=='Y') {
                $result = $ci->m_theme->theme_rank_is_del_update($req_data);
                if (!$result) {
                    return false;
                    exit;
                }
            }
            $result = $ci->m_theme->theme_insert($arr_theme);

        } else {
            $arr_theme['idx'] = $result[0]['idx'];
            $result = $ci->m_theme->theme_item_update($arr_theme);

        }


        return $result;



    }


    #---------------------------------------------------------------------------
    # PURPOSE : 기타섹션 등록
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_etc_section_reg($req_data){
        # request
        $etc_section_type   = $req_data['etc_section_type'];
        $content_type       = $req_data['content_type'];
        $rank               = $req_data['rank'];
        $content_idx        = $req_data['content_idx'];
        $filename           = $req_data['filename'];
        $title              = $req_data['title'];
        $is_display         = $req_data['is_display'];

        $head_title = '';
        if(isset($req_data['head_title']) == true)      $head_title     = $req_data['head_title'];


        $is_file_copy = '';
        if(isset($req_data['is_file_copy']) == true)    $is_file_copy     = $req_data['is_file_copy'];


        $filename2 = '';
        if(isset($req_data['filename2']) == true)       $filename2     = $req_data['filename2'];


        $video_url = '';
        if(isset($req_data['video_url']) == true)      $video_url     = $req_data['video_url'];



        $ci=& get_instance();


        if($filename!='' && $is_file_copy!='N') {
            # image upload
            $upload_path = './files/etc_section/'.date("Y")."/".date("m");
            if (!is_dir($upload_path)) {
                mkdir($upload_path, 0777, TRUE);
            }
            $arr_new_file =  explode('/', $filename);
            copy('.' . $filename, $upload_path . '/' . $etc_section_type.$rank. '_' . $arr_new_file[5]);
            $filename = '/files/etc_section/'.date("Y").'/'.date("m").'/'.$etc_section_type.$rank.'_'.$arr_new_file[5];
        }

        $req_data1 = array();
        $req_data1['etc_section_type']  = $etc_section_type;
        $req_data1['content_type']      = $content_type;
        $req_data1['rank']              = $rank;
        $req_data1['content_idx']       = $content_idx;

        # model
        $ci->load->model('admin/etc_section/m_etc_section');
        $result = $ci->m_etc_section->etc_section_rank_content_idx_select($req_data1);

        $arr_etc_section = array('etc_section_type'=> $etc_section_type
                               , 'content_type'=> $content_type
                               , 'rank'=> $rank
                               , 'head_title'=>$head_title
                               , 'title'=>$title
                               , 'main_img'=>$filename
                               , 'main_video'=>$filename2
                               , 'video_url'=>$video_url
                               , 'content_idx'=>$content_idx
                               , 'is_display'=>$is_display);
        if(count($result)==0) {
            $req_data2 = array();
            $req_data2['etc_section_type']     = $etc_section_type;
            $req_data2['rank']                 = $rank;

            # model
            $ci->load->model('admin/etc_section/m_etc_section');
            $rank_count = $ci->m_etc_section->etc_section_rank_count($req_data);
            if($rank_count > 0 && $is_display == 'Y') {
                $result = $ci->m_etc_section->etc_section_rank_is_del_update($req_data);
                if(!$result) {
                    return false;
                    exit;
                }
            }
            $result = $ci->m_etc_section->etc_section_insert($arr_etc_section);


        } else {
            $arr_etc_section['idx'] = $result[0]['idx'];
            $result = $ci->m_etc_section->etc_section_update($arr_etc_section);

        }


        return $result;
    }



    #---------------------------------------------------------------------------
    # PURPOSE : 페이징
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_pagination($req_data){
        # request
        $total_count= $req_data['total_count'];
        $limit      = $req_data['limit'];
        $base_url   = $req_data['base_url'];
        $suffix     = $req_data['suffix'];

        $ci=& get_instance();

        $ci->load->library('pagination');
		$config['base_url']         = $base_url ;
		$config['total_rows']       = $total_count;
		$config['per_page']         = $limit;
		$config['uri_segment']      = 5;
		$config['num_links']        = 4;
		$config['use_page_numbers'] = TRUE;
		$config['suffix']           = $suffix;
        $config['first_link']       = '<i style="vertical-align:bottom" class="fa fa-2x fa-angle-double-left" style="margin:0;"></i>';
        $config['prev_link']        = '<i style="vertical-align:bottom" class="fa fa-2x fa-angle-left" style="margin:0;"></i>';
        $config['next_link']        = '<i style="vertical-align:bottom" class="fa fa-2x fa-angle-right" style="margin:0;"></i>';
        $config['last_link']        = '<i style="vertical-align:bottom" class="fa fa-2x fa-angle-double-right" style="margin:0;"></i>';
        $config['cur_tag_open']     = '<b class="curr">';
        $config['cur_tag_close']    = ' </b>';
        $config['num_tag_open']     = '<span>';
        $config['num_tag_close']    = ' </span>';

		$ci->pagination->initialize($config);
        $pagination = $ci->pagination->create_links();


        return $pagination;
    }


    #---------------------------------------------------------------------------
    # PURPOSE : 블라인드 제재처리
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_blind_limit_proc($req_data){
        $ci=& get_instance();

        # request
        $limit_content      = $req_data['limit_content'];
        $limit_count        = $req_data['limit_count'];
        $member_email       = $req_data['member_email'];
        $member_id          = $req_data['member_id'];
        $limit_type         = $req_data['limit_type'];
        $end_date           = $req_data['end_date'];
        $member_name        = $req_data['member_name'];

        $def_arr_blind_limit_type = array('경고메일발송', '댓글작성불가', '게시물열람불가', '불량회원등록', '회원영구탈퇴');

        $limit_content = str_replace('{{블라인드누적횟수}}', $limit_count, $limit_content);
        $limit_content = str_replace('{{제재내용}}', $def_arr_blind_limit_type[$limit_type], $limit_content);
        $limit_content = str_replace('{{제재만료일시}}', $end_date, $limit_content);

        $ci->load->model('admin/member/m_member');

        $req_data = array();
        if($limit_type == '3') {    // 불량회원등록
            $req_data['member_group_idx']   = '3';
            $req_data['member_id']          = $member_id;
            $ci->m_member->member_where_member_id_update($req_data);
        } else if($limit_type == '4') {         // 회원영구탈퇴
            $req_data['member_group_idx']   = '5';
            $req_data['member_status']      = '2';
            $req_data['member_id']          = $member_id;
            $ci->m_member->member_where_member_id_update($req_data);
        }


        $limit_content.='<p></p><p>&nbsp;</p>많은 사람들이 자유롭게 편히 쉴 수 있는 크리스천 놀이터, 크로스맵이 될 수 있도록 도움주시길 부탁 드립니다. <p></p><p>&nbsp;</p>';
        $limit_content.='P.S) 하나님의 은혜와 평강이 '.$member_name.'님의 삶 가운데 늘 가득하기를 <p></p>';
        $limit_content.='크로스맵이 응원하고 기도합니다! <p></p><p>&nbsp;</p>';
        $limit_content.=' - 크로스맵 일동 올림 -  <p></p>';

        $mail_result['content'] = $limit_content;
        $message = $ci->load->view('/web/comm/mail_template', $mail_result, TRUE);

        // 경고메일발송
        $ci->load->library('email');
        $config = array();
        $config['wordwrap'] = true;
        $config['mailtype'] = 'html';
        $ci->email->initialize($config);
        $ci->email->from(CROSSMAP_EMAIL, CROSSMAP_EMAIL_NAME);
        $ci->email->to($member_email);
        $ci->email->subject('크로스맵에서 주의를 당부 드립니다.');
        $ci->email->message($message);

        $ci->email->send();


    }


    #---------------------------------------------------------------------------
    # PURPOSE : 유투브 주소로 playtime 알아내기
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_youtube_playtime($youtube_id){


        $url = 'https://www.googleapis.com/youtube/v3/videos?id='.$youtube_id.'&key=AIzaSyBuZrx6ZUeeFuBFho6dU2vJB-b5CAyHrdQ&part=contentDetails';
        $ch = curl_init(); //curl 사용 전 초기화 필수(curl handle)
        curl_setopt($ch, CURLOPT_URL, $url); //URL 지정하기
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json')); //header 지정하기
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $response = curl_exec ($ch);
        $content_detail = json_decode($response, true);

        $duration_txt='00:00';
        if(isset($content_detail['items'][0]['contentDetails']['duration'])==true){
            $duration = $content_detail['items'][0]['contentDetails']['duration'];
            $d = new DateInterval($duration);
            if($d->format('%H')=='00'){
                $duration_txt = $d->format('%I:%S');
            } else {
                $duration_txt = $d->format('%H:%I:%S');
            }

        }

        return $duration_txt;


    }





    #---------------------------------------------------------------------------
    # PURPOSE : 테마 노출로 변경
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_theme_change_is_display($req_data){
        # request
        $content_type       = $req_data['content_type'];

        $ci=& get_instance();

        $ci->load->model('admin/comm/m_theme');
        $result2 = $ci->m_theme->theme_content_idx_list($req_data);
        if(count($result2) > 0) {
            foreach ($result2 as $val2) {
                $req_data1 = array();
                $req_data1['content_type']  = $content_type;
                $req_data1['theme_type']    = $val2['theme_type'];
                $req_data1['rank']          = $val2['rank'];
                $rank_count = $ci->m_theme->theme_rank_count($req_data1);
                if($rank_count > 0) {
                    $ci->m_theme->theme_rank_is_del_update($req_data1);
                }
            }
            $ci->m_theme->theme_is_display_update_batch($result2);
        }

        return '';
    }



    #---------------------------------------------------------------------------
    # PURPOSE : 기타섹션 노출로 변경
    # CREATE  :	2018-03-22
    #---------------------------------------------------------------------------
    public function fn_etc_section_change_is_display($req_data){

        $ci=& get_instance();

        $ci->load->model('admin/etc_section/m_etc_section');
        $result3 = $ci->m_etc_section->etc_section_content_idx_list($req_data);
        if(count($result3) > 0) {
            foreach ($result3 as $val3) {
                $req_data1 = array();
                $req_data1['etc_section_type']  = $val3['etc_section_type'];
                $req_data1['rank']              = $val3['rank'];
                $rank_count = $ci->m_etc_section->etc_section_rank_count($req_data1);
                if($rank_count > 0) {
                    $ci->m_etc_section->etc_section_rank_is_del_update($req_data1);
                }
            }
            $ci->m_etc_section->etc_section_is_display_update_batch($result3);
        }

        return '';
    }



}


