Location: PHPKode > scripts > XML Weblication > xml-weblication/lib/menu.php
<?

/**
  * Menu Class for weblication
  * @author Andreas Altendorfer <hide@address.com>
  * @date 2003-12-08
  */
  
class Menu
{

   var      $filename;        // filename 
   var      $xmlreader;       // class xml
   var      $xmlmenu;         // xml-structure for this menu
   
   var      $title;
   var      $mid;             // Menue ID (alqhanum)
   var      $type;            // none .... direct url
                              // module .. aid is modulename
							  // article . aid is ArticleID
   var      $url;             // "" or SELF will be replaced by PHP_SELF
   
   var      $htmltemplate;    // XML-Element <htmltemplate>....
   var      $menuinit;        // html-init eg table-open
   var      $itemtemplate;   // html-template with LINK to replace
   var      $menuclose;       // html-close eg table-close
   
   
   
   //////////////////////////////////////////////////////////////////////
   //  INTERFACE
   //////////////////////////////////////////////////////////////////////
   // Constructor
   function Menu( $filename ) {
      $this->filename = $filename;
   }

   function read() {
     global $cfg;
	 
     if ( ! file_exists( $this->filename ) ) {
	   return(false);
     }
	 $this->xmlreader = new xml( $this->filename );
	 $this->xmlreader->load();
	 
	 $htmltemplate = $this->xmlreader->element->findElement("htmltemplate");
	 if ( $htmltemplate ) 
	 {
	 
	   if ( $inc = $htmltemplate->getAttr("include") ) {
	     $xmlinc = new xml( $cfg["templates"] . "/" . $inc );
		 if ( $xmlinc->load() ) {
		   $newtemplate = $xmlinc->element;
		   $htmltemplate = $newtemplate;
		 }
		 else {
		   Fatal( "XML-Template " . $cfg["templates"] . "/" . $inc . " not accessable");
		 }
	   }
	   
	   
	   if ( $htmltemplate )  
	   {
	 
		   if ( $menuinit = $htmltemplate->findElement("menuinit") ) {
			 $this->menuinit = $menuinit->data;
		   } else { $this->menuinit = "["; }
		 
		   if ( $menuclose = $htmltemplate->findElement("menuclose") ) {
			 $this->menuclose = $menuclose->data;
		   } else { $this->menuclose = "]"; }
		   
		   $this->itemtemplate = $htmltemplate->data;
	   }
	 }
	 else {
	   $this->menuinit = "<table border=1>";
	   $this->menuclose= "</table>";
	   $this->itemtemplate = "<tr><td>LINK</td></tr>";
	 }
	 return( true );
   }
   
   function buildItem( $item ) {
     if ( $item->getAttr("type") == "seperator" ) {
	   $i1 = $this->xmlmenu->findElement( "seperator" );
	   if ( $i1 ) {
	     $init = $i1->getAttr("init");
		 $close= $i1->getAttr("close");
		 $rc = $init . $item->data . $close;
	   }
	   else { $rc = "<hr>"; }
       return( str_replace("LINK",$rc,$this->itemtemplate) );
	 }
	 else {
	 $a  = "<a href=" . $item->url . "?mid=" . $this->mid . 
	           "&id=" . $item->getAttr("id") . 
			   "&aid=" . $item->getAttr("aid") . 
			   "&type=" . $item->getAttr("type") . 
	       ">" . $item->data . "</a>";
	 }
		   
	 return( str_replace("LINK",$a,$this->itemtemplate ));
   }
   
   
   function parse() { 
     global $PHP_SELF;
     $this->xmlmenu = $this->xmlreader->element;
	 $this->title   = $this->xmlmenu->findElement("title");
	 $this->mid     = $this->xmlmenu->getAttr("mid");
	 $this->url     = $this->xmlmenu->getAttr("url");
	 $this->type    = $this->xmlmenu->getAttr("type");
	 if ( $this->url == "" || $this->url == false) $this->url = $PHP_SELF;
	 $items = array();
	 $n = $this->xmlmenu->findElements( $items, "item" );
	 
	 $rc = $this->title->data;
	 while ( list( $i, $item ) = each( $items ) ) {
	   $rc .= $this->buildItem( $item );
	 }
	 
	 return($rc);
   }

   // Main display Menu
   function display() {
     if ( $this->read() ) {
	   return( $this->menuinit . $this->parse() . $this->menuclose );
	 }
	 else {
	   return( "COULD NOT DISPLAY MENU ". $this->filename );
	 }
	 
   } // -- display
   
   
  
} //- class Menu

?>
Return current item: XML Weblication