<?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($_GET['dir'])) { $dir = inCode($_GET['dir']); } else { $dir = ""; }

function navList() {
	global $dblink;

	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td colspan='4'>"._NAVIGATOR_."</td></tr>";
	echo "</thead>\n";
	echo "<tbody>\n";
	$n = 0;
	$result = $dblink->get_list("SELECT * FROM memht_navigator ORDER BY position");
	foreach ($result as $row) {
		$id = intval($row['id']);
		$position = intval($row['position']);
		$type = intval($row['type']);
		$content = outCode($row['content']);
		$html = outCode($row['html']);
		$icon = outCode($row['icon']);
		
		//===TYPE===
		//0 = Page
		//1 = Manual link "url|name|target"
		//2 = Spacer
		//3 = HR line
		//4 = HTML
		//5 = Title
		//6 = MyPage
		
		$class = (($n++%2)!=0) ? "hlight" : "clean" ;	
		if ($icon!="") { $icon = "<img src='images/navicons/$icon' border='0' align='absbottom'> "; }
		
		switch($type) {
			case 0:
				$pag = $dblink->get_row("SELECT * FROM memht_pagine WHERE nome='$content'");
				$title = outCode($pag['titolo']);
				echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$icon<a href='index.php?page=$content' title='$title'>$title</a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
			break;
			case 1:
				$link = explode("|",$content);
				if (!eregi("http://",$link[0])) {
					$link[0] = "http://".$link[0];
				}
				if (isset($link[2])) {
					echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$icon<a href='".$link[0]."' target='".$link[2]."'>".$link[1]."</a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
				} else {
					echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$icon<a href='".$link[0]."'>".$link[1]."</a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
				}
			break;
			case 2:
				echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$icon</td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
			break;
			case 3:
				echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'><hr size='1px' style='margin:3px 1px;' noshade></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
			break;
			case 4:
				echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$html</td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
			break;
			case 5:
				echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$icon<b>$content</b></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
			break;
			case 6:
				$row = $dblink->get_row("SELECT id,title FROM memht_mypage WHERE id=$content");
				$mtitle = outCode($row['title']);
				
				echo "<tr><td width='10%' class='$class'>$position</td><td class='$class'>$icon<a href='index.php?page=mypage&op=openPage&id=$content&title=".mem_urlencode($mtitle)."' title='$mtitle'>$mtitle</a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=changePosition&id=$id&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=navigator&op=changePosition&id=$id&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td><td class='$class' width='1%' nowrap><a href='admin.php?page=navigator&op=editNavItem&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=navigator&op=deleteNavItem&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>";
			break;
		}
	}
	echo "</tbody>\n";
	echo "</table>";
}

function changePos($id,$dir) {
	global $dblink;
	
	$id = intval($id);
	$row_pos = $dblink->get_row("SELECT * FROM memht_navigator WHERE id=$id");
	$pos = intval($row_pos['position']);
	switch($dir) {
		case "up":
			$pos2 = $pos - 1;
			if ($dblink->get_num("SELECT * FROM memht_navigator WHERE position<'$pos'")>0) {
				$dblink->query("UPDATE memht_navigator SET position=position+1 WHERE position='$pos2'");
				$dblink->query("UPDATE memht_navigator SET position=position-1 WHERE id=$id");
			}
		break;
		case "down":
			$pos2 = $pos + 1;
			if ($dblink->get_num("SELECT * FROM memht_navigator WHERE position>'$pos'")>0) {
				$dblink->query("UPDATE memht_navigator SET position=position-1 WHERE position='$pos2'");
				$dblink->query("UPDATE memht_navigator SET position=position+1 WHERE id=$id");
			}
		break;
	}
	echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=navigator'>";
}

function addNavItem($ok=false) {
	global $dblink;
	if (!$ok) {
		?>
		<script type="text/javascript">
			function selectInput(type) {
				if (type==6) {
					//MyPage
					document.getElementById('mypage').style.display = 'inline';
					document.getElementById('content').style.display = 'none';
					document.getElementById('contentinfo').style.display = 'none';
				} else {
					document.getElementById('mypage').style.display = 'none';
					document.getElementById('content').style.display = 'inline';
					document.getElementById('contentinfo').style.display = 'inline';
				}
			}
		</script>
		<?php
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='nav_item' method='post' action='admin.php?page=navigator&op=addNavItem&ok=true'>";
			echo "<tr><td><b>"._TYPE_."</b></td><td>";
				echo "<select name='type' onChange='selectInput(this.options[this.selectedIndex].value);'>\n";
					echo "<option value='0'>"._PAGE_."</option>\n";
					echo "<option value='1'>"._LINK_."</option>\n";
					echo "<option value='2'>Spacer</option>\n";
					echo "<option value='3'>HR Line</option>\n";
					echo "<option value='4'>HTML</option>\n";
					echo "<option value='5'>"._TITLE_."</option>\n";
					echo "<option value='6'>MyPage</option>\n";
				echo "</select>\n";
			echo "</td></tr>\n";
			echo "<tr><td><b>"._CONTENT_."</b></td><td><input type='text' name='content' size='30' maxlength='255' id='content'> <img src='images/note.gif' alt='Note' border='0' title='header=[Example] body=[<b>"._PAGE_.":</b> pagename<br><b>"._LINK_.":</b> <i>url</i> | <i>link_name</i> | <i>target</i><br><b>"._TITLE_.":</b> title_name]' id='contentinfo'>";
				echo "<select name='mypage' id='mypage' style='display:none;'>\n";
					$result = $dblink->get_list("SELECT * FROM memht_mypage ORDER BY title");
					foreach ($result as $row) {
						$mid = intval($row['id']);
						$mtitle = outCode($row['title']);
						
						echo "<option value='$mid'>$mtitle</option>\n";
					}
				echo "</select>\n";
			echo "</td></tr>\n";
			echo "<tr><td valign='top'><b>HTML</b></td><td>";
				textarea("html","100%","100px",0);
			echo "</td></tr>\n";
			
			echo "<tr><td><b>"._ICON_."</b></td><td>";
			echo "<div><select name='icon'>\n";
			$handle = opendir('images/navicons/');
			echo "<option value='' selected>-- "._SELECT_." --</option>\n";
			while (false !== ($file = readdir($handle))) {
				if ($file != "." AND $file != ".." AND $file!="Thumbs.db") {
					echo "<option value='$file'>$file</option>\n";
				}
			}
			closedir($handle);
			echo "</select> <span id='info'>root/images/navicons/...</span></div>\n";
			
			echo "</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 (isset($_POST['type'])) { $type = intval($_POST['type']); } else { $type = ""; }
		if (isset($_POST['content'])) { $content = trim(inCode($_POST['content'])); } else { $content = ""; }
		if (isset($_POST['html'])) { $html = inCode($_POST['html']); } else { $html = ""; }
		if (isset($_POST['icon'])) { $icon = inCode($_POST['icon']); } else { $icon = ""; }
		if (isset($_POST['mypage'])) { $mypage = intval($_POST['mypage']); } else { $mypage = 0; }
		if ($type==6) { $content = $mypage; }
		
		$lprow = $dblink->get_row("SELECT * FROM memht_navigator ORDER BY position DESC LIMIT 1");
		$lastpos = intval($lprow['position']);
		$position = $lastpos + 1;
	
		if ($save) {
			$dblink->query("INSERT INTO memht_navigator (id,position,type,content,html,icon)
							VALUES ('null','$position','$type','$content','$html','$icon')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=navigator'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function editNavItem($id,$ok=false) {
	global $dblink;
	
	if (!$ok) {
		?>
		<script type="text/javascript">
			function selectInput(type) {
				if (type==6) {
					//MyPage
					document.getElementById('mypage').style.display = 'inline';
					document.getElementById('content').style.display = 'none';
					document.getElementById('contentinfo').style.display = 'none';
				} else {
					document.getElementById('mypage').style.display = 'none';
					document.getElementById('content').style.display = 'inline';
					document.getElementById('contentinfo').style.display = 'inline';
				}
			}
		</script>
		<?php
		
		$editrow = $dblink->get_row("SELECT * FROM memht_navigator WHERE id=$id");
		$position = intval($editrow['position']);
		$type = intval($editrow['type']);
		$content = outCode($editrow['content']);
		$html = stripslashes($editrow['html']);
		$icon = outCode($editrow['icon']);
		
		$hcontent  = ($type==6) ? " style='display:none;'" : "" ;
		$hmypage  = ($type==6) ? "" : " style='display:none;'" ;
	
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='nav_item' method='post' action='admin.php?page=navigator&op=editNavItem&id=$id&ok=true'>";
			echo "<tr><td><b>"._POSITION_."</b></td><td><input type='text' name='position' value=\"$position\" size='5' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._TYPE_."</b></td><td>";
				echo "<select name='type' onChange='selectInput(this.options[this.selectedIndex].value);'>\n";
					if ($type==0) { $p0 = " selected"; } else { $p0 = ""; }
					if ($type==1) { $p1 = " selected"; } else { $p1 = ""; }
					if ($type==2) { $p2 = " selected"; } else { $p2 = ""; }
					if ($type==3) { $p3 = " selected"; } else { $p3 = ""; }
					if ($type==4) { $p4 = " selected"; } else { $p4 = ""; }
					if ($type==5) { $p5 = " selected"; } else { $p5 = ""; }
					if ($type==6) { $p6 = " selected"; } else { $p6 = ""; }
					echo "<option value='0'$p0>"._PAGE_."</option>\n";
					echo "<option value='1'$p1>"._LINK_."</option>\n";
					echo "<option value='2'$p2>Spacer</option>\n";
					echo "<option value='3'$p3>HR Line</option>\n";
					echo "<option value='4'$p4>HTML</option>\n";
					echo "<option value='5'$p5>"._TITLE_."</option>\n";
					echo "<option value='6'$p6>MyPage</option>\n";
				echo "</select>\n";
			echo "</td></tr>\n";
			echo "<tr><td><b>"._CONTENT_."</b></td><td><input type='text' name='content' value=\"$content\" size='30' maxlength='255' id='content'{$hcontent}> <img src='images/note.gif' alt='Note' border='0' title='header=[Example] body=[<b>"._PAGE_.":</b> pagename<br><b>"._LINK_.":</b> <i>url</i> | <i>link_name</i> | <i>target</i><br><b>"._TITLE_.":</b> title_name]' id='contentinfo'{$hcontent}> ";
				echo "<select name='mypage' id='mypage'{$hmypage}>\n";
					$result = $dblink->get_list("SELECT * FROM memht_mypage ORDER BY title");
					foreach ($result as $row) {
						$mid = intval($row['id']);
						$mtitle = outCode($row['title']);
						
						echo "<option value='$mid'>$mtitle</option>\n";
					}
				echo "</select>\n";
			echo "</td></tr>\n";
			echo "<tr><td valign='top'><b>HTML</b></td><td>";
				textarea("html","100%","100px",0,"",$html);
			echo "</td></tr>\n";
			
			echo "<tr><td><b>"._ICON_."</b></td><td>";
			echo "<div><select name='icon'>\n";
			$handle = opendir('images/navicons/');
			if ($icon=="") { echo "<option value='' selected>-- "._SELECT_." --</option>\n"; } else { echo "<option value=''>-- "._SELECT_." --</option>\n"; }
			while (false !== ($file = readdir($handle))) {
				if ($file != "." AND $file != ".." AND $file!="Thumbs.db") {
					if ($icon==$file) {
						echo "<option value='$file' selected>$file</option>\n";
					} else {
						echo "<option value='$file'>$file</option>\n";
					}
				}
			}
			closedir($handle);
			echo "</select> <span id='info'>root/images/navicons/...</span></div>\n";
			
			echo "</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 (isset($_POST['position'])) { $position = intval($_POST['position']); } else { $position = ""; }
		if (isset($_POST['type'])) { $type = intval($_POST['type']); } else { $type = ""; }
		if (isset($_POST['content'])) { $content = trim(inCode($_POST['content'])); } else { $content = ""; }
		if (isset($_POST['html'])) { $html = inCode($_POST['html']); } else { $html = ""; }
		if (isset($_POST['icon'])) { $icon = inCode($_POST['icon']); } else { $icon = ""; }
		if (isset($_POST['mypage'])) { $mypage = intval($_POST['mypage']); } else { $mypage = 0; }
		if ($type==6) { $content = $mypage; }
		
		if ($dblink->get_num("SELECT * FROM memht_navigator WHERE position=$position AND id!=$id")>0) {
			$lprow = $dblink->get_row("SELECT * FROM memht_navigator ORDER BY position DESC LIMIT 1");
			$lastpos = intval($lprow['position']);
			$position = $lastpos + 1;
		}
	
		if ($save) {
			$dblink->query("UPDATE memht_navigator SET position='$position',type='$type',content='$content',html='$html',icon='$icon' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=navigator'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function deleteNavItem($id,$ok=false) {
	global $dblink;
	$id = intval($id);
	
	if ($ok) {
		$dblink->query("DELETE FROM memht_navigator WHERE id=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=navigator'>";
	} else {
		echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=navigator&op=deleteNavItem&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=navigator' 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=navigator' title='"._LIST_."'><img src='admin/icons/pages.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=navigator&op=addNavItem' title='"._ADD_."'><img src='admin/icons/newpage.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {			
			case "changePosition":
				changePos($id,$dir);
			break;
			
			case "addNavItem":
				addNavItem($ok);
			break;
			
			case "editNavItem":
				editNavItem($id,$ok);
			break;
			
			case "deleteNavItem":
				deleteNavItem($id,$ok);
			break;
		
			default:
				navList();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>