Location: PHPKode > projects > ClassSimple > modules/menu/main.php
<?php
/******************************************************
 * main.php
 * MODULE: Menu
 * ADMINISTRATION
 *
 * Edits Menu
 ******************************************************/

include ("../../admin.header.php");

$db['menu'] = open_db(__FILE__, __LINE__, "../../db/menu.db");
$me = "main.php";

switch ($_GET['mode']){
	case "del":
		$id = $_GET['id'];
		query(__FILE__, __LINE__, $db['menu'], "DELETE FROM menu WHERE id='$id'");
		
		//Correct all of the positions in the database
		fixpossequance();
		
		$_SESSION['msg']['type'] = "good";
		$_SESSION['msg']['text'] = "Menu option removed successfully.";
		
		header("Location: $me");
		
	break;
	case "move":
		//Move current item to new possition
		$result = query(__FILE__, __LINE__, $db['menu'], "SELECT * FROM menu WHERE id = " . $_GET['id']);
		$menu_item = fetch($result);
		$pos = $menu_item['placement'];
		query(__FILE__, __LINE__, $db['menu'],
		 "UPDATE menu SET placement=" . $_GET['pos'] . " WHERE id = " . $_GET['id']);
		
		//Move item of new possition to the old one's position
		$result = query(__FILE__, __LINE__, $db['menu'], "SELECT * FROM menu WHERE placement = " . $_GET['pos'] .
		  " AND id != " . $_GET['id'] . " LIMIT 1");
		$menu_item = fetch($result);
		$id = $menu_item['id'];
		query(__FILE__, __LINE__, $db['menu'], "UPDATE menu SET placement=" . $pos . " WHERE id = " . $id);
		header("Location: main.php");
		
	break;
	case "add":
		//Figgure out what the link is
		if ($_POST['type'] == "internal_page"){
			$link = "../pages/view.php?p=" . $_POST['id'];
		}elseif ($_POST['type'] == "external_page"){
			$link = $_POST['link'];
		}elseif($_POST['type'] == "doc"){
			$link = "../docs/view.php?id=" . $_POST['doc_id'];
		}elseif($_POST['type'] == "doccat"){
			$link = "../docs/viewlist.php?cat=" . $_POST['doccat_id'];
		}
		
		//Database
		query(__FILE__, __LINE__, $db['menu'] , 
		  "INSERT INTO menu (title, link, placement) VALUES ('" . $_POST['title'] . 
		  "', '$link', '" . $_POST['pos'] . "')");
		
		$_SESSION['msg']['type'] = "good";
		$_SESSION['msg']['text'] = "Menu item added successfully.";
		
		header("Location: $me");

	break;

}
?>

<h1>Menu Editor</h1>

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr align="left"><th>Title</th><th>Link</th><th>&nbsp;</th><th>&nbsp;</th>
  <th>&nbsp;</th></tr>
<?php
	$result = query(__FILE__, __LINE__, $db['menu'], "SELECT * FROM menu ORDER BY (placement +0)");
	$inital = true;
	$highest_pos = 0; $old_id = 0;
	while ($menu_item = fetch($result)){
		global $highest_pos;
		if (!$inital){
		  echo "<td align=\"center\"><a href=\"$me?mode=move&id=$old_id&pos=" . ($menu_item['placement']) . 
		    "\"><img src=\"../../down.gif\" width=\"15\" height=\"15\" title=\"Down\" " . 
		    "alt=\"Down\" border=\"0\"></a></td>";
		  echo "<td align=\"center\"><a href=\"$me?mode=del&id=$old_id\">" . 
		    "<img src=\"../../rmv.gif\" width=\"15\" height=\"15\" title=\"Remove\" " . 
		    "alt=\"Remove\" border=\"0\"></a></td></tr>\n";

			if ($menu_item['placement'] % 2 == 0){
				echo "<tr style=\"background-color: #EEE\">";
			}else{
				echo "<tr>";
			}		
					  	
		  	echo "<td>" . $menu_item['title'] . "</td>";
			echo "<td><a href=\"" . $menu_item['link'] . "\">" . lookup($menu_item['link']) . "</a></td>";
			echo "<td align=\"center\"><a href=\"$me?mode=move&id={$menu_item['id']}&pos=" . 
			  ($menu_item['placement'] - 1) . "\"><img src=\"../../up.gif\" width=\"15\" " . 
			  "height=\"15\" title=\"Up\" alt=\"Up\" border=\"0\"></a></td>";
		}else{
			echo "<tr><td>" . $menu_item['title'] . "</td>";
			echo "<td><a href=\"" . $menu_item['link'] . "\">" . lookup($menu_item['link']) . "</a></td>";
			echo "<td>&nbsp;</td>";
			$inital = false;
		}
		$old_id = $menu_item['id'];
		$highest_pos = $menu_item['placement'];
	}
	echo "<td>&nbsp;</td><td align=\"center\"><a href=\"$me?mode=del&id=$old_id\">" . 
	  "<img src=\"../../rmv.gif\" width=\"15\" height=\"15\" title=\"Remove\" " . 
	  "alt=\"Remove\" border=\"0\"></a></td></tr>\n";
	//The add an item form
?>	
	

</table><br>
<form action="<?=$me?>?mode=add" method="post" name="additem">
<input type="hidden" name="pos" value="<?=$highest_pos + 1?>">
<table width="100%" border="0">
<tr><td colspan="2"><h1>New Menu Item</h1></td></tr>
<tr><td class="form-label">Title:&nbsp;</td><td><input type="text" name="title"></td></tr>
<tr><td class="form-label">Link:&nbsp;</td><td>
<optgroup label="LinkType">
<table border="0">
	<tr><td><input type="radio" name="type" value="doc" id="type_doc"> Document:</td><td>
		<select name="doc_id" size="0" onChange="document.additem.type_doc.click()">
				<?php
				//Create list of pages
				$db['docs'] = open_db(__FILE__, __LINE__, "../../db/docs.db");				
				
				$result = query(__FILE__, __LINE__, $db['docs'], "SELECT * FROM docs");
				
				while ($set = fetch($result)){
					echo "<option value=\"" . $set['id'] . "\">" . $set['filename'] . "</option>\n";
				}
				
				?>		
		</select></td></tr>
	<tr><td><input type="radio" name="type" value="doccat" id="type_doccat"> Document Category List:</td><td>
		<select name="doccat_id" size="0" onChange="document.additem.type_doccat.click()">
				<?php
				//Create list of doc cats
				$db['docs'] = open_db(__FILE__, __LINE__, "../../db/docs.db");				
				
				$result = query(__FILE__, __LINE__, $db['docs'], "SELECT * FROM categories");
				
				while ($set = fetch($result)){
					echo "<option value=\"" . $set['id'] . "\">" . $set['name'] . "</option>\n";
				}
				
				?>		
		</select></td></tr>
	<tr><td><input type="radio" name="type" value="internal_page" id="type_page"> Internal page:</td><td>
			  <select name="id" size="0" onChange="document.additem.type_page.click()">
				<?php
				//Create list of pages
				$db['pages'] = open_db(__FILE__, __LINE__, "../../db/pages.db");				
				
				$result = query(__FILE__, __LINE__, $db['pages'], "SELECT * FROM pages");
				
				while ($set = fetch($result)){
					echo "<option value=\"" . $set['id'] . "\">" . $set['title'] . "</option>\n";
				}
				
				?></option></select></td></tr>
	<tr><td><input type="radio" name="type" value="external_page" id="type_ext"> External Page:</td><td>
				<input type="text" name="link" size="50"  onChange="document.additem.type_ext.click()">&nbsp; 
				(Don't forget http://)</td></tr>
</table>
</optgroup>
</td></tr>	
<tr><td colspan="2"><input type="submit" value="Add Menu Item"></td></tr>
</table>
</form>





<?php
/****************************
 * Functions
 ****************************/


function fixpossequance(){
	global $db;
	$result = query(__FILE__, __LINE__, $db['menu'], "SELECT * FROM menu");
	
	$i=0;
	while ($entry = fetch($result)){
		$i++;
		query(__FILE__, __LINE__, $db['menu'], "UPDATE menu SET placement='$i' WHERE id='" . $entry['id'] . "'");
	}
}

function lookup($link_text){
	//Sees if this is an internal link and if so return a linked title, otherwise it gives the orriginal back
	list($page, $id) = explode("?p=", $link_text);
	if ($page == "../pages/view.php"){
		//Open page DB
		$db['pages'] = open_db(__FILE__, __LINE__, "../../db/pages.db");
	
		$result = query(__FILE__, __LINE__, $db['pages'], "SELECT * FROM pages WHERE id='$id' LIMIT 1");
		$result = fetch($result);
		return $result['title'];
	}
	//Sees if its a doc
	list($doc, $id) = explode ("?id=", $link_text);
	if ($doc == "../docs/view.php"){
		//Open docs db
		
		$db['docs'] = open_db(__FILE__, __LINE__, "../../db/docs.db");
	
		$result = query(__FILE__, __LINE__, $db['docs'], "SELECT * FROM docs WHERE id='$id' LIMIT 1");
		$result = fetch($result);
		return "[Document] " . $result['filename'];		
	}
	
	//See if its a doclist
	list($doclist, $id) = explode ("?cat=", $link_text);
	if ($doclist == "../docs/viewlist.php"){
		//Open docs db
		
		$db['docs'] = open_db(__FILE__, __LINE__, "../../db/docs.db");
	
		$result = query(__FILE__, __LINE__, $db['docs'], "SELECT * FROM categories WHERE id='$id' LIMIT 1");
		$result = fetch($result);
		return "[Document List] " . $result['name'];		
	}
	
	//Unknown, return orriginal
	return $link_text;
	
}


include ("../../admin.footer.php");
?>
Return current item: ClassSimple