<?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>");
}

$ptitle = array();
$result = $dblink->get_list("SELECT nome,titolo FROM memht_pagine WHERE (nome='articles' OR nome='guide' OR nome='news') AND enabled=1 ORDER BY titolo");
foreach ($result as $row) {
	$ptitle[$row['nome']] = $row['titolo'];
}

function topicsMain() {
	global $dblink;
	
	echo "<form name='topics' action='index.php?page=topics' method='POST'>\n";
	echo "<div align='right' style='margin-bottom:8px;'>\n";
	
				echo "<select name='topic'>\n";
					echo "<option value='all' selected>"._ARGUMENT_."</option>\n";
					echo "<option value='all'>"._ALL_."</option>\n";
						$result = $dblink->get_list("SELECT id,nome FROM memht_argomenti ORDER BY nome");
						foreach ($result as $row) {
							$id = intval($row['id']);
							$name = outCode($row['nome']);
							
							echo "<option value='$id'>$name</option>\n";
						}
				echo "</select>\n";

				echo "&nbsp;<select name='language'>\n";
					echo "<option value='all' selected>"._LANGUAGE_."</option>\n";
					echo "<option value='all'>"._ALL_."</option>\n";
					echo "<option value='0'>"._GLOBAL_."</option>\n";
						$result = $dblink->get_list("SELECT id,language,def FROM memht_content_languages ORDER BY language");
						foreach ($result as $row) {
							$id = intval($row['id']);
							$language = outCode($row['language']);
							
							echo "<option value='$id'>$language</option>\n";
						}
				echo "</select>\n";
			
				echo "&nbsp;<select name='results'>\n";
					echo "<option value='20' selected>"._MAXRES_."</option>\n";
					echo "<option value='10'>10</option>\n";
					echo "<option value='20'>20</option>\n";
					echo "<option value='50'>50</option>\n";
				echo "</select>\n";
		
				echo "<input type='submit' name='Submit' value='"._SEARCH_."'>\n";
				
			echo "</div>\n";
	echo "</form>\n";
}

function topics() {
	global $dblink,$siteConfig,$ptitle;
	
	topicsMain();
	
	$topic = (isset($_REQUEST['topic'])) ? inCode($_REQUEST['topic']) : "all" ;
	$language = (isset($_REQUEST['language'])) ? inCode($_REQUEST['language']) : "all" ;
	$results = (isset($_REQUEST['results'])) ? intval($_REQUEST['results']) : 20 ;

	$argquery = ($topic!="all") ? " WHERE id=".intval($topic) : "" ;
	$langquery = ($language!="all") ? " AND language=".intval($language) : "" ;
	
	$res = 0;
	$result = $dblink->get_list("SELECT * FROM memht_argomenti{$argquery} ORDER BY nome");
	foreach ($result as $row) {
		$argid = intval($row['id']);
		$anome = outCode($row['nome']);
		$img = outCode($row['img']);
		
		if ($img!="" AND file_exists("templates/".$siteConfig['template']."/images/topics/$img.gif")) {
			$img = "<img src='templates/".$siteConfig['template']."/images/topics/$img.gif' border='0' alt=\"$anome\">";
		} else if ($img!="" AND file_exists("images/topics/$img.gif")) {
			$img = "<img src='images/topics/$img.gif' border='0' alt=\"$anome\">";
		} else {
			$img = "<img src='images/topics/empty.gif' border='0' alt='"._EMPTY_."'>";
		}
		
		$num_art = $dblink->get_num("SELECT id FROM memht_articoli WHERE argomento=$argid AND enabled=1{$langquery}");
		$num_gui = $dblink->get_num("SELECT id FROM memht_guide WHERE argomento=$argid AND enabled=1{$langquery}");
		$num_news = $dblink->get_num("SELECT id FROM memht_news WHERE argomento=$argid AND enabled=1{$langquery}");
		
		if ($num_art>0 OR $num_gui>0 OR $num_news>0) {
			openTable($anome);
			echo "<table width='100%'>\n";
				echo "<tr><td valign='top'>";
					
					//Articoli
					if ($num_art>0) {
						$title_pgnm = $ptitle['articles'];
						echo "<b>$title_pgnm</b> <a href='rss.php?page=articles&topic=$argid' title='"._ARTRSSFEED_."'><img src='images/rss-syndicate_box.gif' border='0' alt='RSS'></a><br>";
						$result_art = $dblink->get_list("SELECT id,nome FROM memht_articoli WHERE argomento=$argid AND enabled=1{$langquery} ORDER BY id DESC LIMIT $results");
						foreach ($result_art as $row_art) {
							$aid = intval($row_art['id']);
							$anome = outCode($row_art['nome']);
								
							echo "&nbsp;&nbsp;<span class='small'>- <a href='index.php?page=articles&op=readArticle&id=$aid&title=".mem_urlencode($anome)."' title=\"$anome\">$anome</a></span><br>";
						}
						$res++;
					}
					
					//Guide
					if ($num_gui>0) {
						$title_pgnm = $ptitle['guide'];
						echo "<b>$title_pgnm</b> <a href='rss.php?page=guide&topic=$argid' title='"._GUIRSSFEED_."'><img src='images/rss-syndicate_box.gif' border='0' alt='RSS'></a><br>";
						$result_guide = $dblink->get_list("SELECT id,nome FROM memht_guide WHERE argomento=$argid AND enabled=1{$langquery} ORDER BY id DESC LIMIT $results");
						foreach ($result_guide as $row_guide) {
							$gid = intval($row_guide['id']);
							$gnome = outCode($row_guide['nome']);
								
							echo "&nbsp;&nbsp;<span class='small'>- <a href='index.php?page=guide&op=readGuide&id=$gid&title=".mem_urlencode($gnome)."' title=\"$gnome\">$gnome</a></span><br>";
						}
						$res++;
					}
					
					//News
					if ($num_news>0) {
						$title_pgnm = $ptitle['news'];
						echo "<b>$title_pgnm</b> <a href='rss.php?page=news&topic=$argid' title='"._NEWSRSSFEED_."'><img src='images/rss-syndicate_box.gif' border='0' alt='RSS'></a><br>";
						$result_news = $dblink->get_list("SELECT id,nome FROM memht_news WHERE argomento=$argid AND enabled=1{$langquery} ORDER BY id DESC LIMIT $results");
						foreach ($result_news as $row_news) {
							$nid = intval($row_news['id']);
							$nnome = outCode($row_news['nome']);
								
							echo "&nbsp;&nbsp;<span class='small'>- <a href='index.php?page=news&op=readNews&id=$nid&title=".mem_urlencode($nnome)."' title=\"$nnome\">$nnome</a></span><br>";
						}
						$res++;
					}
					
				echo "</td><td valign='top' width='5%'>$img</td></tr>\n";
			echo "</table>\n";
			closeTable();
			echo "<br>\n";
		}
	}
	if ($res==0) {
		echo "<div class='box' id='errorText' align='center'><b>"._NORES_."</b></div>\n";
	}
}
topics();

?>