View file admin/includes/menu.php

File size: 14.11Kb
<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - [email protected]
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

if (empty($action)) {
	$action = "";
} else {
	$action = strtolower($action);
}

// Init
if (empty($id)) {
	$id = null;
}
if (empty($published)) {
	$published=0;
}
if (empty($content)) {
	$content="";
}
if (empty($itemid)) {
	$itemid = null;
}

switch($action) {
	case "new":
		display_edit_form($id);
		break;
	case "edit":
		display_edit_form($id);
		break;
	case "save":
		save($id, $title, $type, $content, $published, $itemid);
		break;
	case "delete":
		delete($id);
		break;
	case "publish":
		publish($id);
		break;
	case "reorder":
		reorder ($id, $direction);
		break;
	default:
		display_menu();
		break;
}

function publish ($id) {

	$query = "SELECT type, itemid FROM wps_menu WHERE id='$id'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$row = mysql_fetch_object($result);

	if ($row->type=="section") {
		$from = "wps_sections";
	} else if ($row->type=="component") {
		$from = "wps_components";
	} else {
		$from = "wps_items";
	}

	$query = "SELECT id, published FROM ".$from." WHERE id='$row->itemid'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$row = mysql_fetch_object($result);

	if ($row->published) {
		$query = "UPDATE ".$from." SET published=0 WHERE id = '$row->id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	} else {
		$query = "UPDATE ".$from." SET published=1 WHERE id = '$row->id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_menu();
}

function reorder ($id, $direction) {
	$query = "SELECT id FROM wps_menu";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$total = mysql_num_rows($result);

	$query = "SELECT ordering FROM wps_menu WHERE id = '$id'";
	$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	$row = mysql_fetch_object($result);
	$ordering = $row->ordering;

	if ($direction=="up" && $ordering>0) {
		$ordering--;
		$query = "UPDATE wps_menu SET ordering='$ordering' WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	} else if ($direction=="down" && $ordering<$total) {
		$ordering++;
		$query = "UPDATE wps_menu SET ordering='$ordering' WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_menu();
}

function delete ($id) {
	if (is_array($id)) {
		foreach($id as $value) {
			$query = "SELECT type, itemid FROM wps_menu WHERE id = '$value'";
			$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
			$row = mysql_fetch_object($result);
			if ($row->type=="item") {
				$query = "DELETE FROM wps_items WHERE id = '$row->itemid'";
				mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
			}
			$query = "DELETE FROM wps_menu WHERE id = '$value'";
			mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		}
	} else {
		$query = "SELECT type, itemid FROM wps_menu WHERE id = '$id'";
		$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		$row = mysql_fetch_object($result);
		if ($row->type=="item") {
			$query = "DELETE FROM wps_items WHERE id = '$row->itemid'";
			mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		}
		$query = "DELETE FROM wps_menu WHERE id = '$id'";
		mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
	}
	display_menu();
}

function save($id, $title, $type, $content, $published, $itemid) {
	// Replace quotes
	if (!get_magic_quotes_gpc()) {
		$title = str_replace("'","\'",$title);
		$content = str_replace("'","\'",$content);
	}
	$date = date ("Y-m-d H:i:s");
	if ($id) {
		if ($type=="item") {
			$query = "SELECT itemid FROM wps_menu WHERE id = '$id'";
			$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
			$row = mysql_fetch_object($result);
			$itemid = $row->itemid;
			$query = "UPDATE wps_items SET sectionid=0, title = '$title', content = '$content', date = '$date', published='$published' WHERE id = '$itemid'";
			mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		}
		$query = "UPDATE wps_menu SET title = '$title', type = '$type', itemid='$itemid' WHERE id = '$id'";
	} else {
		if ($type=="item") {
			$query = "INSERT INTO wps_items VALUES ('', 0, '$title', '$content', '$date', 0, '$published')";
			mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
			$query = "SELECT id FROM wps_items ORDER BY id DESC LIMIT 1";
			$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
			$row = mysql_fetch_object($result);
			$itemid = $row->id;
		}
		$query = "INSERT INTO wps_menu VALUES ('', '$title', 0, '$type', '$itemid')";
	}
	mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());

	if ($id) {
		display_edit_form($id);
	} else {
		display_menu();
	}
}

function display_edit_form($id) {
	global $type, $common;

	if (isset($id)) {
		if (is_array($id)) $id = $id[0];
		$query = "SELECT * FROM wps_menu WHERE id = '$id'";
		$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
		$row = mysql_fetch_object($result);

		$type = $row->type;
	}

	echo "<table width=\"600\">\n";
    echo "<tr>\n";
    echo "<td><a href=\"index.php\" border=0>Home</a> &gt;
    	<a href=\"index.php?option=menu\" border=0>Menu Management</a> &gt;
    	Add / Edit</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"center\"><b>Main Menu</b></td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td valign=\"top\">\n";
    echo "<br>\n";

	if (empty($type) && empty($id)) {
		echo "<form name=\"form\" method=\"post\" action=\"index.php?option=menu&action=edit\">\n";
		echo "<select name=\"type\">\n";
		echo "<option value=\"item\">Typed Content</option>\n";
		echo "<option value=\"section\">Section</option>\n";
		echo "<option value=\"component\">Component</option>\n";
		echo "</select>\n";
		echo "<br>\n";
		echo "<br>\n";
    	echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n";
    	echo "<input type=\"button\" name=\"Cancel\" value=\"Cancel\" onclick=\"document.location.href='index.php?option=menu'\">\n";
    	echo "</form>\n";
	} else {
		echo "<form name=\"form\" method=\"post\" action=\"index.php?option=menu\" onsubmit=\"return validate()\">\n";
		echo "<b>Title:</b>\n";
		echo "<br>\n";
		if ($type=="section") {
    		echo "<input type=\"text\" name=\"title\" size=\"40\" value=\"";
    		if (isset($id)) echo $row->title;
    		echo "\">\n";
    		echo "<br>\n";
			echo "<b>Section:</b>\n";
			echo "<br>\n";
			echo "<select name=\"itemid\">\n";
			$query2 = "SELECT * FROM wps_sections ORDER BY title";
			$result2 = mysql_query ($query2);
			for($i = 1; $row2 = mysql_fetch_object ($result2); $i++) {
    			echo "<option value=\"$row2->id\"";
    			if (isset($id) && $row2->id==$row->itemid) echo " selected";
    			echo ">$row2->title</option>\n";
			}
			echo "</select>\n";
			echo "<br>\n";
			echo "<br>\n";
		} else if ($type=="component") {
    		echo "<input type=\"text\" name=\"title\" value=\"";
    		if (isset($id)) echo $row->title;
    		echo "\">\n";
    		echo "<br>\n";
			echo "<b>Component:</b>\n";
			echo "<br>\n";
			echo "<select name=\"itemid\">\n";
			$query2 = "SELECT * FROM wps_components ORDER BY name";
			$result2 = mysql_query ($query2);
			for($i = 1; $row2 = mysql_fetch_object ($result2); $i++) {
    			echo "<option value=\"$row2->id\"";
    			if (isset($id) && $row2->id==$row->itemid) echo " selected";
    			echo ">$row2->name</option>\n";
			}
			echo "</select>\n";
			echo "<br>\n";
			echo "<br>\n";
		} else {
			if (isset($id)) {
				$type = $row->type;
				$query = "SELECT * FROM wps_items WHERE id = '$row->itemid'";
				$result = mysql_query($query) or die("MySQL query: ".$query." failed with error: ".mysql_error());
				$row = mysql_fetch_object($result);
			}
    		echo "<input type=\"text\" name=\"title\" size=\"40\" value=\"";
    		if (isset($id)) echo $row->title;
    		echo "\">\n";
    		echo "<br>\n";
			echo "<b>Content:</b>\n";
			echo "<br>\n";
			echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
			echo "<tr>\n";
			echo "<td>\n";
			echo "<textarea name=\"content\" rows=\"12\" cols=\"60\" onselect=\"storeCaret(this);\" onclick=\"storeCaret(this);\" onkeyup=\"storeCaret(this);\">";
			if (isset($id)) echo $row->content;
			echo "</textarea>\n";
			echo "</td>\n";
			echo "<td width=\"50%\">\n";
			insertUBBbuttons();
			echo "</td>\n";
			echo "</tr>\n";
			echo "</table>\n";
			insertUBBhelp();
    		echo "<br>\n";
    		echo "Published: <input type=\"checkbox\" name=\"published\" value=\"1\"";
    		if (empty($id) || $row->published==1) echo " checked";
    		echo ">\n";
    		echo "<br>\n";
    		echo "<br>\n";
    	}
    	echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
    	echo "<tr>\n";
    	echo "<td>\n";
    	if (isset($id))
    		echo "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
    	echo "<input type=\"hidden\" name=\"type\" value=\"$type\">\n";
		echo "<input type=\"hidden\" name=\"action\" value=\"save\">\n";
    	echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">\n";
    	echo "<input type=\"button\" name=\"Cancel\" value=\"Cancel\" onclick=\"document.location.href='index.php?option=menu'\">\n";
		echo "</td>\n";
		echo "<td align=\"right\">\n";
		$common->setup_default_emulator();
		$common->display_emulator();
		echo "</td>\n";
		echo "</tr>\n";
		echo "</table>\n";
		echo "</form>\n";
    }
    echo "</td>\n";
    echo "</tr>\n";
	echo "</table>\n";
}

function display_menu() {
	global $limit, $limitstart, $common;

	// Total
	$query = "SELECT * FROM wps_menu";
	$result = mysql_query($query) or die("Query failed");
	$total = mysql_num_rows ($result) or die ("Query failed");

	// Select query
	if (empty($limitstart)) $limitstart = 0;
	if (empty($limit)) $limit = 10;
	if ($limit > $total) {
		$limitstart = 0;
	}
	$query = "SELECT * FROM wps_menu ORDER BY ordering LIMIT $limitstart, $limit";
	$result = mysql_query ($query);

	echo "<table width=\"600\">\n";
    echo "<tr>\n";
    echo "<td><a href=\"index.php\" border=0>Home</a> &gt; Menu Management</td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td align=\"center\"><b>Main Menu</b></td>\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "<td valign=\"top\">\n";
    echo "<br>\n";
	echo "<form name=\"form\" method=\"post\" action=\"index.php?option=menu\">\n";
	echo "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
    echo "<tr>\n";
	echo "<td width=\"5%\"><input type=\"checkbox\" name=\"toggle\" value=\"\" onclick=\"checkAll();\"></td>\n";
	echo "<td width=\"60%\"><b>Title</b></td>\n";
	echo "<td width=\"15%\"><b>Type</b></td>\n";
	echo "<td width=\"10%\" align=\"center\"><b>Reorder</b></td>\n";
	echo "<td width=\"10%\" align=\"center\"><b>Published</b></td>\n";
    echo "</tr>\n";
    echo "<tr><td colspan=\"5\">&nbsp;</td></tr>\n";
	for($i = 1; $row = mysql_fetch_object ($result); $i++) {
		if ($row->type=="section") {
			$query2 = "SELECT published FROM wps_sections WHERE id = '$row->itemid'";
		} else if ($row->type=="component") {
			$query2 = "SELECT published FROM wps_components WHERE id = '$row->itemid'";
		} else {
			$query2 = "SELECT published FROM wps_items WHERE id = '$row->itemid'";
		}
		$result2 = mysql_query ($query2);
		$row2 = mysql_fetch_object ($result2);
		$row->published = $row2->published;

	    echo "<tr>\n";
		echo "<td><input name=\"id[]\" type=\"checkbox\" value=\"$row->id\"></td>\n";
		echo "<td><a href=\"index.php?option=menu&action=edit&id=$row->id\">$row->title</a></td>\n";
		echo "<td>$row->type</td>\n";
		echo "<td align=\"center\"><a href=\"index.php?option=menu&action=reorder&id=$row->id&direction=up\"><img src=\"images/arrow_up.png\" alt=\"Up\" width=\"12\" height=\"12\" border=\"0\"></a>
			$row->ordering
			<a href=\"index.php?option=menu&action=reorder&id=$row->id&direction=down\"><img src=\"images/arrow_down.png\" alt=\"Down\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		if ($row->published) {
			echo "<td align=\"center\"><a href=\"index.php?option=menu&action=publish&id=$row->id\"><img src=\"images/publish_tick.png\" alt=\"Unpublish\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		} else {
			echo "<td align=\"center\"><a href=\"index.php?option=menu&action=publish&id=$row->id\"><img src=\"images/publish_x.png\" alt=\"Publish\" width=\"12\" height=\"12\" border=\"0\"></a></td>\n";
		}
		echo "</tr>\n";
	}
	echo "<tr><td colspan=\"5\">&nbsp;</td></tr>\n";
	echo "<tr><td colspan=\"5\" align=\"center\">";
	writePagesLinks($limitstart, $limit, $total, "menu");
	echo "</td></tr>\n";
	echo "<tr><td colspan=\"5\" align=\"center\">";
	writePagesCounter($limitstart, $limit, $total);
	echo "</td></tr>\n";
    echo "<tr>\n";
    echo "<td colspan=\"3\">
		<input type=\"submit\" name=\"action\" value=\"New\">
		<input type=\"submit\" name=\"action\" value=\"Edit\">
		<input type=\"submit\" name=\"action\" value=\"Delete\" onclick=\"return confirm_delete();\">\n";
	echo "</td>\n";
	echo "<td colspan=\"2\" align=\"right\">\n";
	$common->setup_default_emulator();
	$common->display_emulator();
	echo "</td>\n";
	echo "</tr>\n";
	echo "</table>\n";
	echo "</form>\n";
    echo "</td>\n";
    echo "</tr>\n";
	echo "</table>\n";
}
?>