<?
defined('BASEPATH') OR exit('No direct script access allowed');

class m_vw_content extends CI_Model{

	// 생성자, 해당 모델이 생성될때 항상 실행되는 초기화작업
	function __construct(){
		parent::__construct();

        $this->load->database();
        $this->load->library('session');
	}


    #---------------------------------------------------------------------------
    # PURPOSE :	select
    # CREATE  :	2018-04-09
    #---------------------------------------------------------------------------
	function vw_content_select($req_data){
	     # request
        $content_type       = $req_data['content_type'];
        $idx                = $req_data['idx'];


        $sql="select * from vw_content where content_type= '".$content_type."' COLLATE utf8_general_ci and idx=".$idx;
        $res = $this->db->query($sql);
        $row = $res->row_array();
        
        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function vw_content_list($req_data){
	     # request
        $search_type        = $req_data['search_type'];
        $search_keyword     = $req_data['search_keyword'];
        $offset             = $req_data['offset'];
        $limit              = $req_data['limit'];

        if($search_keyword!='') $arr_search_keyword = explode(" ", $search_keyword);

        $where_query = '';
        if($search_type == 'main_search_and_video_url' && $search_keyword!='') {
            $where_query1 = '';
            $i=1;
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;
            }
            $where_query.=" and video_url!='' and (".$where_query1.")";
        }
        if($search_type == 'main_search' && $search_keyword!='') {

            $where_query1 = '';
            $i=1;
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;
            }
            $where_query.=" and (".$where_query1.")";

        }

        $sql="select vw_content.*, @num:=@num+1 as row_num
                from vw_content, (select @num:=0) as a
               where is_del= 'N' 
                 and is_display = 'Y'".$where_query." order by display_date desc limit ".$offset.",".$limit;

        $res = $this->db->query($sql);
        $row = $res->result_array();


        return $row;


    }

    #---------------------------------------------------------------------------
    # PURPOSE :	list - total count
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function vw_content_list_total_count($req_data){

	    $search_type        = $req_data['search_type'];
        $search_keyword     = $req_data['search_keyword'];

        if($search_keyword!='') $arr_search_keyword = explode(" ", $search_keyword);
        if($search_type == 'main_search_and_video_url' && $search_keyword!='') {
            $i=1;$where_query1 = '';
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;

            }
            $where_query1="(".$where_query1.")";
            $this->db->where($where_query1);
            $this->db->where('video_url!=', '');

        }
        if($search_type == 'main_search' && $search_keyword!='') {
            $i=1;$where_query1 = '';
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;

            }
            $where_query1="(".$where_query1.")";
            $this->db->where($where_query1);
        }

        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('vw_content');
        $row = $this->db->count_all_results();
        

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	select
    # CREATE  :	2018-05-04
    #---------------------------------------------------------------------------
	function vw_content_next_select($req_data){
	     # request
        $search_type        = $req_data['search_type'];
        $search_keyword     = $req_data['search_keyword'];
        $row_num             = $req_data['row_num'];

        $where_query = '';
        if($search_type == 'main_search' && $search_keyword!='') {
            if($search_keyword!='') $arr_search_keyword = explode(" ", $search_keyword);
            $where_query1 = '';
            $i=1;
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;
            }
            $where_query.=" and (".$where_query1.")";

        }

        $sql="select * from (select vw_content.*, @num:=@num+1 as row_num
                from vw_content,(select @num:=0) as a
               where is_del= 'N' 
                 and is_display = 'Y'".$where_query." order by display_date desc) as a
                where a.row_num > '".$row_num."' order by a.row_num asc limit 1";
        $res = $this->db->query($sql);
        $row = $res->row_array();


        return $row;


    }




    #---------------------------------------------------------------------------
    # PURPOSE :	select
    # CREATE  :	2018-05-04
    #---------------------------------------------------------------------------
	function vw_content_prev_select($req_data){
	     # request
        $search_type        = $req_data['search_type'];
        $search_keyword     = $req_data['search_keyword'];
        $row_num            = $req_data['row_num'];

        $where_query = '';
        if($search_type == 'main_search' && $search_keyword!='') {
            if($search_keyword!='') $arr_search_keyword = explode(" ", $search_keyword);
            $where_query1 = '';
            $i=1;
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;
            }
            $where_query.=" and (".$where_query1.")";

        }

        $sql="select * from (select vw_content.*, @num:=@num+1 as row_num
                from vw_content,(select @num:=0) as a
               where is_del= 'N' 
                 and is_display = 'Y'".$where_query." order by display_date desc) as a
               where a.row_num < '".$row_num."' order by a.row_num desc limit 1";
        $res = $this->db->query($sql);
        $row = $res->row_array();


        return $row;


    }


    #---------------------------------------------------------------------------
    # PURPOSE :	list
    # CREATE  :	2018-05-03
    #---------------------------------------------------------------------------
	function vw_content_where_not_idx_list($req_data){
	     # request
        $search_type        = $req_data['search_type'];
        $search_keyword     = $req_data['search_keyword'];
        $idx                = $req_data['idx'];
        $offset             = $req_data['offset'];
        $limit              = $req_data['limit'];

        $where_query = '';
        if($search_type == 'main_search' && $search_keyword!='') {
            if($search_keyword!='') $arr_search_keyword = explode(" ", $search_keyword);
            $where_query1 = '';
            $i=1;
            foreach ($arr_search_keyword as $val) {
                if($val == '') continue;
                if($i!=1)   $where_query1.=' or';
                $where_query1.='  (title like "%'.$val.'%" or content like "%'.$val.'%" or tag like "%'.$val.'%")';
                $i++;
            }
            $where_query.=" and (".$where_query1.")";
        }

        $sql="select vw_content.*, @num:=@num+1 as row_num
                from vw_content, (select @num:=0) as a
               where is_del= 'N' and idx!='".$idx."'
                 and is_display = 'Y'".$where_query." order by display_date desc limit ".$offset.",".$limit;
        $res = $this->db->query($sql);
        $row = $res->result_array();


        return $row;


    }





}
?>
