<?php


require_once( "_inc.php" );
$action = !empty( $_GET['a'] ) ? trim( $_GET['a'] ) : "all";
$page = !empty( $_GET['p'] ) ? intval( $_GET['p'] ) : 1;
$pageSize = 100;
$timeStart = date( "Y-m-d H:i:s" );
echo "<style>body{font-size:14px}</style>";
if ( "index" == $action || "all" == $action && "1" == $page )
{
	$tpl->assign( $gSite );
	$tpl->assign( "systemVersion", $gVersion );
	$tpl->assign( "categoryDir", $gCategoryDir );
	$html = $tpl->fetch( "../templates/index.tpl" );
	if ( util::writefile( "../index.html", $html ) )
	{
		echo "生成首页HTML ... 成功！<br>";
	}
}
if ( "category" == $action )
{
	$categoryId = !empty( $_GET['cId'] ) ? intval( $_GET['cId'] ) : "";
	$tpl->assign( "categoryId", $categoryId );
	if ( !file_exists( "../".$gCategoryDir ) )
	{
		mkdir( "../".$gCategoryDir );
	}
	if ( !empty( $categoryId ) )
	{
		$tpl->assign( $gSite );
		$tpl->assign( "systemVersion", $gVersion );
		$tpl->assign( "categoryDir", $gCategoryDir );
		$html = $tpl->fetch( "../templates/category.tpl" );
		$englishName = $db->fetchone( "SELECT f_englishName FROM ".$gDb['prefix'].( "categories WHERE (f_id='".$categoryId."') order by f_order" ) );
		$fileName = !empty( $englishName ) ? trim( $englishName ) : $categoryId;
		if ( util::writefile( "../".$gCategoryDir.( "/".$fileName.".html" ), $html ) )
		{
			echo "生成分类 [".$categoryId."] HTML ... 成功！";
		}
	}
}
if ( "all" == $action )
{
	$arr = $db->fetchpage( "SELECT f_id, f_name FROM ".$gDb['prefix']."categories", $page, $pageSize );
	if ( $arr )
	{
		echo "正在生成全站HTML，请稍候……<br><br>";
		if ( !file_exists( "../".$gCategoryDir ) )
		{
			mkdir( "../".$gCategoryDir );
		}
		$i = 0;
		for ( ;	$i < count( $arr );	++$i	)
		{
			$categoryId = $arr[$i]['f_id'];
			$tpl->assign( "categoryId", $categoryId );
			$tpl->assign( $gSite );
			$tpl->assign( "systemVersion", $gVersion );
			$tpl->assign( "categoryDir", $gCategoryDir );
			$html = $tpl->fetch( "../templates/category.tpl" );
			$englishName = $db->fetchone( "SELECT f_englishName FROM ".$gDb['prefix'].( "categories WHERE (f_id='".$categoryId."') order by f_order" ) );
			$fileName = !empty( $englishName ) ? trim( $englishName ) : $categoryId;
			if ( util::writefile( "../".$gCategoryDir.( "/".$fileName.".html" ), $html ) )
			{
				echo "[".$arr[$i]['f_id']."] ".$arr[$i]['f_name']." ... 成功！<br>";
			}
		}
		++$page;
		echo "<meta http-equiv='refresh' content='0;url=?p=".$page."'>";
	}
	else
	{
		echo "全站HTML生成成功！<br><br><a href=\"../\" target=\"_blank\">访问前台</a><br><br><font color=\"red\">注意</font>:如果生成全站后，主页无法访问，请用CTRL + F5 强制刷新缓存，并重新打开浏览器";
		$page = 0;
	}
}
?>
