<?php

require_once("src/core/managers/LoginManager.php");
//require_once("src/AnnouncementManager.php");
require_once("src/widgets/MainMenu.php");
require_once("src/widgets/SubMenu.php");
require_once("src/widgets/CookieCrumble.php");
require_once("src/widgets/UserLogin.php");
require_once("src/widgets/AnnouncementViewer.php");
require_once("src/widgets/AnnouncementEditor.php");

session_start();

// The ID of this page is in SiteMap.xml
$curPageId = 'announcements';
if (isset($_GET['edit'])) $curPageId = 'addannouncement';

// Get an instance of the LoginManager
$lm = LoginManager::instance();

// Log the user out if that's what they want
$url = explode("?", $_SERVER['PHP_SELF']);
$lm->logoutOnRequest($url[0]);

// Check if the user is logged in
$user =& $lm->getLoggedInUser();

// Get the user's level
$userLevel = 0;
if (isset($user)) {
	$userLevel = $user->getUserLevel();
}

// Check to see if we want to edit an announcement and that 
// we have tha appropriate privilages if we want to do taht
$announcemnt = null;
$editor = null;
if (isset($_GET['edit']) || isset($_GET['remove'])) {
	
	// Is the user logged in?
	if (!isset($user)) {
		header("Location: " . $url[0]);
	}
	
 	if (isset($_GET['id'])) {
		$am = AnnouncementManager::instance();
		$announcemnt = $am->get(&$user, $_GET['id']);
 	}
 	
 	if (!isset($announcement) && isset($_GET['remove'])) {
 		header("Location: " . $url[0]);
 	}
 	
 	$editor =& new AnnouncementEditor(&$user, $announcemnt);
 	$editor->remove();
 	if ($editor->update()) {
 		header("Location: " . $url[0]);
 	}
}

//
// Set up the widgets
//

$mainMenu =& new MainMenu($curPageId, $userLevel);
$cookieCrumble =& new CookieCrumble($curPageId);
$userLogin =& new UserLogin(&$user);
$subMenu =& new SubMenu($curPageId, $userLevel);

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>EECS REFS</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<link href="core.css" rel="stylesheet" type="text/css">
	</head>
	<body>
		<table align="center" class="bodyTable">
			<tr><td><img src="images/logo.jpg" alt="EECS REFS Logo"/></td><td colspan="2" valign="top"><? echo $mainMenu->toHtml() ?></td></tr>
  			<tr><td colspan="2" valign="top"><img src="images/header.jpg" alt=""/></td></tr>
  			<tr><td valign="top" class="cookieCrumbleTd"><? echo $cookieCrumble->toHtml() ?></td><td class="cookieCrumbleTd"><? echo $userLogin->getLoginWidget() ?></td></tr>
  			<tr>
  				<td class="bodyLeftTd"><? echo $subMenu->toHtml() ?></td>
    			<td width="*" class="bodyCenterTd">
					<?
					if (isset($_GET['edit'])) {
						echo $editor->toHtml();
					} elseif (isset($_GET['view']) && isset($_GET['id'])) {
						$viewer =& new AnnouncementViewer($user, ANNOUNCEMENTVIEWER_SINGLE, $_GET['id']);
						echo $viewer->toHtml();
					} else {
						$viewer =& new AnnouncementViewer($user, ANNOUNCEMENTVIEWER_FULL);
						echo $viewer->toHtml();
					}
					?>
				</td>
  			</tr>
  			<tr><td colspan="3" class="footerTd">&copy; EECS REFS</td></tr>
  			<tr><td colspan="3"><img src="images/footerShadow.jpg" alt=""/></td></tr>
		</table>
	</body>
</html>
