<?php 
class Attachment_Controller extends Front_Controller{
	function __construct(){
		parent::__construct();
		$this->db	=	new Database();
		$this->html =	new Html_Model();
		$this->attach=	new Attachment_Model();
	}
	function image($channelid,$catid,$contentid,$annexid){

		$channel	=	$this->db->where("channel_id",$channelid)->join("model as m","c.model_id","m.model_id")->get("channel as c")->current();
		if($annexid<=0){
			$content				=	$this->html->conCurr($channel->model_value,$contentid,$channelid);
			$this->lmtpl->content	=	$content;
			$GLOBALS['channel_id']	=	$channelid;
			$this->lmtpl->render($channel->channel_down_tpl);
		}
		else{
			$annex					=	$this->html->getannexs($channel->model_value,$contentid,$annexid)->current();
			$content				=	$this->html->conCurr($channel->model_value,$contentid,$channelid);
			$this->lmtpl->annex		=	$annex;
			$GLOBALS['channel_id']	=	$channelid;
			$GLOBALS['catid']		=	$catid;
			$GLOBALS['content_id']	=	$contentid;
			$this->lmtpl->content	=	$content;
			if(!@$_SESSION['user']){
				$this->lmtpl->imgurl	=	path::getImgUrl($annex->path);
				$this->lmtpl->render($channel->channel_pictrue_tpl);
				return '';
			}
			$note		=	array(
				"memberid"	=>	$_SESSION['user']->user_id,
				"attachid"	=>	$annexid,
				"channelid"	=>	$channelid,
				"contentid"	=>	$contentid,
			);
			$attachs	=	$this->attach->downNote($note)->count();
			if(($annex->read_pointer>$_SESSION['user']->integral||$annex->read_money>$_SESSION['user']->integral)&&$attachs<=0){
				$this->Report("你的积分不够,可以通过发布文档来获取积分","mydocument/select_channel_category");
			}
			else{
				if($attachs<=0){
					$integral	=	$_SESSION['user']->integral-$annex->read_pointer;
					$rmb		=	$_SESSION['user']->rmb-$annex->read_money;
					$this->attach->updateMemberIntegral($integral,$rmb,$_SESSION['user']->user_id);
					$note['integral']	=	$annex->read_pointer;
					$note['time']		=	time();
				}
				else{
					$note['time']		=	time();
					$note['integral']	=	0;
				}
				$this->attach->addDownNote($note);
				if($annex->is_image){
					$this->lmtpl->imgurl	=	path::getImgUrl($annex->path);
					$this->lmtpl->render($channel->channel_pictrue_tpl);
				}
			}
		}
	}
	function download($channelid,$catid,$contentid,$annexid=0){
		
		$channel	=	$this->db->where("channel_id",$channelid)->join("model as m","c.model_id","m.model_id")->get("channel as c")->current();
		if($annexid<=0){
			$content	=	$this->html->conCurr($channel->model_value,$contentid,$channelid);
			$this->lmtpl->content	=	$content;
			$GLOBALS['channel_id']	=	$channelid;
			$GLOBALS['catid']		=	$catid;
			$GLOBALS['content_id']	=	$contentid;
			$this->lmtpl->render($channel->channel_down_tpl);
		}
		else{
			
			$annex		=	$this->html->getannexs($channel->model_value,$contentid,$annexid)->current();
			$this->lmtpl->annex	=	$annex;
			if(!@$_SESSION['user']){
				if(!preg_match("#http:#i",$annex->path)){
					download::force($annex->path);
				}
				else{
					header("location:".$annex->path);
				}
				return '';	
			}
			$note		=	array(
				"memberid"	=>	$_SESSION['user']->user_id,
				"attachid"	=>	$annexid,
				"channelid"	=>	$channelid,
				"contentid"	=>	$contentid,
			);
			$attachs	=	$this->attach->downNote($note)->count();
			if(($annex->read_pointer>$_SESSION['user']->integral||$annex->read_money>$_SESSION['user']->integral)&&$attachs<=0){
				$this->Report("你的积分不够,可以通过发布文档来获取积分","mydocument/select_channel_category");
			}
			else{
				if($attachs<=0){
					$integral	=	$_SESSION['user']->integral-$annex->read_pointer;
					$rmb		=	$_SESSION['user']->rmb-$annex->read_money;
					$this->attach->updateMemberIntegral($integral,$rmb,$_SESSION['user']->user_id);
					$note['integral']	=	$annex->read_pointer;
					$note['time']		=	time();
				}
				else{
					$note['time']		=	time();
					$note['integral']	=	0;
				}
				$this->attach->addDownNote($note);
				if(!preg_match("#http:#i",$annex->path)){
					download::force($annex->path);
				}
				else{
					header("location:".$annex->path);
				}
			}
		}
		
	}
}