<?php
class Imgdemo_Model extends Model {
	function __construct(){
		parent::__construct();
	}
	function demolists($channel_id,$typeid=false,$demoid=false){
		if($demoid){	$this->db->where("id",$demoid);		}
		if($typeid){	$this->db->where("type_id",$typeid);}
		$this->db->where("channel_id",$channel_id);
		$this->db->orderby("order","DESC");
		$this->db->orderby("time","DESC");
		return $this->db->get("imgdemo");
	}
	function add($insert){
		return $this->db->insert("imgdemo",$insert);
	}
	function updateImgdemo($update_data,$slider_id){
		$this->db->where("id",$slider_id);
		return $this->db->update("imgdemo",$update_data);
	}
	function deleteImgdemo($slider_id=false){
		if($slider_id){
			$this->db->in("id",$slider_id);
			$slider	=	$this->db->get("imgdemo")->result_array();
			foreach ($slider as $row){
				@unlink($row->img_url);
			}
			$this->db->in("id",$slider_id);
			return $this->db->delete("imgdemo");
		}
		return false;
	}
	
	function gettypes($channel_id=false,$type_id=false){
		if($type_id){
			$this->db->where("type_id",$type_id);
		}
		if($channel_id){
			$this->db->where("channel_id",$channel_id);
		}
		return $this->db->get('imgdemo_type');
	}
	
	function addtype($insert_type){
		return $this->db->insert('imgdemo_type',$insert_type);
	}
	function check_slider_type($name,$value,$channel_id){
		$sql	=	"SELECT * FROM (`".$this->table['slider_type']."`) WHERE (`slider_type_value` = '$value' OR `slider_type_name` = '$name') AND `channel_id` = '$channel_id'";
		return $this->db->query($sql)->num_rows();
	}
	function deleteType($type_id=false){
		if($type_id){
			$this->db->in("type_id",$type_id);
			$slider	=	$this->db->get("imgdemo")->result_array();
			foreach ($slider as $row){
				@unlink($row->slider_img_url);
			}
			$this->db->in("type_id",$type_id);
			$this->db->delete("imgdemo_type");
			$this->db->in("type_id",$type_id);
			$this->db->delete("imgdemo");
			return true;
		}
		return false;
	}
	function upType($typeid,$data){
		return $this->db->where("type_id",$typeid)->update("imgdemo_type",$data);
	}
		//图片展示
	function get_channel_slider($channel_id=false,$slider_type=false,$start=0,$showline=5){
		$this->db->where("picture_slider.channel_id",$channel_id);
		$this->db->join($this->table['slider_type']." as picture_slider_type","picture_slider_type.slider_type_value='".$slider_type."'");
		$this->db->where("picture_slider.slider_type_id=picture_slider_type.slider_type_id");
		$this->db->where("picture_slider.slider_status=1");
		$this->db->orderby("picture_slider.slider_create_time DESC");
		$this->db->orderby("picture_slider.slider_order DESC");
		$this->db->limit($showline,$start);
		return $this->db->get($this->table['slider']." as picture_slider")->result_object();
		
	}
}