<?php
//--------------- 收藏夹函数 ---------------

//增加收藏
function AddFavaq($id,$bookid,$from){
	global $empire,$level_r,$class_r,$dbtbpre,$emod_r;
	//是否登陆
	$user_r=islogin();
	$id=(int)$id;
	$bookid=(int)$bookid;
	if(empty($id)||empty($bookid))
	{
		printerror("ErrorUrl","history.go(-1)",1);
    }
	$bi=$empire->fetch1("select * from {$dbtbpre}ecms_bookname_index where id='$bookid' limit 1");
	if(empty($bi['id'])||$bi['id']!=$bookid)
	{
		printerror("ErrorUrl","",1);
	}
	$bk=$empire->fetch1("select * from {$dbtbpre}ecms_bookname where id='$bookid' limit 1");
	$classid=$bk[classid];
	$tb=GetBdTbname($bookid);
	$bdi=$empire->fetch1("select * from {$dbtbpre}ecms_".$tb[tbname]."_index where id='$id' limit 1");
	if(empty($bdi['id'])||$bdi['id']!=$id||!$bdi[checked])
	{
		printerror("ErrorUrl","",1);
	}
	$bdd=$empire->fetch1("select * from {$dbtbpre}ecms_".$tb[tbname]." where id='$id' limit 1");
	
	//是否已收藏
	$newsnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfavaq where id='$id' and bookid='$bookid' and userid='$user_r[userid]'");
	if($newsnum)
	{
		printerror("ReFavaq","history.go(-1)",1);
	}
	$favanum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfavaq where userid='$user_r[userid]'");
/*
	$groupid=$user_r[groupid];
	if($level_r[$groupid][favanum]<=$favanum)
	{
		printerror("MoreFava","history.go(-1)",1);
	}
*/
	$favatime=time();
	$sql=$empire->query("insert into {$dbtbpre}enewsfavaq(id,favatime,userid,username,bookid) values('$id','$favatime','$user_r[userid]','$user_r[username]','$bookid');");
	if($sql)
	{
		printerror("AddFavaqSuccess",$from,1);
	}
	else
	{
		printerror("DbError","history.go(-1)",1);
	}
}

//批量删除收藏
function DelFavaq_All($favaid){
	global $empire,$dbtbpre;
	//是否登陆
	$user_r=islogin();
	$count=count($favaid);
	if(empty($count))
	{printerror("NotDelFavaqid","history.go(-1)",1);}
	for($i=0;$i<$count;$i++)
	{
		$add.="fqid='".intval($favaid[$i])."' or ";
    }
	$add=substr($add,0,strlen($add)-4);
	$sql=$empire->query("delete from {$dbtbpre}enewsfavaq where (".$add.") and userid='$user_r[userid]'");
	if($sql)
	{printerror("DelFavaqSuccess","../member/fava/q/",1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}



?>