Location: PHPKode > projects > HTMLresourceKit > mysql_treemenu.php
<!-- mysql_treemenu - Klasse für die Erzeugung von dHTML-Baummenüs
aus MySQL-Datenbanken. Verwendet mysql_system.php
Version 0.0.1 - copyright by Achim Schrepfer, 2001 -->

<?
	//Hauptklasse für die Menüerzeugung
	class mysql_treemenu {
	
		var $js_props; //Hash mit JavaScript-Parametern (sh. Function output())
		var $elements; //Menüelemente
		var $submenus; //Submenüs
	
		function mysql_treemenu() {
		global $rk_path;
			$this->js_props = array (
				MTMenuImageDirectory => "$rk_path/images/",
				MTMRootIcon => "menu_new_root.gif",
				MTMenuText => "HTMLresourceKit",
				MTMRootColor => "#ff6600",
				MTMRootFont => "Arial, Helvetica, sans-serif",
				MTMRootCSSize => "84%",
				MTMRootFontSize => "-1"
			);
			
			$this->elements = array ();
			$this->submenus = array ();
		}
		
		//Integriert eine MySQL-Tabelle in das Menü
		//$group, $type, $content, $id sind Spaltennamen der
		//mit $table übergebenen Tabelle. Die vorliegende Funktion
		//ist noch sehr unflexibel, da sie nur zwei Hierarchiebenen
		//unterstützt.
		function add_table($table, $group, $type, $content, $id, $href) {
			//Alle Gruppen holen
			$g_query = mysql_query("SELECT DISTINCT $group FROM $table");
			
			//Gruppen durchlaufen
			$grp_id = 0;
			while ($row = mysql_fetch_array($g_query)) {
			
				//Untermenü für Gruppe erstellen
				$this->add_submenu("menu",$row[0],"grp$grp_id");
				
				//Typen für aktuelle Gruppe holen
				$t_query = mysql_query("SELECT DISTINCT $type FROM $table WHERE $group='$row[0]'");
				
				$typ_id=0;
				while ($row1 = mysql_fetch_array($t_query)) {
					
					//Untermenü für Typ hinzufügen
					$this->add_submenu("grp$grp_id",$row1[0],"type".$grp_id."_".$typ_id);
					
					//Text und Datensatz-ID holen
					$i_query=mysql_query("SELECT $id,$content FROM $table WHERE ($group='$row[0]') AND ($type='$row1[0]')");
					
					$text_id=0;
					while ($row2=mysql_fetch_array($i_query)) {
						$myhref = $href;
						$myhref = str_replace("#id#",$row2[0],$myhref);
						$this->add_item("type".$grp_id."_".$typ_id,$row2[1],$myhref);
						$text_id++;
					}
					
					$typ_id++;
				}
			
				$grp_id++;
			}
			
		}
		
		function add_item($to_menu,$text,$href) {
			$elmt = array ( 
				"to" => $to_menu,
				"text" => $text,
				"href" => $href
			);
			array_push($this->elements, $elmt); 
		}
		
		function add_submenu($to_menu,$text,$name) {
			$elmt = array ( 
				"to" => $to_menu,
				"text" => $text,
				"name" => $name
			);
			array_push($this->submenus, $elmt); 
		}
		
		//Titel für das Treemenu setzen
		function title($title) {
			$this->js_props["MTMenuText"] = $title;
		}
		
		//Gibt den JavaScript/HTML-Code für das Baummenü aus
		function output() {
		global $rk_path;
			echo '
			<script type="text/javascript" src="'.$rk_path.'/treemenu/mtmcode.js"></script>
			
			<script type="text/javascript">
			// Mortens JavaScript Tree Menu
			// version 2.3.0, dated 2001-04-30
			// http://www.treemenu.com/
			
			//HTMLresourceKit-Version modified by Achim Schrepfer,
			//version 0.0.1
			
			// Copyright (c) 2001, Morten Wang & contributors
			// All rights reserved.
			
			// This software is released under the BSD License which should accompany
			// it in the file "COPYING".  If you do not have this file you can access
			// the license through the WWW at http://www.treemenu.com/license.txt
			
			/******************************************************************************
			* User-configurable options.                                                  *
			******************************************************************************/
			
			// Menu table width, either a pixel-value (number) or a percentage value.
			var MTMTableWidth = "100%";
			
			// Name of the frame where the menu is to appear.
			var MTMenuFrame = "menu";
			
			// variable for determining whether a sub-menu always gets a plus-sign
			// regardless of whether it holds another sub-menu or not
			var MTMSubsGetPlus = "Always";
			
			// variable that defines whether the menu emulates the behaviour of
			// Windows Explorer
			var MTMEmulateWE = false;
			
			// Directory of menu images/icons
			'.$this->jsp("MTMenuImageDirectory").'
			
			// Variables for controlling colors in the menu document.
			// Regular BODY atttributes as in HTML documents.
			var MTMBGColor = "#ffffcc";
			var MTMBackground = "";
			var MTMTextColor = "#0033cc";
			
			// color for all menu items
			var MTMLinkColor = "#0066ff";
			
			// Hover color, when the mouse is over a menu link
			var MTMAhoverColor = "#9999ff";
			
			// Foreground color for the tracking & clicked submenu item
			var MTMTrackColor ="Black";
			var MTMSubExpandColor = "#cc0000";
			var MTMSubClosedColor = "#3333ff";
			
			// All options regarding the root text and its icon
			'.$this->jsp("MTMRootIcon").'
			'.$this->jsp("MTMenuText").'
			'.$this->jsp("MTMRootColor").'
			'.$this->jsp("MTMRootFont").'
			'.$this->jsp("MTMRootCSSize").'
			'.$this->jsp("MTMRootFontSize").'
			
			// Font for menu items.
			var MTMenuFont = "Arial, Helvetica, sans-serif";
			var MTMenuCSSize = "84%";
			var MTMenuFontSize = "-1";
			
			// Variables for style sheet usage
			// "true" means use a linked style sheet.
			var MTMLinkedSS = false;
			var MTMSSHREF = "style/menu.css";
			
			// Additional style sheet properties if you re not using a linked style sheet.
			// See the documentation for details on IDs, classes & elements used in the menu.
			// Empty string if not used.
			var MTMExtraCSS = "";
			
			// Header & footer, these are plain HTML.
			// Leave them to be "" if youre not using them
			
			var MTMHeader = "";
			var MTMFooter = "";
			
			// Whether you want an open sub-menu to close automagically
			// when another sub-menu is opened.  "true" means auto-close
			var MTMSubsAutoClose = false;
			
			// This variable controls how long it will take for the menu
			// to appear if the tracking code in the content frame has
			// failed to display the menu. Number if in tenths of a second
			// (1/10) so 10 means "wait 1 second".
			var MTMTimeOut = 25;
			
			// Cookie usage.  First is use cookie (yes/no, true/false).
			// Second is cookie name to use.
			// Third is how many days we want the cookie to be stored.
			
			var MTMUseCookies = false;
			var MTMCookieName = "MTMCookie";
			var MTMCookieDays = 3;
			
			// Tool tips.  A true/false-value defining whether the support
			// for tool tips should exist or not.
			var MTMUseToolTips = true;
			
			/******************************************************************************
			* User-configurable list of icons.                                            *
			******************************************************************************/
			
			var MTMIconList = null;
			MTMIconList = new IconList();
			MTMIconList.addIcon(new MTMIcon("menu_link_external.gif", "http://", "pre"));
			MTMIconList.addIcon(new MTMIcon("menu_link_pdf.gif", ".pdf", "post"));
			
			
			var menu = null;
			menu = new MTMenu();
			
			'; //Ende Echo
			
			//Submenüs mit Items
			echo "\n//Submenüs erzeugen\n";
			while ( list($key,$value) = each ($this->submenus) ) {
				echo 'var '.$value['name'].' = null; '.$value['name'].' = new MTMenu();'."\n";
				
				//Submenuitems
				echo "\n//Unterelemente für ".$value['name']." erzeugen\n";
				while ( list($k,$v) = each ($this->elements) ) {
					if ($v["to"] == $value["name"]) {
						echo $v["to"].'.MTMAddItem(new MTMenuItem("'.$v["text"].'", "'.$v["href"].'", "text"));'."\n";
					}
				}
				reset($this->elements);
				
			}
			reset($this->submenus);
			
			//Hauptmenuitems
			echo "\n//Hauptmenüelemente\n";
			while ( list($key,$value) = each ($this->elements) ) {
				if ($value["to"] == "menu") {
					echo $value["to"].'.MTMAddItem(new MTMenuItem("'.$value["text"].'", "'.$value["href"].'", "text"));'."\n";
				}
			}
			
			echo "\n//Submenüs anhängen\n";
			while ( list($key,$value) = each ($this->submenus) ) {
			
				//Element erzeugen, an das das Submenu angehängt wird
				echo $value["to"].'.MTMAddItem(new MTMenuItem("'.$value["text"].'"));'."\n";
			
				//Submenü anhängen
				echo $value["to"].'.items['.$value["to"].'.items.length-1].MTMakeSubmenu('.$value["name"].');'."\n";
			}
			
			echo "</script>\n\n";
		}
		
		//JavaScriptProperty: gibt eine JS-Variablendefinition zurück
		function jsp($key, $type="string") {
			switch ($type):
				default:
					return 'var '.$key.' = "'.$this->js_props[ $key ].'";';
			endswitch;
		}
		
	} //Ende mysql_treemenu

?>
Return current item: HTMLresourceKit