<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'article' );
$aid = intval( $_REQUEST['aid'] );
include SAXUE_ROOT_PATH . "/model/article.php";
$article_handler = saxuearticlehandler :: getinstance( "saxuearticlehandler" );
$article = $article_handler -> get( $aid );
if ( !is_object( $article ) ) {
		saxue_printfail( "文章不存在" );
} 
if ( $article -> getvar( 'ischapter' ) == 0 ) {
		saxue_printfail( "操作错误" );
} 
saxue_getconfigs( 'configs', 'article' );
include SAXUE_ROOT_PATH . "/model/article_chapter.php";
$chapter_handler = saxuearticlechapterhandler :: getinstance( "saxuearticlechapterhandler" );
if ( !empty( $_REQUEST['action'] ) ) {
		@set_time_limit( 0 );
		include SAXUE_ROOT_PATH . "/common/packarticle.php";
		switch ( $_REQUEST['action'] ) {
				case 'clean':
						$criteria = new criteriacompo( new criteria( "aid", $aid ) );
						$chapter_handler -> delete( $criteria );
						$packflag = array( "makecds" => 1 );
						$txtdir = $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . "/" . $aid;
						if ( is_dir( $txtdir ) ) {
								saxue_delfolder( $txtdir );
						} 
						if ( $saxueConfigs['article']['url_detail'] > 2 ) {
								if ( $saxueConfigs['article']['url_detail'] == 4 ) {
										$htmldir = $saxueConfigs['article']['htmldir'] . '/' . date( 'Ymd', $article -> getvar( 'addtime' ) ) . '/' . $aid;
										$mhtmldir = $saxueConfigs['article']['htmldir'] . '/' . date( 'Ymd', $article -> getvar( 'addtime' ) ) . '/' . $aid;
								} else {
										$htmldir = $saxueConfigs['article']['htmldir'] . saxue_getsubdir( $aid ) . '/' . $aid;
										$mhtmldir = $saxueConfigs['article']['htmldir'] . saxue_getsubdir( $aid ) . '/' . $aid;
								}
								if ( is_dir( $htmldir ) ) {
										saxue_delfolder( $htmldir );
								} 
								if ( is_dir( $mhtmldir ) ) {
										saxue_delfolder( $mhtmldir );
								} 
								$packflag['makeindex'] = 1;
						}
						RepackArticle( $aid, $packflag );
						break;
				case 'delete':
						if ( !isset( $_REQUEST['ids'] ) || !is_array( $_REQUEST['ids'] ) || count( $_REQUEST['ids'] ) < 1 ) {
								saxue_printfail( "请选择要删除章节" );
						}
						$criteria = new criteriacompo( new criteria( "aid", $aid ) );
						$criteria -> add( new criteria( "chapterid", '(' . implode( ',', $_REQUEST['ids'] ) . ')', 'IN' ) );
						$chapter_handler -> delete( $criteria );
						$packflag = array( "makecds" => 1 );
						$txtdir = $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . "/" . $aid;
						if ( $saxueConfigs['article']['url_detail'] > 2 ) {
								if ( $saxueConfigs['article']['url_detail'] == 4 ) {
										$htmldir = $saxueConfigs['article']['htmldir'] . '/' . date( 'Ymd', $article -> getvar( 'addtime' ) ) . '/' . $aid;
										$mhtmldir = $saxueConfigs['article']['htmldir'] . '/' . date( 'Ymd', $article -> getvar( 'addtime' ) ) . '/' . $aid;
								} else {
										$htmldir = $saxueConfigs['article']['htmldir'] . saxue_getsubdir( $aid ) . '/' . $aid;
										$mhtmldir = $saxueConfigs['article']['htmldir'] . saxue_getsubdir( $aid ) . '/' . $aid;
								}
								$packflag['makeindex'] = 1;
								$packflag['makehtml'] = 1;
						}
						foreach ( $_REQUEST['ids'] as $_chapterid ) {
								if ( is_file( $txtdir . '/' . $_chapterid . '.txt' ) ) {
									saxue_delfile( $txtdir . '/' . $_chapterid . '.txt' );
								} 
								if ( !empty( $htmldir ) && is_file( $htmldir . '/' . $_chapterid . '.html' ) ) {
									saxue_delfile( $htmldir . '/' . $_chapterid . '.html' );
								} 
								if ( !empty( $mhtmldir ) && is_file( $mhtmldir . '/' . $_chapterid . '.html' ) ) {
									saxue_delfile( $mhtmldir . '/' . $_chapterid . '.html' );
								} 
						}
						RepackArticle( $aid, $packflag );
						break;
				case 'repack':
						if ( !is_array( $_REQUEST['packflag'] ) || count( $_REQUEST['packflag'] ) < 1 ) {
								saxue_printfail( "请选择生成选项" );
						}
						if ( in_array( 'makeindex', $_REQUEST['packflag'] ) || in_array( 'makechapter', $_REQUEST['packflag'] ) ) {
								if ( $saxueConfigs['article']['url_detail'] <= 2 ) {
										saxue_printfail( "不能生成HTML页面，请先设置内容页URL为静态格式！" );
								}
						}
						$packflag = array( );
						foreach ( $_REQUEST['packflag'] as $v ) {
								$packflag[$v] = 1;
						}
						echo "                                                                                                                                                                                                                                                                ";
						echo '开始生成。。。';
						ob_flush( );
						flush( );
						RepackArticle( $aid, $packflag );
						break;
				case 'sort':
						$chaptercount = $chapter_handler -> getcount( new criteriacompo() );
						if ( !isset( $_REQUEST['fromid'] ) || $_REQUEST['fromid'] < 1 || $chaptercount < $_REQUEST['fromid'] || !isset( $_REQUEST['toid'] ) || $_REQUEST['toid'] < 0 || $chaptercount < $_REQUEST['toid'] ) {
								saxue_printfail( "章节排序错误" );
						}
						if ( $_REQUEST['fromid'] != $_REQUEST['toid'] && $_REQUEST['fromid'] != $_REQUEST['toid'] + 1 ) {
								$criteria = new criteriacompo( new criteria( "aid", $aid ) );
								$criteria -> add( new criteria( "chapterorder", $_REQUEST['fromid'] ) );
								$chapter_handler -> queryobjects( $criteria );
								$chapter = $chapter_handler -> getobject( );
								unset( $criteria );
								if ( !is_object( $chapter ) ) {
										saxue_printfail( "选择的章节不存在" );
								}
								$chapterid = $chapter -> getvar( "chapterid" );
								if ( $_REQUEST['fromid'] < $_REQUEST['toid'] ) {
										$criteria = new criteriacompo( new criteria( "aid", $aid ) );
										$criteria -> add( new criteria( "chapterorder", $_REQUEST['fromid'], ">" ) );
										$criteria -> add( new criteria( "chapterorder", $_REQUEST['toid'], "<=" ) );
										$chapter_handler -> updatefields( 'chapterorder=chapterorder-1', $criteria );
										unset( $criteria );
										$chapter_handler -> updatefields( 'chapterorder=' . $_REQUEST['toid'], 'chapterid=' . $chapterid );
								} else {
										$criteria = new criteriacompo( new criteria( "aid", $aid ) );
										$criteria -> add( new criteria( "chapterorder", $_REQUEST['fromid'], "<" ) );
										$criteria -> add( new criteria( "chapterorder", $_REQUEST['toid'], ">" ) );
										$chapter_handler -> updatefields( 'chapterorder=chapterorder+1', $criteria );
										unset( $criteria );
										$chapter_handler -> updatefields( 'chapterorder=' . ( $_REQUEST['toid'] + 1 ), 'chapterid=' . $chapterid );
								}
								$packarticle = new saxuepackarticle( $aid );
								$packarticle -> sortchapter( $_REQUEST['fromid'], $_REQUEST['toid'] );
						}
						break;
		}
		saxue_jumppage( 'chapter.php?aid=' . $aid, LANG_DO_SUCCESS );
}
$row = $article -> getvars( 'n' );
include SAXUE_ROOT_PATH . "/model/article_data.php";
$data_handler = saxuearticledatahandler :: getinstance( "saxuearticledatahandler" );
$data_handler -> setdatatable( $row['tbnum'] );
$data = $data_handler -> get( $aid );
$row = $row + ( $data -> getvars( 'n' ) );
$criteria = new criteriacompo( new criteria( "aid", $aid ) );
$criteria -> setsort( "chapterorder ASC, chapterid" );
$criteria -> setorder( "ASC" );
$chapter_handler -> queryobjects( $criteria );
$k = 0;
$rows = array( );
$_prefix = $saxueConfigs['article']['url_detail'] ? $row['url'] : '/article/';
while ( $chapter = $chapter_handler -> getobject() ) {
		$rows[$k] = $chapter -> getvars( 'n' );
		$rows[$k]['url'] = $_prefix . saxue_geturl( 'article_chapter', $rows[$k]['chapterid'], $rows[$k]['aid'] );
		++$k;
}
// 父分类定位
saxue_getconfigs( "cate", "article" );
$catepos = array();
$pid = $saxueCate['article'][$row['cid']]['pid'];
if ( $pid ) {
		$catepos[] = $saxueCate['article'][$pid];
}
$catepos[] = $saxueCate['article'][$row['cid']];
include SAXUE_ADMIN_PATH . "/header.php";
$saxueTpl -> assign_by_ref( "row", $row );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign_by_ref( "catepos", $catepos );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/chapter.html";
include SAXUE_ADMIN_PATH . "/footer.php";
