<?php
class view
{
	var $timeout = 86400;
	var $queue_num = 100;
	var $view_queue = false;
	var $mod = '';
	var $table = '';
	var $channelid = 0;
	var $id = 0;
	function view($channelid, $id){
		global $_MODULE, $channel, $_SET;
		$this->view_queue = intval($_SET['cache_views_queue']) ? true : false;
		$this->timeout = 3600 * 24 * 365;
		$this->channelid = $channelid;
		$this->id = $id;
		$this->mod = $_MODULE[$channel[$this->channelid]['channelselect']]['table_name'];	
		$this->table = "".TABLEPRE."" . $this->mod . "_" . $this->channelid;		
	}
	
	function get(){
		$data = $this->_get();
		return array($data['info']['dview'],$data['info']['wview'],$data['info']['mview'],$data['info']['views'], $data['digg']['good'], $data['digg']['bad']);
	}
	
	function update()
	{
		if(empty($this->mod)) return array();
		$key = $this->_get_key();
		$data = $this->_get();
		if(qbase::UpdateCount('d',$data['info']['viewtime'])){
				 $setup = " dview=dview+1 , ";   
				 $dview = $data['info']['dview'] = $data['info']['dview']+1;
		}else{
				 $setup = " dview=1 , ";  
				 $dview = $data['info']['dview'] = 1;
		}
		if(qbase::UpdateCount('w',$data['info']['viewtime'])){
				 $setup .=" wview=wview+1 , ";   
				 $wview = $data['info']['wview'] = $data['info']['wview']+1;
		}else{
				 $setup .= " wview=1 , ";   
				 $wview = $data['info']['wview'] = 1;
		}
		if(qbase::UpdateCount('m',$data['info']['viewtime'])){
				 $setup .= " mview=mview+1 , ";   
				 $mview = $data['info']['mview'] = $data['info']['mview']+1;
		}else{
				 $setup .= " mview=1 , ";   
				 $mview = $data['info']['mview'] = 1;
		}
		$timetemp = $_SERVER['REQUEST_TIME'];
		$setup .= " views = views+1 ,viewtime= '{$timetemp}' ";
		$data['info']['views'] = $data['info']['views'] + 1;
		$data['info']['viewtime'] = $timetemp;
		if($this->view_queue == true){
			if($data['info']['views'] % $this->queue_num == 0){
				$sql = qbase::createSql($this->table, $data['info'] , 'update') . " where id = '{$this->id}' ";
				qdb::query($sql);
				$param = array();
				$param['good'] = intval($data['digg']['good']);
				$param['bad'] = intval($data['digg']['bad']);
				$param['diff'] = abs($param['good'] - $param['bad']);
				$sql = qbase::createSql(TABLEPRE.'digg', $param , 'update') . " where channelid = '{$this->channelid}' and id = '{$this->id}' ";
				qdb::query($sql);
			}
			cache::set($key, $data, $this->timeout);
		}else{
			qdb::Query("UPDATE {$this->table} SET ".$setup." WHERE id = '{$this->id}' ");
		}
		return array($dview,$wview,$mview,$data['info']['views'], $data['digg']['good'], $data['digg']['bad']);
	}

	function update_digg($type){
		if($type){
			$good = 1;
			$bad = 0;
		}else{
			$good = 0;
			$bad = 1;
		}
		$data = array();
		if($this->view_queue == true){
			$data = $this->_update_digg_width_cache($good, $bad);
		}else{
			$data = $this->_update_digg_no_cache($good, $bad);
		}
		return $data;
	}
	
	function _update_digg_no_cache($good, $bad){
		$param = qdb::queryFirst("select good, bad, diff from ".TABLEPRE."digg where channelid = '{$this->channelid}' and id = '{$this->id}' ");
		if(empty($param)){
			$diff = 1;
			qdb::query("replace into ".TABLEPRE."digg (channelid, id, good, bad, diff) values ({$this->channelid}, {$this->id}, {$good}, {$bad}, {$diff})");
			$data = array('good'=>$good,'bad'=>$bad, 'status'=>1);
		}else{
			$diff = abs(($param['good']+$good) - ($param['bad']+$bad));
			qdb::query("update ".TABLEPRE."digg set good = good + {$good}, bad = bad + {$bad}, diff = {$diff} where channelid = '{$this->channelid}' and id = '{$this->id}'");
			$data = array('good'=>$param['good']+$good,'bad'=>$param['bad']+$bad, 'status'=>1);
		}
		return $data;
	}
	
	function _update_digg_width_cache($good, $bad){
		$key = $this->_get_key();
		$cdata = $this->_get();
		if($cdata['digg']['good'] == 0 && $cdata['digg']['bad'] == 0){
			qdb::query("replace into ".TABLEPRE."digg (channelid, id, good, bad, diff) values ({$this->channelid}, {$this->id}, {$good}, {$bad}, 1)");
			$cdata['digg']['good'] = $good;
			$cdata['digg']['bad'] = $bad;
			$cdata['digg']['diff'] = 1;
		}else{
			$cdata['digg']['good'] = $cdata['digg']['good'] + $good;
			$cdata['digg']['bad'] = $cdata['digg']['bad'] + $bad;
			$cdata['digg']['diff'] = abs($cdata['digg']['good'] - $cdata['digg']['bad']);
		}
		cache::set($key, $cdata, $this->timeout);
		return array('good'=>$cdata['digg']['good'],'bad'=>$cdata['digg']['bad'],'status'=>1);
	}
	
	function _get(){
		$key = $this->_get_key();
		if($this->view_queue == false || false === ($data = cache::get($key))){
			$instance = $data === false ? true : false;
			$data = array();
			$data['info'] = qdb::QueryFirst("select dview,wview,mview,views,viewtime from  {$this->table}  WHERE id = '{$this->id}' limit 1");
			$data['digg'] = qdb::QueryFirst("select good, bad from ".TABLEPRE."digg where channelid = '{$this->channelid}' and id = '{$this->id}' ");
			if($this->view_queue == true){
				cache::set($key, $data, $this->timeout);
				if($instance) $this->_update_queue_index();
			}
		}
		$data['digg']['good'] = intval($data['digg']['good']);
		$data['digg']['bad'] = intval($data['digg']['bad']);
		return $data;
	}
	
	function _get_key(){
		return 'count_' . $this->channelid . '_' . $this->id;
	}
	
	function _update_queue_index(){
		$key = "queue_" . $this->channelid;
		$string = cache::get($key);
		$item = $this->channelid .'_'. $this->id;
		if($string !== false && strpos($string, $item) !== false) return;
		$timeout = 3600 * 24 * 365;
		if($string === false){
			cache::set($key, $item, $timeout);
		}else{
			if(strlen($string) > 1024 * 20){
				$temp = explode(",", $string);
				foreach($temp as $val){
					$c = cache::get( 'count_' . $val);
					if($c === false) continue;
					$t = explode("_", $val);
					$this->_update_from_cache(intval($t[0]), intval($t[1]), $c);
				}
				cache::delete($key);
			}else{
				cache::set($key, $string .','. $item, $timeout);
			}
		}
	}

	function _update_from_cache($channelid, $id, &$data){
		$table = $this->_get_table($channelid);
		if(empty($table)) return;
		if(!empty($data['info'])){
			$sql = qbase::createSql($table, $data['info'] , 'update') . " where id = '{$id}' ";
			qdb::query($sql);
		}
		if(!empty($data['digg'])){
			$param = array();
			$param['good'] = intval($data['digg']['good']);
			$param['bad'] = intval($data['digg']['bad']);
			$param['diff'] = abs($param['good'] - $param['bad']);
			$sql = qbase::createSql(TABLEPRE.'digg', $param , 'update') . " where channelid = '{$channelid}' and id = '{$id}' ";
			qdb::query($sql);	
		}
	}

	function _get_table($channelid){
		static $data = array();
		if(isset($data[$channelid])) return $data[$channelid];
		global $_MODULE, $channel;
		$channelselect = $channel[$channelid]['channelselect'];
		$table = isset($_MODULE[$channelselect]) ? "".TABLEPRE."" . $_MODULE[$channelselect]['table_name'] . "_" . $channelid : false;
		if(!$table) return false;
		$data[$channelid] = $table;
		return $table;
	}
}
?>