<?php
class tag
{
	var $channelid;
	var $table;

	function __construct()
	{
		$this->channelid = $GLOBALS['channelid'];
		$this->table = $GLOBALS['module'][$GLOBALS['channel'][$this->channelid]['channelselect']]['table_name'];
	}

	function tag()
	{
		$this->__construct();
	}

	function addKeyTag( $id, $param)
	{
		if( !$param) return true;
		if( !is_array( $param)) $param = $this->keyStringToArray( $param);
		if( !is_array( $param) || count($param)==1) return true;
		foreach($param as $key => $val){
			if (strlen(@trim($val['name'])) > 2){
				$result = qdb::queryFirst("select kid from ".TABLEPRE."tag where name='{$val['name']}' limit 1");
				if ($result['kid'] > 0){ // 系统存在该tag
					$tid = qdb::query("replace into ".TABLEPRE."tag_mapping (id, channelid, kid) values ('$id', '{$this->channelid}', '{$result['kid']}')", true);
					qdb::query("update ".TABLEPRE."tag set rank=rank+1, num = num + 1 where kid='{$result['kid']}'");
				}else{ // 系统不存在该tag，则增加
					$kid = qdb::query("replace into ".TABLEPRE."tag (name,  rank, num) values ('{$val['name']}', '{$val['rank']}', 1)", true);
					qdb::query("replace into ".TABLEPRE."tag_mapping (id, channelid, kid) values ('$id', '{$this->channelid}', '$kid')");
				}
			}
		}
		$this->_clear_cache($id);
		return true;	
	}

	function add()
	{
		//检测该文章是否有此tag
		$id   = intval( $_POST['id']);
		$name_temps = $_POST['name'];
		$name_temps = str_replace(array(",","，"," "), "|", $name_temps);
		$name_temp = strpos( $name_temps, '|') ? explode( '|', $name_temps) : array( $name_temps);
		foreach( $name_temp as $key=>$val){
			$name = $val;
			if ( strlen(trim($name)) > 2){
				$result = qdb::queryFirst("select kid from ".TABLEPRE."tag where name='{$name}' limit 1");
				if( $result['kid'] > 0){//系统存在该tag
					$tag_mapping = qdb::queryFirst("select tid from ".TABLEPRE."tag_mapping where channelid = '{$this->channelid}' and id='$id' and kid='{$result['kid']}' limit 1");
					if( $tag_mapping['tid'] >0){//当前文章存在该tag,返回错误
						$data[$key] = array( 'status'=>0, 'message'=>8);
					}else{//为该文章增加tag
						$tid = qdb::query("insert into ".TABLEPRE."tag_mapping (id, channelid, kid) values ('$id', '{$this->channelid}', '{$result['kid']}')", true);
						qdb::query("update ".TABLEPRE."tag set rank=rank+1, num = num + 1 where kid='{$result['kid']}'");
						$data[$key] = array( 'status'=>1, 'data'=> array( 'tid'=>$tid, 'kid'=>$kid, 'name'=>$val));
					}
				}else{//系统不存在该tag，则增加
					$kid = qdb::query("insert into ".TABLEPRE."tag (name, rank, num) values ('{$name}', 100, 1)", true);
					$tid = qdb::query("insert into ".TABLEPRE."tag_mapping (id, channelid, kid) values ('{$id}', '{$this->channelid}', '$kid')", true);
					$data[$key] = array( 'status'=>1, 'data'=> array( 'tid'=>$tid, 'kid'=>$kid, 'name'=>$val));
				} 
			}else{
				$data[$key] = array( 'status'=>0, 'message'=>9);
			}
		}
		$this->_clear_cache($id);
		echo qbase::xmlPrint(qbase::jsonencode($data));
		exit();
	}

	function del()
	{
		$tid  = intval( $_POST['tid']);
		$item = qdb::queryFirst("select id, kid, channelid from ".TABLEPRE."tag_mapping where tid ='{$tid}'");
		if($item['id'] > 0){
			qdb::query("delete from ".TABLEPRE."tag_mapping where tid='{$tid}'");
			qdb::query("update ".TABLEPRE."tag set num = num - 1 where kid ='{$item['kid']}'");
			$this->_clear_cache($item['channelid'], $item['id']);
		}
		$data = array( 'status'=>1 );
		echo qbase::xmlPrint(qbase::jsonencode($data));
		exit();
	}
	
	function delKeyword($kid){
		$word = qdb::fetchAll("select tid, id, channelid from ".TABLEPRE."tag_mapping where kid ='{$kid}'");
		if(empty($word)) return false;
		foreach($word as $val){
			qdb::Query("delete from ".TABLEPRE."tag_mapping where tid = '{$val['tid']}' ");
			$this->_clear_cache($val['channelid'], $val['id']);
		}
		$num = count($word);
		qdb::Query("update ".TABLEPRE."tag set num = num - {$num} where kid='{$kid}' limit 1");
		return true;
	}

	function keyStringToArray( $param)
	{
		$param = str_replace(array(",","，"), "|", $param);
		$param = explode('|', $param);
		require_once(R_P.'/include/class.splitword.php');
		$sp = new SplitWord();
		$newWords = array();
		foreach($param as $key=>$val){
			$newWords[$key]['name'] = $val;
			$newWords[$key]['rank'] = $sp->GetRank($val);			
		}
		return $newWords;
	}
	
	function _clear_cache($channelid, $id){
		$key = 'tag_' . $channelid.'_'. $id;
		cache::delete($key);
	}
}
?>