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

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0 ;

$whr = (isset($_POST['whr'])) ? intval($_POST['whr']) : 0 ;
$argument = (isset($_POST['argument'])) ? intval($_POST['argument']) : 0 ;
$author = (isset($_POST['author'])) ? inCode($_POST['author']) : "" ;
$rssurl = (isset($_POST['rssurl'])) ? inCode($_POST['rssurl']) : "" ;
$numfeeds = (isset($_POST['numfeeds'])) ? intval($_POST['numfeeds']) : 0 ;
$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;

$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;

function rssList() {
	global $dblink;
	
	echo "<table width='100%' border='0' cellspacing='1' cellpadding='0' align='center' class='std_nicetable'>\n";
	echo "<thead>\n";
	echo "<tr><td width='20%'><b>"._PAGE_."</b></td><td width='20%'><b>"._ARGUMENT_."</b></td><td width='20%'><b>"._AUTHOR_."</b></td><td width='25%'><b>"._URL_."</b></td><td width='15%' align='center' nowrap><b>"._NUMFEEDS_."</b></td><td width='1%'>&nbsp;</td></tr>";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT * FROM memht_aggregator ORDER BY id DESC")) {
		$n = 0;
		foreach ($result as $row) {
			$id = intval($row['id']);
			$whr = intval($row['whr']);
			$argument = intval($row['argument']);
			$author = outCode($row['author']);
			$rssurl = outCode($row['rssurl']);
			$numfeeds = intval($row['numfeeds']);
			$enabled = intval($row['enabled']);
			
			$info = ($enabled==1) ? " <img src='images/on.gif' alt='"._ON2_."' title='"._ON2_."'>" : " <img src='images/off.gif' alt='"._OFF_."' title='"._OFF_."'>" ;
			$arow = $dblink->get_row("SELECT nome FROM memht_argomenti WHERE id=$argument");
			$argname = outCode($arow['nome']);
			
			switch ($whr) {
				case 1: $whrpg = "articles"; break;
				case 2: $whrpg = "guide"; break;
				case 3: $whrpg = "news"; break;
			}
			$prow = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='$whrpg'");
			$whrname = outCode($prow['titolo']);
			
			if ($author=="") { $author = "-"; }
			
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
			echo "<tr>\n";
				echo "<td class='$class'>$whrname</td>\n";
				echo "<td class='$class'>$argname</td>\n";
				echo "<td class='$class'>$author</td>\n";
				echo "<td class='$class' nowrap><input type='text' value=\"$rssurl\" size='20' disabled> <a href=\"$rssurl\" title=\"$rssurl\" target='_blank'><img src='images/rss-syndicate_box.gif' alt='RSS' border='0' title=\"$rssurl\"></a></td>\n";
				echo "<td class='$class' align='center'>$numfeeds</td>\n";
				echo "<td class='$class' align='right' nowrap><a href='admin.php?page=aggregator&op=edit&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=aggregator&op=delete&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a> $info</td>\n";
			echo "</tr>\n";
		}
	} else {
		echo "<tr><td align='center' id='errorText' colspan='6' class='clean'><b>"._EMPTY_."</b></td></tr>";
	}
	echo "</tbody>\n";
	echo "</table>\n";
}

function addRss($whr,$argument,$author,$rssurl,$numfeeds,$enabled,$ok=false) {
	global $dblink;

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form' method='post' action='admin.php?page=aggregator&op=add&ok=true'>";
			
			$row = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='articles'"); $art_title = outCode($row['titolo']);
			$row = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='guide'"); $gui_title = outCode($row['titolo']);
			$row = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='news'"); $nws_title = outCode($row['titolo']);
			
			echo "<tr><td width='25%'><b>"._PAGE_."</b></td><td>\n";
				echo "<select name='whr'>\n";
					echo "<option value='1'>$art_title</option>\n";
					echo "<option value='2'>$gui_title</option>\n";
					echo "<option value='3'>$nws_title</option>\n";
				echo "</select></td></tr>\n";
			echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
				echo "<select name='argument'>\n";
					$result = $dblink->get_list("SELECT id,nome FROM memht_argomenti ORDER BY nome");
					foreach ($result as $row) {
						$aid = intval($row['id']);
						$name = outCode($row['nome']);

						echo "<option value='$aid'>$name</option>\n";
					}
				echo "</select></td></tr>\n";
			echo "<tr><td><b>"._AUTHOR_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._INFO_EXPLAIN_AGGREGATOR_AUTHOR_."]\"></td><td><input type='text' name='author' size='30' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._RSS_FEEDS_."</b> ("._URL_.")</td><td><input type='text' name='rssurl' size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._NUMFEEDS_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._INFO_EXPLAIN_AGGREGATOR_NUMFEEDS_."]\"></td><td><input type='text' name='numfeeds' size='10' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='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_."'></td></tr>\n";

		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($rssurl=="") { $save = false; $msg = _URL_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		if ($numfeeds==0) { $numfeeds = 10; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_aggregator (id,whr,argument,author,rssurl,numfeeds,enabled)
							VALUES (null,'$whr','$argument','$author','$rssurl','$numfeeds','$enabled')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=aggregator'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function editRss($id,$whr,$argument,$author,$rssurl,$numfeeds,$enabled,$ok=false) {
	global $dblink;
	
	$id = intval($id);

	if (!$ok) {
		$row = $dblink->get_row("SELECT * FROM memht_aggregator WHERE id=$id");
		$whr = intval($row['whr']);
		$argument = intval($row['argument']);
		$author = outCode($row['author']);
		$rssurl = outCode($row['rssurl']);
		$numfeeds = intval($row['numfeeds']);
		$enabled = intval($row['enabled']);
		
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form' method='post' action='admin.php?page=aggregator&op=edit&id=$id&ok=true'>";
			
			$row = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='articles'"); $art_title = outCode($row['titolo']);
			$row = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='guide'"); $gui_title = outCode($row['titolo']);
			$row = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='news'"); $nws_title = outCode($row['titolo']);
			
			echo "<tr><td width='25%'><b>"._PAGE_."</b></td><td>\n";
				echo "<select name='whr'>\n";
					$selected = ($whr==1) ? "selected" : "" ;
					echo "<option value='1' $selected>$art_title</option>\n";
					$selected = ($whr==2) ? "selected" : "" ;
					echo "<option value='2' $selected>$gui_title</option>\n";
					$selected = ($whr==3) ? "selected" : "" ;
					echo "<option value='3' $selected>$nws_title</option>\n";
				echo "</select></td></tr>\n";
			echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
				echo "<select name='argument'>\n";
					$result = $dblink->get_list("SELECT id,nome FROM memht_argomenti ORDER BY nome");
					foreach ($result as $row) {
						$aid = intval($row['id']);
						$name = outCode($row['nome']);
						
						$selected = ($argument==$aid) ? "selected" : "" ;
						echo "<option value='$aid' $selected>$name</option>\n";
					}
				echo "</select></td></tr>\n";
			echo "<tr><td><b>"._AUTHOR_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._INFO_EXPLAIN_AGGREGATOR_AUTHOR_."]\"></td><td><input type='text' name='author' value=\"$author\" size='30' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._RSS_FEEDS_."</b> ("._URL_.")</td><td><input type='text' name='rssurl' value=\"$rssurl\" size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._NUMFEEDS_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._INFO_EXPLAIN_AGGREGATOR_NUMFEEDS_."]\"></td><td><input type='text' name='numfeeds' value=\"$numfeeds\" size='10' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='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 ($rssurl=="") { $save = false; $msg = _URL_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		if ($numfeeds==0) { $numfeeds = 10; }
	
		if ($save) {
			$dblink->query("UPDATE memht_aggregator SET whr='$whr',argument='$argument',author='$author',rssurl='$rssurl',numfeeds='$numfeeds',enabled='$enabled' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=aggregator'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function deleteRss($id,$ok=false) {
	global $dblink;
	if ($ok) {
		$dblink->query("DELETE FROM memht_aggregator WHERE id=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=aggregator'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=aggregator&op=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=aggregator' 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=aggregator' title='"._LIST_."'><img src='admin/icons/rss.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=aggregator&op=add' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
			echo " - <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._INFO_EXPLAIN_AGGREGATOR_."]\">";
		echo "</div>";
	closeTable();
	
	openTable();
		switch($op) {
			case "add":
				addRss($whr,$argument,$author,$rssurl,$numfeeds,$enabled,$ok);
			break;
			case "edit":
				editRss($id,$whr,$argument,$author,$rssurl,$numfeeds,$enabled,$ok);
			break;
			case "delete":
				deleteRss($id,$ok);
			break;
			default:
				rssList();
			break;
		}
	closeTable();
require_once("admin/inc/inc_footer.php");

?>