<?php

/*
	[SupeSite/X-Space] (C)2001-2006 Comsenz Inc.
	TAG­¶­±

	$RCSfile: tag.php,v $
	$Revision: 1.11 $
	$Date: 2006/09/30 08:53:07 $
*/

if(!empty($_GET['k'])) {
	$_GET['k'] = str_replace(array('_', '\''), '', $_GET['k']);
	header('location: index.php?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'];

	$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';

	include template('site_tagall');
	exit;
}

if(!empty($tagid)) {
	if(!empty($_SCONFIG['htmlviewnews'])) {
		$_SHTML['action'] = 'tag';
		$_SHTML['tagid'] = $tagid;
		$_SGLOBAL['htmlfile'] = gethtmlfile($_SHTML);
		ehtml('get', $_SCONFIG['htmlviewnewstime']);
		$_SCONFIG['debug'] = 0;
	}
	$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', S_URL);
if($tag['close']) messagebox('error', 'not_found', S_URL);

$tagid = $tag['tagid'];
if(!empty($_SCONFIG['htmlviewnews'])) {
	$_SHTML['action'] = 'tag';
	$_SHTML['tagid'] = $tagid;
	$_SGLOBAL['htmlfile'] = gethtmlfile($_SHTML);
	ehtml('get', $_SCONFIG['htmlviewnewstime']);
	$_SCONFIG['debug'] = 0;
}

if(empty($tag['relativetags'])) {
	$relativetags = array();
} else {
	$relativetags = explode("\t", $tag['relativetags']);
}
$tag['relativetags'] = array();
foreach ($relativetags as $value) {
	$tag['relativetags'][rawurlencode($value)] = $value;
}

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';

include template($tplname);

if(!empty($_SCONFIG['htmlviewnews'])) {
	ehtml('make');
} else {
	maketplblocknames($tplname);
	maketplblockvalue('tagcache');
}

?>