Login   Register  
PHP Classes
elePHPant
Icontem

File: _class.menu.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Justin Koivisto  >  phpTierMenu  >  _class.menu.php  >  Download  
File: _class.menu.php
Role: ???
Content type: text/plain
Description: Class definition file with examples at the end.
Class: phpTierMenu
A tree menu that has unlimited number of branches.
Author: By
Last change:
Date: 2001-03-21 20:00
Size: 8,324 bytes
 

Contents

Class file image Download
<?
	/*
	   This is my multi-tier menu system. The menu itself is printed into
	   a one-celled table. There are no collapse or expand functions, but
	   you have a high level of control for the rest of the looks. This has
	   great potential since I designed it in such a way to make it very
	   easy to use loops to set the data (especially if you store it in a
	   database like I do).

	   There is no limit to the number of tiers for this menu system, but
	   there are drawbacks also. For instance, if you specify your width
	   say as 150 pixels, and you use indent spacing, you'll have problems
	   with text wrapping or images cutting off. Text wrapping wouldn't be
	   so bad if it also justified itself after the line break.

	   One thing to note is the way the indents and spacing is done. I use
	   an image (1x1 pixel, transparent gif "images/spacer.gif") for all
	   the spacing, so you may want to either create that image, or find it
	   in the code below (lines 114, 160) to change it to your preference.

	   This is a great startting point for a more advanced menuing system.
	   What I have here serves my needs for now, so I don't think I'll be
	   doing a lot of work on it until it's needed. However, feel free to
	   make improvements and fix fea^H^H^H bugs. If you do, please send
	   me a copy!

	   Examples on use at the bottom of the file.

	   Last Modified by Justin Koivisto (justin@koivi.com) 3/21/01 6:42PM
	*/

$MENU_CLASS_INC=TRUE;

class menu_item_class{
	var $text;	// text of this item
	var $url;	// link - unquoted for ability to add target info
	var $font_size;	// the size that the text will appear as (if any)
	var $font_face;	// the face that the text will appear as (if any)
	var $font_color;	// the color that the text will appear as (if any)
			// NOTE: for the color, you can either use the names
			// (like 'blue') or the hexadecimal value beginning
			// with a pound sign (#)
	var $image;	// file to use as an image link (if any)
	var $image_height;	// the height in pixels of above
	var $image_width;	// the width in pixels of above
	var $items;	// array of children objects (indexed by child text)
	var $num_items;	// the number of sub-items for this one

	// Constructor
	function menu_item_class($text,$url=''){
		$this->text=$text;
		$this->url=$url;
		$this->num_items=0;
	}

	// functions to set values
	function setFontFace($value){$this->font_face=$value;}
	function setFontSize($value){$this->font_size=$value;}
	function setFontColor($value){$this->font_color=$value;}
	function setImage($value){$this->image=$value;}
	function setImageSize($width,$height){
		$this->image_width=$width;
		$this->image_height=$height;
	}

	// adding children to menu items
	function addSub($name,$url=''){
		$this->items[$name]=new menu_item_class($name,$url);
		$this->num_items++;
	}
}

class menu_class{
	var $width;		// the width of the table
	var $spacer;		// the height between items in pixels
	var $indent;		// the width in pixels of sub-item's indent
	var $bgcolor;		// the background color of the row (if any)
	var $background;	// the background image of the row (if any)
	var $padding;		// the cellpadding of the table
	var $border;		// the width of the border around the table
	var $align;		// the horizontal alignment of the cell
	var $valign;		// the vertical alignment of the cell
	var $items;		// the children of the menu (indexed by name)

	// Constructor
	function menu_class($name,$width){
		$this->width=$width;
		$this->padding=0;
		$this->border=0;
		$this->num_items=0;
	}

	// function to set table values
	function setSpacer($value){$this->spacer=$value;}
	function setIndent($value){$this->indent=$value;}
	function setColor($value){$this->bgcolor=$value;}
	function setBorder($value){$this->border=$value;}
	function setBackImage($value){$this->background=$value;}
	function setPadding($value){$this->padding=$value;}
	function setAlignment($horiz,$vert){$this->align=$horiz;$this->valign=$vert;}

	// how to add menu heading items
	function addItem($text,$url=''){
		$this->items[$text]=new menu_item_class($text,$url);
		$this->num_items++;
	}

	// print an item menu
	function printItem(&$tmp,$tier){
		$output='';

		// we need to check if we need an indent
		if($tier>0){
			$output.="<img src=images/spacer.gif width=";
			$indent=$tier*$this->indent;
			$output.="$indent height=1 align=left>";
		}
		$tier++;

		// now we need to add the link stuff
		if($tmp->url)
			$output.="<a href=$tmp->url>";

		// take care of the font stuff if set (otherwise we'll just
		// print an empty font tag - no big deal
		$output.="<font";
		if($tmp->font_face)
			$output.=" face=$tmp->font_face";
		if($tmp->font_size)
			$output.=" size=$tmp->font_size";
		if($tmp->font_color)
			$output.=" color=$tmp->font_color";
		$output.=">";

		// if we are using images, show it
		if($tmp->image){
			$output.="<img src=$tmp->image";
			if($tmp->image_width)
				$output.=" width=$tmp->image_width";
			if($tmp->image_height)
				$output.=" height=$tmp->image_height";
			$output.=" border=0 alt=\"$tmp->text\">";
		}else{
			// if not images, we need the text output then
			$output.=$tmp->text;
		}

		// close font tag
		$output.="</font>";

		// don't forget to close the anchor tag
		if($tmp->url)
			$output.="</a>";

		// add line break after each item
		$output.="<br>";

		// give the spacer a chance
		if($this->spacer)
			$output.="\n        <img src=images/spacer.gif width=1 height=$this->spacer><br>";

		// How about this item's children?
		if($tmp->num_items>0){
			foreach($tmp->items as $submenu){
				$output.=$this->printItem($submenu,$tier);
			}
		}

		return $output;
	}

	// print menu. returns HTML code from function rather than
	// printing directly to output
	function printMenu(){
		$output="\n\n<!-- BEGIN: $this->text -->\n";
		$output.=" <table width=$this->width cellspacing=0 cellpadding"
			."=$this->padding border=$this->border>\n  <tr";
		if($this->background)
			$output.=" background=$this->background";
		if($this->bgcolor)
			$output.=" bgcolor=$this->bgcolor";
		$output.=">\n   <td";
		if($this->align)
			$output.=" align=$this->align";
		if($this->valign)
			$output.=" valign=$this->valign";
		$output.=">";

		// now we need to print the menu items
		foreach($this->items as $item)
			$output.=$this->printItem($item,0);

		$output.="\n   </td>\n  </tr>\n </table>\n<!-- END: "
			."$this->text -->\n\n";
		return $output;
	}
}



// -------------------------------- EXAMPLES ---------------------------------

// start by defining the menu's looks and name
$menu=new menu_class('Menu Title',175);
$menu->setPadding(4);
$menu->setIndent(15);
$menu->setBorder(2);
$menu->setSpacer(7);
$menu->setColor("#dfdfdf");
$menu->setAlignment('left','top');

// now we will add in the menu items
$menu->addItem('Heading 1','1.htm'); // tier 1
$menu->items['Heading 1']->setFontSize(1);
$menu->items['Heading 1']->setFontFace("arial");
$menu->items['Heading 1']->setFontColor("#000000");
$menu->items['Heading 1']->addSub('Sub-heading','images/');
$menu->items['Heading 1']->addSub('Sub-heading0','images/');

$menu->addItem('Heading 2','2.htm'); // tier 1
$menu->items['Heading 2']->setImage("images/test.gif");
$menu->items['Heading 2']->setImageSize(125,20);

$menu->addItem('Heading 3','3.htm'); // tier 1
$menu->items['Heading 3']->addSub('1','images/');
$menu->items['Heading 3']->items['1']->setFontSize(1);
$menu->items['Heading 3']->items['1']->addSub('2','url');
$menu->items['Heading 3']->items['1']->items['2']->setFontSize(1);
$menu->items['Heading 3']->items['1']->items['2']->addSub('3','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->addSub('4','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->items['4']->addSub('5','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->items['4']->items['5']->addSub('6','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->items['4']->items['5']->items['6']->addSub('7','url');

$menu->addItem('Heading 4','4.htm'); // tier 1

echo $menu->printMenu();

?>