<?php
class content{
	var $channelid = 0;
	var $channelselect = 0;
	var $id = 0;
	var $mod = '';
	var $table = '';
	var $has_extend = false;
	var $cache_content = false;
	var $timeout = 3600 ;
	function content($channelid, $id, $cache = true){
		global $_MODULE, $channel, $_SET;
		$this->channelid = $channelid;
		$this->channelselect = $channel[$channelid]['channelselect'];
		$this->id = $id;
		$this->mod = $_MODULE[$this->channelselect]['table_name'];
		if(empty($this->mod)) die("module not found");
		$this->table = "".TABLEPRE."" . $this->mod . "_" . $this->channelid;
		$this->has_extend = is_array($_MODULE[$this->channelselect]['extend']) && (count($_MODULE[$this->channelselect]['extend']) > 0);
		$this->cache_content = (intval($_SET['cache_content']) && defined('A_P') === false && $cache == true) ? true : false;
		$this->timeout = 3600 * 24 * 30;
	}

	/*获取单个内容*/
	function get(){
		$data = array();
		$_pay_default = array(
			'requirepoint'=>0,	
			'read_purview'=>0,	
			'pay_type'=>0,	
			'read_last'=>0
		);
		$key = $this->_get_key($this->id);
		if($this->cache_content == false || false === ($data = cache::get($key))){
			$sql = "select * from {$this->table} USE INDEX (PRIMARY) where id ='{$this->id}' limit 1 ";
			$_output = qdb::QueryFirst($sql);
			if(empty($_output) || !is_array($_output)){
				if(!qbase::is_admin()) return array();
				$tp = qdb::queryAlone("select data from " . TABLEPRE . "recycle where id = '{$this->id}' and channelid = '{$this->channelid}' ");
				if(!$tp) return array();
				$_output = @unserialize($tp);
				if(empty($_output) || !is_array($_output)) return array();
			}
			$_output['status'] = intval($_output['status']);
			$_output['paid'] = 0;
			if($this->has_extend) $_output = array_merge($_output , $this->_get_extend());
			if($_output['pay'] == 0){
				$data = array_merge($_output, $_pay_default);
			}else{
				$_pay = qdb::QueryFirst("select requirepoint, read_purview, pay_type, read_last from ".TABLEPRE."pay_mapping where channelid = '{$this->channelid}' and  id = '{$this->id}' limit 1");
				if(empty($_pay)){
					$_pay = $_pay_default;
				}
				$data = array_merge($_output, $_pay);
			}
			if($data['extdata']){
				$temp = @unserialize($data['extdata']);
				if(is_array($temp)){
					$data = array_merge($data, $temp);
				}
			}
			unset($data['extdata']);
			if($this->cache_content == true) cache::set($key, $data, $this->timeout);
		}
		return $data;
	}
	
	/*获取单个内容的关联内容,比如一个下载内容的所有下载地址*/
	function get_s(){
		$key = $this->_get_key_s($this->id);
		if($this->cache_content == false || false === ($data = cache::get($key))){	
			$data = qdb::fetchAll("select * from ".TABLEPRE."{$this->mod}s_{$this->channelid} where id = '{$this->id}' order by ordering asc");
			if($this->cache_content == true) cache::set($key, $data, $this->timeout);
		}
		return $data;
	}

	/*如果开启内容缓存,内容更新时删除缓存*/
	function clear(){
		$ids = !is_array($this->id) ? array($this->id) : $this->id;
		foreach($ids as $id){
			cache::delete($this->_get_key($id));
			cache::delete($this->_get_key_s($id));
			cache::delete('tag_' . $this->channelid . '_' . $id );
		}
		return true;
	}
	function clear_s($pid = 0){
		if($pid > 0){
			$this->id = qdb::queryAlone("select id from ".TABLEPRE."{$this->mod}s_{$this->channelid} where pid = '{$pid}' ");
			$this->clear_s();
		}else{
			cache::delete($this->_get_key_s($this->id));
		}
	}
	
	/*获取内容的扩展数据*/
	function _get_extend(){
		global $_MODULE;
		$_output = qdb::queryFirst("select * from ".TABLEPRE."extend_data_{$this->channelselect} where channelid = '{$this->channelid}' and id = '{$this->id}' ");
		unset($_output['channelid'], $_output['id']);
		return empty($_output) ? array() : $_output;
	}
	
	function _get_key($id){
		return 'detail_' . $this->channelid .'_'. $id;
	}
	
	function _get_key_s($id){
		return 'detail_s_' . $this->channelid .'_'. $id;
	}

	function get_item(){
		if($this->cache_content == false){
			$sql = "select a.*, c.pur_browse, c.ciddir, c.html, c.category_html, c.title as parent_title, c.c_allow from {$this->table} as a left join ".TABLEPRE."acategory as c on a.cid=c.cid where a.id = '{$this->id}' ";
			$info = qdb::queryFirst($sql);
			if(empty($info)) return array();
		}else{
			$info = $this->get();
			if(empty($info)) return array();
			$_category_info = getCidInfo($this->channelid, $info['cid']);
			$info = array_merge($info, $_category_info);
		}
		$iif = !( $info['pur_browse'] || $info['html'] || $info['pay']);
		$info['parent_url'] = qbase::GetCidHtmlDirType($info['channelid'], $info['cid'], $info['ciddir'], $info['cfilename'], !($info['pur_browse'] || $info['category_html']));
		if(isset($info['is_link']) && $info['is_link'] > 0){
			$info['url'] = $info['linkurl'];
		}else{
			$info['url']  = qbase::GetHtmlDirType($info['channelid'], $info['id'], $info['ciddir'], $info['dateline'] , $info['filename'], $info['ver'], $iif);
		}
		return $info;
	}
}
?>