<?php
defined('IN_YUNYECMS') or exit('No permission.');
core::load_class('base',false);
core::load_fun('content');
core::load_fun('tree');
core::load_fun('shop');
class notice extends base {
	function __construct() {
	 parent::__construct();
	 $this->db->tablename = 'msg';
	 }
	//首页
	 public function index(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('my_notice')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
		$cfg=$this->cfg;
		$lang=$this->lang; 
		islogin(); 
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_notice'),'url'=>url("member/notice/index"))
			);		 
		$userid=usafestr(yunyecms_strdecode(ugetcookie("userid")));
		$member=$this->member;
		 $pagesize=20;
		 $sqlquery="select * from `#yunyecms_notice`  ";
		 $where=" where status=1 and lang={$this->lang["id"]}  and touserid=$userid ";
		 $sqlcnt=" select count(*) from `#yunyecms_notice` ";
		 $order=" order by `addtime` desc,`id` desc ";
		  if(isset($_REQUEST)){
		   if(!empty($_REQUEST["searchkey"])){
		        $searchkey=usafestr(trim($_REQUEST["searchkey"]));
		        $where=$where." and ( `title`  like '%{$searchkey}%' )";
			  }
		 }
		 $pagearr=$this->db->pagelist($sqlcnt,$sqlquery,$where,$order,$pagesize);
		 if($pagearr["count"]!=0){
			 $list=$pagearr["query"];
			 foreach($list as $key=>$var){
				   $list[$key]["content"]=uhtmlspecialchars_decode($var["content"]);
				   $list[$key]["content_en"]=uhtmlspecialchars_decode($var["content_en"]);
						 if($var["exlink"]){
				               $list[$key]["url"]=$var["exlink"];
						 }else{
				               $list[$key]["url"]=url('member/notice/show',array('id'=>$var['id'])); 
						 }
			 }
			 $page=$pagearr["page"];
		 }
        include tpl('notice','member');
	}

	
	 public function show(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$cfg=$this->cfg;
		$lang=$this->lang;
		if($this->member){
			$token=ugetcookie("loginrnd");
		 }
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_notice'),'url'=>url("member/notice/index"))
			);	
		if(!empty($_REQUEST["id"])){
		$id = intval(usafestr($_REQUEST['id']));
		if(!is_numeric($id)){
		  messagebox(Lan('error_parameter'));		
			 }
		$row=$this->db->find("select * from `#yunyecms_notice`  where id={$id}");
		if(empty($row)){
		      messagebox(Lan('error_parameter'));		
		}
		$row["content"]=uhtmlspecialchars_decode($row["content"]);
		$row["content_en"]=uhtmlspecialchars_decode($row["content_en"]);
		$prevsql='';
		$nextsql='';
		$prevsql=" addtime>=".$row['addtime']." and id<>".$id."  ";
		$nextsql=" addtime<=".$row['addtime']."  and id<>".$id."  ";	
		$prev=$this->db->find("select * from `#yunyecms_notice`  where {$prevsql} order by addtime asc limit 0,1");
	    if($prev){
			$prev["url"]=url("member/notice/show",array("id"=>$prev["id"]));
		}		 
		$next=$this->db->find("select * from `#yunyecms_notice`  where {$nextsql}  order by addtime desc limit 0,1");
		if($next){
			$next["url"]=url("member/notice/show",array("id"=>$next["id"]));
		}		 
		$this->db->query("update  `#yunyecms_notice` set hits=hits+1  where id={$id}");
		$row['time']=udate($row['addtime']);
			$seo['title']=$row["title"].'-'.$seo['title'];
			$seo["keywords"]=(empty($row["keywords"])?$row["title"]:$row["keywords"]).','.$seo["keywords"];
			$seo["description"]=(empty($row["description"])? strcut(trim(strip_tags($row["content"])),200):$cat["description"]);			 
		 }else{
		      messagebox(Lan('error_parameter'));		
		}
		$pinglun_list=$this->db->select("select *  from `#yunyecms_comment` where status=1 and  `infoid`= '{$id}' and flag=0" );
		foreach($pinglun_list as $key=>$var){
			$plid=$var['id'];
			$replay_list=$this->db->select("select *  from `#yunyecms_replay` where status=1 and  plid={$plid} and `infoid`= '{$id}' and flag=0 " );
			$pinglun_list[$key]['replay']= $replay_list;
		}
        include tpl('notice_show','member');
	}	
	
	 function addpinglun() {
		$cfg=$this->cfg;
		$lang=$this->lang; 
		$cat=$this->cat; 
	    islogin();
		$member=$this->member;	
		$userid=$this->member['id'];
		$groupid=$this->member["groupid"]; 
		if($groupid==2){
			$exptime=$this->member["exptime"];
			if($exptime&&$exptime<=time()){
				 messagebox("您的会员已到期！请联系管理员",$_SERVER['HTTP_REFERER'],"info");	
			}
			$haspower=1;
			$hasinfopower=1;
		 }else{
			messagebox("您必须先开通VIP会员才可以评论！",url('member/membergroup_goods/index'),"info");				
		 }


		$_REQUEST=yytrim($_REQUEST);
	    if(!empty($_REQUEST['infoid'])){
			$kcid=$_REQUEST['infoid'];
			if(!is_numeric($kcid)){
		     messagebox("参数错误");		
             }
		 }else{
			 $curnotice=$this->db->find("select * from `#yunyecms_notice` where id={$kcid}");
			 if(!$curnotice){
				messagebox("参数错误");		
			}
		 }
	     if(empty($userid)){
			  messagebox("您还没有登录");	
		 }

	    if(empty($_REQUEST['content'])){
		     messagebox("评论内容不能为空");		
		 }else{
			$content=usafestr($_REQUEST['content']);
		}
		 checktoken(trim($_POST["token"]));
		  $ip=getip();
		  $data["ip"]=$ip;
		  $todaytime=getToday();
		  $cntcheckone=$this->db->GetCount("select count(*) from `#yunyecms_comment` where  addtime>={$todaytime['start']} and addtime<{$todaytime['end']}  and ip='$ip'");
		  if($cntcheckone>=5){
				 messagebox(Lan('submit_daylimit'));
		   }		 
	          $curtime=time();
			  $data['userid'] = $userid;
			  $data['infoid'] = $kcid;
			  $data['content'] = $content;
			  $data['title'] = $curnotice['title'];
			  $data['addtime'] = $curtime;
			  $data['updatetime'] = $curtime;
			  $data['status'] = 0;
			  $data['ip'] = $ip;
              $insertid = $this->db->insert($data,'pinglun',true);
			  if ($insertid !== false){
		          messagebox("提交成功，您的评论需经过管理员审核才能够显示，请耐心等待 !",url('member/notice/show',array('id'=>$kcid)));
              }else {
		        messagebox("评论失败","back",'warn');		
             }
      }
	
	
	
	
	
	
	private function check_exist($id) {
			  $id = trim($id);
			  if(empty($id)||empty($id)){
				 return false;
				 }else{
				  if(!is_numeric($id)){
						  return false;
					 }
				  $cnt=$this->db->GetCount("select count(*) from `#yunyecms_notice` where    `id`= {$id}");
				  if($cnt>=1){
					 return true;
				  }else{ return false;}			 
			  }
		  }		
}

?>