Location: PHPKode > projects > Informium > class/menu-class.php
<?

//
// Copyright (c) 2002, Cameron McKay
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:

// * Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright 
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

//
// Informium -- Advanced News Script
//
// Menu Class (menu-class.php)
//
// Author: Cameron McKay
// Note:   Class for constructing Informium menus.
//

class menu
{
	var $access;        // Access Level.
	var $access_title;  // Access Title.

	var $title_list;    // Access Title List.

	var $name;          // The menu's name.
	var $state;         // The menu's state (STATE_COL for collapsed, STATE_EXP for expanded).

	var $cookie_name;   // The name of the menu's state cookie.

	var $menu;          // The menu array.

	// Menu Constructor.
	function menu ($name = 'DEFAULT', $access = 0, $state = 'STATE_EXP')
	{
		// Make the 'menu' an array.
		$menu = array();

		// Define the access level for the user.
		$this->access = $access;

		// Define the name of the menu.
		// Make sure it only contains alphanumerics.
		$this->name = preg_replace('/\W+/s', '', $name);

		// Generate cookie name.
		$this->cookie_name = 'COOKIE_MENU_' . $this->name;

		// Determine the state of the menu.
		$cookie_state = $this->get_state($GLOBALS[$this->cookie_name]);

		// Check if we're collapsed or expanded.
		if (!strcmp($cookie_state, 'STATE_COL')) {
			$this->state = 'STATE_COL';

		} else if (!strcmp($cookie_state, 'STATE_EXP')) {
			$this->state = 'STATE_EXP';

		} else {
			$this->state = $state;

		}

		// Define Access Title List.
		$this->title_list[0] = 'User';
		$this->title_list[1] = 'Member';
		$this->title_list[2] = 'Moderator';
		$this->title_list[3] = 'Administrator';

		// Define the title for this user.
		$this->access_title = $this->title_list[$access];

	}

	//
	// Function: add ( $name, $address, $access )
	//
	// Purpose:  Inserts the menu option into the structure (I know, sooooo descriptive).
	//
	// Arguments:
	//   $name    -> The option's name (i.e. 'Insert New Article').
	//   $address -> The address of the option (i.e. 'admin.article.php?command=add').
	//   $access  -> The minimum access for that option (i.e. '1').
	//
	// Returns:  Nothing.
	//

	function add($name, $address, $access)
	{
		// Make it easier to reference.
		$menu =& $this->menu[];

		// The first item of every menu is the title.
		if (count($this->menu) == 1) {
			$menu[name] = $name;
		
		} else {
			$menu[name] = '&nbsp;&nbsp;&nbsp;' . $name;

		}

		$menu[address] = $address;
		$menu[access]  = $access;
	}

	//
	// Function: put ( )
	//
	// Purpose:  Prints a stylized menu for the access level defined by 'access' when the
	//           object was made.
	//
	// Returns:  Nothing.
	//

	function put()
	{
		// Import CONF.
		global $CONF;

		// Import SYSTEM class, if needed.
		require_once("$CONF[local_path]/class/system-class.php");

		// Make new SYSTEM object.
		$system = new system();

		// If the state is collapsed, then only show the title.
		if (!strcmp($this->state, 'STATE_COL')) {

			// Reference variable to easier name.
			$option =& $this->menu[0];

			// Encrypt the address.
			$address = $system->encrypt("menu.php?menu_name={$this->name}&state=STATE_EXP");

			// Display the option.
			echo "<option value='$address'>+ $option[name]</option>\n";

		// If the state is expanded, then show everything.
		} else if (!strcmp($this->state, 'STATE_EXP')) {

			for ($i = 0; $i < count($this->menu); ++$i) {
	
				// Reference variable to easier name.
				$option =& $this->menu[$i];

				// Check access.
				if ($this->access >= $option[access]) {

					// If it's the title, then indicate that the menu is expanded.
					if ($i == 0) {
						
						// Encrypt the address.
						$address = $system->encrypt("menu.php?menu_name={$this->name}&state=STATE_COL");
						
						// Display the option.
						echo "<option value='$address'>- $option[name]</option>\n";

					} else {		
						echo "<option value='$option[address]'>$option[name]</option>\n";

					}

				} // End if.

			} // End for.

		} // End if.

	}

	//
	// Function: set_state ( $state )
	//
	// Purpose:  Sets the collapsed or expanded state of the menu to a cookie
	//           on the user's machine.
	//
	// Arguments:
	//   $state -> The state of the menu, either STATE_COL (collapsed) or STATE_EXP (expanded).
	//
	// Returns:  Whatever setcookie() would return.
	//

	function set_state($state)
	{
		// Import CONF.
		global $CONF;

		// Import COOKIE class, if needed.
		require_once("$CONF[local_path]/class/cookie-class.php");

		// Make new COOKIE object.
		$cookie = new cookie();

		// Set the cookie.
		return $cookie->encode($this->cookie_name, $state);
	}

	//
	// Function: get_state ( $cookie_name )
	//
	// Purpose:  Gets the collapsed or expanded state of the menu in the cookie
	//           on the user's machine.
	//
	// Returns:  The menu's state (either STATE_COL or STATE_EXP).
	//

	function get_state($cookie_name)
	{
		// Import CONF.
		global $CONF;

		// Import COOKIE class, if needed.
		require_once("$CONF[local_path]/class/cookie-class.php");

		// Make new COOKIE object.
		$cookie = new cookie();

		// Read the cookie.
		$value = $cookie->decode($cookie_name);

		// Return the state.
		return $value[0];
	}

}
Return current item: Informium