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

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

$year = (isset($_GET['year'])) ? intval($_GET['year']) : "" ;
$month = (isset($_GET['month'])) ? intval($_GET['month']) : "" ;

//----------------------------------------------------------------------

$pagerow = $dblink->get_row("SELECT * FROM memht_pagine WHERE nome='$page'");
$fullScreen = intval($pagerow['fullscreen']);

require_once("inc/inc_header.php");
	if (isset($_GET['page'])) { page_title($page); }
	openTable();
	
	switch ($arg) {
		case "articles":
			arch_articles($year,$month);
		break;
		case "blog":
			arch_blog($year,$month);
		break;
		case "guide":
			arch_guide($year,$month);
		break;
		case "news":
			arch_news($year,$month);
		break;
		default:
			archiveList();
		break;
	}
	
	closeTable();
require_once("inc/inc_footer.php");

function archiveList() {
	global $dblink;
	
	//Articles
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='articles'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=articles' title='$title'><b>$title</b></a></div>\n";
		
		if ($result = $dblink->get_list("SELECT YEAR(data) AS year, MONTH(data) AS month, COUNT(id) AS tot FROM memht_articoli WHERE enabled=1 GROUP BY year DESC ,month DESC")) {
			foreach ($result as $row) {
				$year = intval($row['year']);
				$month = intval($row['month']);
				$tot = intval($row['tot']);
				
				echo "<div class='small'>&nbsp;&nbsp;- <a href='index.php?page=archive&arg=articles&year=$year&month=$month'>".numToMonth($month)." $year ($tot)</a></div>";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp;"._EMPTY_."</div>";
		}
		echo "<br>";
	}
	
	//Blog
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='blog'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=blog' title='$title'><b>$title</b></a></div>\n";
		
		if ($result = $dblink->get_list("SELECT YEAR(date) AS year, MONTH(date) AS month, COUNT(id) AS tot FROM memht_blog_posts WHERE enabled=1 GROUP BY year DESC ,month DESC")) {
			foreach ($result as $row) {
				$year = intval($row['year']);
				$month = intval($row['month']);
				$tot = intval($row['tot']);
				
				echo "<div class='small'>&nbsp;&nbsp;- <a href='index.php?page=archive&arg=blog&year=$year&month=$month'>".numToMonth($month)." $year ($tot)</a></div>";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp;"._EMPTY_."</div>";
		}
		echo "<br>";
	}
	
	//Guide
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='guide'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=guide' title='$title'><b>$title</b></a></div>\n";
	
		$result = $dblink->get_list("SELECT YEAR(data) AS year, MONTH(data) AS month, COUNT(id) AS tot FROM memht_guide WHERE enabled=1 GROUP BY year DESC ,month DESC");
		foreach ($result as $row) {
			$year = intval($row['year']);
			$month = intval($row['month']);
			$tot = intval($row['tot']);
			
			echo "<div class='small'>&nbsp;&nbsp;- <a href='index.php?page=archive&arg=guide&year=$year&month=$month'>".numToMonth($month)." $year ($tot)</a></div>";
		}
		echo "<br>";
	}
	
	//News
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='news'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=news' title='$title'><b>$title</b></a></div>\n";
		
		if ($result = $dblink->get_list("SELECT YEAR(data) AS year, MONTH(data) AS month, COUNT(id) AS tot FROM memht_news WHERE enabled=1 GROUP BY year DESC ,month DESC")) {
			foreach ($result as $row) {
				$year = intval($row['year']);
				$month = intval($row['month']);
				$tot = intval($row['tot']);
				
				echo "<div class='small'>&nbsp;&nbsp;- <a href='index.php?page=archive&arg=news&year=$year&month=$month'>".numToMonth($month)." $year ($tot)</a></div>";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp "._EMPTY_."</div>";
		}
	}
}

function arch_articles($year,$month) {
	global $dblink,$siteConfig;
	
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='articles'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=articles' title='$title'><b>$title</b></a> (".numToMonth($month)." $year)<br>\n";
		
		if ($result = $dblink->get_list("SELECT id,nome FROM memht_articoli WHERE MONTH(data) = $month AND YEAR(data) = $year AND enabled=1 ORDER BY id DESC")) {
			foreach ($result as $row) {
				$id = intval($row['id']);
				$nome = outCode($row['nome']);
						
				echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=articles&op=readArticle&id=$id&title=".mem_urlencode($nome)."' title='$nome'>$nome</a></span><br>\n";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp "._EMPTY_."</div>";
		}
	} else {
		echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php'>";
	}
}

function arch_blog($year,$month) {
	global $dblink,$siteConfig;
	
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='blog'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=blog' title='$title'><b>$title</b></a> (".numToMonth($month)." $year)<br>\n";
		
		if ($result = $dblink->get_list("SELECT id,title FROM memht_blog_posts WHERE MONTH(date) = $month AND YEAR(date) = $year AND enabled=1 ORDER BY id DESC")) {
			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";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp "._EMPTY_."</div>";
		}
	} else {
		echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php'>";
	}
}

function arch_guide($year,$month) {
	global $dblink,$siteConfig;
	
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='guide'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=guide' title='$title'><b>$title</b></a> (".numToMonth($month)." $year)<br>\n";
		
		if ($result = $dblink->get_list("SELECT id,nome FROM memht_guide WHERE MONTH(data) = $month AND YEAR(data) = $year AND enabled=1 ORDER BY id DESC")) {
			foreach ($result as $row) {
				$id = intval($row['id']);
				$nome = outCode($row['nome']);
						
				echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=guide&op=readGuide&id=$id&title=".mem_urlencode($nome)."' title='$nome'>$nome</a></span><br>\n";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp "._EMPTY_."</div>";
		}
	} else {
		echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php'>";
	}
}

function arch_news($year,$month) {
	global $dblink,$siteConfig;
	
	$row = $dblink->get_row("SELECT titolo,enabled FROM memht_pagine WHERE nome='news'");
	if ($row['enabled']==1) {
		$title = outCode($row['titolo']);
		echo "<img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=news' title='$title'><b>$title</b></a> (".numToMonth($month)." $year)<br>\n";
		
		if ($result = $dblink->get_list("SELECT id,nome FROM memht_news WHERE MONTH(data) = $month AND YEAR(data) = $year AND enabled=1 ORDER BY id DESC")) {
			foreach ($result as $row) {
				$id = intval($row['id']);
				$nome = outCode($row['nome']);
						
				echo "&nbsp;&nbsp;- <span class='small'><a href='index.php?page=news&op=readNews&id=$id&title=".mem_urlencode($nome)."' title='$nome'>$nome</a></span><br>\n";
			}
		} else {
			echo "<div id='errorText'>&nbsp;&nbsp;&nbsp;&nbsp "._EMPTY_."</div>";
		}
	} else {
		echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php'>";
	}
}

?>