<?php
header( "Content-Type:text/html;charset=utf8;" );
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'topichtml' );
if ( !empty( $_REQUEST['action'] ) ) {
		saxue_getconfigs( "configs", "topic" );
		if ( $saxueConfigs['topic']['url_detail'] <= 2 ) {
				saxue_printfail( "请设置内容页URL为静态格式！" );
		}
		if ( empty( $saxueConfigs['topic']['htmldir'] ) || empty( $saxueConfigs['topic']['htmlurl'] ) ) {
				saxue_printfail( "请设置HTML文件目录和访问URL！" );
		}
		include( SAXUE_ROOT_PATH . "/common/packtopic.php" );
		echo '<style>body{font-size:12px;padding:10px;}a{color:blue}</style>';
		switch ( $_REQUEST['action'] ) {
				case 'ids':
						if ( empty( $_REQUEST['ids'] ) ) {
								saxue_printfail( "请输入专题ID列表！" );
						}
						$ids = explode( ',', trim( $_REQUEST['ids'] ) );
						if ( !is_array( $ids ) || count( $ids ) <= 0 ) {
								saxue_printfail( "专题ID列表为空！" );
						}
						saxue_obflush( '开始生成HTML.....<br><br>' );
						foreach ( $ids as $topicid ) {
								$packtopic = new saxuepacktopic( $topicid );
								$ret = $packtopic -> maketopic( true );
								if ( !$ret ) {
										saxue_obflush( $topicid . '：<font color="red">生成失败</font><br>' );
								} else {
										saxue_obflush( $topicid . '：《<a href="' . $ret['url'] . '" target="_blank">' . $ret['title'] . '</a>》<font color="green">生成成功</font><br>' );
								}
						}
						echo '<br><br>HTML生成完成';
						exit;
				case 'time':
						if ( empty( $_REQUEST['fromtime'] ) ) {
								saxue_printfail( "开始日期不能为空！" );
						}
						$fromtime = strtotime( $_REQUEST['fromtime'] );
						if ( !empty( $_REQUEST['totime'] ) ) {
								$totime = strtotime( $_REQUEST['totime'] . ' 23:59:59' );
								if ( $fromtime > $totime ) {
										saxue_printfail( "结束日期不能早于开始日期！" );
								}
						}
						include( SAXUE_ROOT_PATH . "/model/topic.php" );
						$topic_handler = saxuetopichandler :: getinstance( "saxuetopichandler" );
						$sql = "SELECT * FROM " . saxue_dbprefix( 'topic' ) . " WHERE addtime>=" . $fromtime . " AND status=1";
						if ( !empty( $totime ) ) {
								$sql .= " AND addtime<=" . $totime;
						}
						$_result = $topic_handler -> db -> query( $sql );
						saxue_obflush( '开始生成HTML.....<br><br>' );
						while ( $v = $topic_handler -> getobject( $_result ) ) {
								$topicid = $v -> getvar( 'topicid' );
								$packtopic = new saxuepacktopic( $topicid );
								$ret = $packtopic -> maketopic();
								if ( !$ret ) {
										saxue_obflush( $topicid . '：<font color="red">生成失败</font><br>' );
								} else {
										saxue_obflush( $topicid . '：《<a href="' . $ret['url'] . '" target="_blank">' . $ret['title'] . '</a>》<font color="green">生成成功</font><br>' );
								}
						}
						echo '<br><br>HTML生成完成';
						exit;
				case 'range':
						$startid = intval( $_REQUEST['startid'] );
						$endid = intval( $_REQUEST['endid'] );
						if ( empty( $startid ) || empty( $endid ) ) {
								saxue_printfail( "专题开始ID和结束ID不能为空！" );
						} elseif ( $startid > $endid ) {
								saxue_printfail( "专题开始ID不能大于结束ID！" );
						}
						if ( $endid - $startid > 100 ) {
								$toid = $startid + 100 - 1;
						} else {
								$toid = $endid;
						}
						saxue_obflush( '开始生成HTML.....<br><br>' );
						for ( $topicid = $startid; $topicid <= $toid; $topicid++ ) {
								$packtopic = new saxuepacktopic( $topicid );
								$ret = $packtopic -> maketopic( true );
								if ( !$ret ) {
										saxue_obflush( $topicid . '：<font color="red">生成失败</font><br>' );
								} else {
										saxue_obflush( $topicid . '：《<a href="' . $ret['url'] . '" target="_blank">' . $ret['title'] . '</a>》<font color="green">生成成功</font><br>' );
								}
						}
						if ( $toid < $endid ) {
								$startid = $startid + 100;
								saxue_jumppage( 'html.php?action=range&startid=' . $startid . '&endid=' . $endid );
						} else {
								echo '<br><br>HTML生成完成';
						}
						exit;
		}
}
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/topic/templates/html.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
