<?
//comments
include "config.inc.php";
include "class/member.class.php";
//设定
$confirm='yes';//yes:需要管理员认证后才能显示,no:直接显示
$member=new member();
$user_info=$member->member_auth();
$ulevel=$user_info[user_level];
########设置#############
$action = $_REQUEST[action];
$page="10";//每页的评论数
$need_user = "0";//是否需要登陆
$id = $_REQUEST[id];//需要评论内容的ID
$fdb = $PlusDB->prefix(forum);//评论的数据库
switch($action){
	case "showNum":
	$sql="select count(*) as num from ".$fdb." where post_type='$id'";
	$numrow=$PlusDB->query_firstrow($sql);
	$showNum = $numrow[num];
	echo "
	function comment(id)
	 {
	 var page = \"".PLUS_URL."/comments.php?id=\" + id ;
	 popwin = window.open(page,\"\",\"width=460,height=500,scrollbars,resizable\")
	 popwin.focus();
	 }
	document.open();
 	document.write(\"<a href=\\\"javascript:comment('$id')\\\" title=\\\""._LANG_0930."\\\">"._LANG_0931." $showNum "._LANG_0932."</a>\");
 	document.close();";
	break;
	case "saveComment":
	//保存comment
	$comment_text= $_POST[comment_text];
	$name = $_POST[name];
	$email = $_POST[email];
	$fid = $_POST[id];
	if($comment_text=="" || !$comment_text) error(_LANG_0933);
	if($name == "" || !$name) error(_LANG_0934);
	if($fid == "" || !$fid) error(_LANG_0935);
	if(!checkEmail($email)) error(_LANG_0936);
	$ctime=time();
	$sql="Insert into $fdb (post_uid,post_aid,post_title,post_content,post_date,post_type) values ('$name','$id','$email','$comment_text','$ctime','$confirm')";
	if($PlusDB->queryF($sql)){
		succ("$url",_LANG_0937);
	}else{
		error(_LANG_0938);
	}
	break;
	case "del":
		$post_id=$_GET[cid];
		$sql="delete from $fdb where post_id='$post_id'";
		$PlusDB->queryF($sql);
		redirect($url);
		break;
	case "showMark":
		    header("Cache-Control: no-store,no-cache,must-revalidate");
		    $mark = showMark($id);
			if($mark>""){
			$mark=str_replace("\r\n","",addslashes($mark));
			echo "document.write(\"$mark\");";
			}
	break;
	case "showList":
		header("Cache-Control: no-store,no-cache,must-revalidate");
		$sql="select * from $fdb where post_aid='$id' and post_type='yes'  order by post_id desc";
		$rt=$PlusDB->selectLimit($sql,5,0);
		$num = $rt->recordCount();
		if($num==0){
			$ret=array();
		}else{
			while($row=$rt->fetchRow()){
				$row[num]=$num;
				$row[comments] = format_text($row[post_content],1,0,1,0);
				$patt = "/\<[^\>]+\>/";
				$row[comments]  =msubstr(preg_replace($patt,"",$row[comments]),0,100);
				$row[post_date] = date("Y-m-d H:i",$row[post_date]);
				$ret[]=$row;
				$num=$num-1;
			}
			}
			include "include/smarty.php";
			$smarty->assign("aid",$id);
			$smarty->assign("comms",$ret);
			$cm = $smarty->fetch("template/comm_s_list.tpl");
			if($cm>""){
			$cm=str_replace("\r\n","",addslashes($cm));
			echo "document.write(\"$cm\");";
		}
		break;
	default:
	?>
<body  topmargin=0>
<LINK href="fm.css" type=text/css rel=stylesheet>
<table width="100%"  border="0" cellpadding="0" cellspacing="1" bgcolor=white>
  <tr>
    <td width="413" align="center" ><table width="411"  border="0" cellpadding="5" cellspacing="0"  style="margin-top: 2px;">
      <tr>
        <td  colspan="3">
<?
	$sql="select * from $fdb where post_aid='$id' and post_type='yes'  order by post_id desc";
	$rt=$PlusDB->query($sql);
	$num=$rt->RecordCount($rt);
	if($num==0){
		echo _LANG_0939;
	}else{
	$start=$_GET[start];
	$start=empty($start)?0:$start;
	$start=($start>$num)?0:$start;
	//$sql .= " limit $start,$page";
	$rt=$PlusDB->SelectLimit($sql,$page,$start);
	$string.="<table width=100% border=0>";
	while($row=$rt->fetchRow($rt)){
		$comments = format_text($row[post_content],1,0,1,0);
		$date = date("Y-m-d H:i",$row[post_date]);
		$string .= "<tr bgcolor=#e8e8e8><td><a href=\"mailto:$row[post_title]\">$row[post_uid]</a>"._LANG_0940." $date "._LANG_0941."&nbsp;&nbsp; <br> $comments ";
		if($ulevel=="Admin") $string.= "<a href=".PLUS_URL."/comments.php?action=del&cid=$row[post_id]>"._LANG_0942."</a></td></tr>
		<tr bgcolor=#ffffff><td><hr size=1></td></tr>
		";
	}
	$string.="</table>";
	echo $string;
	$vr=array("id"=>$id);
	$pages=new pages();
	$pages->cnt=$num;
	$pages->lim=$page; //records per page
	$pages->type="1";
	$pages->script=""; //Call only if you need to link to other document
	$pages->vars=$vr;
	$pages->make_list();
	}
?>
</td></tr>
<form method="post" action=""><div>
<input type="hidden" name="id" value="<?=$id?>" />
<input type="hidden" name="action" value="saveComment" />
<tr>
<td colspan="3">
<b><?=_LANG_0943?></b><br />
<textarea class=input  style="width: 400px;" name="comment_text" cols="45" rows="4"></textarea><br />
</td>
</tr>
<tr>
<td ><?=_LANG_0944?><input class=input  type="text" name="name" value="<?=$user_info[user_name]?>" size="15" maxlength="25" /></td>
<td ><?=_LANG_0945?><input class=input  type="text" name="email" value="<?=$user_info[user_email]?>" size="15" maxlength="60" /></td>
<td valign=middle><input class=input type="submit" value="<?=_LANG_0946?>" /></td>
</tr>
</table>
</div></form>
<?
}
##commet form

function showMark($aid){
	global $PlusDB;
	$adb = $PlusDB->prefix(articles);
	$sql="select voter,vote  from $adb  where id='$aid'";
	$row=$PlusDB->getrow($sql);
	        $mmk = @ceil($row[vote] / $row[voter]);
            $nostar = "<img src=" . PLUS_URL . "/images/star_nocolor.gif border=0>";
            $star = "<img src=" . PLUS_URL . "/images/star.gif border=0>";
            switch ($mmk) {
                case 0:$mark = $nostar . $nostar . $nostar . $nostar . $nostar;
                    break;
                case 1:$mark = $nostar . $nostar . $nostar . $nostar . $star;
                    break;
                case 2:$mark = $nostar . $nostar . $nostar . $star . $star;
                    break;
                case 3:$mark = $nostar . $nostar . $star . $star . $star;
                    break;
                case 4:$mark = $nostar . $star . $star . $star . $star;
                    break;
                case 5:$mark = $star . $star . $star . $star . $star;
                    break;
            } 
			return $mark;
}
?>