<?php
/**
* Tag标签统计
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id$
*/

define('SCRIPTNAV', 'tag');
require_once './include/inc_common.php';

if(!empty($tagname)) {
    $tagname = trim($tagname);
	if(!$tagname || strlen($tagname) > 21) {
		redirect('无效的标签或标签大于21个字符。');
	}
    $result = $db->get_one("SELECT closed FROM {$dbpre}tags WHERE tagname='$tagname'");
    if(!$result) {
        redirect('标签不存在！');
    } elseif($result['closed']) {
        redirect('标签已关闭！');
    }

    $total = $db->get_value("SELECT count(shopid) FROM {$dbpre}shoptags WHERE tagname='$tagname'");

    $offset = 15;
    $limit = get_limit($page, $offset);

	$query = $db->query("SELECT a.*,b.* FROM {$dbpre}shoptags a LEFT JOIN {$dbpre}shops b ON(a.shopid=b.sid) WHERE tagname='$tagname' GROUP BY a.shopid ORDER BY b.reviews DESC,pageviews DESC LIMIT $limit");
	$shops = array();
	while($shop = $db->fetch_array($query)){
        $shop['fullname'] = $shop['shopname'] . ($shop['subname'] ? "({$shop['subname']})" : "");
        $shop['pcd'] = substr($shop['classcode'],0,2);
        $shop['pcdname'] = $_classcodes[$shop['pcd']]['classname'];
        $shop['cdname'] = $_classcodes[$shop['classcode']]['classname'];
        $shops[] = $shop;
	}

    $multipage = multi($total, $offset, $page, 'tag.php?tagname='.rawurlencode($tagname));

    $tagname = html_special_chars_ex($tagname);
    $tpl_name = 'shop_tag_shops';
    $subtitle = $tagname;

} else {

    $offset = 100;

	$query = $db->query("SELECT tagname,total FROM {$dbpre}tags WHERE closed='0' ORDER BY total DESC LIMIT $offset");
	$tags = array();
	while($tag = $db->fetch_array($query)){
		$tag['tagnamenec'] = rawurlencode($tag['tagname']);
        $tags[] = $tag;
	}

    //$multipage = multi($total, $offset, $page, 'tag.php');

    $tpl_name = 'shop_tag_index';
    $subtitle = '热门标签';
}

require_once MUDDER_ROOT.'header.php';
require_once template($tpl_name);
footer();
?>
