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

class Comm
{

    #---------------------------------------------------------------------------
    # PURPOSE : 로그인
    # CREATE  :	2018-04-24
    #---------------------------------------------------------------------------
    public function fn_login_proc($req_data){
        $ci=& get_instance();
        $ci->load->helper('cookie');
        $ci->load->library('session');

        # 회원정보
        $idx                =  $req_data['idx'];
        $member_name        =  $req_data['member_name'];
        $member_group_idx   =  $req_data['member_group_idx'];
        $member_nickname    =  $req_data['member_nickname'];
        $member_intro       =  $req_data['member_intro'];
        $member_img         =  $req_data['member_img'];
        $member_id          =  $req_data['member_id'];
        $member_email       =  $req_data['member_email'];
        if($member_nickname == '')  $member_nickname = $member_name;

        $save_login = 'N';
        if(isset($req_data['save_login'])==true) {
            $save_login = $req_data['save_login'];
        }

        # 마지막 로그인 정보 업데이트
        $ci->m_member->member_visit_count_last_login_update($idx);

        # 로그인 로그 남기기
        $req_data = array();
        $req_data['member_id']          = $member_id;
        $req_data['user_agent']         = $_SERVER['HTTP_USER_AGENT'];

        # model
        $ci->load->model('web/m_member_login_log');
        $ci->m_member_login_log->member_login_log_insert($req_data);

        # model
        $ci->load->model('web/m_member_group');
        $arr_member_group = $ci->m_member_group->member_group_select($member_group_idx);
        $member_group_name = '';
        if(isset($arr_member_group['member_group_name'])==true){
            $member_group_name = $arr_member_group['member_group_name'];
        }

        $session_id = $ci->session->session_id;


        # 세션저장
        $ci->session->set_userdata('C_Member_Name', $member_name);
        $ci->session->set_userdata('C_Member_Id', $member_id);
        $ci->session->set_userdata('C_Member_Nicname', $member_nickname);
        $ci->session->set_userdata('C_Member_Group', $member_group_idx);
        $ci->session->set_userdata('C_Member_Group_Name', $member_group_name);
        $ci->session->set_userdata('C_Member_Intro', $member_intro);
        $ci->session->set_userdata('C_Member_Profile', $member_img);
        $ci->session->set_userdata('C_Member_Email', $member_email);

        set_cookie('crossmap_save_login', $save_login, 86400 * 90);
        if($save_login=='Y') {
            set_cookie('crossmap_save_id', $session_id, 86400 * 90);
        } else {
            set_cookie('crossmap_save_id', '', 86400 * 90);
        }

        set_cookie('crossmap_comment_sns_id', $member_id, 0);
        set_cookie('crossmap_comment_sns_name', $member_nickname, 0);
        set_cookie('crossmap_comment_sns_type', 'cm', 0);


        $expire_date = date("Y-m-d H:i:s", strtotime("+10 minutes"));

        # model
        $msg = '';
        $req_data = array();
        $req_data['member_id']      = $member_id;
        $req_data['member_ip']      = $_SERVER['REMOTE_ADDR'];

        $ci->load->model('web/m_member_login');
        $member_login_count = $ci->m_member_login->member_login_member_id_count($req_data);
        if($member_login_count > 0){
            $msg = '현재 동시접속중인 사용자가 있습니다.';
            $ci->m_member_login->member_login_delete($req_data);

        }
        
        $req_data = array();
        $req_data['member_id']      = $member_id;
        $req_data['member_ip']      = $_SERVER['REMOTE_ADDR'];
        $req_data['expire_date']    = $expire_date;
        $ci->m_member_login->member_login_insert($req_data);


        

        return $msg;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : 로그아웃
    # CREATE  :	2018-04-25
    #---------------------------------------------------------------------------
    public function fn_logout_proc(){
        $ci=& get_instance();
        $ci->load->helper('cookie');
        $ci->load->library('session');

        
        $member_id  = $ci->session->userdata('C_Member_Id');
        
        
        # 세션정보 unset
        $ci->session->unset_userdata('C_Member_Name');
        $ci->session->unset_userdata('C_Member_Id');
		$ci->session->unset_userdata('C_Member_Nicname');
		$ci->session->unset_userdata('C_Member_Group');
		$ci->session->unset_userdata('C_Member_Group_Name');
		$ci->session->unset_userdata('C_Member_Intro');
		$ci->session->unset_userdata('C_Member_Profile');
		$ci->session->unset_userdata('C_Member_Email');

        set_cookie('crossmap_save_login', 'N', 86400 * 90);
        set_cookie('crossmap_save_id', '', 86400 * 90);
        set_cookie('crossmap_comment_sns_id', '', 86400 * 90);
        set_cookie('crossmap_comment_sns_name', '', 86400 * 90);
        set_cookie('crossmap_comment_sns_type', '', 86400 * 90);
        
        $req_data = array();
        $req_data['member_id']      = $member_id;
        $req_data['member_ip']      = $_SERVER['REMOTE_ADDR'];
        
        $ci->load->model('web/m_member_login');
        $ci->m_member_login->member_login_delete($req_data);
        

    }


    #---------------------------------------------------------------------------
    # PURPOSE : hit_count update
    # CREATE  :	2018-04-26
    #---------------------------------------------------------------------------
    public function fn_hit_count_update($req_data){
        $ci=& get_instance();

        $reg_date = date("Y-m-d H:i:s", strtotime("-1 hours"));

        if($req_data['member_id']=='')  $req_data['member_id'] = '';

        $ci->load->model('web/m_hit_log');
        $count = 0;
        if($req_data['member_id'] !='') {
            $req_data1 = array();
            $req_data1['reg_date']          = $reg_date;
            $req_data1['content_type']      = $req_data['content_type'];
            $req_data1['content_idx']       = $req_data['content_idx'];
            $req_data1['member_id']         = $req_data['member_id'];

            $count = $ci->m_hit_log->hit_log_where_member_id_content_idx_count($req_data1);
        }

        if($count==0) {
            $ci->m_hit_log->hit_log_insert($req_data);

            $content_type   = $req_data['content_type'];
            $idx            = $req_data['content_idx'];

            # 각 글 hit+1
            if($content_type == '3') {
                $ci->load->model('web/m_life');
                $ci->m_life->life_hit_count_update($idx);

            } else if($content_type == '2') {
                $ci->load->model('web/m_column');
                $ci->m_column->column_hit_count_update($idx);

            } else if($content_type == '1') {
                $ci->load->model('web/m_news');
                $ci->m_news->news_hit_count_update($idx);
            }

        }


    }


    #---------------------------------------------------------------------------
    # PURPOSE : 댓글 로그인
    # CREATE  :	2018-04-27
    #---------------------------------------------------------------------------
    public function fn_comment_login_proc($req_data){
        $ci=& get_instance();
        $ci->load->helper('cookie');

        # 회원정보
        $member_name        =  $req_data['member_name'];
        $member_id          =  $req_data['member_id'];
        $sns_type_name      =  $req_data['sns_type_name'];

        set_cookie('crossmap_comment_sns_id', $member_id, 0);
        set_cookie('crossmap_comment_sns_name', $member_name, 0);
        set_cookie('crossmap_comment_sns_type', $sns_type_name, 0);

    }


    #---------------------------------------------------------------------------
    # PURPOSE : 뉴스 footer
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
    public function fn_news_footer() {

        $ci=& get_instance();

        $theme_types = array('3');
        $ci->load->model('web/m_theme');
        $req_data = array();
        $req_data['theme_types'] = $theme_types;
        $arr_theme = $ci->m_theme->theme_join_vw_content_where_in_theme_type_list($req_data);

        $arr_recomm_news        = array();
        foreach ($arr_theme as $val) {
            if($val['theme_type'] == 3)     $arr_recomm_news[$val['rank']]  = $val;
        }

		$ci=& get_instance();

		$theme_types = array('6');
		$ci->load->model('web/m_theme');
		$req_data = array();
		$req_data['theme_types'] = $theme_types;
		$arr_theme = $ci->m_theme->theme_join_vw_content_where_in_theme_type_list($req_data);

		$arr_m_recomm_news        = array();
		foreach ($arr_theme as $val) {
			if($val['theme_type'] == 6)     $arr_m_recomm_news[$val['rank']]  = $val;
		}



		# 실시간급상승
        $req_data = array();
        $req_data['offset']             = 0;
        $req_data['limit']              = 20;

        $ci->load->model('web/m_realtime_keyword');
        $arr_realtime_keyword    = $ci->m_realtime_keyword->realtime_keyword_list($req_data);


        $news_footer = array();
		$news_footer['arr_recomm_news']           = $arr_recomm_news;
		$news_footer['arr_m_recomm_news']           = $arr_m_recomm_news;
		$news_footer['arr_realtime_keyword']      = $arr_realtime_keyword;


        return $news_footer;


    }





    #---------------------------------------------------------------------------
    # PURPOSE : 뉴스 right
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
    public function fn_news_right() {

        $ci=& get_instance();

        # 테마
        # 2: 주요뉴스 / 4: 오늘의포토 / 7: 헤드라인칼럼 / 8: 오늘의칼럼 / 9:추천연재 / 15: 추천라이프
        $theme_types = array('2','4','7','8','9','15');
        $ci->load->model('web/m_theme');
        $req_data = array();
        $req_data['theme_types'] = $theme_types;
        $arr_theme = $ci->m_theme->theme_join_vw_content_where_in_theme_type_list($req_data);


        $arr_main_news          = array();
        $arr_today_photo        = array();
        $arr_headline_column    = array();
        $arr_today_column       = array();
        $arr_recomm_column      = array();
        $arr_recomm_life        = array();
        foreach ($arr_theme as $val) {
            if($val['theme_type'] == 2)     $arr_main_news[$val['rank']]        = array('rank'=>$val['rank'], 'title' => $val['title'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx']);
            if($val['theme_type'] == 4)     $arr_today_photo[$val['rank']]  = array('rank'=>$val['rank'], 'title' => $val['title'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx']);
            if($val['theme_type'] == 7)     $arr_headline_column[$val['rank']]  = array('rank'=>$val['rank'], 'title' => $val['title'], 'column_name'=> $val['column_name'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx'], 'partner_idx' => $val['partner_idx'], 'content' => $val['content']);
            if($val['theme_type'] == 8)     $arr_today_column[$val['rank']]     = array('title' => $val['title'], 'column_name'=> $val['column_name'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx']);
            if($val['theme_type'] == 9)     $arr_recomm_column[$val['rank']]     = array('rank'=>$val['rank'], 'title' => $val['title'], 'column_name'=> $val['column_name'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx'], 'partner_idx' => $val['partner_idx']);
            if($val['theme_type'] == 15)    $arr_recomm_life[$val['rank']]      = array('rank'=>$val['rank'], 'title' => $val['title'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'main_img' => $val['main_img'], 'content_idx' => $val['content_idx']);
        }

        $ci->load->model('life_model');
        $qnaCategoryPosts = $ci->life_model->findByCategoryId(34)->order_by('l.reg_date', 'desc')->limit(2)->get()->result();
        foreach($qnaCategoryPosts as $k=>$post) {
            $arr_recomm_column[$k+1] = array('rank'=>$k+1, 'title' => $post->title, 'column_name'=> $post->editorIntro, 
            'theme_img' => $post->mainImg, 'content_idx' => $post->idx, 'partner_idx' => $post->editorId);    
        }
        
        # 기타섹션 - 인기영상 1 / 인기포토 2
        // $ci->load->model('web/m_etc_section');
        // $req_data = array();
        // $req_data['etc_section_types'] = array('1', '2');
        // $arr_etc_section = $ci->m_etc_section->etc_section_where_in_etc_section_type_list($req_data);

        // $arr_p_photo    = array();
        // $arr_p_video    = array();
        // foreach ($arr_etc_section as $val) {
        //     if($val['etc_section_type'] == 2)     $arr_p_photo[$val['rank']]  = array('content_type'=>$val['content_type'], 'head_title'=>$val['head_title'], 'title' => $val['title'], 'main_video' => $val['main_video'], 'video_url' => $val['video_url'], 'main_img' => $val['main_img'], 'content_idx' => $val['content_idx']);
        //     if($val['etc_section_type'] == 1)     $arr_p_video[$val['rank']]  = array('content_type'=>$val['content_type'], 'head_title'=>$val['head_title'], 'title' => $val['title'], 'main_video' => $val['main_video'], 'video_url' => $val['video_url'], 'main_img' => $val['main_img'], 'content_idx' => $val['content_idx']);
        // }

        $cmtvCategoryPosts = $ci->life_model->findByCategoryId(22)->order_by('l.reg_date', 'desc')->limit(2)->get()->result();
        $arr_p_video    = array();
        foreach ($cmtvCategoryPosts as $k=>$post) {           
            $arr_p_video[$k+1]  = array('content_type'=>3, 'head_title'=>$post->head_title, 'title' => $post->title, 'main_img' => $post->mainImg, 'content_idx' => $post->idx);
        }

        # 라이브인기글 --> 라이프 랭킹 - 많이본 TOP5
        // $req_data = array();
        // $req_data['ranking_type']   = '1';
        // $req_data['content_type']   = '3';
        // $req_data['offset']         = 0;
        // $req_data['limit']          = 50;
        // $ci->load->model('web/m_ranking');
        // $arr_life_ranking    = $ci->m_ranking->ranking_join_life_list($req_data);
        
        $arr_life_ranking = array();
        $qnaCategoryPosts = $ci->life_model->findByCategoryId()->order_by('l.hit_count', 'desc')->limit(10)->get()->result();
        foreach($qnaCategoryPosts as $k=>$post) {
            $arr_life_ranking[$k+1] = array('rank'=>$k+1, 'title' => $post->title, 'column_name'=> $post->editorIntro, 
            'theme_img' => $post->mainImg, 'content_idx' => $post->idx, 'partner_idx' => $post->editorId);    
        }

        # 실시간급상승
        $req_data = array();
        $req_data['offset']             = 0;
        $req_data['limit']              = 20;

        $ci->load->model('web/m_realtime_keyword');
        $arr_realtime_keyword    = $ci->m_realtime_keyword->realtime_keyword_list($req_data);


        $arr_news_right = array();
        $arr_news_right['arr_main_news']             = $arr_main_news;
        $arr_news_right['arr_today_photo']           = $arr_today_photo;
        $arr_news_right['arr_recomm_column']         = $arr_recomm_column;
        $arr_news_right['arr_recomm_life']           = $arr_recomm_life;
        $arr_news_right['arr_life_ranking']          = $arr_life_ranking;
        // $arr_news_right['arr_p_photo']               = $arr_p_photo;
        $arr_news_right['arr_p_video']               = $arr_p_video;
        $arr_news_right['arr_realtime_keyword']      = $arr_realtime_keyword;
        $arr_news_right['arr_headline_column']       = $arr_headline_column;
        $arr_news_right['arr_today_column']          = $arr_today_column;

        return $arr_news_right;

    }


    #---------------------------------------------------------------------------
    # PURPOSE : 댓글
    # CREATE  :	2018-04-29
    #---------------------------------------------------------------------------
    public function fn_comment($req_data) {

        $ci=& get_instance();

        $content_type   = $req_data['content_type'];
        $content_idx    = $req_data['content_idx'];
        $offset         = $req_data['offset'];
        $limit          = $req_data['limit'];
        $sort_column    = $req_data['sort_column'];
        $sort_type      = $req_data['sort_type'];
        $write_id       = $req_data['write_id'];

        # 댓글
        $ci->load->model('web/m_comment');

        # 총댓글수
        $req_data = array();
        $req_data['content_type']   = $content_type;
        $req_data['content_idx']    = $content_idx;
        $comment_total_count    = $ci->m_comment->comment_parent_where_content_idx_count($req_data);

        # 본인삭제 수
        $req_data['write_id']    = $write_id;
        $comment_my_delete_count    = $ci->m_comment->comment_my_delete_where_content_idx_count($req_data);


        # 댓글 리스트
        $req_data = array();
        $req_data['content_type']   = $content_type;
        $req_data['content_idx']    = $content_idx;
        $req_data['offset']         = $offset;
        $req_data['limit']          = $limit;
        $req_data['sort_column']    = $sort_column;
        $req_data['sort_type']      = $sort_type;

        # list
        $arr_comment         = $ci->m_comment->comment_parent_list($req_data);


        $return_data = array();
        $return_data['comment_total_count']         = $comment_total_count;
        $return_data['comment_my_delete_count']     = $comment_my_delete_count;
        $return_data['arr_comment']                 = $arr_comment;

        return $return_data;


    }


    #---------------------------------------------------------------------------
    # PURPOSE : 라이프 right
    # CREATE  :	2018-04-28
    #---------------------------------------------------------------------------
    public function fn_life_right_comm($theme_types) {

        $ci=& get_instance();

        # 테마
        # 13: 라이프 스타일, 14: 지금뜨는 라이프
        $ci->load->model('web/m_theme');
        $req_data = array();
        $req_data['theme_types'] = $theme_types;
        $arr_theme = $ci->m_theme->theme_join_vw_content_where_in_theme_type_list($req_data);


        $arr_life_style         = array();
        $arr_boomup_life        = array();
        foreach ($arr_theme as $val) {
            if($val['theme_type'] == 13)    $arr_life_style[$val['rank']]       = array('rank'=>$val['rank'], 'title' => $val['title'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx']);
            if($val['theme_type'] == 14)    $arr_boomup_life[$val['rank']]      = array('rank'=>$val['rank'], 'title' => $val['title'], 'sub_content' => $val['sub_content'], 'theme_img' => $val['theme_img'], 'content_idx' => $val['content_idx']);
        }


        # 라이프 공감글 --> 라이프 랭킹 - 공감많은 TOP3
        $req_data = array();
        $req_data['ranking_type']   = '3';
        $req_data['content_type']   = '3';
        $req_data['offset']         = 0;
        $req_data['limit']          = 3;
        $ci->load->model('web/m_ranking');
        $arr_life_ranking    = $ci->m_ranking->ranking_list($req_data);


        $arr_news_comm = array();
        $arr_news_comm['arr_life_style']            = $arr_life_style;
        $arr_news_comm['arr_boomup_life']           = $arr_boomup_life;
        $arr_news_comm['arr_life_ranking']          = $arr_life_ranking;


        return $arr_news_comm;


    }



    #---------------------------------------------------------------------------
    # PURPOSE : 맵툰 maptoon right
    # CREATE  :	2019-03-26
    #---------------------------------------------------------------------------
    public function fn_mt_right() {
        $ci=& get_instance();

        # 테마
        # 2: 주요뉴스 / 4: 오늘의포토 / 7: 헤드라인칼럼 / 8: 오늘의칼럼 / 9:추천연재 / 15: 추천라이프
        $theme_types = array('15');
        $ci->load->model('web/m_theme');
        $req_data = array();
        $req_data['theme_types'] = $theme_types;
        $arr_theme = $ci->m_theme->theme_join_vw_content_where_in_theme_type_list($req_data);

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

        
        $ci->load->model('maptoon/m_maptoon');
        //맵툰 실시간 조회 순위
        $ep_view_rank = $ci->m_maptoon->get_ep_view_rank();
        //맵툰 인기 순위
        $mt_pop_rank = $ci->m_maptoon->get_mt_pop_rank();
        
        $return_data['arr_recomm_life']=$arr_recomm_life;
        $return_data['arr_ep_view_rank']=$ep_view_rank;
        $return_data['arr_mt_pop_rank']=$mt_pop_rank;
        

        return $return_data;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : 금칙어 체크
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
    public function fn_forbidden_word($word) {

        $ci=& get_instance();

        $ci->load->model('web/m_forbidden_word');
        $count = $ci->m_forbidden_word->forbidden_word_where_word_count($word);

        return $count;

    }





}


