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

class m_news extends CI_Model
{

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

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

    // ---------------------------------------------------------------------------
    // PURPOSE : list
    // CREATE : 2018-04-11
    // ---------------------------------------------------------------------------
    function news_list($req_data)
    {
        // request
        $offset = $req_data['offset'];
        $limit = $req_data['limit'];
        $sort_column = $req_data['sort_column'];

        $search_type = '';
        $search_keyword = '';
        $sort_type = 'desc';
        if (isset($req_data['search_type']) == true)
            $search_type = $req_data['search_type'];
        if (isset($req_data['search_keyword']) == true)
            $search_keyword = $req_data['search_keyword'];
        if (isset($req_data['sort_type']) == true)
            $sort_type = $req_data['sort_type'];

        if ($search_type == 'category_code' && $search_keyword != '') {
            $this->db->where('category_code like "' . $search_keyword . '%"');
        }

        if ($search_type == 'main_search' && $search_keyword != '') {
            $keywordAsteriskRemoved = str_replace('*','', $search_keyword);
            $this->db->group_start()
            ->like('title', $keywordAsteriskRemoved)
//             ->or_like('content', $keywordAsteriskRemoved)
//             ->or_like('tag', $keywordAsteriskRemoved)
            ->group_end();
//             $this->db->where("match(title, content, tag) against('" . $search_keyword . "'  in boolean mode)");
        }

        if ($search_type == 'main_search_and_video_url' && $search_keyword != '') {
            $this->db->where("match(title, content, tag) against('" . $search_keyword . "'  in boolean mode)");
            $this->db->where('video_url!=', '');
        }

        $this->db->select('*, "1" as content_type, (select homepage from press where is_del="N" and idx=news.press_idx) as press_homepage, (select category_name from news_section where category_code = SUBSTRING(news.category_code, 1, 2) and depth=1) as category_name');
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('news');
        $this->db->limit("10", $offset);
        $this->db->order_by($sort_column, $sort_type);
        $this->db->order_by('display_date', 'desc');
        $res = $this->db->get();
        $row = $res->result_array();
        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : list - total count
    // CREATE : 2018-04-11
    // ---------------------------------------------------------------------------
    function news_list_total_count($req_data)
    {
        if (isset($req_data['search_type']) == false)
            $req_data['search_type'] = '';
        if (isset($req_data['search_keyword']) == false)
            $req_data['search_keyword'] = '';
        $search_type = $req_data['search_type'];
        $search_keyword = $req_data['search_keyword'];

        if ($search_type == 'category_code' && $search_keyword != '')
            $this->db->where('category_code like "' . $search_keyword . '%"');
        if ($search_type == 'main_search' && $search_keyword != '')
            $this->db->where("match(title, content, tag) against('" . $search_keyword . "'  in boolean mode)");

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

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : select idx
    // CREATE : 2018-04-16
    // ---------------------------------------------------------------------------
    function news_join_news_section_press_select($idx)
    {
        $this->db->select('a.*, b.press_name, b.logo_img, b.homepage, (select category_name from news_section where category_code = SUBSTRING(a.category_code, 1, 2) and is_del = "N" and depth=1) as category_name1 , (select category_name from news_section where category_code = SUBSTRING(a.category_code, 1, 4) and is_del = "N"  and depth=2) as category_name2 , (select category_name from news_section where category_code = SUBSTRING(a.category_code, 1, 6) and is_del = "N"  and depth=3) as category_name3');
        $this->db->where('a.idx', $idx);
        $this->db->where('a.is_del', 'N');
        $this->db->where('a.is_display', 'Y');
        $this->db->where('b.is_del', 'N');
        $this->db->from('news as a');
        $this->db->join('press as b', 'a.press_idx=b.idx');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : update
    // CREATE : 2018-04-16
    // ---------------------------------------------------------------------------
    function news_review_update($req_data)
    {
        // request
        $idx = $req_data['idx'];
        $type = $req_data['type'];
        $value = $req_data['value'];

        $sql = "update news 
                   set review_count" . $type . " = review_count" . $type . " + " . $value . "
                  where idx= " . $idx;
        $row = $this->db->query($sql);

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : select
    // CREATE : 2018-04-17
    // ---------------------------------------------------------------------------
    function news_select($idx)
    {
        $this->db->select('*, (select homepage from press where is_del="N" and idx=news.press_idx) as press_homepage, (select category_name from news_section where category_code = SUBSTRING(news.category_code, 1, 2) and depth=1) as category_name');
        $this->db->where('idx', $idx);
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('news');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : select
    // CREATE : 2018-04-17
    // ---------------------------------------------------------------------------
    function news_select_idx($idx)
    {
        $this->db->where('idx', $idx);
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('news');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : hit +1 update
    // CREATE : 2018-04-27
    // ---------------------------------------------------------------------------
    function news_hit_count_update($idx)
    {
        $sql = "update news 
                   set hit_count = hit_count + 1
                  where idx= " . $idx;
        $row = $this->db->query($sql);

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : update
    // CREATE : 2018-04-28
    // ---------------------------------------------------------------------------
    function news_share_count_update($req_data)
    {
        // request
        $idx = $req_data['idx'];
        $value = $req_data['value'];

        $sql = "update news 
                   set share_count = share_count + " . $value . "
                  where idx= " . $idx;
        $row = $this->db->query($sql);

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : comment_count +1 update
    // CREATE : 2018-05-11
    // ---------------------------------------------------------------------------
    function news_comment_count_update($idx)
    {
        $sql = "update news 
                   set comment_count = comment_count + 1
                  where idx= " . $idx;
        $row = $this->db->query($sql);

        return $row;
    }
}
?>
