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

if (isSet($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isSet($_GET['id'])) { $id = inCode($_GET['id']); } else { $id = ""; }

if (isSet($_GET['pg'])) { $pg = inCode($_GET['pg']); } else { $pg = 1; }
$ofsppg = 30; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function readFaq($id) {
	global $dblink,$siteConfig;
	
	$id = intval($id);
	
	if ($row = $dblink->get_row("SELECT domanda,risposta FROM memht_faq WHERE id=$id")) {
		$domanda = outCode($row['domanda']);
		$risposta = outcode($row['risposta']);
		
		echo "<div class='box' style='margin-bottom:10px;'><b>$domanda</b></div>\n";
		echo "<div>$risposta</div>\n";
	} else {
		echo "<meta http-equiv='refresh' content='0;URL=".$siteConfig['site_url']."/index.php?page=faq'>";
	}
}

function viewArgument($id,$ofsbgn,$ofsppg,$pg) {
	global $dblink;

	$aid = intval($id);
	
	if ($dblink->get_num("SELECT id FROM memht_faq WHERE argomento=$aid AND enabled=1")) {
		//Anchor Link
		$result = $dblink->get_list("SELECT id,domanda FROM memht_faq WHERE argomento='$aid' AND enabled=1 ORDER BY domanda LIMIT $ofsbgn,$ofsppg");
		foreach ($result as $row) {
			$id = intval($row['id']);
			$domanda = outCode($row['domanda']);
			
			echo "<div><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=faq&op=read&id=$id' title='$domanda'>$domanda</a></div>";
		}
		
		//Pages
		include_once("inc/class/paginationSystem.class.php");
		$ps = new paginationSystem();
		$ps->items = $ofsppg;
		$ps->actpg = $pg;
		$ps->query = "SELECT id FROM memht_faq WHERE argomento=$aid";
		$ps->url = "index.php?page=faq&op=arg&id=$aid&pg={{N}}";
		$ps->show();
	} else {
		echo "<div align='center' id='errorText'><b>"._NOFAQINTHISSECTION_."</b></div>";
	}
}

function faqList() {
	global $dblink;
	
	$n = 0;
	echo "<table width='100%' align='center' border='0' cellspacing='0' cellpadding='2'>";
	$result = $dblink->get_list("SELECT id,nome FROM memht_faq_categorie ORDER BY nome");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$nome = outCode($row['nome']);
				
		if ($dblink->get_num("SELECT id FROM memht_faq WHERE argomento=$id AND enabled=1")>0) {
			echo "<tr><th><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=faq&op=arg&id=$id&title=".mem_urlencode($nome)."' title='$nome'>$nome</a></th></tr>\n";
		}
		$n++;
	}
	if ($n<=0) {
		echo "<tr><td align='center' id='errorText'><b>"._NOARGUMENTS_."</b></td></tr>\n";
	}
	echo "</table>";
}
	
	openTable();
	
	switch($op) {
		case "read":
			readFaq($id);
		break;
		case "arg":
			viewArgument($id,$ofsbgn,$ofsppg,$pg);
		break;
				
		default:
			faqList();
		break;
	}
	
	closeTable();

?>