<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'tagcate' );
if ( !empty( $_REQUEST['action'] ) ) {
		include SAXUE_ROOT_PATH . "/model/tag.php";
		$tag_handler = &saxuetaghandler :: getinstance( "saxuetaghandler" );
		$criteria = new criteriacompo();
		switch ( $_REQUEST['action'] ) {
				case 'ids':
						if ( empty( $_REQUEST['ids'] ) ) {
								saxue_printfail( "请输入TAGID列表！" );
						}
						$criteria -> add( new criteria( 'tagid', '(' . $_REQUEST['ids'] . ')', 'IN' ) );
						if ( !isset( $_REQUEST['recate'] ) ) {
								$criteria -> add( new criteria( 'cid', 0 ) );
						}
				case 'range':
						$fromid = intval( $_REQUEST['fromid'] );
						$toid = intval( $_REQUEST['toid'] );
						$pagesize = intval( $_REQUEST['pagesize'] );
						if ( $fromid > $toid ) {
								saxue_printfail( "TAG开始ID不能大于结束ID！" );
						}
						if ( !empty( $fromid ) ) {
								$criteria -> add( new criteria( 'tagid', $fromid, '>=' ) );
						}
						if ( !empty( $toid ) ) {
								$criteria -> add( new criteria( 'tagid', $toid, '<=' ) );
						}
						if ( !isset( $_REQUEST['recate'] ) ) {
								$criteria -> add( new criteria( 'cid', 0 ) );
						}
						if ( !empty( $pagesize ) ) {
								$criteria -> setlimit( $pagesize );
						}
		}
		$tag_handler -> queryobjects( $criteria );
		$tagids = array();
		while ( $v = $tag_handler -> getobject() ) {
				$tagids[] = $v -> getvar( 'tagid' );
		} 
		$count = count( $tagids );
		if ( $count > 0 ) {
				$sql = 'SELECT aid, tagid FROM ' . saxue_dbprefix( 'tag_article' ) . ' WHERE tagid IN(' . implode( ',', $tagids ) . ') GROUP BY tagid';
				$res = $tag_handler -> db -> query( $sql );
				$aids = array();
				$tagarticle = array();
				while ( $v = $tag_handler -> getobject( $res ) ) {
						$aid = $v -> getvar( 'aid' );
						$aids[] = $aid;
						$tagarticle[$aid][] = $v -> getvar( 'tagid' );
				} 
				$aids = array_unique( $aids );
				if ( count( $aids ) > 0 ) {
						$sql = 'SELECT aid, cid FROM ' . saxue_dbprefix( 'article' ) . ' WHERE aid IN(' . implode( ',', $aids ) . ')';
						$res = $tag_handler -> db -> query( $sql );
						$tags = array();
						while ( $v = $tag_handler -> getobject( $res ) ) {
								$aid = $v -> getvar( 'aid' );
								$cid = $v -> getvar( 'cid' );
								if ( isset( $tags[$cid] ) ) {
										$tags[$cid] = array_merge( $tags[$cid], $tagarticle[$aid] );
								} else {
										$tags[$cid] = $tagarticle[$aid];
								}
						} 
						if ( count( $tags ) > 0 ) {
								foreach ( $tags as $cid => $tagids ) {
										$tag_handler -> updatefields( 'cid=' . $cid, 'tagid IN(' . implode( ',', $tagids ) . ')' );
								}
						}
				}
		}
		if ( $_REQUEST['action'] == 'range' && $count == $pagesize ) {
				$page = intval( $_REQUEST['page'] );
				if ( empty( $page ) ) {
						$page = 2;
				} else {
						++$page;
				}
				$url = 'cate.php?action=range&pagesize=' . $pagesize . '&fromid=' . $fromid . '&toid=' . $toid . '&page=' . $page;
				if ( isset( $_REQUEST['recate'] ) ) {
						$url .= '&recate=1';
				}
				saxue_jumppage( $url, '更新成功，正在跳转到第 '. $page .' 页' );
		}
		saxue_jumppage( 'tag.php', '自动分类完成' );
}
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/tag/templates/cate.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
