<?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 ;
$decision = (isset($_GET['decision'])) ? intval($_GET['decision']) : 0 ;

function commentsList() {
	global $dblink,$siteConfig;
	
	echo "<table width='100%' border='0' cellspacing='1' cellpadding='0' align='center' class='std_nicetable'>\n";
	echo "<thead>\n";
	echo "<tr><td colspan='7'>"._COMMENTSWAITINGMODERATION_."</td></tr>";
	echo "<tr><td>"._NAME_."</td><td>"._EMAIL_."</td><td>"._DATE_."</td><td>"._PAGE_."</td><td>"._LEVEL_."</td><td width='1%'>&nbsp;</td><td width='1%'>&nbsp;</td></tr>";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as dateout FROM memht_comments WHERE moderate=1 AND whr<8 ORDER BY date")) {
		$n = 0;
		foreach ($result as $row) {
			$id = intval($row['id']);
			$whr = intval($row['whr']);
			$wid = intval($row['wid']);
			$name = outCode($row['name']);
			$email = outCode($row['email']);
			$text = censure(outCode($row['text'],0));
			$guest = intval($row['guest']);
			$date = outCode($row['dateout']);
			$ip = outCode($row['ip']);
						
			switch ($whr) {
				case 1:
					$whrpg = "articles";
					$irow = $dblink->get_row("SELECT nome FROM memht_articoli WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=articles&op=readArticle&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 2:
					$whrpg = "guide";
					$irow = $dblink->get_row("SELECT nome FROM memht_guide WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=guide&op=readGuide&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 3:
					$whrpg = "news";
					$irow = $dblink->get_row("SELECT nome FROM memht_news WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=news&op=readNews&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 4:
					$whrpg = "download";
					$irow = $dblink->get_row("SELECT nome FROM memht_download WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=download&op=getFile&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 5:
					$whrpg = "blog";
					$row = $dblink->get_row("SELECT title FROM memht_blog_posts WHERE id=$wid");
					$whritem  = outCode($row['title']);
					$whrlink = "index.php?page=blog&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 6:
					$whrpg = "surveys";
					$irow = $dblink->get_row("SELECT question FROM memht_survey WHERE id=$wid");
					$whritem = trimString(outCode($irow['question']),40,1);
					$whrlink = "index.php?page=surveys&op=result&id=$wid#comments";
				break;
			}
			$prow = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='$whrpg'");
			$whrname = outCode($prow['titolo']);
						
			$guest = ($guest==1) ? _GUEST_ : _USER_ ;
						
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			echo "<tr>";
			
			$text = str_replace("[","{",$text);
			$text = str_replace("]","}",$text);
						
			echo "<td class='$class' width='20%'><b>$name</b></td>";
			echo "<td class='$class' width='25%'>$email</td>";
			echo "<td class='$class' width='20%'>$date</td>";
			echo "<td class='$class' width='15%'>$whrname <a href='$whrlink' title=\"header=[$whrname] body=[$whritem]\"><img src='images/info.gif' alt='Info' border='0'></a></td>";
			echo "<td class='$class' width='10%'>$guest</td>";
			echo "<td class='$class' width='1%'><img src='images/documents.gif' alt='Documents' border='0' title=\"header=["._COMMENT_."] body=[$text]\"></td>";
			echo "<td class='$class' width='1%'>";
				echo "<select onChange='location = this.options[this.selectedIndex].value;' style='font-size:9px; border:0;' class='$class'>";
					echo "<option value='' selected>-- "._APPROVE_." --</option>\n";
					echo "<option value='admin.php?page=comments&op=approvation&id=$id&decision=1' style='font-weight:bold; color:green;'>"._YES_."</option>\n";
					echo "<option value='admin.php?page=comments&op=approvation&id=$id&decision=0' style='font-weight:bold; color:red;'>"._NO_."</option>\n";
				echo "</select>\n";
			echo "</td>";
	
			echo "</tr>\n";
		}
	} else {
		echo "<tr><td align='center' id='errorText' colspan='7' class='clean'><b>"._EMPTY_."</b></td></tr>";
	}
	echo "</tbody>\n";
	echo "</table>\n";
	echo "<br>";
	
	echo "<table width='100%' border='0' cellspacing='1' cellpadding='0' align='center' class='std_nicetable'>\n";
	echo "<thead>\n";
	echo "<tr><td colspan='6'><b>"._LAST_." 30</b></td></tr>";
	echo "<tr><td>"._NAME_."</td><td>"._EMAIL_."</td><td>"._DATE_."</td><td>"._PAGE_."</td><td>"._LEVEL_."</td><td width='1%'>&nbsp;</td></tr>";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(date, '".$siteConfig['timestamp']."') as dateout FROM memht_comments WHERE moderate=0 AND whr<8 ORDER BY date DESC LIMIT 30")) {
		$n = 0;
		foreach ($result as $row) {
			$id = intval($row['id']);
			$whr = intval($row['whr']);
			$wid = intval($row['wid']);
			$name = outCode($row['name']);
			$email = outCode($row['email']);
			$text = censure(outCode($row['text'],0));
			$guest = intval($row['guest']);
			$date = outCode($row['dateout']);
			$ip = outCode($row['ip']);
						
			switch ($whr) {
				case 1:
					$whrpg = "articles";
					$irow = $dblink->get_row("SELECT nome FROM memht_articoli WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=articles&op=readArticle&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 2:
					$whrpg = "guide";
					$irow = $dblink->get_row("SELECT nome FROM memht_guide WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=guide&op=readGuide&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 3:
					$whrpg = "news";
					$irow = $dblink->get_row("SELECT nome FROM memht_news WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=news&op=readNews&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 4:
					$whrpg = "download";
					$irow = $dblink->get_row("SELECT nome FROM memht_download WHERE id=$wid");
					$whritem = outCode($irow['nome']);
					$whrlink = "index.php?page=download&op=getFile&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 5:
					$whrpg = "blog";
					$row = $dblink->get_row("SELECT title FROM memht_blog_posts WHERE id=$wid");
					$whritem  = outCode($row['title']);
					$whrlink = "index.php?page=blog&id=$wid&title=".mem_urlencode($whritem);
				break;
				case 6:
					$whrpg = "surveys";
					$irow = $dblink->get_row("SELECT question FROM memht_survey WHERE id=$wid");
					$whritem = trimString(outCode($irow['question']),40,1);
					$whrlink = "index.php?page=surveys&op=result&id=$wid#comments";
				break;
			}
			$prow = $dblink->get_row("SELECT titolo FROM memht_pagine WHERE nome='$whrpg'");
			$whrname = outCode($prow['titolo']);
						
			$guest = ($guest==1) ? _GUEST_ : _USER_ ;
						
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			echo "<tr>";
						
			echo "<td class='$class' width='20%'><b>$name</b></td>";
			echo "<td class='$class' width='25%'>$email</td>";
			echo "<td class='$class' width='20%'>$date</td>";
			echo "<td class='$class' width='15%'>$whrname <a href='$whrlink' title=\"header=[$whrname] body=[$whritem]\"><img src='images/info.gif' alt='Info' border='0'></a></td>";
			echo "<td class='$class' width='10%'>$guest</td>";
			$text = str_replace("[","{",$text);
			$text = str_replace("]","}",$text);
			echo "<td class='$class' width='1%'><img src='images/documents.gif' alt='Documents' border='0' title=\"header=["._COMMENT_."] body=[$text]\"></td>";
	
			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 approveWaitingComments($id,$decision) {
	global $dblink;
	$id = intval($id);
	
	if ($dblink->get_num("SELECT id FROM memht_comments WHERE id=$id AND moderate=1")>0) {
		if ($decision==1) {
			$dblink->query("UPDATE memht_comments SET moderate=0 WHERE id=$id AND moderate=1");
			echo "<div align='center'><b>"._APPROVED_."</b></div>";
		} else {
			$dblink->query("DELETE FROM memht_comments WHERE id=$id AND moderate=1");
			echo "<div align='center'><b>"._DELETED_."</b></div>";
		}
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=comments'>";
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
									
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=comments' title='"._LIST_."'><img src='admin/icons/messages.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {			
			case "approvation":
				approveWaitingComments($id,$decision);
			break;
		
			case "showComments":
			default:
				commentsList();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>