<?php 
/********Site站点模型**********/
/**
 *  获得某文档的所有tag
 *
 * @param     int     $aid  文档id
 * @return    string
 */
if ( ! function_exists('GetSiteTags'))
{
    function GetSiteTags($aid)
    {
        global $dsql;
        $tags = '';
        $query = "SELECT tag FROM `#@__webtaglist` WHERE aid='$aid' ";
        $dsql->Execute('tag',$query);
        while($row = $dsql->GetArray('tag'))
        {
            $tags .= ($tags=='' ? $row['tag'] : ','.$row['tag']);
        }
        return $tags;
    }
}

/**
 *  获取一个微表的索引键
 *
 * @access    public
 * @param     string  $arcrank  权限值
 * @param     int  $typeid  栏目ID
 * @param     int  $sortrank  排序ID
 * @param     int  $channelid  模型ID
 * @param     int  $senddate  发布日期
 * @param     int  $mid  会员ID
 * @return    int
 */
if ( ! function_exists('GetSiteIndexKey'))
{
    function GetSiteIndexKey($arcrank, $typeid, $sortrank=0, $channelid=1, $senddate=0, $mid=1)
    {
        global $dsql,$senddate,$typeid2;
        if(empty($typeid2)) $typeid2 = 0;
        if(empty($senddate)) $senddate = time();
        if(empty($sortrank)) $sortrank = $senddate;
        $iquery = "
          INSERT INTO `#@__arctiny` (`arcrank`,`typeid`,`typeid2`,`channel`,`senddate`, `sortrank`, `mid`)
          VALUES ('$arcrank','$typeid','$typeid2' , '$channelid','$senddate', '$sortrank', '$mid') ";
        $dsql->ExecuteNoneQuery($iquery);
        $aid = $dsql->GetLastID();
        return $aid;
    }
}


/**
 *  更新微表key及Tag
 *
 * @access    public
 * @param     int  $id  文档ID
 * @param     string  $arcrank  权限值
 * @param     int  $typeid  栏目ID
 * @param     int  $sortrank  排序ID
 * @param     string  $tags  tag标签
 * @return    string
 */
if ( ! function_exists('UpSiteIndexKey'))
{
    function UpSiteIndexKey($id, $arcrank, $typeid, $sortrank=0, $tags='')
    {
        global $dsql,$typeid2;
        if(empty($typeid2)) $typeid2 = 0;
        $addtime = time();
        $query = " UPDATE `#@__arctiny` SET `arcrank`='$arcrank', `typeid`='$typeid', `typeid2`='$typeid2', `sortrank`='$sortrank' WHERE id = '$id' ";
        $dsql->ExecuteNoneQuery($query);

        /*
        * 处理修改后的Tag
        */
        if($tags!='')
        {
            $oldtag = GetSiteTags($id);
            $oldtags = explode(',',$oldtag);
            $tagss = explode(',',$tags);
            foreach($tagss as $tag)
            {
                $tag = trim($tag);
                if(isset($tag[100]) || $tag!=stripslashes($tag))
                {
                    continue;
                }
                if(!in_array($tag,$oldtags))
                {
                    InsertOneSiteTag($tag,$id);
                }
            }
            foreach($oldtags as $tag)
            {
                if(!in_array($tag,$tagss))
                {
                    $dsql->ExecuteNoneQuery("DELETE FROM `#@__webtaglist` WHERE aid='$id' AND tag LIKE '$tag' ");
					$atags=$dsql->GetOne("SELECT total FROM `#@__webtagindex` WHERE tag LIKE '$tag' ");
						if($atags=1){
						$dsql->ExecuteNoneQuery("DELETE FROM `#@__webtagindex` WHERE tag LIKE '$tag' ");
						}
						else{
							$dsql->ExecuteNoneQuery("UPDATE `#@__webtagindex` SET total=total-1 WHERE tag LIKE '$tag' ");
							}
                }
                else
                {
                    $dsql->ExecuteNoneQuery("UPDATE `#@__webtaglist` SET `arcrank` = '$arcrank',`typeid` = '$typeid' WHERE tag LIKE '$tag' AND aid = '$id' ");
                }
            }
        }
    }
}


/**
 *  插入Tags
 *
 * @access    public
 * @param     string  $tag  tag标签
 * @param     int  $aid  文档AID
 * @return    void
 */
if ( ! function_exists('InsertSiteTags'))
{
    function InsertSiteTags($tag, $aid)
    {
        $tags = explode(',',$tag);
        foreach($tags as $tag)
        {
            $tag = trim($tag);
            if(isset($tag[100]) || $tag!=stripslashes($tag))
            {
                continue;
            }
            InsertOneSiteTag($tag,$aid);
        }
    }
}


/**
 *  插入一个tag
 *
 * @access    public
 * @param     string  $tag  标签
 * @param     int  $aid  文档AID
 * @return    void
 */
if ( ! function_exists('InsertOneSiteTag'))
{
    function InsertOneSiteTag($tag, $aid)
    {
        global $typeid,$arcrank,$dsql;
        $tag = trim($tag);
        if($tag == '')
        {
            return '';
        }
        if(empty($typeid))
        {
            $typeid = 0;
        }
        if(empty($arcrank))
        {
            $arcrank = 0;
        }
        $rs = false;
        $addtime = time();
        $row = $dsql->GetOne("SELECT * FROM `#@__webtagindex` WHERE tag LIKE '$tag' ");
        if(!is_array($row))
        {
            $rs = $dsql->ExecuteNoneQuery(" INSERT INTO `#@__webtagindex`(`tag`,`typeid`,`total`) VALUES('$tag','$typeid','1'); ");
            $tid = $dsql->GetLastID();
        }
        else
        {
            $rs = $dsql->ExecuteNoneQuery(" UPDATE `#@__webtagindex` SET total=total+1 WHERE tag LIKE '$tag' ");
            $tid = $row['id'];
        }
        if($rs)
        {
            $dsql->ExecuteNoneQuery("INSERT INTO `#@__webtaglist`(`tid`,`aid`,`arcrank`,`typeid`,`tag`) VALUES('$tid','$aid','$arcrank','$typeid' ,'$tag'); ");
        }
    }
}

//删除单个文章时同步删除文章中的TAGS
function deltagsindex($aid)
{
global $dsql;
            $oldtag = GetSiteTags($aid);
            $oldtags = explode(',',$oldtag);
            foreach($oldtags as $tag)
            {
				$dsql->ExecuteNoneQuery("UPDATE `#@__webtagindex` SET total=total-1 WHERE tag LIKE '$tag' ");
						$dsql->ExecuteNoneQuery("DELETE FROM `#@__webtagindex` WHERE total=0 ");
						$dsql->ExecuteNoneQuery("DELETE FROM `#@__webtaglist` WHERE aid='$aid' AND tag LIKE '$tag' ");
            }
}

