<?php 
class Mydocument_Model extends Model{
	function __construct(){
		parent::__construct();
	}
	function conlist($channelid,$catid,$page=0,$showline=30){
		$this->db->select("c.channel_name,s.*")
		->from("search_cache as s")
		->join("channel as c","c.channel_id","s.channel_id")
		->where("s.memberid",$_SESSION['user']->user_id)
		->orderby("s.publish_date","DESC");
		if($page>0){
			$this->db->limit($showline,$page*$showline-$showline);
		}
		return $this->db->get();
	}
	function addContent($model_vlaue="",$insert){
		if($this->db->insert($model_vlaue."_content",$insert)){
			//取得当前文章ID
			$content_id	=	$this->db->insert_id();
			return $content_id;
		}
		else{
			false;
		}
	}
	function updateContent($model_value,$content,$content_id){
		$this->db->where("content_id",$content_id);
		return $this->db->update($model_value."_content",$content);
	}
	function delete($model_value,$channel_id,$content_id=false){
		//删除搜索缓存
		if($content_id){
			$this->db->in("content_id",$content_id)->where("memberid",$_SESSION['user']->user_id)->delete("search_cache");
		}
		//删除附件
		if($content_id){
			$this->db->in("content_id",$content_id);
		}
		$this->db->where("channel_id",$channel_id);
		$annexs	=	$this->db->get($model_value."_annex")->result_array();
		foreach($annexs as $annex){
			@unlink($annex->path);
			@unlink($annex->thumbs_path);
		}
		//删除内容缩略图
		if($content_id){
			$this->db->in("content_id",$content_id);
		}
		$this->db->where("channel_id",$channel_id);
		$contents	=	$this->db->get($model_value."_content")->result_array();
		foreach($contents as $content){
			@unlink($content->thumbs_path);
		}
		//删除内容
		if($content_id){
			$this->db->in("content_id",$content_id);
		}
		return $this->db->where("memberid",$_SESSION['user']->user_id)
		->where("channel_id",$channel_id)
		->delete($model_value."_content");
	}
	function deleteSearch($channelid,$contentid=false){
		if($contentid)	$this->db->in("content_id",$contentid);
		$this->db->where("channel_id",$channelid);
		$this->db->where("memberid",$_SESSION['user']->user_id);
		return $this->db->delete("search_cache");
	}
}