<?php


require_once( "_inc.php" );
$applyId = !empty( $_GET['aId'] ) ? trim( $_GET['aId'] ) : "";
$categoryId = !empty( $_GET['cId'] ) ? trim( $_GET['cId'] ) : "";
$backUrl = !empty( $_GET['back'] ) ? trim( $_GET['back'] ) : base64_encode( "category_list.php?pId=".$categoryId );
$addUrl = !empty( $_GET['addUrl'] ) ? trim( $_GET['addUrl'] ) : base64_encode( $_SERVER['REQUEST_URI'] );
if ( !empty( $_GET['btnSubmit'] ) )
{
	util::auth( );
	$name = !empty( $_GET['name'] ) ? trim( $_GET['name'] ) : "";
	$color = !empty( $_GET['color'] ) ? trim( $_GET['color'] ) : "";
	$url = !empty( $_GET['url'] ) ? trim( $_GET['url'] ) : "";
	$categoryId = !empty( $_GET['categoryId'] ) ? trim( $_GET['categoryId'] ) : "";
	$isRecommend = !empty( $_GET['isRecommend'] ) ? trim( $_GET['isRecommend'] ) : "0";
	$order = !empty( $_GET['order'] ) ? trim( $_GET['order'] ) : "";
	$expireTime = !empty( $_GET['expireTime'] ) ? trim( $_GET['expireTime'] ) : "0";
	$description = !empty( $_GET['description'] ) ? trim( $_GET['description'] ) : "";
	if ( empty( $name ) || empty( $url ) || empty( $categoryId ) )
	{
		$tpl->assign( "msg", "缺少参数。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	if ( !empty( $expireTime ) )
	{
		$expireTime = strtotime( $expireTime );
	}
	$exist = $db->fetchone( "SELECT f_id FROM ".$gDb['prefix'].( "sites WHERE (f_categoryId='".$categoryId."' AND f_name='{$name}')" ) );
	if ( $exist )
	{
		$tpl->assign( "msg", "该分类已存在。" );
		$tpl->assign( "addUrl", base64_decode( $addUrl ) );
		$tpl->assign( "backUrl", base64_decode( $backUrl ) );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	$sql = "INSERT INTO ".$gDb['prefix'].( "sites SET f_name='".$name."', f_color='{$color}', f_url='{$url}', f_categoryId='{$categoryId}', f_isRecommend='{$isRecommend}', f_order='{$order}', f_expireTime='{$expireTime}', f_description='{$description}'" );
	$db->query( $sql );
	$tpl->assign( "msg", "添加成功。" );
	$tpl->assign( "addUrl", base64_decode( $addUrl ) );
	$tpl->assign( "backUrl", base64_decode( $backUrl ) );
	$tpl->display( "_msg.tpl" );
	if ( $applyId )
	{
		$db->query( "DELETE FROM ".$gDb['prefix'].( "applies WHERE (f_id='".$applyId."')" ) );
	}
	exit( );
}
$tpl->assign( "applyId", $applyId );
$tpl->assign( "categoryId", $categoryId );
$tpl->assign( "backUrl", $backUrl );
$tpl->assign( "addUrl", $addUrl );
$tpl->display( );
?>
