<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'artcate' );
if ( !isset( $_REQUEST['pid'] ) || !is_numeric( $_REQUEST['pid'] ) ) {
		$_REQUEST['pid'] = 0;
} 
include SAXUE_ROOT_PATH . "/common/cache.php";
include SAXUE_ROOT_PATH . "/model/article_cate.php";
$cate_handler = saxuearticlecatehandler :: getinstance( "saxuearticlecatehandler" );
saxue_getconfigs( "cate", "article" );
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' );
						} elseif ( !empty( $_REQUEST['pid'] ) && !isset( $saxueCate['article'][$_REQUEST['pid']] ) ) {
								saxue_printfail( '父分类不存在' );
						}
						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' => '英文别名格式错误' ) ) );
								} 
								if ( $_POST['alias'] != $row['alias'] ) {
										$criteria = new criteriacompo( new criteria( 'pid', intval( $_REQUEST['pid'] ) ) );
										$criteria -> add( new criteria( 'alias', $_POST['alias'] ) );
										if ( 0 < $cate_handler -> getcount( $criteria ) ) {
												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_article_cate();
								exit( json_encode( array( 'flag' => 1 ) ) );
						}
						$temps = array();
						$temp_path = SAXUE_THEME_PATH . '/article';
						$temp_list = glob( $temp_path . '/*.html' );
						if ( $temp_list ) {
								foreach( $temp_list as $file ) {
										$filename = basename( $file );
										if ( preg_match( "/^(channel|list|detail|catalog|chapter)_([a-z0-9_]+)\.html$/i", $filename, $matchs ) ) {
												$temps[$matchs[1]][] = $filename;
										}
								}
						}
						include_once( SAXUE_ADMIN_PATH . "/header.php" );
						if ( !empty( $_REQUEST['pid'] ) ) {
								$saxueTpl -> assign( "pid", $_REQUEST['pid'] );
								$saxueTpl -> assign( "pname", $saxueCate['article'][$_REQUEST['pid']]['cname'] );
								$row['pnum'] = $saxueCate['article'][$_REQUEST['pid']]['pnum'];
						} else {
								$saxueTpl -> assign( "pid", $row['pid'] );
								$saxueTpl -> assign( "pname", $saxueCate['article'][$row['pid']]['cname'] );
						}
						$saxueTpl -> assign_by_ref( "row", $row );
						$saxueTpl -> assign_by_ref( "temps", $temps );
						$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/cate_add.html";
						include_once( SAXUE_ADMIN_PATH . "/footer.php" );
						exit;
				case "batch" :
						if ( isset( $_POST['dosubmit'] ) ) {
								if ( !isset( $_POST['catids'] ) || !is_array( $_POST['catids'] ) || count( $_POST['catids'] ) < 1 ) {
										saxue_printfail( '请选择要修改的分类' );
								}
								$up = array();
								$_POST['pnum'] = intval( $_POST['pnum'] );
								if ( $_POST['pnum'] ) {
										$up['pnum'] = $_POST['pnum'];
								}
								if ( $_POST['templist'] != -1 ) {
										$up['templist'] = $_POST['templist'];
								}
								if ( $_POST['tempdetail'] != -1 ) {
										$up['tempdetail'] = $_POST['tempdetail'];
								}
								if ( $_POST['tempcatalog'] != -1 ) {
										$up['tempcatalog'] = $_POST['tempcatalog'];
								}
								if ( $_POST['tempchapter'] != -1 ) {
										$up['tempchapter'] = $_POST['tempchapter'];
								}
								if ( $_POST['showchannel'] != -1 ) {
										$up['showchannel'] = intval( $_POST['showchannel'] );
								}
								if ( count( $up ) < 1 ) {
										saxue_printfail( '请填写修改项' );
								}
								$cate_handler -> updatefields( $up, 'cid IN(' . implode( ',', $_POST['catids'] ) . ')' );
								cache_article_cate();
						} else {
								$temps = array();
								$temp_path = SAXUE_THEME_PATH . '/article';
								$temp_list = glob( $temp_path . '/*.html' );
								if ( $temp_list ) {
										foreach( $temp_list as $file ) {
												$filename = basename( $file );
												if ( preg_match( "/^(channel|list|detail|catalog|chapter)_([a-z0-9_]+)\.html$/i", $filename, $matchs ) ) {
														$temps[$matchs[1]][] = $filename;
												}
										}
								}
								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 );
								$saxueTpl -> assign_by_ref( "temps", $temps );
								$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/cate_batch.html";
								include SAXUE_ADMIN_PATH . "/footer.php";
								exit;
						} 
						break;
				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( '分类不存在' );
						} 
						if ( $saxueCate['article'][$_REQUEST['cid']]['child'] ) {
								saxue_printfail( '请先删除子分类' );
						}
						include_once( SAXUE_ROOT_PATH . "/model/article.php" );
						$article_handler = saxuearticlehandler :: getinstance( "saxuearticlehandler" );
						if ( 0 == $cate -> getvar( 'pid' ) ) {
								if ( 0 < $article_handler -> getcount( new criteria( 'pid', $_REQUEST['cid'] ) ) ) {
										saxue_printfail( '请先删除或转移分类下的文章' );
								} 
						} elseif ( 0 < $article_handler -> getcount( new criteria( 'cid', $_REQUEST['cid'] ) ) ) {
								saxue_printfail( '请先删除或转移分类下的文章' );
						}
						$cate_handler -> delete( $_REQUEST['cid'] );
						cache_article_cate();
						break;
				case "cache" :
						cache_article_cate();
						break;
				case "spider" :
						ksort( $saxueCate['article'] );
						$spider = '';
						foreach ( $saxueCate['article'] as $cid => $v ) {
								if ( !$v['child'] ) {
										$spider .= $cid . '|' . $v['cname'] . '=,' . $v['cname'] . ",\n";
								}
						}
						include SAXUE_ADMIN_PATH . "/header.php";
						$saxueTpl -> assign( "spider", $spider );
						$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/cate_spider.html";
						include SAXUE_ADMIN_PATH . "/footer.php";
						exit;
				case "expand" :
						if ( !isset( $_REQUEST['cid'] ) || !is_numeric( $_REQUEST['cid'] ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						include SAXUE_ROOT_PATH . "/model/article_expand.php";
						$expand_handler = &saxuearticleexpandhandler :: getinstance( "saxuearticleexpandhandler" );
						$sql = "SELECT * FROM " . saxue_dbprefix( 'article_expand' ) . " WHERE cids LIKE '%," . $_REQUEST['cid'] . ",%' ORDER BY listorder ASC";
						$res = $expand_handler -> db -> query( $sql );
						$rows = array();
						$k = 0;
						while ( $v = $expand_handler -> getobject( $res ) ) {
								$rows[$k] = $v -> getvars( "n" );
								++$k;
						} 
						include SAXUE_ADMIN_PATH . "/header.php";
						$saxueTpl -> assign_by_ref( "types", $expand_handler -> type() );
						$saxueTpl -> assign_by_ref( "rows", $rows );
						$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/cate_expand.html";
						include SAXUE_ADMIN_PATH . "/footer.php";
						exit;
		} 
		saxue_jumppage( 'cate.php', LANG_DO_SUCCESS );
} 
include_once( SAXUE_ROOT_PATH . "/common/funadmin.php" );
$cate_handler -> queryobjects();
$rows = array();
while ( $v = $cate_handler -> getobject() ) {
		$k = $v -> getvar( "cid" );
		$rows[$k]['cid'] = $v -> getvar( "cid" );
		$rows[$k]['pid'] = $v -> getvar( "pid" );
		$rows[$k]['cname'] = $v -> getvar( "cname" );
		$rows[$k]['curl'] = $saxueCate['article'][$rows[$k]['cid']]['curl'];
		$rows[$k]['alias'] = $v -> getvar( "alias" );
		$rows[$k]['pnum'] = $v -> getvar( "pnum" );
		$rows[$k]['templist'] = $v -> getvar( "templist" );
		$rows[$k]['tempdetail'] = $v -> getvar( "tempdetail" );
		$rows[$k]['tempcatalog'] = $v -> getvar( "tempcatalog" );
		$rows[$k]['tempchapter'] = $v -> getvar( "tempchapter" );
		$showchannel = $v -> getvar( "showchannel" );
		if ( $showchannel && $saxueCate['article'][$k]['child'] ) {
				$rows[$k]['showchannel'] = '是';
		} else {
				$rows[$k]['showchannel'] = '否';
		}
		if ( $rows[$k]['pid'] == 0 ) {
				$rows[$k]['str_manage'] = '<a href="javascript:doDialog(\'' . SAXUE_URL . '/article/admin/cate.php?action=add&pid=' . $k . '\',\'添加子分类——' . $rows[$k]['cname'] . '\',\'550\',\'450\')">' . saxue_geticon( 'add', '添加子分类' ) . '</a>';
		} else {
				$rows[$k]['str_manage'] = saxue_geticon( 'add', '添加子分类', 0 );
		}
		$rows[$k]['str_manage'] .= '&nbsp;&nbsp;&nbsp;<a href="javascript:doDialog(\'' . SAXUE_URL . '/article/admin/cate.php?action=add&cid=' . $k . '\',\'修改分类——' . $rows[$k]['cname'] . '\',\'550\',\'450\')">' . saxue_geticon( 'edit', '修改分类' ) . '</a>&nbsp;&nbsp;&nbsp;<a href="?action=delete&cid=' . $k . '" onclick="return confirm(\'你确定要删除分类『' . $rows[$k]['cname'] . '』吗？\')">' . saxue_geticon( 'del', '删除分类' ) . '</a>&nbsp;&nbsp;&nbsp;<a href="javascript:doDialog(\'' . SAXUE_URL . '/article/admin/cate.php?action=expand&cid=' . $k . '\',\'扩展属性——' . $rows[$k]['cname'] . '\',\'550\',\'350\')">' . saxue_geticon( 'set', '扩展属性' ) . '</a>';
} 
include SAXUE_ROOT_PATH . "/lib/util/tree.php";
$tree = new tree;
$tree -> icon = array( '&nbsp;&nbsp;&nbsp;<span class="f_tree">│</span>&nbsp;', '&nbsp;&nbsp;&nbsp;<span class="f_tree">├─</span> ', '&nbsp;&nbsp;&nbsp;<span class="f_tree">└─</span> ' );
$tree -> nbsp = '&nbsp;&nbsp;&nbsp;';
$tree -> mid = 'cid';
$tree -> pid = 'pid';
$str = "<tr class='rowbg'>
			<td>\$cid</td>
			<td>\$spacer<a href='\$curl' target='_blank'>\$cname</a></td>
			<td>\$alias</td>
			<td>\$pnum</td>
			<td>\$showchannel</td>
			<td>\$templist&nbsp;&nbsp;\$tempdetail&nbsp;&nbsp;\$tempcatalog&nbsp;&nbsp;\$tempchapter</td>
			<td>\$str_manage</td>
		</tr>";
$tree -> init( $rows );
$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/cate.html";
include SAXUE_ADMIN_PATH . "/footer.php";
