<?php


require_once( "_inc.php" );
$parentId = !empty( $_GET['pId'] ) ? trim( $_GET['pId'] ) : "0";
$backUrl = !empty( $_GET['back'] ) ? trim( $_GET['back'] ) : base64_encode( "category_list.php?pId=".$parentId );
$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'] ) : "";
	$parentId = !empty( $_GET['parentId'] ) ? trim( $_GET['parentId'] ) : "0";
	$autoList = !empty( $_GET['autoList'] ) ? trim( $_GET['autoList'] ) : "0";
	$englishName = !empty( $_GET['englishName'] ) ? trim( $_GET['englishName'] ) : "";
	$order = !empty( $_GET['order'] ) ? trim( $_GET['order'] ) : "";
	$keywords = !empty( $_GET['keywords'] ) ? trim( $_GET['keywords'] ) : "";
	$description = !empty( $_GET['description'] ) ? trim( $_GET['description'] ) : "";
	$url = !empty( $_GET['url'] ) ? trim( $_GET['url'] ) : "";
	if ( empty( $name ) )
	{
		$tpl->assign( "msg", "缺少参数。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	$exist = $db->fetchone( "SELECT f_id FROM ".$gDb['prefix'].( "categories WHERE (f_parentId='".$parentId."' 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( );
	}
	if ( !empty( $englishName ) )
	{
		$id = $db->fetchone( "SELECT f_id FROM ".$gDb['prefix'].( "categories WHERE (f_englishName='".$englishName."')" ) );
		if ( $id )
		{
			$tpl->assign( "msg", "英文名称“ ".$englishName." ”已经存在。请返回修改。" );
			$tpl->display( "_msg.tpl" );
			exit( );
		}
	}
	$sql = "INSERT INTO ".$gDb['prefix'].( "categories SET f_name='".$name."',f_parentId='{$parentId}', f_autoList='{$autoList}', f_order='{$order}', f_englishName='{$englishName}', f_keywords='{$keywords}', f_description='{$description}', f_url='{$url}'" );
	$db->query( $sql );
	$sql = "UPDATE ".$gDb['prefix'].( "categories SET f_haveChild='1' WHERE (f_id='".$parentId."')" );
	$db->query( $sql );
	$tpl->assign( "msg", "添加成功。" );
	$tpl->assign( "refreshUrl", "sort.php" );
	$tpl->assign( "addUrl", base64_decode( $addUrl ) );
	$tpl->assign( "backUrl", base64_decode( $backUrl ) );
	$tpl->display( "_msg.tpl" );
	exit( );
}
$tpl->assign( "parentId", $parentId );
$tpl->assign( "backUrl", $backUrl );
$tpl->assign( "addUrl", $addUrl );
$tpl->display( );
?>
