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

class m_tag extends CI_Model{

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

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


    #---------------------------------------------------------------------------
    # PURPOSE :	tag count
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function tag_select_where_tag_count($req_data){
	    # request
	    $tag                = $req_data['tag'];
        $content_type       = $req_data['content_type'];
        $content_idx        = $req_data['content_idx'];


        $this->db->where('tag', $tag);
        $this->db->where('content_type', $content_type);
        $this->db->where('content_idx', $content_idx);
        $this->db->from('tag');
        $row = $this->db->count_all_results();

        return $row;
    }



    #---------------------------------------------------------------------------
    # PURPOSE :	tag insert
    # CREATE  :	2018-03-08
    #---------------------------------------------------------------------------
	function tag_insert($req_data){
        $user_id    = $this->session->userdata('Adm_Id');
        $now_date   = date("Y-m-d H:i:s");

        $req_data['reg_id']     = $user_id;
        $req_data['reg_date']   = $now_date;
        $row = $this->db->insert('tag', $req_data);

        return $row;
    }


    #---------------------------------------------------------------------------
    # PURPOSE :	delete
    # CREATE  : 2018-03-12
    #---------------------------------------------------------------------------
	function tag_delete($req_data){
        # request
	    $content_idx        = $req_data['content_idx'];
        $content_type       = $req_data['content_type'];

        $delete_data = array('content_idx' => $content_idx,
                             'content_type' => $content_type
        );
        $row = $this->db->delete('tag', $delete_data);

        return $row;

    }

    #---------------------------------------------------------------------------
    # PURPOSE :	tag - list
    # CREATE  :	2018-03-12
    #---------------------------------------------------------------------------
	function tag_list($req_data){
        # request
        $offset                 = $req_data['offset'];
        $limit                  = $req_data['limit'];

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

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }

        if($sort_column == 'hit_count') {
           $sort_column =  'count(*)';
        }  else {
            $sort_column =  'max('.$sort_column.')';
        }


        $this->db->select('tag, count(*) as cnt, max(reg_date) as reg_date');
        $this->db->where('content_type', $content_type);
        $this->db->from('tag');
        $this->db->group_by('tag');
        $this->db->limit($limit, $offset);
        $this->db->order_by($sort_column,  'desc');
        $res = $this->db->get();

        $row = $res->result_array();

        return $row;
    }

    #---------------------------------------------------------------------------
    # PURPOSE : tag - total count
    # CREATE  : 2018-03-12
    #---------------------------------------------------------------------------
	function tag_list_total_count($req_data){
	    # request
        $content_type           = $req_data['content_type'];
	    $search_type            = $req_data['search_type'];
        $search_keyword         = $req_data['search_keyword'];

        if($search_type!='' && $search_keyword!=''){
            $this->db->like($search_type, $search_keyword);
        }
        $this->db->select('tag');
        $this->db->where('content_type', $content_type);
        $this->db->from('tag');
        $this->db->group_by('tag');
        $row = $this->db->count_all_results();
        $this->db->last_query();



        return $row;
    }

































}
?>
