<?php
header( "Content-Type:text/html;charset=utf8;" );
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'arthtml' );
saxue_getconfigs( 'cate', 'article' );
if ( isset( $_REQUEST['dosubmit'] ) ) {
		if ( !is_array( $_REQUEST['packflag'] ) || count( $_REQUEST['packflag'] ) < 1 ) {
				saxue_printfail( "请选择要生成HTML页或文件" );
		}
		saxue_getconfigs( "configs", "article" );
		if ( in_array( 'makehtml', $_REQUEST['packflag'] ) || in_array( 'makeindex', $_REQUEST['packflag'] ) || in_array( 'makechapter', $_REQUEST['packflag'] ) ) {
				if ( empty( $_REQUEST['pc'] ) && empty( $_REQUEST['mobile'] ) ) {
						saxue_printfail( "PC版和手机版至少选择一种！" );
				} 
				if ( $saxueConfigs['article']['url_detail'] <= 2 ) {
						saxue_printfail( "请设置内容页URL为静态格式！" );
				}
		}
		$client = 0;
		if ( !empty( $_REQUEST['pc'] ) ) {
				if ( empty( $saxueConfigs['article']['htmldir'] ) || empty( $saxueConfigs['article']['htmlurl'] ) ) {
						saxue_printfail( "请设置PC版HTML文件目录和访问URL！" );
				}
				$client += 1;
		}
		if ( !empty( $_REQUEST['mobile'] ) ) {
				if ( empty( $saxueConfigs['article']['mobile_htmldir'] ) || empty( $saxueConfigs['article']['htmlurl'] ) ) {
						saxue_printfail( "请设置手机版HTML文件目录和访问URL！" );
				}
				$client += 2;
		}
		
		include SAXUE_ROOT_PATH . "/model/article.php";
		$article_handler = saxuearticlehandler :: getinstance( "saxuearticlehandler" );
		$criteria = new criteriacompo( new criteria( 'status', 1 ) );
		if ( $_REQUEST['type'] == 'ids' ) {
				if ( empty( $_REQUEST['ids'] ) ) {
						saxue_printfail( "请输入文章ID列表！" );
				}
				$criteria -> add( new criteria( 'aid', '(' . $_REQUEST['ids'] . ')', 'IN' ) );
				$packflag = array( );
				foreach ( $_REQUEST['packflag'] as $k => $v ) {
						if ( $v != 'makehtml' ) $packflag[$v] = 1;
				}
		} else {
				$page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1;
				$total = isset( $_REQUEST['total'] ) ? intval( $_REQUEST['total'] ) : 0;
				$pagesize = !empty( $_REQUEST['pagesize'] ) ? intval( $_REQUEST['pagesize'] ) : 100;
				if ( !is_array( $_REQUEST['catids'] ) ) {
						$catids = $_REQUEST['catids'];
				} elseif ( $_REQUEST['catids'][0] > 0 ) {
						$catids = '';
						foreach ( $_REQUEST['catids'] as $_cid ) {
								if ( $saxueCate['article'][$_cid]['child'] ) {
										$catids .= ',' . $saxueCate['article'][$_cid]['arrchild'];
								} else {
										$catids .= ',' . $_cid;
								}
						}
						$catids = substr( $catids, 1 );
				} else {
						$catids = '';
				} 
				if ( !in_array( 'makehtml', $_REQUEST['packflag'] ) ) {
						$criteria -> add( new criteria( 'ischapter', 1 ) );
				} elseif ( count( $_REQUEST['packflag'] ) == 1 && in_array( 'makehtml', $_REQUEST['packflag'] ) ) {
						$criteria -> add( new criteria( 'ischapter', 0 ) );
				}
				if ( !empty( $catids ) ) {
						$criteria -> add( new criteria( 'cid', '(' . $catids . ')', 'IN' ) );
				}
				$url = '?dosubmit=1&catids=' . $catids . '&type=' . $_REQUEST['type'] . '&pagesize=' . $pagesize;
				if ( !empty( $_REQUEST['pc'] ) ) {
						$url .= '&pc=1';
				}
				if ( !empty( $_REQUEST['mobile'] ) ) {
						$url .= '&mobile=1';
				}
				$packflag = array( );
				foreach ( $_REQUEST['packflag'] as $k => $v ) {
						$url .= '&packflag[' . $k . ']=' . $v;
						if ( $v != 'makehtml' ) $packflag[$v] = 1;
				}
				switch ( $_REQUEST['type'] ) {
						case 'lastinput':
								$criteria -> setsort( "aid" );
								$criteria -> setorder( "DESC" );
								break;
						case 'date':
								if ( !empty( $_REQUEST['fromtime'] ) ) {
										$criteria -> add( new criteria( 'addtime', strtotime( $_REQUEST['fromtime'] . ' 00:00:00' ), '>=' ) );
										$url .= '&fromtime=' . $_REQUEST['fromtime'];
								}
								if ( !empty( $_REQUEST['totime'] ) ) {
										$criteria -> add( new criteria( 'addtime', strtotime( $_REQUEST['totime'] . ' 23:59:59' ), '<=' ) );
										$url .= '&totime=' . $_REQUEST['totime'];
								}
								break;
						case 'id':
								if ( !empty( $_REQUEST['fromid'] ) ) {
										$criteria -> add( new criteria( 'aid', intval( $_REQUEST['fromid'] ), '>=' ) );
										$url .= '&fromid=' . $_REQUEST['fromid'];
								}
								if ( !empty( $_REQUEST['toid'] ) ) {
										$criteria -> add( new criteria( 'aid', intval( $_REQUEST['toid'] ), '<=' ) );
										$url .= '&toid=' . $_REQUEST['toid'];
								}
								break;
				} 
				$criteria -> setlimit( $pagesize );
				$criteria -> setstart( ( $page - 1 ) * $pagesize );
		}
		$article_handler -> queryobjects( $criteria );
		include SAXUE_ROOT_PATH . "/common/packarticle.php";
		echo '<style>body{font-size:12px;padding:10px;}a{color:blue}</style>';
		saxue_obflush( '开始生成HTML.....<br><br>' );
		while ( $v = $article_handler -> getobject() ) {
				$aid = $v -> getvar( 'aid' );
				$ischapter = $v -> getvar( 'ischapter' );
				if ( $ischapter ) {
						$ret = RepackArticle( $aid, $packflag, true, $client );
				} else {
						$packarticle = new saxuepackarticle( $aid );
						$ret = $packarticle -> makearticle( $client );
				}
				if ( is_object( $ret ) ) {
						saxue_obflush( $aid . '：《<a href="' . $ret['url'] . '" target="_blank">' . $ret['title'] . '</a>》<font color="green">生成成功</font><br>' );
				} elseif ( $ret ) {
						saxue_obflush( $aid . '：<font color="green">生成成功</font><br>' );
				} else {
						saxue_obflush( $aid . '：<font color="red">生成失败</font><br>' );
				}
		}
		if ( $_REQUEST['type'] != 'ids' ) {
				if ( empty( $total ) ) {
						$total = $article_handler -> getcount( $criteria );
				} 
				$pages = ceil( $total / $pagesize );
				if ( $pages > $page ) {
						$msg = '<br><br>总文章数：' . $total . '，已生成 ' . ( $page * $pagesize ) . ' 篇，正在跳转到下一页';
						++$page;
						$url .= '&total=' . $total . '&page=' . $page;
						saxue_jumppage( $url, $msg, 1 );
				} 
		}
		exit( '<br><br>HTML生成完成' );
}
include SAXUE_ROOT_PATH . "/lib/util/tree.php";
$tree = new tree;
$tree -> icon = array( '&nbsp;│&nbsp;', '&nbsp;├─&nbsp;', '&nbsp;└─&nbsp;' );
$tree -> nbsp = '&nbsp;';
$tree -> mid = 'cid';
$tree -> pid = 'pid';
$tree -> init( $saxueCate['article'] );
$str = "<option value='\$cid'>\$spacer\$cname</option>";
$cates = $tree -> get_tree( 0, $str );
include SAXUE_ADMIN_PATH . "/header.php";
$saxueTpl -> assign( "cates", $cates );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/html.html";
include SAXUE_ADMIN_PATH . "/footer.php";
