<?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['argomento'])) { $argomento = inCode($_POST['argomento']); } else { $argomento = ""; }
if (isset($_POST['nome'])) { $nome = eregi_replace("[/_']","-",inCode($_POST['nome'])); } else { $nome = ""; }
if (isset($_POST['testo_home'])) { $testo_home = inCode($_POST['testo_home']); } else { $testo_home = ""; }
if (isset($_POST['testo'])) { $testo = inCode($_POST['testo']); } else { $testo = ""; }
if (isset($_POST['tags'])) { $tags = inCode($_POST['tags']); } else { $tags = ""; }
if (isset($_POST['enabled'])) { $enabled = inCode($_POST['enabled']); } else { $enabled = ""; }
$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
$language = (isset($_POST['language'])) ? intval($_POST['language']) : 0 ;

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

function newsList($ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;

	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>"._TITLE_."<td width='25%'>"._TAGS_."</td><td width='15%'>"._LANGUAGE_."</td><td width='15%'>"._AUTHOR_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	if ($result = $dblink->get_list("SELECT n.*,DATE_FORMAT(n.data, '".$siteConfig['timestamp']."') as data,l.language AS languagetxt FROM memht_news AS n LEFT JOIN memht_content_languages AS l ON n.language = l.id ORDER BY n.id DESC LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result as $row) {
			$id = intval($row['id']);
			$argomento = intval($row['argomento']);
			$nome = outCode($row['nome']);
			$autore = outCode($row['autore']);
			$data = $row['data'];
			$hits = intval($row['hits']);
			$language = outCode($row['languagetxt']);
			$enabled = intval($row['enabled']);
			
			if ($language=="") { $language = _GLOBAL_; }
			
			$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='index.php?page=news&op=readNews&id=$id&title=".mem_urlencode($nome)."' title='$nome'><b>$nome</b></a><td class='$class' id='info'>";
			if ($result = $dblink->get_list("SELECT tag FROM memht_tags WHERE whr=3 AND cid=$id ORDER BY tag")) {
				foreach ($result as $row) {
					echo "<span style='padding: 0 4px;'><a href=\"index.php?page=tags&op=list&tag=".mem_urlencode(outCode($row['tag']))."\" title=\"".outCode($row['tag'])."\" target='_blank'>".outCode($row['tag'])."</a></span>";
				}
			}
			echo "</td><td class='$class'>$language</td><td class='$class'>$autore</td><td class='$class' nowrap><a href='admin.php?page=news&op=editNews&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=news&op=deleteNews&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' colspan='5' class='clean'><b>"._EMPTY_."</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_news";
	$ps->url = "admin.php?page=news&op=showNews&pg={{N}}";
	$ps->show();
}	

function add($argomento,$nome,$testo_home,$testo,$tags,$usecomments,$language,$enabled,$ok=false) {
	global $dblink,$userInfo,$tzNOW;

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_news' method='post' action='admin.php?page=news&op=addNews&ok=true'>";
		
			echo "<tr><td width='25%'><b>"._TITLE_."</b></td><td><input type='text' name='nome' size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
				echo "<select name='argomento'>\n";
					//Lista argomenti
					$result = $dblink->get_list("SELECT * FROM memht_argomenti 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 valign='top'><b>"._HOMETEXT_."</b></td><td>";
				
				textarea("testo_home","100%","400px",1,"fulladmin");
				
				echo "</td></tr>\n";
			echo "<tr><td valign='top'><b>"._TEXT_."</b></td><td>";
				
				textarea("testo","100%","600px",1,"fulladmin");
				
				echo "</td></tr>\n";
			echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
			echo "<tr><td><b>"._LANGUAGE_."</b></td><td>\n";
				echo "<select name='language'>\n";
					echo "<option value='0'>"._GLOBAL_."</option>\n";
					$result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language");
					foreach ($result as $row) {
						$lid = intval($row['id']);
						$llanguage = outCode($row['language']);
						$default = intval($row['default']);
						
						$selected = ($default==1) ? " selected" : "" ;
						echo "<option value='$lid'{$selected}>$llanguage</option>\n";
					}
				echo "</select>\n";
			echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
			echo "<select name='usecomments'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></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 ($nome=="") { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		if ($testo_home=="") { $save = false; $msg = _HOMETEXT_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
	
		if ($save) {
			if (memRunHooks('AddNews',array($nome,$argomento,$testo_home,$testo,$userInfo['user'],$userInfo['email'],$language))) {
				$dblink->query("INSERT INTO memht_news (id,argomento,nome,testo_home,testo,autore,email,data,usecomments,language,enabled)
								VALUES (null,'$argomento','$nome','$testo_home','$testo','".$userInfo['user']."','".$userInfo['email']."',$tzNOW,'$usecomments','$language','$enabled')");
				if ($tags!="") {
					$row = $dblink->get_row("SELECT id FROM memht_news ORDER BY id DESC LIMIT 1");
					$lastid = intval($row['id']);
					
					$tags = explode(",",$tags);
					foreach ($tags as $tag) {
						$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$lastid','3')");
					}
				}
				
				memRunHooks('AddNewsEnd',array($nome,$argomento,$testo_home,$testo,$userInfo['user'],$userInfo['email'],$language));
			}
			
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=news'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function edit($argomento,$nome,$testo_home,$testo,$tags,$usecomments,$language,$enabled,$ok=false,$id) {
	global $dblink;
	
	$id = intval($id);

	if (!$ok) {
		$row_edit = $dblink->get_row("SELECT * FROM memht_news WHERE id=$id");
		$nargomento = intval($row_edit['argomento']);
		$nnome = outCode($row_edit['nome']);
		$ntesto_home = outCode($row_edit['testo_home']);
		$ntesto = outCode($row_edit['testo']);
		$nlanguage = intval($row_edit['language']);
		$usecomments = intval($row_edit['usecomments']);
		$nenabled = intval($row_edit['enabled']);
		
		if ($result = $dblink->get_list("SELECT tag FROM memht_tags WHERE whr=3 AND cid=$id")) {
			$ntags = array();
			foreach ($result as $row) {
				$ntags[] = outCode($row['tag']);
			}
			$ntags = implode(",",$ntags);
		} else {
			$ntags = "";
		}
		
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_news' method='post' action='admin.php?page=news&op=editNews&id=$id&ok=true'>";
		
			echo "<tr><td width='25%'><b>"._TITLE_."</b></td><td><input type='text' name='nome' size='40' maxlength='255' value=\"$nnome\"></td></tr>\n";
			echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
				echo "<select name='argomento'>\n";
					//Lista argomenti
					$result = $dblink->get_list("SELECT * FROM memht_argomenti ORDER BY nome");
					foreach ($result as $row) {
						$aid = intval($row['id']);
						$nome = outCode($row['nome']);
						if ($nargomento==$aid) {
							echo "<option value='$aid' selected>$nome</option>\n";
						} else {
							echo "<option value='$aid'>$nome</option>\n";
						}
					}
				echo "</select></td></tr>\n";
			echo "<tr><td valign='top'><b>"._HOMETEXT_."</b></td><td>";
				
				textarea("testo_home","100%","400px",1,"fulladmin",$ntesto_home);
				
			echo "</td></tr>\n";
			echo "<tr><td valign='top'><b>"._TEXT_."</b></td><td>";
				
				textarea("testo","100%","600px",1,"fulladmin",$ntesto);
				
			echo "</td></tr>\n";
			echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' value=\"$ntags\" size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
			echo "<tr><td><b>"._LANGUAGE_."</b></td><td>\n";
				echo "<select name='language'>\n";
					$selected = ($nlanguage==0) ? " selected" : "" ;
					echo "<option value='0'{$selected}>"._GLOBAL_."</option>\n";
					$result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language");
					foreach ($result as $row) {
						$lid = intval($row['id']);
						$llanguage = outCode($row['language']);
						
						$selected = ($nlanguage==$lid) ? " selected" : "" ;
						echo "<option value='$lid'{$selected}>$llanguage</option>\n";
					}
				echo "</select>\n";
			echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
			echo "<select name='usecomments'>\n";
				if ($usecomments==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><b>"._ENABLED_."</b></td><td>";
			echo "<select name='enabled'>\n";
			if ($nenabled==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 ($nome=="") { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		if ($testo_home=="") { $save = false; $msg = _HOMETEXT_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
	
		if ($save) {
			$dblink->query("UPDATE memht_news SET argomento='$argomento',nome='$nome',testo_home='$testo_home',testo='$testo',language='$language',usecomments='$usecomments',enabled='$enabled' WHERE id=$id");
			
			$dblink->query("DELETE FROM memht_tags WHERE whr=3 AND cid=$id");
			if ($tags!="") {	
				$tags = explode(",",$tags);
				foreach ($tags as $tag) {
					$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$id','3')");
				}
			}
			
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=news'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function delete($id,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	if ($ok) {
		$dblink->query("DELETE FROM memht_news WHERE id=$id");
		$dblink->query("DELETE FROM memht_comments WHERE whr=3 AND wid=$id");
		$dblink->query("DELETE FROM memht_ratings WHERE whr=3 AND wid=$id");
		$dblink->query("DELETE FROM memht_tags WHERE whr=3 AND cid=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=news'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETENEWS_."</b><br><a href='admin.php?page=news&op=deleteNews&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=news' 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=news' title='"._LIST_."'><img src='admin/icons/messages.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=news&op=addNews' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "addNews":
				add($argomento,$nome,$testo_home,$testo,$tags,$usecomments,$language,$enabled,$ok);
			break;
			
			case "deleteNews":
				delete($id,$ok);
			break;
			
			case "editNews":
				edit($argomento,$nome,$testo_home,$testo,$tags,$usecomments,$language,$enabled,$ok,$id);
			break;
		
			case "showNews":
			default:
				newsList($ofsbgn,$ofsppg,$pg);
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>