<?php
if (!defined("SAXUE_CORE_INCLUDE")) {
	include "../../core.php";
} 
saxue_checkpower('tag');
include SAXUE_ROOT_PATH . "/model/tag.php";
$tag_handler = &saxuetaghandler :: getinstance("saxuetaghandler");
$action = trim($_REQUEST['action']);
if ($action == 'add') {
	if (isset($_REQUEST['tagid']) && is_numeric($_REQUEST['tagid'])) {
		$tag = $tag_handler -> get($_REQUEST['tagid']);
		if (!$tag) {
			saxue_printfail('TAG不存在');
		} 
		$row = $tag -> getvars('n');
	} 
	if (isset($_POST['dosubmit'])) {
		$_POST['tagname'] = strtolower(trim($_POST['tagname']));
		$_POST['cid'] = intval($_POST['cid']);
		if (mb_strlen($_POST['tagname'], 'utf-8')>10 || mb_strlen($_POST['tagname'], 'utf-8')<2) {
			exit(json_encode(array('flag' => 0, 'msg' => 'TAG长度在2-10个字符之间')));
		} elseif (false!==strpos($_POST['tagname'],',')) {
			exit(json_encode(array('flag' => 0, 'msg' => 'TAG格式错误')));
		} elseif ($_POST['tagname'] != $row['tagname'] && 0 < $tag_handler -> getcount(new criteria('tagname', $_POST['tagname']))) {
			exit(json_encode(array('flag' => 0, 'msg' => 'TAG已存在')));
		} 
		if (isset($_REQUEST['tagid']) && $_POST['tagname'] != $row['tagname']) {
			include_once( SAXUE_ROOT_PATH . "/common/funarticle.php" );
			include_once( SAXUE_ROOT_PATH . "/model/tag_article.php" );
			$tagarticle_handler = saxuetagarticlehandler :: getinstance( "saxuetagarticlehandler" );
			$tagarticle_handler -> queryobjects( new criteria( 'tagid', $_REQUEST['tagid'] ) );
			while ( $v = $tagarticle_handler -> getobject() ) {
					$ret = EditTagArticle( $v -> getvar( 'aid' ), $row['tagname'], $_POST['tagname'] );
					if ( !$ret ) {
							exit(json_encode(array('flag' => 0, 'msg' => '修改TAG文章失败')));
					}
			}
		} 
		if (isset($_REQUEST['tagid'])) {
			$newtag = $tag_handler -> create(false);
		} else {
			$newtag = $tag_handler -> create();
		} 
		if (!$tag_handler -> insert($newtag)) {
			exit(json_encode(array('flag' => 0, 'msg' => LANG_ERROR_DATABASE)));
		} 
		exit(json_encode(array('flag' => 1)));
	} else {
		saxue_getconfigs("cate", "article");
		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( $row['cid'] ) );
		include_once(SAXUE_ADMIN_PATH . "/header.php");
		$saxueTpl -> assign_by_ref("row", $row);
		$saxueTpl -> assign_by_ref("cates", $cates);
		$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/tag/templates/tag_add.html";
		include_once(SAXUE_ADMIN_PATH . "/footer.php");
	} 
	exit();
} else if ($action == 'delete') {
	$ids = explode( ',', $_REQUEST['ids'], 2 );
	$tag = $tag_handler -> get($ids[0]);
	if ($tag) {
		@set_time_limit( 0 );
		echo "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ";
		echo '正在删除标签：' . $tag -> getvar( 'tagname' ) . '<br>';
		ob_flush( );
		flush( );
		include_once( SAXUE_ROOT_PATH . "/common/funarticle.php" );
		include_once( SAXUE_ROOT_PATH . "/model/tag_article.php" );
		$tagarticle_handler = saxuetagarticlehandler :: getinstance( "saxuetagarticlehandler" );
		$tagarticle_handler -> queryobjects( new criteria( 'tagid', $ids[0] ) );
		while ( $v = $tagarticle_handler -> getobject() ) {
				$ret = DelTagArticle( $v -> getvar( 'aid' ), $ids[0], $tag -> getvar( 'tagname' ) );
				if ( $ret['flag'] ) {
						echo '《' . $ret['title'] . '》文章标签处理完成<br>';
						ob_flush( );
						flush( );
				}
		}
		$tag_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "tag_article" ) . " WHERE tagid=" . $ids[0] );
		$tag_handler -> delete( $ids[0] );
	} 
	if ( !empty( $ids[1] ) ) {
			saxue_jumppage( 'tagop.php?action=delete&ids=' . $ids[1] );
	}
	saxue_jumppage( 'tag.php', LANG_DO_SUCCESS );
} else if ($action == 'merge') {
	$tagid = intval( $_REQUEST['tagid'] );
	$_totag = $tag_handler -> get($tagid);
	if (!$_totag) {
			saxue_printfail( '目标TAG不存在' );
	}
	$tagname = $_totag -> getvar( 'tagname' );
	$ids = explode( ',', $_REQUEST['ids'], 2 );
	$tag = $tag_handler -> get($ids[0]);
	if ($tag && $tag -> getvar( 'tagname' ) != $tagname) {
		@set_time_limit( 0 );
		echo "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ";
		echo '正在合并标签：' . $tag -> getvar( 'tagname' ) . '<br>';
		ob_flush( );
		flush( );
		include_once( SAXUE_ROOT_PATH . "/common/funarticle.php" );
		include_once( SAXUE_ROOT_PATH . "/model/tag_article.php" );
		$tagarticle_handler = saxuetagarticlehandler :: getinstance( "saxuetagarticlehandler" );
		$tagarticle_handler -> queryobjects( new criteria( 'tagid', $ids[0] ) );
		$counts = 0;
		$uptag = $totag = array();
		$totag['tagid'] = $tagid;
		$totag['tagname'] = $tagname;
		while ( $v = $tagarticle_handler -> getobject() ) {
				$fromtag = array();
				$fromtag['tagid'] = $ids[0];
				$fromtag['tagname'] = $tag -> getvar( 'tagname' );
				$ret = MergeTagArticle( $v -> getvar( 'aid' ), $fromtag, $totag );
				if ( $ret['flag'] ) {
						if ( $ret['flag'] == 4 ) {
								++$counts;
								$uptag[] = $v -> getvar( 'aid' );
						}
						echo '《' . $ret['title'] . '》文章标签处理完成<br>';
						ob_flush( );
						flush( );
				}
		}
		if ( $counts > 0 ) {
				$tag_handler -> updatefields( 'counts=counts+' + $counts, 'tagid=' . $tagid );
				$tag_handler -> db -> query( "UPDATE " . saxue_dbprefix( "tag_article" ) . " SET tagid=" . $tagid . " WHERE tagid=" . $ids[0] . " AND aid IN(" . implode( ',', $uptag ) . ")" );
		}
		$tag_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "tag_article" ) . " WHERE tagid=" . $ids[0] );
		$tag_handler -> delete( $ids[0] );
	} 
	if ( !empty( $ids[1] ) ) {
			saxue_jumppage( 'tagop.php?action=merge&tagid=' . $tagid . '&ids=' . $ids[1] );
	}
	saxue_jumppage( 'tag.php', LANG_DO_SUCCESS );
} else if ($action == 'clean') {
	$tag_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "tag" ) . " WHERE counts=0" );
	saxue_jumppage( 'tag.php', LANG_DO_SUCCESS );
} 
