<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'topicadd' );
include( SAXUE_ROOT_PATH . "/model/topic.php" );
$topic_handler = saxuetopichandler :: getinstance( "saxuetopichandler" );
include SAXUE_ROOT_PATH . "/model/topic_data.php";
$topicdata_handler = saxuetopicdatahandler :: getinstance( "saxuetopicdatahandler" );
$row = array();
$_isnew = true;
$topicid = intval( $_REQUEST['topicid'] );
if ( $topicid ) {
		$_topic = $topic_handler -> get( $topicid );
		if ( !is_object( $_topic ) ) {
				saxue_printfail( "专题不存在" );
		} 
		$row = $_topic -> getvars( 'n' );
		$data = $topicdata_handler -> get( $topicid );
		$row = $row + ( $data -> getvars( 'n' ) );
		$_isnew = false;
}
if ( isset( $_POST['dosubmit'] ) ) {
		$_POST['status'] = intval( $_POST['status'] );
		$_POST['views'] = intval( $_POST['views'] );
		$_POST['cid'] = intval( $_POST['cid'] );
		$_POST['pnum'] = intval( $_POST['pnum'] );
		$_POST['title'] = trim( $_POST['title'] );
		$_POST['alias'] = trim( $_POST['alias'] );
		$_POST['temp'] = trim( $_POST['temp'] );
		$_POST['intro'] = trim( $_POST['intro'] );
		$_POST['thumb'] = trim( $_POST['thumb'] );
		if ( !empty( $_POST['thumb'] ) ) {
				$_POST['isthumb'] = 1;
		} else {
				$_POST['isthumb'] = 0;
		}
		if ( empty( $_POST['title'] ) || empty( $_POST['cid'] ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => '专题名称，分类不能为空' ) ) );
		}
		if ( !empty( $_POST['alias'] ) ) {
				if ( !preg_match( "/^[a-z][a-z0-9_-]+$/i", $_POST['alias'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '英文名称格式错误' ) ) );
				} elseif ( $_POST['alias'] != $row['alias'] && 0 < $topicdata_handler -> getcount( new criteria( 'alias', $_POST['alias'] ) ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '英文名称已存在' ) ) );
				} 
		}
		if ( $_isnew ) {
				$newtopic = $topic_handler -> create();
				$newtopic -> setvar( "addtime", SAXUE_NOW_TIME );
		} else {
				$newtopic = $topic_handler -> create( false );
		} 
		if ( !$topic_handler -> insert( $newtopic ) ) {
				exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
		} 
		if ( $_isnew ) {
				$_POST['topicid'] = $newtopic -> getvar( 'topicid' );
				$newdata = $topicdata_handler -> create();
		} else {
				$_POST['url'] = saxue_geturl( 'topic_show', $_POST['topicid'], $_POST['cid'], $_POST['alias'] );
				$newdata = $topicdata_handler -> create( false );
		} 
		if ( !$topicdata_handler -> insert( $newdata ) ) {
				if ( $_isnew ) {
						$topic_handler -> delete( $_POST['topicid'] );
				}
				exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
		} 
		$topic = ( $newtopic -> getvars( 'n' ) ) + ( $newdata -> getvars( 'n' ) );
		// URL处理
		if ( $_isnew ) {
				$topic['url'] = $_POST['url'] = saxue_geturl( 'topic_show', $_POST['topicid'], $_POST['cid'], $_POST['alias'] );
				$topicdata_handler -> updatefields( array( 'url' => $_POST['url'] ), 'topicid=' . $_POST['topicid'] );
		} 
		// 生成HTML
		if ( $_POST['status'] == 1 && $saxueConfigs['topic']['url_detail'] > 2 ) {
				if ( $_isnew ) {
						$packtopic = new saxuepacktopic( $topic );
				} else {
						$packtopic = new saxuepacktopic( $topic['topicid'] );
				}
				$packtopic -> maketopic();
		}
		exit( json_encode( array( 'flag' => 1 ) ) );
}
saxue_getconfigs( "cate", "topic" );
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "cates", $saxueCate['topic'] );
$saxueTpl -> assign_by_ref( "row", $row );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/topic/templates/addtopic.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
