Location: PHPKode > projects > Aukyla Platform > aukyla/base/Navigation.php
<?php
/*
     Navigation.php, provides widgets used for navigation
     Copyright (C) 2004 Arend van Beelen, Auton Rijnsburg

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the Free
     Software Foundation; either version 2 of the License, or (at your option)
     any later version.

     This program is distributed in the hope that it will be useful, but WITHOUT
     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Config.php');
require_once('Login.php');
require_once('Output.php');
require_once('Widgets.php');
require_once('Windows.php');

/**
 * @internal
 */
class Tab
{
	public function __construct($id, $label)
	{
		$this->id = $id;
		$this->label = $label;
		$this->container = new RootContainer();
	}

	public $id;
	public $label;
	public $container;
}

/**
 * @brief A widget containing tab panels.
 *
 * A tab widget is a widget which contains tab panels. Every tab panel is a
 * container which can be accessed using the tab() function. You can add new
 * tabs using the addTab() function.
 *
 * When you compose a tab widget, you should provide the contents for all tabs,
 * the widget will take care of which tab to show. This is unlike HTML where
 * you would have to compose every panel yourself at different times.
 */
class TabWidget extends Widget
{
	/**
	 * Constructor.
	 *
	 * Creates a new tab widget.
	 *
	 * @param parent Parent container this widget should be added to.
	 * @param id     The ID of this widget.
	 */
	public function __construct(Container $parent, $id)
	{
		parent::__construct($parent, $id);

		if(Config::request("{$id}_currentTab") != '')
		{
			$this->currentTab = Config::request("{$id}_currentTab");
			Login::setSessionVariable("{$id}_currentTab", $this->currentTab);
		}
		else
		{
			$this->currentTab = Login::sessionVariable("{$id}_currentTab");
		}

		$this->panels = array();
		$this->parameters = '';
	}

	/**
	 * Adds a new tab to the widget.
	 *
	 * @param id    The ID of the tab panel.
	 * @param label The label to show on the tab.
	 *
	 * @return A container widget representing the tab to which you can add
	 *         contents.
	 */
	public function addTab($id, $label)
	{
		$this->tabs[] = $tab = new Tab($id, $label);

		return $tab->container;
	}

	/**
	 * Returns the container of the given tab. Use this container to add contents
	 * to the tab.
	 *
	 * @param id ID of the tab panel whose container will be returned.
	 *
	 * @return A container widget to which you can add contents. Returns @p null
	 *         if the given @p id is not found.
	 */
	public function tab($id)
	{
		foreach($this->tabs as $tab)
		{
			if($tab->id == $id)
			{
				return $tab->container;
			}
		}

		return null;
	}

	/**
	 * This is an advanced function which sets the internal container of a tab
	 * panel to the specified @p container. By doing this, you can create your
	 * own container and insert it into the tab widget afterwards.
	 */
	public function setTabContainer($id, Container $container)
	{
		foreach($this->tabs as $tab)
		{
			if($tab->id == $id)
			{
				$tab->container = $container;
				return;
			}
		}
	}

	/**
	 * Sets the tab with the given @p id to be the currently opened tab. As the
	 * tab widget will normally remember which tab was opened last, you should
	 * only call this function if you really want to force a certain tab to be
	 * opened.
	 */
	public function setCurrentTab($id)
	{
		$this->currentTab = $id;
	}

	/**
	 * Returns the currently opened tab.
	 *
	 * @return ID of the currently opened tab.
	 *
	 * @since 1.1
	 */
	public function currentTab()
	{
		return $this->currentTab;
	}

	/**
	 * Use this function to set any @p parameters on the "tab links" which show
	 * when the TabWidget is shown with XHTML output. These parameters can be used
	 * to make sure the correct page is served when the user switches tabs.
	 */
	public function setParameters($parameters)
	{
		$this->parameters = $parameters;
	}

	public function show()
	{
		if(sizeof($this->tabs) == 0)
		{
			return;
		}

		if($this->currentTab == '')
		{
			$this->currentTab = $this->tabs[0]->id;
		}

		Output::write('<tabwidget>'.
		              '<tabs>');

		$firstTab = true;
		foreach($this->tabs as $tab)
		{
			$selectedProperty = ($this->currentTab == $tab->id ? ' selected="true"' : '');
			$parameters = ($this->parameters == '' ? '' : "&amp;{$this->parameters}");

			if($firstTab == true)
			{
				$firstTab = false;
			}
			else
			{
				Output::write("<tabseperator/>");
			}

			Output::write("<tab id=\"{$tab->id}\" label=\"{$tab->label}\" ".
			              "href=\"".Config::globals('baseURL')."?{$this->id}_currentTab={$tab->id}$parameters\"$selectedProperty />");
		}

		Output::write('</tabs>'.
		              '<tabpanels>');

		foreach($this->tabs as $tab)
		{
			if($this->currentTab == $tab->id)
			{
				Output::write('<tabpanel>');
				$tab->container->show();
				Output::write('</tabpanel>');
			}
		}

		Output::write('</tabpanels>'.
		              '</tabwidget>');
	}

	private $currentTab;
	private $parameters;
	private $tabs;
}

/**
 * @brief General class for providing navigation.
 *
 * This class provides functions which can be used to navigate the user through
 * your application. Particular uses are creating links to other windows,
 * views, et cetera.
 */
class Navigation
{
	/**
	 * Returns a link to a window. This may be a URL or JavaScript code.
	 *
	 * @param window ID of the window to link to.
	 * @param secure Boolean determining whether the link should go over an SSL
	 *               connection.
	 *
	 * @sa Window
	 */
	public static function windowLink($window, $secure = false)
	{
		$baseUrl = ($secure ? Config::globals('secureBaseURL') : Config::globals('baseURL'));

		return "$baseUrl?window=$window";
	}

	/**
	 * Returns a link to a view. This may be a URL or JavaScript code.
	 *
	 * @param viewContainer The ID of the ViewContainer.
	 * @param view          ID of the view to link to.
	 * @param secure        Boolean determining whether the link should go over an SSL
	 *                      connection.
	 *
	 * @sa ViewContainer, View
	 */
	public static function viewLink($viewContainer, $view, $secure = false)
	{
		$baseUrl = ($secure ? Config::globals('secureBaseURL') : Config::globals('baseURL'));

		$windowManager = WindowManager::instance();

		return "$baseUrl?window=".$windowManager->window()."&amp;$viewContainer=$view";
	}
}

?>
Return current item: Aukyla Platform