<?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['nome'])) { $nome = incode($_POST['nome']); } else { $nome = ""; }
if (isset($_POST['img'])) { $img = incode($_POST['img']); } else { $img = ""; }

function argList() {
	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";
		if ($result = $dblink->get_list("SELECT * FROM memht_argomenti ORDER BY nome")) {
			foreach ($result as $row) {
				$id = intval($row['id']);
				$nome = outCode($row['nome']);
				
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
		
				echo "<tr><td class='$class'><b>$nome</b></td><td class='$class' nowrap><div align='right'><a href='admin.php?page=arguments&op=editArgument&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=arguments&op=deleteArgument&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></div></td></tr>\n";		
			}
		} else {
			echo "<tr><td align='center' id='errorText' class='clean'><b>"._NOARGUMENTS_."</b></td></tr>";
		}
	echo "</tbody>\n";
	echo "</table>";
}	

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

function edit($id,$nome,$img,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	
	$row_edit = $dblink->get_row("SELECT * FROM memht_argomenti WHERE id='$id'");
	$aid = intval($row_edit['id']);
	$anome = outCode($row_edit['nome']);
	$aimg = outCode($row_edit['img']);

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>\n";
		echo "<form name='form_arg' method='post' action='admin.php?page=arguments&op=editArgument&id=$aid&ok=true'>";
		
			echo "<tr><td width='25%'><b>"._ARGNAME_."</b><td><input type='text' name='nome' size='40' maxlength='255' value=\"$anome\">\n";
			echo "<tr><td><b>"._TOPICIMAGE_."</b><td><input type='text' name='img' size='40' maxlength='255' value=\"$aimg\">\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";

		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($nome=="") { $save = false; }
		if ($img=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_argomenti SET nome='$nome',img='$img' WHERE id='$id'");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=arguments'>";
		} 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_argomenti WHERE id='$id'");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=arguments'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEARGUMENT_."</b><br><a href='admin.php?page=arguments&op=deleteArgument&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=arguments' 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=arguments' title='"._LIST_."'><img src='admin/icons/list.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=arguments&op=addArgument' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();

		switch($op) {
			case "addArgument":
				add($nome,$img,$ok);
			break;
			
			case "deleteArgument":
				delete($id,$ok);
			break;
			
			case "editArgument":
				edit($id,$nome,$img,$ok);
			break;
		
			default:
				argList();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>