<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');
	
/*
 *제품 자료관리 
 * */
	
class adProduct_m extends CI_Model {
    function __construct() {
        parent::__construct();
    }
 	
    /**
     * 
     *  Functions of admin. By rwi, 2015-10-26
     * 
     */
    function get_list() {
        $sql = "SELECT 
        			c.f_id AS sn,
        			c.f_title_kn AS title, 
        			(SELECT f_title_kn FROM tbl_enum WHERE f_topid=d.f_topid LIMIT 1) AS LCategoryID, 
        			(SELECT f_title_kn FROM tbl_enum WHERE f_id=c.f_categoryid) AS SCategoryID, 
        			c.f_createdate AS DATE, 
        			c.f_visible AS isActive 
        		FROM tbl_product AS c 
        			LEFT JOIN tbl_enum AS d ON c.f_categoryid=d.f_id 
        		ORDER BY c.f_id DESC";
        
        $sql = "SELECT 
        			@SEQ := @SEQ+1 AS num, 
        			tbl.sn,
			        tbl.title, 
			        tbl.LCategoryID, 
			        tbl.SCategoryID, 
			        tbl.date, 
			        tbl.isActive,
			        0 AS isSelect
		        FROM ($sql) AS tbl, (SELECT @SEQ := 0) A";
        
        $query = $this->db->query($sql);
        $result = $query -> result();
        return $result;
    }
    
    function get_data($sn) {
    	$sql = "SELECT 
        			c.f_id AS sn,
        			c.f_title_kn AS title, 
        			f_topid AS LCategoryID, 
        			f_subid AS SCategoryID, 
        			c.f_createdate AS date, 
        			c.f_preView AS slideImage,
    				c.f_contents_kn AS contents,
    				c.f_visible AS status
        		FROM tbl_product AS c 
        			LEFT JOIN tbl_enum AS d ON c.f_categoryid=d.f_id 
    			WHERE c.f_id=$sn
        		ORDER BY c.f_id DESC";
    	$query = $this->db->query($sql);
    	$result = $query -> row();
    	return $result;
    }
    
    function insert_Product($LCategoryID, $SCategoryID, $title, $slideImage, $contents, $date, $status){
    	$cid = $this->db->get_where('tbl_enum', array('f_topid'=>$LCategoryID, 'f_subid'=>$SCategoryID))->row();
    	 
    	$title = mysql_real_escape_string($title);
    	$slideImage = mysql_real_escape_string($slideImage);
    	$contents = mysql_real_escape_string($contents);
    	 
    	$sql = "INSERT INTO tbl_product (f_categoryid, f_title_kn, f_preView, f_contents_kn, f_createdate, f_visible) VALUE ('$cid->f_id', '$title', '$slideImage', '$contents', '$date', '$status')";
    	$query = $this->db->query($sql);
    	return;
    }
    
    function save_Product($sn, $LCategoryID, $SCategoryID, $title, $slideImage, $contents, $date, $status){ 
    	$cid = $this->db->get_where('tbl_enum', array('f_topid'=>$LCategoryID, 'f_subid'=>$SCategoryID))->row();
    	
    	$title = mysql_real_escape_string($title);
    	$slideImage = mysql_real_escape_string($slideImage);
    	$contents = mysql_real_escape_string($contents);
    	
    	$sql = "UPDATE tbl_product SET f_categoryid='$cid->f_id', f_title_kn='$title', f_preView='$slideImage', f_contents_kn='$contents', f_createdate='$date', f_visible='$status' WHERE f_id=$sn";
    	$query = $this->db->query($sql); 
    	return;    	
    }
    
    function del_Product($CatgoryID){
    	$sql = "DELETE FROM tbl_product WHERE f_id=" . $CatgoryID;
    	$query = $this->db->query($sql);
    	return;
    }
    
    function get_lcid() {
    	$sql = "SELECT f_title_kn as text, f_topid as value FROM tbl_enum WHERE f_subid=0 ORDER BY f_id ";
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function get_scid($lcid) {
    	$sql = "SELECT f_title_kn as text, f_subid as value FROM tbl_enum WHERE f_topid=$lcid AND f_subid>0 ORDER BY f_id ";
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    /**
     *
     *  Functions of user. By rwi, 2015-10-27
     *
     */
    
    function get_large_category() {
    	$sql = "SELECT f_topid AS cid, f_title_kn AS title FROM tbl_enum WHERE f_subid=0 ORDER BY f_topid";
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function get_small_category($LCategoryID) {
    	$sql = "SELECT f_id AS cid, f_title_kn AS title FROM tbl_enum WHERE f_topid=$LCategoryID AND f_subid>0 ORDER BY f_subid";
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function get_slideImage($cateId) {
    	$sql = "SELECT f_id as id, f_preview AS url FROM tbl_product WHERE f_categoryid=$cateId AND f_visible=1 ORDER BY f_id";
    	$query = $this->db->query($sql);
    	$result = $query->result();
    	return $result;
    }
    
    function get_contents($id) {
    	$sql = "SELECT f_title_kn AS title, f_contents_kn AS contents FROM tbl_product WHERE f_id=$id ORDER BY f_id";
    	$query = $this->db->query($sql);
    	$result = $query->row();
    	return $result;
    }
 
}