<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'tag' );
include SAXUE_ROOT_PATH . "/model/tag.php";
$tag_handler = &saxuetaghandler :: getinstance( "saxuetaghandler" );
saxue_getconfigs( "cate", "article" );
if ( !empty( $_REQUEST['action'] ) ) {
		if ( empty( $_REQUEST['ids'] ) || !is_array( $_REQUEST['ids'] ) || count( $_REQUEST['ids'] ) == 0 ) {
				saxue_printfail( '请选择文章' );
		}
		switch ( $_REQUEST['action'] ) {
				case 'autocate':
						saxue_jumppage( 'cate.php?action=ids&ids=' . implode( ',', $_REQUEST['ids'] ) );
						break;
				case 'setgood':
						$tag_handler -> updatefields( array( 'isgood' => 1 ), 'tagid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'cacelgood':
						$tag_handler -> updatefields( array( 'isgood' => 0 ), 'tagid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'movecate':
						$tocid = intval( $_REQUEST['tocid'] );
						if ( empty( $tocid ) || isset( $saxueCate['article'][$tocid] ) ) {
								$tag_handler -> updatefields( array( 'cid' => $tocid ), 'tagid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						}
						break;
				case 'delete':
						saxue_jumppage( 'tagop.php?action=delete&ids=' . implode( ',', $_REQUEST['ids'] ) );
						break;
				case 'merge':
						$tagname = trim( $_REQUEST['totag'] );
						if ( empty( $tagname ) ) {
								saxue_printfail( '目标TAG不存在' );
						}
						$_res = $tag_handler -> db -> query( "SELECT tagid FROM " . saxue_dbprefix( "tag" ) . " WHERE tagname='" . $tagname . "' LIMIT 1" );
						$_obj = $tag_handler -> getobject( $_res ); 
						if ( !is_object( $_obj ) ) {
								saxue_printfail( '目标TAG不存在' );
						}
						saxue_jumppage( 'tagop.php?action=merge&tagid=' . ( $_obj -> getvar( 'tagid' ) ) . '&ids=' . implode( ',', $_REQUEST['ids'] ) );
						break;
				case 'counts':
						$_tagcount = array();
						$_sql = "SELECT tagid,COUNT(*) as counts FROM " . saxue_dbprefix( "tag_article" ) . " WHERE tagid IN(" . implode( ',', $_REQUEST['ids'] ) . ") GROUP BY tagid";
						$_res = $tag_handler -> db -> query( $_sql );
						while ( $v = $tag_handler -> getobject( $_res ) ) {
								$_tagcount[$v -> getvar( 'tagid' )] = $v -> getvar( 'counts' );
						}
						foreach ( $_REQUEST['ids'] as $tagid ) {
								$counts = intval( $_tagcount[$tagid] );
								$tag_handler -> updatefields( array( 'counts' => $counts ), 'tagid=' . $tagid );
						}
						break;
		}
		if ( empty( $_REQUEST['jumpurl'] ) ) {
				saxue_jumppage( 'tag.php', LANG_DO_SUCCESS );
		} else {
				saxue_jumppage( $_REQUEST['jumpurl'], LANG_DO_SUCCESS );
		}
}
if ( empty( $_REQUEST['pagesize'] ) || !is_numeric( $_REQUEST['pagesize'] ) || !in_array( $_REQUEST['pagesize'], array( '20', '30', '50', '100' ) ) ) $_REQUEST['pagesize'] = 30;
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
$criteria = new criteriacompo();
if ( !empty( $_REQUEST['cid'] ) ) {
		if ( $saxueCate['article'][$_REQUEST['cid']]['child'] ) {
				$criteria -> add( new criteria( 'cid', '(' . $saxueCate['article'][$_REQUEST['cid']]['arrchild'] . ')', 'IN' ) );
		} else {
				$criteria -> add( new criteria( 'cid', $_REQUEST['cid'] ) );
		} 
} elseif ( isset( $_REQUEST['cid'] ) && $_REQUEST['cid'] == '0' ) {
		$criteria -> add( new criteria( 'cid', 0 ) );
}
if ( !empty( $_REQUEST['isgood'] ) ) {
		$criteria -> add( new criteria( 'isgood', 1 ) );
} 
if ( !empty( $_REQUEST['tagname'] ) ) {
		$criteria -> add( new criteria( 'tagname', $_REQUEST['tagname'], 'REGEXP' ) );
} 
if ( isset( $_REQUEST['form_sort'] ) && !empty( $_REQUEST['form_sort'] ) ) {
		$criteria -> setsort( $_REQUEST['form_sort'] );
} else {
		$criteria -> setsort( "tagid" );
} 
if ( isset( $_REQUEST['form_order'] ) && !empty( $_REQUEST['form_order'] ) ) {
		$criteria -> setorder( $_REQUEST['form_order'] );
} else {
		$criteria -> setorder( "DESC" );
} 
$criteria -> setlimit( $_REQUEST['pagesize'] );
$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] );
$tag_handler -> queryobjects( $criteria );
$rows = array();
$k = 0;
while ( $v = $tag_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( 'n' );
		$rows[$k]['url'] = saxue_geturl( "tag_show", $rows[$k]['tagid'] ); 
		if ( !$rows[$k]['cid'] ) $rows[$k]['cname'] = '-';
		else $rows[$k]['cname'] = $saxueCate['article'][$rows[$k]['cid']]['cname'];
		++$k;
} 
include SAXUE_ROOT_PATH . "/lib/util/tree.php";
$tree = new tree;
$tree -> icon = array( '&nbsp;<span class="f_tree">│</span>&nbsp;', '&nbsp;<span class="f_tree">├─</span> ', '&nbsp;<span class="f_tree">└─</span> ' );
$tree -> nbsp = '&nbsp;';
$tree -> mid = 'cid';
$tree -> pid = 'pid';
$tree -> init( $saxueCate['article'] );
$str = "<option value=\$cid \$selected>\$spacer\$cname</option>";
$cates = $tree -> get_tree( 0, $str, intval( $_REQUEST['cid'] ) );
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign_by_ref("cates", $cates);
include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
$jumppage = new saxuepage( $tag_handler -> getcount( $criteria ), $_REQUEST['pagesize'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/tag/templates/tag.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );