<?	// Preamble
	$pageName = "Delete Plan";
	$need = 'auth';
	require 'parts/preamble.php';	// Load most of document

	if (!$userInfo['u_admin'])	// no access from non-admin yet
		bailout ("<P>You don't have access to this page (yet?). Bug an admin to delete a news post.</P>");

	need ('boxes news sql');

	function convertToHTML ($string)
	{
		$table = array_flip (get_html_translation_table (HTML_ENTITIES));

		return strtr ($string, $table);
	}

	function convertFromHTML ($string)
	{
		$table = get_html_translation_table (HTML_ENTITIES);

		return strtr ($string, $table);
	}

	function formatPlanArray ($a)
	{
		need ("date");

		return formatNews (dateFromSQLDateTime ($a['p_date']), $a['p_user'], stripSlashes($a['p_plan']));
	}

	function planEntrySummary ($it)
	{
		need ("date");

		return
			'<TR>'
			.'	<TD><A href="' . thisURL . '?planID=' . $it['p_id'] . '">' . $it['p_id'] . '</A></TD>'
			.'	<TD>' . dateFromSQLDateTime ($it['p_date']) . '</TD>'
			.'	<TD>' . $it['p_user'] . '</TD>'
			.'	<TD>' . substr (convertFromHTML (stripSlashes ($it['p_plan'])), 0, 50) . '&#8230;</TD>'
			.'</TR>';
	}

	function planEntryConfirmation ($a)
	{
		need ("date");

		$id = $a['p_id'];

		newsBoxOpen ();
		newsBoxTitle ("Confirmation: Delete #$id?", thisURL . "?planID=$id&confirm=yes");
		echo formatPlanArray ($a);
		newsBoxClose ();
	}

	$planID = addSlashes ($_REQUEST['planID']);
	$confirm = $_REQUEST['confirm'];

	if ($planID) {
		if ($confirm) {
			$query = "DELETE FROM plans WHERE p_id='$planID'";

			$rows = sqlWriteQuery ($query);
			if ($rows === null) {
				echo "<P>Bad mojo, man. I couldn't talk to the SQL server. It said '$sqlError'.</P>";
			} elseif ($rows === false) {
				echo "<P>Something bad happened, and MySQL said '$sqlError'. Bug an admin.</P>";
			} elseif (!$rows) {
				echo "<P>Plan entry $planID didn't exist.";
			} else {
				echo "<P>Plan entry $planID has been deleted.";
			}
		} else {
			$query = 'SELECT p_id, p_date, p_user, p_plan FROM plans'
					." WHERE p_id='$planID'";
			$entries = sqlReadQuery ($query);
			if ($entries === null) {
				echo "<P>Bad mojo, man. I couldn't talk to the SQL server. It said '$sqlError'.</P>";
			} elseif ($entries === false) {
				echo "<P>Something bad happened, and MySQL said '$sqlError'. Bug an admin.</P>";
			} elseif (count ($entries) == 1) {
				planEntryConfirmation ($entries[0]);
			} else {
				echo "<P>This shouldn't even be possible, but there's more than one plan entry with ID '$planID'!</P>";
			}
		}
	} else {
		newsBoxOpen ("All Plan Postings");

		$query = 'SELECT p_id, p_date, p_user, p_plan FROM plans'
				.' ORDER BY p_date DESC';

		$entries = sqlReadQuery ($query);
		if ($entries && is_array ($entries) && count ($entries)) {
			tableHeader ("100%");
			?><TR>
				<TH align="left">ID</TH>
				<TH align="left">Date</TH>
				<TH align="left">User</TH>
				<TH align="left">Text</TH>
			</TR><?
			for ($i = 0; $i < count ($entries); $i++) {
				echo planEntrySummary ($entries[$i]);
			}
			tableFooter ();
		} else {
			echo "<P>No plans available.";
		}
		newsBoxClose ();
	}
?>