<?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( "操作错误" );
} 
$chapterid = intval( $_REQUEST['chapterid'] );
include SAXUE_ROOT_PATH . "/model/article_chapter.php";
$chapter_handler = saxuearticlechapterhandler :: getinstance( "saxuearticlechapterhandler" );
if ( $chapterid ) {
		$chapter = $chapter_handler -> get( $chapterid );
		if ( !is_object( $chapter ) || $aid != $chapter -> getvar( 'aid' ) ) {
				saxue_printfail( "章节不存在" );
		} 
		$isvolume = $chapter -> getvar( 'isvolume' );
		$_isnew = false;
} else {
		$isvolume = intval( $_REQUEST['isvolume'] );
		$_isnew = true;
}
include_once( SAXUE_ROOT_PATH . "/common/funpost.php" );
saxue_getconfigs( "configs", "article" );
if ( isset( $_POST['dosubmit'] ) ) {
		@set_time_limit( 0 );
		$_POST['chaptername'] = trim( $_POST['chaptername'] );
		if ( strlen( $_POST['chaptername'] ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '章节标题不能为空' ) ) );
		}
		$_POST['chaptercontent'] = trim( $_POST['chaptercontent'] );
		if ( !$isvolume && strlen( $_POST['chaptercontent'] ) == 0 ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '章节内容不能为空' ) ) );
		}
		if ( $_isnew ) {
				$_POST['posttime'] = SAXUE_NOW_TIME;
				$_POST['chapterorder'] = $chapter_handler -> getcount( new criteriacompo( new criteria( "aid", $aid ) ) ) + 1;
				$newchapter = $chapter_handler -> create();
		} else {
				$_POST['posttime'] = $chapter -> getvar( 'posttime' );
				$_POST['chapterorder'] = $chapter -> getvar( 'chapterorder' );
				$newchapter = $chapter_handler -> create( false );
		}
		if ( !$chapter_handler -> insert( $newchapter ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '章节保存失败' ) ) );
		} 
		$_POST['chapterid'] = $newchapter -> getvar( 'chapterid' );
		$timeline = intval( $_POST['timeline'] );
		if ( !$isvolume ){
				// 格式化内容
				if ( isset( $_POST['auto_format'] ) ) {
						$_POST['chaptercontent'] = saxue_setcontent( $_POST['chaptercontent'] ); 
				} else {
						$_POST['chaptercontent'] = '<!--Saxue Copy From-->' . str_replace( '<!--Saxue Copy From-->', '', $_POST['chaptercontent'] );
				}
				// 清除非本站链接
				if ( isset( $_POST['auto_link'] ) ) {
						$_POST['chaptercontent'] = clear_link( $_POST['chaptercontent'] );
				} 
				// 下载图片
				if ( isset( $_POST['auto_remote'] ) ) {
						$_POST['chaptercontent'] = save_remote( $_POST['chaptercontent'], $timeline );
				} 
		}
		include SAXUE_ROOT_PATH . "/common/packarticle.php";
		$packarticle = new saxuepackarticle( $aid );
		if ( $_isnew ) {
				$packarticle -> addchapter( $_POST );
		} else {
				$packarticle -> editchapter( $_POST );
		}
		if ( !empty( $timeline ) ) {
				include_once SAXUE_ROOT_PATH . "/model/attachs.php";
				$attachs_handler = saxueattachshandler :: getinstance( "saxueattachshandler" );
				$attachs_handler -> updateattachs( $aid, $timeline );
		}
		exit( json_encode( array( 'flag' => 1 ) ) );
} 
$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' ) );
if ( !$_isnew ) {
		$row = $row + ( $chapter -> getvars( 'n' ) );
		$txtfile = $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $aid ) . '/' . $aid . '/' . $chapterid . '.txt';
		if ( is_file( $txtfile ) ) {
				$row['chaptercontent'] = saxue_readfile( $txtfile );
				$row['chaptercontent'] = str_replace( array( "{saxue_innerad}\r\n", '{saxue_innerad}' ), '', $row['chaptercontent'] );
		}
}
// 父分类定位
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( "isvolume", $isvolume );
$saxueTpl -> assign_by_ref( "row", $row );
$saxueTpl -> assign_by_ref( "catepos", $catepos );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/addchapter.html";
include SAXUE_ADMIN_PATH . "/footer.php";
