<?php
class comment{
	var $channelid = 0;
	var $id = 0;
	var $key = '';
	var $cache = false;
	var $cache_time = 86400;
	var $self_see = false;
	function comment($channelid, $id, $self_see = 0){
		global $_SET;
		$this->channelid = $channelid;
		$this->id = $id;
		$this->key = "r_{$this->channelid}_{$this->id}";
		$this->cache = intval($_SET['comment_cache']) ? true : false;
		$this->self_see = intval($self_see);
	}
	function get($offset, $pagesize){
		$data = array('data'=>array(), 'total'=>0);
		if($this->cache == false){
			$data['total'] = $this->_get_total();
			if($data['total'] > 0){
				$data['data'] = qdb::fetchAll("SELECT * FROM ".TABLEPRE."comment  where channelid='{$this->channelid}' and id='{$this->id}' order by commentid desc limit {$offset}, {$pagesize}");
				$data['data'] = $this->_format_self_see($data['data']);
			}
			return $data;
		}else{
			if(false === ($data = cache::get($this->key))){
				$data = array();
				$data['total'] = $this->_get_total();
				$data['data'] = array();
				if($data['total'] > 0) $data['data'] = qdb::fetchAll("SELECT * FROM ".TABLEPRE."comment  where channelid='{$this->channelid}' and id='{$this->id}' order by commentid asc ");
				$this->_set($this->key, $data, $this->cache_time);
			}
			return array(
				'total'=>$data['total'],
				'data'=>array_slice($this->_format_self_see($data['data']), $offset, $pagesize)
			);
		}
	}

	function _format_self_see(&$data){
		global $user_info;
		if(!$this->self_see) return $data;
		$ip = qbase::get_ip();
		foreach($data as &$val){
			if($val['is_pass'] > 0) continue;
			if(($user_info['uid'] > 0 && $val['uid'] == $user_info['uid']) || $val['ip'] == $ip) $val['is_pass'] = 1;
		}
		return $data;
	}

	function _get_total(){
		return qdb::queryAlone("select count(*) as num FROM ".TABLEPRE."comment where  channelid='{$this->channelid}' and id='{$this->id}' ");
	}

	function clear($commentid = 0){
		if($this->cache == false) return;
		if($commentid == 0){
			$this->_delete($this->key);
		}else{
			$param = qdb::queryFirst("select channelid, id from ".TABLEPRE."comment where commentid = '{$commentid}' ");
			if(empty($param)) return;
			$this->key = "r_{$param['channelid']}_{$param['id']}";
			$this->clear();
		}
	}
	
	function add(&$param){
		$mapping = array(
			0 => 'bad',
			1 => 'normal',
			2 => 'good',
		);
		$filed = $mapping[$param['vote']];
		if(empty($filed)) return false;
		$param['content'] = qbase::substrs($param['content'], 1000);
		$sql = qbase::createSql(TABLEPRE."comment", $param, "insert");
		$param['commentid'] = qdb::Query( $sql, true);
		$this->clear();
		$num = qdb::queryFirst("select * from ".TABLEPRE."comment_view where channelid = '{$this->channelid}' and id = '{$this->id}' ");

		$arr = array(
			'good'=>0,
			'bad'=>0,
			'normal'=>0,
			'absolute'=>0
		);
		
		if(empty($num)){
			$arr[$filed] = 1;
			$arr['absolute'] = $arr['good'] + $arr['bad'] - abs($arr['good'] - $arr['bad']);
			qdb::query("insert into ".TABLEPRE."comment_view (channelid, id, total, {$filed}, absolute) values ('{$this->channelid}', '{$this->id}', 1, 1, {$arr['absolute']})");
		}else{
			$num[$filed] = $num[$filed] + 1;
			$num['absolute'] = $num['good'] + $num['bad'] - abs($num['good'] - $num['bad']);
			qdb::query("update ".TABLEPRE."comment_view set total = total + 1, {$filed} = {$filed} + 1, absolute = '{$num['absolute']}' where channelid = '{$this->channelid}' and id = '{$this->id}'");
		}
		
		qbase::get_object('event', 'comment', array('action'=>'add', 'data'=>$param));

		return true;
	}

	function _set($key, $val, $cache_time){
		cache::set($key, $val, $cache_time);
	}
	
	function _delete($key){
		cache::delete($key);
	}
}
?>