<?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_sito'])) { $nome = inCode($_POST['nome_sito']); } else { $nome = ""; }
if (isSet($_POST['url_sito'])) { $url = inCode($_POST['url_sito']); } else { $url = ""; }
if (isSet($_POST['email'])) { $email = inCode($_POST['email']); } else { $email = ""; }

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

function partList($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 width='40%'>"._SITENAME_."</td><td width='30%'>"._SITEURL_."</td><td width='30%'>"._EMAIL_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT * FROM memht_partners ORDER BY nome_sito LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result as $row) {
			$id = intval($row['id']);
			$nome_sito = outCode($row['nome_sito']);
			$url_sito = outCode($row['url_sito']);
			$email_sito = outCode($row['email']);
			
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			echo "<tr><td class='$class'><a href='index.php?page=partners&op=openPartnerLink&id=$id' title='$nome_sito' target='_blank'><b>$nome_sito</b></a></td><td class='$class'>$url_sito</td><td class='$class'>$email_sito</td><td class='$class' nowrap><a href='admin.php?page=partners&op=editPartner&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=partners&op=deletePartner&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
		}
	} else {
		echo "<tr><td colspan='4' align='center' id='errorText' class='clean'><b>"._NOPARTNERS_."</b></td></tr>\n";
	}
	echo "</tbody>\n";
	echo "</table>\n";
	
	//Pages
	include_once("inc/class/paginationSystem.class.php");
	$ps = new paginationSystem();
	$ps->items = $ofsppg;
	$ps->actpg = $pg;
	$ps->query = "SELECT id FROM memht_partners";
	$ps->url = "admin.php?page=partners&op=showPartners&pg={{N}}";
	$ps->show();
}	

function add($nome,$url,$email,$ok=false) {
	global $dblink;
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_part' method='post' action='admin.php?page=partners&op=addPartner&ok=true'>";
			echo "<tr><td><b>"._SITENAME_."</b></td><td><input type='text' name='nome_sito' size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._SITEURL_."</b></td><td><input type='text' name='url_sito' size='40' maxlength='255' value='http://'></td></tr>\n";
			echo "<tr><td><b>"._EMAIL_."</b></td><td><input type='text' name='email' size='40' maxlength='255'></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 ($nome=="") { $save = false; }
		if ($url=="") { $save = false; }
		if ($email=="" || !validEmail($email)) { $save = false; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_partners VALUES (null,'$nome','$url','$email')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=partners'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function edit($id,$nome,$url,$email,$ok=false) {
	global $dblink;

	if (!$ok) {
		$row_edit = $dblink->get_row("SELECT * FROM memht_partners WHERE id=$id");
		$pid = intval($row_edit['id']);
		$pnome_sito = outCode($row_edit['nome_sito']);
		$purl_sito = outCode($row_edit['url_sito']);
		$pemail_sito = outCode($row_edit['email']);
		
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_part' method='post' action='admin.php?page=partners&op=editPartner&id=$pid&ok=true'>";
			echo "<tr><td><b>"._SITENAME_."</b></td><td><input type='text' name='nome_sito' size='40' maxlength='255' value=\"$pnome_sito\"></td></tr>\n";
			echo "<tr><td><b>"._SITEURL_."</b></td><td><input type='text' name='url_sito' size='40' maxlength='255' value=\"$purl_sito\"></td></tr>\n";
			echo "<tr><td><b>"._EMAIL_."</b></td><td><input type='text' name='email' size='40' maxlength='255' value=\"$pemail_sito\"></td></tr>\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 ($url=="") { $save = false; }
		if ($email=="" || !validEmail($email)) { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_partners SET nome_sito='$nome',url_sito='$url',email='$email' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=partners'>";
		} 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_partners WHERE id=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=partners'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEPARTNER_."</b><br><a href='admin.php?page=partners&op=deletePartner&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=partners' 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=partners' title='"._LIST_."'><img src='admin/icons/list.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=partners&op=addPartner' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "addPartner":
				add($nome,$url,$email,$ok);
			break;
			
			case "deletePartner":
				delete($id,$ok);
			break;
			
			case "editPartner":
				edit($id,$nome,$url,$email,$ok);
			break;
		
			case "showPartners":
			default:
				partList($ofsbgn,$ofsppg,$pg);
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>