<?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['cat'])) { $cat = inCode($_POST['cat']); } else { $cat = ""; }
if (isSet($_POST['testo'])) { $testo = inCode($_POST['testo']); } else { $testo = ""; }
if (isSet($_POST['nome'])) { $nome = inCode($_POST['nome']); } else { $nome = ""; }

function notesList() {
	global $dblink;
	
	if ($result = $dblink->get_list("SELECT * FROM memht_admin_notes_cat ORDER BY nome")) {
		echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
		foreach ($result as $row) {
			$cid = intval($row['id']);
			$cnome = outCode($row['nome']);
			
			echo "<thead>\n";
			echo "<tr><td colspan='2'><b>$cnome</b></td></tr>\n";
			echo "</thead>\n";
			
			echo "<tbody>\n";
			$a = 0;
			if ($result2 = $dblink->get_list("SELECT * FROM memht_admin_notes WHERE cat='$cid' ORDER BY nome")) {
				foreach ($result2 as $row2) {
					$nid = intval($row2['id']);
					$nnome = outCode($row2['nome']);
					
					$class = (($a++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><a href='admin.php?page=notes&op=readNote&id=$nid' title=\"$nnome\"><b>$nnome</b></a></td><td width='1%' class='$class' nowrap><a href='admin.php?page=notes&op=editNote&id=$nid' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=notes&op=deleteNote&id=$nid' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
				}
			} else {
				echo "<tr><td align='center' id='errorText' class='clean' colspan='2'><b>"._EMPTY_."</b></td></tr>\n";
			}
			echo "</tbody>\n";
		}
		echo "</table>\n";
	} else {
		echo "<div align='center' id='errorText'><b>"._NOCAT_."</b></div>";
	}
}	

function view($id) {
	global $dblink,$siteConfig;

	$id = intval($id);

	$row_notes = $dblink->get_row("SELECT *,DATE_FORMAT(data, '".$siteConfig['timestamp']."') as data FROM memht_admin_notes WHERE id=$id");
	$id = intval($row_notes['id']);
	$nome = outCode($row_notes['nome']);
	$testo = outCode($row_notes['testo']);
	$autore = outCode($row_notes['autore']);
	$data = $row_notes['data'];
	
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>$nome</td><td width='30%'><div align='right'><a href='admin.php?page=notes&op=editNote&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=notes&op=deleteNote&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></div></td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
		echo "<tr><td class='clean' id='info'>"._AUTHOR_.": $autore</td><td class='clean' id='info' width='10%' align='right'>"._DATE_.": $data</td></tr>";
		echo "<tr><td class='clean' colspan='2'>$testo</td></tr>";
	echo "</tbody>\n";
	echo "</table>";
}

function add($cat,$nome,$testo,$ok=false) {
	global $dblink,$userInfo,$tzNOW;

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_notes' method='post' action='admin.php?page=notes&op=addNote&ok=true'>";
			echo "<tr><td width='25%'><b>"._ARGUMENT_."</b></td><td>";
			echo "<select name='cat'>\n";
			//Lista argomenti
			$result = $dblink->get_list("SELECT * FROM memht_admin_notes_cat ORDER BY nome");
			foreach ($result as $row) {
				$cid = intval($row['id']);
				$cnome = outCode($row['nome']);
					echo "<option value='$cid'>$cnome</option>\n";
			}
			echo "</select></td></tr>\n";
			echo "<tr><td><b>"._TITLE_."</b></td><td><input type='text' name='nome' size='40' maxlength='255'></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 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; }
		if ($testo=="") { $save = false; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_admin_notes (id,cat,nome,testo,autore,data)
							VALUES (null,'$cat','$nome','$testo','".$userInfo['user']."',$tzNOW)");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=notes'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function edit($id,$cat,$nome,$testo,$ok=false) {
	global $dblink;

	if (!$ok) {
		$row_notes = $dblink->get_row("SELECT * FROM memht_admin_notes WHERE id=$id");
		$ncat = intval($row_notes['cat']);
		$nnome = outCode($row_notes['nome']);
		$ntesto = outCode($row_notes['testo']);
		
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_notes' method='post' action='admin.php?page=notes&op=editNote&id=$id&ok=true'>";
			echo "<tr><td width='25%'><b>"._ARGUMENT_."</b></td><td>";
			echo "<select name='cat'>\n";
			//Lista argomenti
			$result = $dblink->get_list("SELECT * FROM memht_admin_notes_cat ORDER BY nome");
			foreach ($result as $row) {
				$cid = intval($row['id']);
				$cnome = $row['nome'];
				echo ($ncat==$cid) ? "<option value='$cid' selected>$cnome</option>\n" : "<option value='$cid'>$cnome</option>\n" ;
			}
			echo "</select></td></tr>\n";
			echo "<tr><td><b>"._TITLE_."</b></td><td><input type='text' name='nome' size='40' maxlength='255' value=\"$nnome\"></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><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($nome=="") { $save = false; }
		if ($testo=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_admin_notes SET cat='$cat', nome='$nome', testo='$testo' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=notes'>";
		} 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_admin_notes WHERE id=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=notes'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETENOTE_."</b><br><a href='admin.php?page=notes&op=deleteNote&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=notes' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function showCat() {
	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_admin_notes_cat ORDER BY nome")) {
		foreach ($result as $row) {
			$cid = intval($row['id']);
			$cnome = outCode($row['nome']);
			
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
			echo "<tr><td class='$class'><b>$cnome</b></td><td class='$class' nowrap><a href='admin.php?page=notes&op=editCategory&id=$cid' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=notes&op=deleteCategory&id=$cid' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
		}
	} else {
		echo "<tr><td align='center' id='errorText' colspan='2' class='clean'><b>"._NOCAT_."</b></td></tr>";
	}
	echo "</tbody>\n";
	echo "</table>\n";
}

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

function editCat($id,$nome,$ok=false) {
	global $dblink;
	$id = intval($id);

	$row_catc = $dblink->get_row("SELECT * FROM memht_admin_notes_cat WHERE id=$id");
	$nomedb = outCode($row_catc['nome']);
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_cat2' method='post' action='admin.php?page=notes&op=editCategory&id=$id&ok=true'>";
			echo "<tr><td width='25%'><b>"._CATEGORYNAME_."</b><td><input type='text' name='nome' size='40' maxlength='255' value=\"$nomedb\">\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_admin_notes_cat SET nome='$nome' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=notes&op=showCategory'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function deleteCat($id,$ok=false) {
	global $dblink;
	$id = intval($id);
	if ($ok) {
		$dblink->query("DELETE FROM memht_admin_notes_cat WHERE id=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=notes&op=showCategory'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETECATEGORY_."</b><br><a href='admin.php?page=notes&op=deleteCategory&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=notes&op=showCategory' 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=notes' title='"._LIST_."'><img src='admin/icons/notes.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=notes&op=addNote' title='"._ADD_NOTE_."'><img src='admin/icons/add.png' border='0' title='"._ADD_NOTE_."' align='top'> "._ADD_NOTE_."</a>";
			echo " - <a href='admin.php?page=notes&op=showCategory' title='"._SHOW_CATEGORIES_."'><img src='admin/icons/category.png' border='0' title='"._SHOW_CATEGORIES_."' align='top'> "._SHOW_CATEGORIES_."</a>";
			echo " - <a href='admin.php?page=notes&op=addCategory' title='"._ADD_CATEGORY_."'><img src='admin/icons/addcategory.png' border='0' title='"._ADD_CATEGORY_."' align='top'> "._ADD_CATEGORY_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "addNote":
				add($cat,$nome,$testo,$ok);
			break;
			
			case "readNote":
				view($id);
			break;
			
			case "deleteNote":
				delete($id,$ok);
			break;
			
			case "editNote":
				edit($id,$cat,$nome,$testo,$ok);
			break;
			
			case "showCategory":
				showCat();
			break;
			
			case "addCategory":
				addCat($nome,$ok);
			break;
			
			case "editCategory":
				editCat($id,$nome,$ok);
			break;
			
			case "deleteCategory":
				deleteCat($id,$ok);
			break;
		
			default:
				notesList();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>