<?php
class commentControl extends skymvc{
	
	function __construct(){
		parent::__construct();
		$this->loadModel(array("comment","login","product"));
		$this->userid=$this->login->userid;
		$this->userInfo=$this->login->getUser();
	}
	
	public function onDefault(){
		 
	}
	
	public function onProduct(){
		$this->login->checklogin(1);
		$id=get_post("id","i");
		$data["object_id"]=get_post("object_id","i");
		$row=$this->product->selectRow(array("where"=>" id=".$data['object_id']." "));
		$data['shopid']=intval($row['shopid']);
		$data["dateline"]=time();
		if(isset($_POST['grade'])){
			$data['grade']=round(post('grade'),1);
		}
		$data["content"]=post("content","x");
		if(empty($data['content'])){
			exit(json_encode(array("error"=>3,"message"=>"评论内容不能为空")));
		}
		$data["siteid"]=SITEID;
		$data["rateId"]=post("rateId","i");
		$data["type_id"]=post("type_id","i");
		$data["object_user_id"]=get_post("object_user_id","i");
		$data["userid"]=$this->userid;
		$data['pid']=post('pid','i');
		$data['ip']=$_SERVER['REMOTE_ADDR'];
		$ipcity=ipcity($_SERVER['REMOTE_ADDR']);
		print_r($ipcity);
		if($ipcity){
			$data['ip_city']=$ipcity['region'].$ipcity['city'].$ipcity['county'];
		}else{
			$data['ip_city']="外星球";
		}
		$data["referer"]=get_post("referer","h");
		if(empty($data['object_id']) or empty($data['userid'])){
			exit(json_encode(array("error"=>1,"message"=>"参数错误")));
		}
		if($id){
			$this->comment->update($data,array('id'=>$id));
		}else{
			$data['id']=$this->comment->setTable("comment_10")->insert($data);
			$this->comment->setTable("comment",$data['object_id'])->insert($data);
		}
		$data['nickname']=$this->userInfo['nickname'];
		$data['content']=stripslashes($data['content']);
		exit(json_encode(array("error"=>0,"message"=>$data)));
		
	}
	
	public function ongetComment(){
		$pid=intval(get('pid'));
		$cache_key="comment_getComment_$pid";
		if(!$this->cache->get($cache_key)){
			$this->cache->set($cache_key,1,600);
			$this->loadClass("taobao");
			require_once(ROOT_PATH."config/taobao_config.php");
			$this->taobao->setConfig($tao_appconfig);
			$this->loadModel(array("comment"));
			$this->loadModel(array("product"));			 
			$goods=$this->product->getRow("SELECT goodsid,userid FROM ".table('product')." WHERE id='$pid' ");
			if(empty($goods)) exit; 				 			 
			$arr=$this->taobao->getCommentList($goods['goodsid']);
			if($arr){
				foreach($arr as $k=>$v){
					$rateId=round($v['rateId']);
					$data=array();
					if(!$this->comment->selectRow(array("where"=>"rateId=$rateId"))){
						$data['object_id']=$pid;
						$data['type_id']=1;
						$data['dateline']=time();
						$data['rateId']=$rateId;
						$data['content']=$v['content'];
						$data['status']=1;
						$data['referer']=APPINDEX."?m=show&id=".$pid;
						$data["object_user_id"]=$goods['userid'];
						$data['id']=$this->comment->setTable("comment_10")->insert($data);
						$this->comment->setTable("comment",$data['object_id'])->insert($data);
					}
				}
			}
			exit('var s="获取评论成功"; ');
		}

	} 
	
	/**
	*我的评论
	*/
	
	public function onMy(){
		$start=get('start','i');
		$limit=10;
		$url="/index.php?m=comment&a=my";
		$where=" status<99 AND userid=".$this->userid." ";
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"where"=>$where,
			"order"=>" id DESC"
		);
		$rscount=true;
		$data=$this->comment->select($option,$rscount);
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(array(
			"data"=>$data,
			"pagelist"=>$pagelist,
			"rscount"=>$rscount,
		));
		$this->smarty->display("comment/u_my.html");
	}
	 
	
}

?>