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

class m_column extends CI_Model
{

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

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

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

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

        if ($search_type != '' && $search_key != '')
            $this->db->where($search_type, $search_key);

        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('*, "2" as content_type, column_name as category_name');
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('column');
        $this->db->limit($limit, $offset);
        $this->db->order_by($sort_column, 'desc');
        $this->db->order_by('display_date', 'desc');
        $res = $this->db->get();
        $row = $res->result_array();

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : list - total count
    // CREATE : 2018-04-28
    // ---------------------------------------------------------------------------
    function column_list_total_count($req_data)
    {
        if (isset($req_data['search_type']) == false)
            $req_data['search_type'] = '';
        if (isset($req_data['search_key']) == false)
            $req_data['search_key'] = '';
        if (isset($req_data['search_keyword']) == false)
            $req_data['search_keyword'] = '';
        $search_type = $req_data['search_type'];
        $search_key = $req_data['search_key'];
        $search_keyword = $req_data['search_keyword'];
        if ($search_type == 'main_search' && $search_keyword != '')
            $this->db->where("match(title, content, tag) against('" . $search_keyword . "'  in boolean mode)");
        if ($search_type != '' && $search_key != '')
            $this->db->where($search_type, $search_key);

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

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : total count
    // CREATE : 2018-04-28
    // ---------------------------------------------------------------------------
    function column_total_count()
    {
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('column');
        $row = $this->db->count_all_results();

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : select idx
    // CREATE : 2018-04-28
    // ---------------------------------------------------------------------------
    function column_select($idx)
    {
        $this->db->select('*, column_name as category_name');
        $this->db->where('idx', $idx);
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('column');
        $res = $this->db->get();
        $row = $res->row_array();

        return $row;
    }

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

        return $row;
    }

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

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

        return $row;
    }

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

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

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : where now_date
    // CREATE : 2018-05-08
    // ---------------------------------------------------------------------------
    function column_where_display_date_count($req_data)
    {
        // request
        $reg_date = $req_data;

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

        return $row;
    }

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

        return $row;
    }

    // ---------------------------------------------------------------------------
    // PURPOSE : where now_date + partner_idx
    // CREATE : 2018-05-30
    // ---------------------------------------------------------------------------
    function column_where_display_date_partner_idx_count($req_data)
    {
        // request
        $partner_idx = $req_data['partner_idx'];
        $now_date = $req_data['now_date'];

        $this->db->where('partner_idx', $partner_idx);
        $this->db->where('display_date >=', $now_date);
        $this->db->where('is_del', 'N');
        $this->db->where('is_display', 'Y');
        $this->db->from('column');
        $row = $this->db->count_all_results();

        return $row;
    }
}
?>
