<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$tag = (isset($_GET['tag'])) ? inCode($_GET['tag']) : "" ;

	openTable();
	
	switch ($op) {
		case "list":
			getList($tag);
		break;
		default:
			tagList();
		break;
	}
	
	closeTable();

function tagList() {
	global $dblink;
	
	echo "<div align='justify' class='box'>";
		$result = $dblink->get_list("SELECT tag FROM memht_tags GROUP BY tag ORDER BY RAND()");
		foreach ($result as $row) {
			$tag = outCode($row['tag']);
			
			$num = $dblink->get_num("SELECT tag FROM memht_tags WHERE tag='".inCode($tag)."'");
			if ($num<=1) { $dim = "xs"; }
			else if ($num<=5) { $dim = "s"; }
			else if ($num<=20) { $dim = "m"; }
			else if ($num<=50) { $dim = "l"; }
			else { $dim = "xl"; }
			
			echo "<span style='padding: 0 2px;' class='$dim'><a href='index.php?page=tags&op=list&tag=".urlencode($tag)."' title='$tag'>$tag</a></span>\n";
		}
	echo "</div>";
}

function getList($tag) {
	global $dblink;
	
	//Articles
	if ($result = $dblink->get_list("SELECT * FROM memht_articoli AS ar JOIN memht_tags AS tg ON ar.id=tg.cid WHERE tg.whr=1 AND tg.tag='$tag' AND ar.enabled=1 ORDER BY id DESC LIMIT 50")) {
		$row = $dblink->get_row("SELECT id,titolo FROM memht_pagine WHERE nome='articles'"); $ptitle['art'] = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=articles' title='".$ptitle['art']."'><b>".$ptitle['art']."</b></a><br>\n";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$title = outCode($row['nome']);
			echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=articles&op=readArticle&id=$id&title=".mem_urlencode($title)."' title='$title'>$title</a></span><br>\n";
		}
		echo "<br>";
	}
	
	//Blog
	if ($result = $dblink->get_list("SELECT * FROM memht_blog_posts AS bp JOIN memht_tags AS tg ON bp.id=tg.cid WHERE tg.whr=5 AND tg.tag='$tag' AND bp.enabled=1 ORDER BY id DESC LIMIT 50")) {
		$row = $dblink->get_row("SELECT id,titolo FROM memht_pagine WHERE nome='blog'"); $ptitle['blog'] = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=blog' title='".$ptitle['blog']."'><b>".$ptitle['blog']."</b></a><br>\n";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$title = outCode($row['title']);
			echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=blog&id=$id&title=".mem_urlencode($title)."' title='$title'>$title</a></span><br>\n";
		}
		echo "<br>";
	}
	
	//Download
	if ($result = $dblink->get_list("SELECT * FROM memht_download AS dw JOIN memht_tags AS tg ON dw.id=tg.cid WHERE tg.whr=4 AND tg.tag='$tag' ORDER BY id DESC LIMIT 50")) {
		$row = $dblink->get_row("SELECT id,titolo FROM memht_pagine WHERE nome='download'"); $ptitle['dwn'] = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=download' title='".$ptitle['dwn']."'><b>".$ptitle['dwn']."</b></a><br>\n";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$title = outCode($row['nome']);
			echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=download&op=getFile&id=$id&title=".mem_urlencode($title)."' title='$title'>$title</a></span><br>\n";
		}
		echo "<br>";
	}
	
	//Forum
	if ($result = $dblink->get_list("SELECT * FROM memht_forum_posts AS fo JOIN memht_tags AS tg ON fo.id=tg.cid WHERE fo.parent=0 AND tg.whr=8 AND tg.tag='$tag' AND fo.status=1 ORDER BY id DESC LIMIT 50")) {
		$row = $dblink->get_row("SELECT id,titolo FROM memht_pagine WHERE nome='forum'"); $ptitle['forum'] = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=forum' title='".$ptitle['forum']."'><b>".$ptitle['forum']."</b></a><br>\n";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$title = outCode($row['name']);
			echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=forum&op=viewThread&id=$id&title=".mem_urlencode($title)."' title='$title'>$title</a></span><br>\n";
		}
		echo "<br>";
	}
	
	//Guide
	if ($result = $dblink->get_list("SELECT * FROM memht_guide AS gu JOIN memht_tags AS tg ON gu.id=tg.cid WHERE tg.whr=2 AND tg.tag='$tag' AND gu.enabled=1 ORDER BY id DESC LIMIT 50")) {
		$row = $dblink->get_row("SELECT id,titolo FROM memht_pagine WHERE nome='guide'"); $ptitle['guide'] = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=guide' title='".$ptitle['guide']."'><b>".$ptitle['guide']."</b></a><br>\n";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$title = outCode($row['nome']);
			echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=guide&op=readGuide&id=$id&title=".mem_urlencode($title)."' title='$title'>$title</a></span><br>\n";
		}
		echo "<br>";
	}
	
	//News
	if ($result = $dblink->get_list("SELECT * FROM memht_news AS nw JOIN memht_tags AS tg ON nw.id=tg.cid WHERE tg.whr=3 AND tg.tag='$tag' AND nw.enabled=1 ORDER BY id DESC LIMIT 50")) {
		$row = $dblink->get_row("SELECT id,titolo FROM memht_pagine WHERE nome='news'"); $ptitle['news'] = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=news' title='".$ptitle['news']."'><b>".$ptitle['news']."</b></a><br>\n";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$title = outCode($row['nome']);
			echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=news&op=readNews&id=$id&title=".mem_urlencode($title)."' title='$title'>$title</a></span><br>\n";
		}
		echo "<br>";
	}
}

?>