<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	TAG页面

	$RCSfile: tag.php,v $
	$Revision: 1.23 $
	$Date: 2007/06/27 17:15:09 $
*/

@error_reporting(E_ERROR);

if(!empty($_GET['k'])) {
	$_GET['k'] = str_replace(array('_', '\''), '', $_GET['k']);
	header('location: ./?action/tag/tagname/'.rawurlencode($_GET['k']));
	exit;
}

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}

$tagid = empty($_SGET['tagid'])?0:intval($_SGET['tagid']);

if(!empty($_SGET['show'])) {
	if(empty($tagid)) messagebox('error', 'not_found', S_URL);
	
	$perpage = 50;
	$page = empty($_SGET['page'])?1:intval($_SGET['page']);
	$page = ($page<1)?1:$page;
	$start = ($page-1)*$perpage;
	
	include_once(S_ROOT.'./include/common.inc.php');
	$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('tags').' WHERE tagid=\''.$tagid.'\'');
	$tag = $_SGLOBAL['db']->fetch_array($query);
	if(empty($tag)) messagebox('error', 'not_found', S_URL);
	$listcount = $tag['spaceallnum'];
	
	//作者链接
	if(!empty($tag['uid'])) {
		$tag['spaceurl'] = geturl('uid/'.$tag['uid'].'/action/space');
	} else {
		$tag['spaceurl'] = '#';
	}

	$multi = '';
	$iarr = array();

	if($listcount) {
		$query = $_SGLOBAL['db']->query('SELECT i.* FROM '.tname('spacetags').' s LEFT JOIN '.tname('spaceitems').' i ON i.itemid=s.itemid WHERE s.tagid=\''.$tagid.'\' ORDER BY s.dateline DESC LIMIT '.$start.','.$perpage);
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$value['typename'] = $lang[$value['type']];
			if($value['type'] == 'news') {
				$value['url'] = geturl('action/viewnews/itemid/'.$value['itemid']);
			} else {
				$value['url'] = geturl('uid/'.$value['uid'].'/action/viewspace/itemid/'.$value['itemid']);
			}
			$iarr[] = $value;
		}
		$urlarr = array(
			'action' => 'tag',
			'tagid' => $tagid,
			'show' => 'all'
		);		
		$multi = multi($listcount, $perpage, $page, $urlarr, 0);
	}
	
	$title = 'TAG: '.$tag['tagname'];
	$keywords = $tag['tagname'];
	$description = $tag['tagname'];

	$guidearr = array();
	$guidearr[] = array('url' => geturl('action/tag/tagid/'.$tagid),'name' => 'TAG: '.$tag['tagname']);
	
	$tplname = 'site_tag';
	
	$title = strip_tags($title);
	$keywords = strip_tags($keywords);
	$description = strip_tags($description);

	include template('site_tagall');
	
	ob_out();
	
	exit();
}

if(!empty($tagid)) {
	$wheresql = 'tagid='.$tagid;
} elseif(!empty($_SGET['tagname'])) {
	$wheresql = 'tagname=\''.$_SGET['tagname'].'\'';
} else {
	messagebox('error', 'not_found', S_URL);
}

include_once(S_ROOT.'./include/common.inc.php');

$query = $_SGLOBAL['db']->query('SELECT * FROM '.tname('tags').' WHERE '.$wheresql);
if(!$tag = $_SGLOBAL['db']->fetch_array($query)) messagebox('error', 'not_found_tag');
if($tag['close']) messagebox('error', 'not_found_tag');

$tagid = $tag['tagid'];
if(empty($tag['relativetags'])) {
	$relativetags = array();
} else {
	$relativetags = explode("\t", $tag['relativetags']);
}
$tag['relativetags'] = array();
foreach ($relativetags as $value) {
	$tag['relativetags'][rawurlencode($value)] = $value;
}
//取出tag数最多的一项
$tabarr = array(
	'news' => $tag['spacenewsnum'],
	'blog' => $tag['spaceblognum'],
	'image' => $tag['spaceimagenum'],
	'file' => $tag['spacefilenum'],
	'goods' => $tag['spacegoodsnum'],
	'link' => $tag['spacelinknum'],
	'video' => $tag['spacevideonum']
);
arsort($tabarr);
$activation = true;
foreach($tabarr as $key => $val) {
	if($activation) {
		$tabarr[$key] = array('curtab', '');
		$activation = false;
	} else {
		$tabarr[$key] = array('', ' style="display: none;"');
	}
}

if(!empty($tag['uid'])) {
	$tag['spaceurl'] = geturl('uid/'.$tag['uid'].'/action/space');
} else {
	$tag['spaceurl'] = '#';
}

$title = 'TAG: '.$tag['tagname'];
$keywords = $tag['tagname'];
$description = $tag['tagname'];

$guidearr = array();
$guidearr[] = array('url' => geturl('action/tag/tagid/'.$tagid),'name' => 'TAG: '.$tag['tagname']);

$tplname = 'site_tag';

$title = strip_tags($title);
$keywords = strip_tags($keywords);
$description = strip_tags($description);

include template($tplname);

ob_out();

maketplblockvalue('tagcache');

?>