<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!check_submit('tagsubmit')) {

    !$tagid && cpmsg('不存在此标签ID，请返回。');
    $taginfo = $db->select_one('*',$dbpre.'tags', "tagid='$tagid'");
    !$taginfo && cpmsg('不存在此标签，请返回。');

    include cptpl('tagedit', MOD_ROOT.'./admin/templates/');

} else {

    !$tagid && cpmsg('不存在此标签ID，请返回。');
    $taginfo = $db->select_one('*',$dbpre.'tags', "tagid='$tagid'");
    !$taginfo && cpmsg('不存在此标签，请返回。');
    $query = $db->query("SELECT * FROM {$dbpre}shoptags WHERE tagname='$old_tagname'");

    $tagname == $old_tagname && cpmsg('未修改，请返回。');
    
    // 新标签是否存在
    $existtag = $db->select_one('*', $dbpre.'tags', "tagid!='$tagid' AND tagname='$tagname' AND panelcode='{$taginfo['panelcode']}'");
    
    if($existtag) {

        // 是否合并
        !$need_merge && cpmsg('标签已存在，无法更新，您可以选择合并2个标签，请返回选择。');

        $cl_begin = $taginfo['panelcode'].'00';
        $cl_end = $taginfo['panelcode'].'99';
        // 查询出同板块的商铺（内的点评ID）
        $query = $db->query("SELECT st.* FROM {$dbpre}shops s LEFT JOIN {$dbpre}shoptags st ON(s.sid=st.shopid) WHERE s.classcode>'$cl_begin' AND s.classcode<='$cl_end' AND tagname='$old_tagname'");
        while($row = $db->fetch_array($query)) {
            $reviewids[] = $row['reviewid'];
            $sourceTags[] = $row;
        }
        $query && $db->free_result($query);
        // 更新点评内的标签副本
        if($reviewids) {
            $reviewids = array_unique($reviewids);
            $db->update("UPDATE {$dbpre}reviews SET shoptags=REPLACE(shoptags,'$old_tagname','$tagname'),producttags=REPLACE(producttags,'$old_tagname','$tagname'),suitplacetags=REPLACE(suitplacetags,'$old_tagname','$tagname') WHERE rid IN(".implode(',',$reviewids).")");
        }
        unset($reviewids);

        // 查询出目标Tags
        $destTags = $db->get_all("SELECT st.* FROM {$dbpre}shops s LEFT JOIN {$dbpre}shoptags st ON(s.sid=st.shopid) WHERE s.classcode>'$cl_begin' AND s.classcode<='$cl_end' AND tagname='$tagname'");
        // 循环对比，出标签名以外的所有项，如果相同，则删除，不同则更新源标签到新标签
        foreach($sourceTags as $stag) {
            $sourcekey = $stag['shopid'].'-'.$stag['reviewid'].'-'.$stag['tagsort'];
            foreach($destTags as $dtag) {
                $destkey = $dtag['shopid'].$dtag['reviewid'].$dtag['tagsort'];
                if($sourcekey == $destkey) {
                    $db->update("DELETE FROM {$dbpre}shoptags WHERE tagname='$old_tagname' AND shopid='{$stag['shopid']}' AND reviewid='{$stag['reviewid']}' AND tagsort='{$stag['tagsort']}'");
                } else {
                    $db->update("UPDATE {$dbpre}shoptags SET tagname='$tagname' WHERE tagname='$old_tagname' AND shopid='{$stag['shopid']}' AND reviewid='{$stag['reviewid']}' AND tagsort='{$stag['tagsort']}'");
                }
            }
        }
        unset($sourceTags, $destTags);

        // 下面合并Tags表
        $sourcetotal = intval($taginfo['total']);
        // 合并数量至目标
        $db->update("UPDATE {$dbpre}tags SET total=total+$sourcetotal WHERE tagid='{$existtag[tagid]}'");
        // 删除源
        $db->update("DELETE FROM {$dbpre}tags WHERE tagid='$tagid'");
        unset($existtag,$taginfo);
        // 合并结束
        cpmsg('标签合并完毕!','admincp.php?action='.$action.'&file=taglist');
        
    
    } else {

        $reviewids = array();
        while($row = $db->fetch_array($query)) {
            $reviewids[] = $row['reviewid'];
        }
        $query && $db->free_result($query);
        if($reviewids) {
            $reviewids = array_unique($reviewids);
            $db->update("UPDATE {$dbpre}reviews SET shoptags=REPLACE(shoptags,'$old_tagname','$tagname'),producttags=REPLACE(producttags,'$old_tagname','$tagname'),suitplacetags=REPLACE(suitplacetags,'$old_tagname','$tagname') WHERE rid IN(".implode(',',$reviewids).")");
        }
        $db->update("UPDATE {$dbpre}tags SET tagname='$tagname' WHERE tagid='$tagid'");
        $db->update("UPDATE {$dbpre}shoptags SET tagname='$tagname' WHERE tagname='$old_tagname'");

        cpmsg('标签编辑完毕!','admincp.php?action='.$action.'&file=taglist');

    }
}
?>