<?php
// $Header: /cvsroot/phptsmadmin/phpTSMadmin/lib/menu_html.php,v 1.4 2009/04/19 04:03:05 wurley Exp $

/**
 * This class will render a HTML version of the Menu.
 *
 * @package leenooksApp
 * @author Deon George
 */
class menu_html extends menu {
	protected $javascript = '';

	public function __construct($index) {
		$this->index = $index;
		$this->servers = $_SESSION[APPCONFIG]->servers->Instance($this->index);
	}

	/**
 	 * Displays the menu in HTML
	 */
	public function draw() {
		if (DEBUG_ENABLED)
			debug_log('Entered with ()',33,__FILE__,__LINE__,__METHOD__);

		echo '<table class="menu" border=0>';
		$this->draw_server_name();

		$this->javascript = '';
		$javascript_id = 0;

		# Is the user logged in, if so, show the menu.
		if ($this->servers->isLoggedIn('user')) {
			$this->draw_menu();
			$this->draw_logged_in_user();

			if ($this->servers->isReadOnly())
				printf('<tr><td class="spacer"></td><td class="links" colspan="%s">(%s)</td></tr>',$this->getDepth()+3-1,_('read only'));
			else
				printf('<tr><td class="blank" colspan="%s">&nbsp;</td></tr>',$this->getDepth()+3);

			# Draw the menu items.
			foreach ($_SESSION[APPCONFIG]->getCommandList() as $command) {
				printf('<tr class="option"><td class="expander"><img src="%s" /></td><td class="item"><a title="%s" href="cmd.php?cmd=%s&amp;index=%s">%s</a>',
					'images/minus.png',$command['desc'],$command['default'],$this->servers->getIndex(),$command['summary']);
			}

		# User not logged in, show a login link.
		} else {
			$this->draw_login_link();
		}

		# Menu Footer.
		# @todo: Need to implement a mechanism to have a footer, but not display it if it is blank.
		#printf('<tr><td class="foot" colspan="%s">%s</td></tr>',$this->getDepth()+3,'&nbsp;');
		echo '</table>';
		echo "\n\n";

		$this->draw_javascript();
	}

	protected function draw_server_name() {
		echo '<tr class="server">';
		printf('<td class="icon"><img src="images/server.png" alt="%s" /></td>',_('Server'));
		printf('<td class="name" colspan="%s">',$this->getDepth()+3-1);
		printf('%s',htmlspecialchars($this->servers->getValue('server','name')));

		if ($this->servers->isLoggedIn('user')) {
			$m = sprintf(_('Inactivity will log you off at %s'),
				strftime('%H:%M',time() + ($this->servers->getValue('login','timeout')*60)));
			printf(' <img width=14 height=14 src="images/timeout.png" title="%s" alt="%s"/>',$m,$m);
		}
		echo '</td></tr>';
	}

	protected function draw_menu() {
		$links = '';
		if (is_array($_SESSION[APPCONFIG]->getValue('menu','session')))
			foreach ($_SESSION[APPCONFIG]->getValue('menu','session') as $link => $title) {
				if ($link = $this->menu_item($link))
					$links .= sprintf('<td class="ds_option">%s</td>',$link);
			}

		# Finally add our logout link.
		$links .= sprintf('<td class="ds_option">%s</td>',$this->get_logout_menu_item());

		# Draw the quick-links below the server name:
		if ($links) {
			printf('<tr class="links"><td class="spacer"></td><td colspan="%s">',$this->getDepth()+3-1);
			printf('<table><tr>%s</tr></table>',$links);
			echo '</td></tr>';
		}
	}

	protected function menu_item($item) {
		$menu = $_SESSION[APPCONFIG]->getValue('menu','session');

		if (isset($menu[$item]))
			return sprintf('<a title="%s" href="cmd.php?cmd=%s&amp;index=%s"><img src="%s%s" alt="%s" /><br />%s</a>',
					$menu[$item]['text'],$menu[$item]['php'],$this->servers->getIndex(),IMGDIR,$menu[$item]['icon'],$item,$item);
		else
			return '&nbsp;';
	}

	protected function get_logout_menu_item() {
		global $app;

		$href = sprintf('cmd.php?cmd=logout&index=%s',$this->servers->getIndex());

		return sprintf('<a title="%s" href="%s"><img src="%s" alt="%s" /><br />%s</a>',
			_('Logout of this server'),htmlspecialchars($href),'images/logout.png',_('logout'),_('logout'));
	}

	protected function draw_logged_in_user() {
		if (! is_null($_SESSION[APPCONFIG]->getValue('appearance','logged_in_chars'))
			&& (strlen($this->servers->getLogin('user')) > $_SESSION[APPCONFIG]->getValue('appearance','logged_in_chars'))) {

			printf('<tr><td class="spacer"></td><td class="logged_in" colspan="%s"><span style="white-space: nowrap;">%s%s <acronym title="%s"><b>%s</b>...</acronym></span></td></tr>',
				$this->getDepth()+3-1,_('Logged in as'),_(':'),
				$this->servers->getLogin('user'),
				substr($this->servers->getLogin('user'),0,$_SESSION[APPCONFIG]->getValue('appearance','logged_in_chars')));

		} else
			printf('<tr><td class="spacer"></td><td class="logged_in" colspan="%s"><span style="white-space: nowrap;">%s%s <b>%s</b></span></td></tr>',
				$this->getDepth()+3-1,_('Logged in as'),_(':'),$this->servers->getLogin('user'));
	}

	protected function draw_login_link() {
		global $recently_timed_out_servers;

		$server = $this->getServer();

		$href = htmlspecialchars(
			sprintf('cmd.php?cmd=%s&index=%s',get_custom_file($server->getIndex(),'login_form',''),$this->servers->getIndex()));

		echo '<tr><td class="spacer"></td>';
		printf('<td class="icon"><a href="%s"><img src="images/uid.png" alt="%s" /></a></td>',$href,_('login'));
		printf('<td class="logged_in" colspan="%s"><a href="%s">%s</a></td>',$this->getDepth()+3-2,$href,_('Login').'...');
		echo '</tr>';

		printf('<tr><td class="blank" colspan="%s">&nbsp;</td>',$this->getDepth()+3);
		printf('<tr><td class="blank" colspan="%s">&nbsp;</td>',$this->getDepth()+3);

		# If the server recently timed out display the message
		if (is_array($recently_timed_out_servers) && in_array($this->servers->getIndex(),$recently_timed_out_servers))
			printf('<tr><td class="spacer"></td><td colspan="%s" class="links">%s</td></tr>',
				$this->getDepth()+3-1,_('(Session timed out. Automatically logged out.)'));
	}

	protected function draw_javascript() {
		if ($this->javascript) {
			echo "<!-- Forms for javascript submit to call to create base_dns -->\n";
			echo $this->javascript;
			echo "<!-- The end of the forms for javascript submit to call to create base_dns -->\n";
		}
	}

	/**
	 * Work out how long the deepest "opened" menu item is.
	 * This is used to dynamically build our cells for our menu table.
	 */
	protected function getDepth() {
		return 1;
	}
}
?>