<?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['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; }
if (isSet($_POST['p_argomento'])) { $p_argomento = inCode($_POST['p_argomento']); } else { $p_argomento = ""; }
if (isSet($_POST['p_domanda'])) { $p_domanda = inCode($_POST['p_domanda']); } else { $p_domanda = ""; }
if (isSet($_POST['p_risposta'])) { $p_risposta = inCode($_POST['p_risposta']); } else { $p_risposta = ""; }
if (isSet($_POST['p_enabled'])) { $p_enabled = inCode($_POST['p_enabled']); } else { $p_enabled = ""; }
if (isSet($_POST['nome'])) { $nome = eregi_replace("[/_']","-",inCode($_POST['nome'])); } else { $nome = ""; }

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

function faqList($ofsbgn,$ofsppg,$pg) {
	global $dblink;
	
	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>"._QUESTION_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result_faq = $dblink->get_list("SELECT * FROM memht_faq ORDER BY domanda LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result_faq as $row_faq) {
			$id = intval($row_faq['id']);
			$domanda = outCode($row_faq['domanda']);
			$enabled = intval($row_faq['enabled']);
			
			$off = ($enabled!="1") ? "<img src='images/off.gif' alt='"._OFF_."' title='"._OFF_."'>" : "" ;
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
			echo "<tr><td class='$class'><a href='admin.php?page=faq&amp;op=readFaq&amp;id=$id'><b>$domanda</b></a></td><td class='$class' nowrap><a href='admin.php?page=faq&op=editFaq&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=faq&op=deleteFaq&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a> $off</td></tr>\n";
		}
	} else {
		echo "<tr><td align='center' id='errorText' class='clean' colspan='2'><b>"._NOFAQ_."</b></td></tr>";
	}
	echo "</tbody>\n";
	echo "</table>";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_faq";
	$ps->url = "admin.php?page=faq&op=showFaq&pg={{N}}";
	$ps->show();
}	

function view($id) {
	global $dblink;

	$row_faq = $dblink->get_row("SELECT * FROM memht_faq WHERE id='$id'");
	$id = intval($row_faq['id']);
	$argomento = intval($row_faq['argomento']);
	$domanda = outCode($row_faq['domanda']);
	$risposta = outCode($row_faq['risposta']);
	$enabled = intval($row_faq['enabled']);
		
	$off = ($enabled!="1") ? "<img src='images/off.gif' alt='"._OFF_."' title='"._OFF_."'>" : "" ;
	
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
		echo "<tr><td>$domanda</td><td width='1%' nowrap><a href='admin.php?page=faq&op=editFaq&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=faq&op=deleteFaq&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a> $off</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		echo "<tr><td colspan='2' class='clean'>$risposta</td></tr>";
	echo "</tbody>\n";
	echo "</table>\n";
}

function add($p_argomento,$p_domanda,$p_risposta,$p_enabled,$ok=false) {
	global $dblink;
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_faq' method='post' action='admin.php?page=faq&op=addFaq&ok=true'>";
			echo "<tr><td><b>"._ARGUMENT_."</b></td><td>";
			echo "<select name='p_argomento'>\n";
			//Lista argomenti
			$result = $dblink->get_list("SELECT * FROM memht_faq_categorie ORDER BY nome");
			foreach ($result as $row) {
				$aid = intval($row['id']);
				$nome = outCode($row['nome']);
					echo "<option value='$aid'>$nome</option>\n";
			}
			echo "</select></td></tr>\n";
			echo "<tr><td><b>"._QUESTION_."</b></td><td><input type='text' name='p_domanda' size='60' maxlength='255'></td></tr>\n";
			
			echo "<tr><td valign='top'><b>"._ANSWER_."</b></td><td>";
				
				textarea("p_risposta","100%","400px",1,"fulladmin");
				
			echo "</td></tr>\n";
				
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='p_enabled'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
				echo "</select></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($p_domanda=="") { $save = false; }
		if ($p_risposta=="") { $save = false; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_faq VALUES (null,'$p_argomento','$p_domanda','$p_risposta','$p_enabled')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=faq'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function edit($id,$p_argomento,$p_domanda,$p_risposta,$p_enabled,$ok=false) {
	global $dblink;
	
	$eid = intval($id);
	
	$row_faq = $dblink->get_row("SELECT * FROM memht_faq WHERE id='$eid'");
	$id = intval($row_faq['id']);
	$argomento = intval($row_faq['argomento']);
	$domanda = outCode($row_faq['domanda']);
	$risposta = outCode($row_faq['risposta']);
	$enabled = intval($row_faq['enabled']);

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_faq' method='post' action='admin.php?page=faq&op=editFaq&id=$id&ok=true'>";
			echo "<tr><td><b>"._ARGUMENT_."</b></td><td>";
			echo "<select name='p_argomento'>\n";
			//Lista argomenti
			$result = $dblink->get_list("SELECT * FROM memht_faq_categorie ORDER BY nome");
			foreach ($result as $row) {
				$aid = intval($row['id']);
				$nome = $row['nome'];
				echo ($argomento==$aid) ? "<option value='$aid' selected>$nome</option>\n" : "<option value='$aid'>$nome</option>\n" ;
			}
			echo "</select></td></tr>\n";
			echo "<tr><td><b>"._QUESTION_."</b></td><td><input type='text' name='p_domanda' value=\"$domanda\" size='60' maxlength='255'></td></tr>\n";
				
			echo "<tr><td valign='top'><b>"._ANSWER_."</b></td><td>";
				textarea("p_risposta","100%","400px",1,"fulladmin",$risposta);
			echo "</td></tr>\n";
				
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='p_enabled'>\n";
			if ($enabled==1) {
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			} else {
				echo "<option value='1'>"._YES_."</option>\n";
				echo "<option value='0' selected>"._NO_."</option>\n";
			}
				echo "</select></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'></td></tr>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($p_domanda=="") { $save = false; }
		if ($p_risposta=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_faq SET argomento='$p_argomento', domanda='$p_domanda', risposta='$p_risposta', enabled='$p_enabled' WHERE id='$eid'");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=faq'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function delete($id,$ok=false) {
	global $dblink;
	
	if ($ok) {
		$dblink->query("DELETE FROM memht_faq WHERE id='$id'");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=faq'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEFAQ_."</b><br><a href='admin.php?page=faq&op=deleteFaq&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=faq' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function showCat() {
	global $dblink;

	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>"._NAME_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	$result = $dblink->get_list("SELECT * FROM memht_faq_categorie");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$nome = outCode($row['nome']);
		
		$class = (($n++%2)!=0) ? "hlight" : "clean" ;
		
		echo "<tr><td class='$class'><a href='index.php?page=faq&op=arg&id=$id&title=".mem_urlencode($nome)."' title='$nome'><b>$nome</b></a></td><td width='1%' class='$class' nowrap><a href='admin.php?page=faq&op=editCategory&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=faq&op=deleteCategory&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
	}
	if ($n<=0) {
		echo "<div align='center' id='errorText' colspan='2' class='clean'><b>"._NOCAT_."</b></div>";
	}
	echo "</thead>\n";
	echo "</table>";
}

function addCat($id,$nome,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_cat2' method='post' action='admin.php?page=faq&op=addCategory&ok=true'>";
			echo "<tr><td><b>"._CATEGORYNAME_."</b><td><input type='text' name='nome' size='40' maxlength='255'>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome=="") { $save = false; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_faq_categorie VALUES (null,'$nome')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=faq&op=showCategory'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function editCat($id,$nome,$ok=false) {
	global $dblink;
	
	$id = intval($id);

	$row_faqc = $dblink->get_row("SELECT * FROM memht_faq_categorie WHERE id='$id'");
	$nomedb = outCode($row_faqc['nome']);
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_cat2' method='post' action='admin.php?page=faq&op=editCategory&id=$id&ok=true'>";
			echo "<tr><td><b>"._CATEGORYNAME_."</b><td><input type='text' name='nome' size='40' maxlength='255' value=\"$nomedb\">\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_faq_categorie SET nome='$nome' WHERE id='$id'");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=faq&op=showCategory'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function deleteCat($id,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	if ($ok) {
		$dblink->query("DELETE FROM memht_faq_categorie WHERE id='$id'");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=faq&op=showCategory'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETECATEGORY_."</b><br><a href='admin.php?page=faq&op=deleteCategory&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=faq&op=showCategory' title='"._NO_."'>"._NO_."</a></div>";
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
					
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=faq' title='"._LIST_."'><img src='admin/icons/list.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=faq&op=addFaq' title='"._ADD_FAQ_."'><img src='admin/icons/add.png' border='0' title='"._ADD_FAQ_."' align='top'> "._ADD_FAQ_."</a>";
			echo " - <a href='admin.php?page=faq&op=showCategory' title='"._SHOW_CATEGORIES_."'><img src='admin/icons/category.png' border='0' title='"._SHOW_CATEGORIES_."' align='top'> "._SHOW_CATEGORIES_."</a>";
			echo " - <a href='admin.php?page=faq&op=addCategory' title='"._ADD_CATEGORY_."'><img src='admin/icons/addcategory.png' border='0' title='"._ADD_CATEGORY_."' align='top'> "._ADD_CATEGORY_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "addFaq":
				add($p_argomento,$p_domanda,$p_risposta,$p_enabled,$ok);
			break;
			
			case "readFaq":
				view($id);
			break;
			
			case "deleteFaq":
				delete($id,$ok);
			break;
			
			case "editFaq":
				edit($id,$p_argomento,$p_domanda,$p_risposta,$p_enabled,$ok);
			break;
			
			case "showCategory":
				showCat();
			break;
			
			case "addCategory":
				addCat($id,$nome,$ok);
			break;
			
			case "editCategory":
				editCat($id,$nome,$ok);
			break;
			
			case "deleteCategory":
				deleteCat($id,$ok);
			break;
		
			case "showFaq":		
			default:
				faqList($ofsbgn,$ofsppg,$pg);
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>