<?php
/**
 * @package iCMS
 * @copyright 2007-2010, iDreamSoft
 * @license http://www.idreamsoft.cn iDreamSoft
 * @author coolmoo <idreamsoft@qq.com>
 */
!defined('iPATH') && exit('What are you doing?');
switch ($operation) {
	case 'add':
		$id=(int)$_GET['id'];
		$rs	= $iCMS->db->getRow("SELECT * FROM icms_tags WHERE id='$id'");
		include iCMS_admincp_tpl('tag.add');
	break;
	case 'list':
		$id	= (int)$_GET['id'];
		$cid= (int)$_GET['cid'];
		$tagName	= $iCMS->db->getValue("SELECT name FROM icms_tags WHERE id='$id'");
		include iPATH.'include/catalog.class.php';
		$catalog =new catalog();
		$sql='#iCMS@__article.id = `indexId`';
		$_GET['keywords'] && $sql.=" AND `title` REGEXP '{$_GET['keywords']}'";
		isset($_GET['nopic'])&&$sql.=" AND `isPic` ='0'";
		$_GET['starttime'] 	&& $sql.=" and `pubdate`>='".strtotime($_GET['starttime'])."'";
		$_GET['endtime'] 	&& $sql.=" and `pubdate`<='".strtotime($_GET['endtime'])."'";
		(isset($_GET['at']) && $_GET['at']!='-1') && $sql.=" AND `type` ='".$_GET['at']."'";
		isset($_GET['keyword']) && $uri.='&keyword='.$_GET['keyword'];
		$cid=$Admin->CP($cid)?$cid:"0";
		if($cid){
			if(isset($_GET['sub'])){
				$sql.=" AND ( cid IN(".$catalog->id($cid).$cid.")";
			}else{
				$sql.=" AND ( cid ='$cid'";
			}
			$sql.=" OR `vlink` REGEXP '[[:<:]]".preg_quote($cid, '/')."[[:>:]]')";
		}else{
			$Admin->cpower && $sql.=" AND cid IN(".implode(',',$Admin->cpower).")";
		}
		$maxperpage =(int)$_GET['perpage']>0?$_GET['perpage']:20;
		$total=$iCMS->db->getValue("SELECT count(#iCMS@__article.id) FROM `#iCMS@__article`,`#iCMS@__taglist` WHERE `tid`='".$id."' AND {$sql} ORDER BY #iCMS@__taglist.indexId DESC");
		page($total,$maxperpage,"篇文章");
		$rs=$iCMS->db->getArray("SELECT #iCMS@__article.* FROM `#iCMS@__article`,`#iCMS@__taglist` WHERE `tid`='".$id."' AND {$sql} ORDER BY #iCMS@__taglist.indexId DESC LIMIT {$firstcount} , {$maxperpage}");
		$_count=count($rs);
		include iCMS_admincp_tpl('tag.list');
	break;
	case 'del':
		$tId=(int)$_GET['id'];
		delArticleTag($tId);
		delTagCache($tId);
		_Header();
	break;
	case 'delist':
		$tId=(int)$_GET['id'];
		$aId=(int)$_GET['aid'];
		delArticleTag($tId,$aId);
		_Header();
	break;
	case 'updateHTML':
		$id=(int)$_GET['id'];
		$name	=isset($_GET['name'])?rawurldecode($_GET['name']):'';
		$cpage	=isset($_GET['cpage'])?$_GET['cpage']:1;
		$loop	=isset($_GET['loop'])?$_GET['loop']:0;
		if($cpage==1){
			$rs=$iCMS->db->getRow("SELECT `name`,`status` FROM `#iCMS@__tags` WHERE `id` ='$id' and `status`='1'");
			empty($rs) && alert("禁用的标签,不能生成静态.");
			tags_cache($id);
			$name=$rs->name;
		}
		$c = MakeTagHtm($name,$cpage,$loop,0);
		if($c['loop']>0 && $c['page']<=$c['pagesize']){
			redirect($c['name']."共".$c['pagesize']."页，已生成".$c['page']."页",__SELF__.'?do=tag&operation=updateHTML&name='.rawurlencode($name).'&cpage='.$c['page'].'&loop='.($c['loop']-1),0);
		}else{
			_Header(__SELF__.'?do=tag&operation=manage');
		}
	break;
	case 'disabled':
		$id=(int)$_GET['id'];
		$id&&$iCMS->db->query("UPDATE `#iCMS@__tags` SET `status` = '0'  WHERE `id` ='$id'");
		tags_cache($id);
		_Header();
	break;
	case 'open':
		$id=(int)$_GET['id'];
		$id&&$iCMS->db->query("UPDATE `#iCMS@__tags` SET `status` = '1'  WHERE `id` ='$id'");
		tags_cache($id);
		_Header();
	break;
	case 'updateCache':
		$id=(int)$_GET['id'];
		$name=$_GET['name'];
		$_count=$iCMS->db->getValue("SELECT count(id) FROM `#iCMS@__taglist` WHERE `tid`='$id'");
		$link=pinyin($name,$iCMS->config['CLsplit']);
		$iCMS->db->query("UPDATE `#iCMS@__tags` SET `count` = '$_count',`link` = '$link'  WHERE `id` ='$id'");
		tags_cache($id);
		_Header();
	break;
	case 'post':
		if($action=='del'){
			if($_POST['id'])foreach($_POST['id'] as $tId){
				delArticleTag($tId);
				delTagCache($tId);
			}
		}elseif($action=='save'){
			$id	= (int)$_POST['id'];
			$name	= dhtmlspecialchars($_POST['name']);
			$sortid	= (int)$_POST['sortid'];
			$link	= dhtmlspecialchars($_POST['link']);
			$count	= (int)$_POST['count'];
			$ordernum	= _int($_POST['ordernum']);
			$tpl	= $_POST['tpl'];
			$status	= (int)$_POST['status'];
			empty($link) && $link=pinyin($name,$iCMS->config['CLsplit']);
			if(empty($id)){
				$iCMS->db->query("INSERT INTO `#iCMS@__tags`(`sortid`, `uid`, `name`, `link`, `count`, `ordernum`, `tpl`,`status`) VALUES ('$sortid', '".$Admin->uId."', '$name', '$link', '$count', '$ordernum','$tpl', '$status');");
			}else{
				$iCMS->db->query("UPDATE `#iCMS@__tags` SET `sortid` = '$sortid', `name` = '$name', `link` = '$link', `count` = '$count', `ordernum` = '$ordernum',`tpl` = '$tpl', `status` = '$status' WHERE `id` = '$id'");
			}
			tags_cache($id);
			_Header(__SELF__.'?do=tag&operation=manage');
		}elseif($action=='sortedit'){
			if($_POST['newsortname']){
				$tSort	= $iCMS->cache('tag.sort','include/syscache',0,true);
				$endArray=end($tSort);
				$nSort	= array(
					'id'=>empty($endArray)?1:$endArray['id']+1,
					'name'=>$_POST['newsortname']
				);
				empty($tSort) && $tSort=array();
				$tSort[]=$nSort;
				$iCMS->cache(false,'include/syscache',0,true);
				$iCMS->addcache('tag.sort',$tSort,0);
			}else{
				$tSort=array();
				if($_POST['name'])foreach($_POST['name'] as $id=>$value){
					$tSort[$id]= array('id'=>$id,'name'=>$value);
				}
				$iCMS->cache(false,'include/syscache',0,true);
				$iCMS->addcache('tag.sort',$tSort,0);
			}
			if(isset($_POST['delete'])){
				$tSort	= $iCMS->cache('tag.sort','include/syscache',0,true);
				foreach($_POST['delete'] as $k=>$id){
					unset($tSort[$id]);
				}
				$iCMS->cache(false,'include/syscache',0,true);
				$iCMS->addcache('tag.sort',$tSort,0);
			}
		}elseif($action=='html'){
//			if($_POST['id'])foreach($_POST['id'] as $k=>$id){
//				$id && $rs=$iCMS->db->getRow("SELECT `name`,`status` FROM `#iCMS@__tags` WHERE `id` ='$id'");
//				$rs->status && MakeTagHtm($rs->name);
//			}
		}elseif($action=='tagEdit'){
			if($_POST['name'])foreach($_POST['name'] as $id=>$value){
				$iCMS->db->query("update `#iCMS@__tags` set `name`='$value',`sortid`='".$_POST['sortid'][$id]."',`ordernum`="._int($_POST['ordernum'][$id])." where `id`='$id'");
				tags_cache($id);
			}
		}
		_Header();
	break;
	case 'delsort':
		$id=(int)$_GET['id'];
		$tSort	= $iCMS->cache('tag.sort','include/syscache',0,true);
		unset($tSort[$id]);
		$iCMS->cache(false,'include/syscache',0,true);
		$iCMS->addcache('tag.sort',$tSort,0);
		_Header();
	break;
	case 'sort':
		$rs	= $iCMS->cache('tag.sort','include/syscache',0,true);
		$_count=count($rs);
		include iCMS_admincp_tpl("tag.sort");
	break;
	case 'manage':
		$Admin->MP("menu_tag_manage");
		$maxperpage =20;
		$total=$iCMS->db->getValue("SELECT count(*) FROM `#iCMS@__tags` order by id DESC");
		page($total,$maxperpage,"个TAG");
		$rs=$iCMS->db->getArray("SELECT * FROM `#iCMS@__tags` order by id DESC LIMIT {$firstcount},{$maxperpage}");
		$_count=count($rs);
		$tSort	= $iCMS->cache('tag.sort','include/syscache',0,true);
		include iCMS_admincp_tpl("tag.manage");
	break;
}
function delArticleTag($tId,$aId=''){
	global $iCMS;
	$sql='#iCMS@__article.id = `indexId`';
	$aId && $sql.=" AND #iCMS@__article.id='$aId'";
	$tagName= $iCMS->db->getValue("SELECT name FROM icms_tags WHERE id='$tId'");
	$rs=$iCMS->db->getArray("SELECT #iCMS@__article.id,#iCMS@__article.tags FROM `#iCMS@__article`,`#iCMS@__taglist` WHERE {$sql} AND `tid`='".$tId."'");
	$_count=count($rs);
	for($i=0;$i<$_count;$i++){
		if($rs[$i]['tags']){
			$tagArray=explode(',',$rs[$i]['tags']);
			$key = array_search($tagName, $tagArray);
			unset($tagArray[$key]);
			$tags=implode(',',$tagArray);
			$iCMS->db->query("update `#iCMS@__article` set `tags`='$tags' where id='".$rs[$i]['id']."'");
			$iCMS->db->query("DELETE FROM `#iCMS@__taglist` WHERE `tid`='$tId' and `indexId`='".$rs[$i]['id']."'");
		}
	}
}
?>
