<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'topiccate' );
include_once( SAXUE_ROOT_PATH . "/common/cache.php" );
include_once( SAXUE_ROOT_PATH . "/model/topic_cate.php" );
$cate_handler = saxuetopiccatehandler :: getinstance( "saxuetopiccatehandler" );
saxue_getconfigs( "cate", "topic" );
if ( $_REQUEST['action'] != "" ) {
		switch ( $_REQUEST['action'] ) {
				case "add" :
						if ( isset( $_REQUEST['cid'] ) && is_numeric( $_REQUEST['cid'] ) ) {
								$cate = $cate_handler -> get( $_REQUEST['cid'] );
								if ( !$cate ) {
										saxue_printfail( '分类不存在' );
								} 
								$row = $cate -> getvars( 'n' );
						}
						if ( isset( $_POST['dosubmit'] ) ) {
								$_POST['cname'] = trim( $_POST['cname'] );
								$_POST['alias'] = strtolower( trim( $_POST['alias'] ) );
								if ( strlen( $_POST['cname'] ) == 0 ) {
										exit( json_encode( array( 'flag' => 0, 'msg' => '分类名称不能为空' ) ) );
								} 
								if ( strlen( $_POST['alias'] ) == 0 ) {
										exit( json_encode( array( 'flag' => 0, 'msg' => '英文别名不能为空' ) ) );
								} elseif ( !preg_match( "/^[a-z][a-z0-9_-]+$/i", $_POST['alias'] ) ) {
										exit( json_encode( array( 'flag' => 0, 'msg' => '英文别名格式错误' ) ) );
								} elseif ( $_POST['alias'] != $row['alias'] && 0 < $cate_handler -> getcount( new criteria( 'alias', $_POST['alias'] ) ) ) {
										exit( json_encode( array( 'flag' => 0, 'msg' => '该英文别名已存在' ) ) );
								} 
								if ( isset( $_REQUEST['cid'] ) ) {
										$newcate = $cate_handler -> create( false );
								} else {
										$newcate = $cate_handler -> create();
								} 
								if ( !$cate_handler -> insert( $newcate ) ) {
										exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
								} 
								cache_topic_cate();
								exit( json_encode( array( 'flag' => 1 ) ) );
						} else {
								include_once( SAXUE_ADMIN_PATH . "/header.php" );
								$saxueTpl -> assign_by_ref( "row", $row );
								$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/topic/templates/cate_add.html";
								include_once( SAXUE_ADMIN_PATH . "/footer.php" );
						} 
						exit();
				case "delete" :
						if ( !isset( $_REQUEST['cid'] ) || !is_numeric( $_REQUEST['cid'] ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						$cate = $cate_handler -> get( $_REQUEST['cid'] );
						if ( !$cate ) {
								saxue_printfail( '分类不存在' );
						} 
						include_once( SAXUE_ROOT_PATH . "/model/topic.php" );
						$topic_handler = saxuetopichandler :: getinstance( "saxuetopichandler" );
						if ( 0 < $topic_handler -> getcount( new criteria( 'cid', $_REQUEST['cid'] ) ) ) {
								saxue_printfail( '请先删除分类下的TAG' );
						}
						$cate_handler -> delete( $_REQUEST['cid'] );
						cache_topic_cate();
						break;
				case "cache" :
						cache_topic_cate();
						break;
		} 
} 
$cate_handler -> queryobjects();
$rows = array();
while ( $v = $cate_handler -> getobject() ) {
		$k = $v -> getvar( "cid" );
		$rows[$k] = $v -> getvars( "n" );
} 
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/topic/templates/cate.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
