see index.php for more information, and comments in classes.
DESCRIPTION
This package is meant to : - generate an XML menu - generate an HTML menu using the generated XML
xmlmenu::getInstance () determines which extensions are or are not enabled, and instanciates the correct class.
First step : - generate the XML. Can be done with the class, or manually (the xml files are stored in 'xml/'. They look like :
3 methods : xmlmenu::defineNode () to define a new node xmlmenu::defineAttributes () to define attributes for a given node (can be done directly with xmlmenu::defineNode()) xmlmenu::defineLink () to define the href link of the node
Notice : the node are attributed an id via xml:id.
- see the generated xml via echo $menu (xmlmenu::__toString() method)
- save the xml file if needed via xmlmenu::xmlToFile()
- load an xml from a file via xmlmenu::fileToXml()
- apply an XSL to the menu to generate an HTML menu via xmlmenu::toHTML() The xsl needs to be created first, of course. I have created 2, they are stored in 'xsl/'.
/** * protected (array) aTypes * XML to HTML XSLT types */ protected $aTypes = array ();
/** * protected (string) sHtml * HTML menu string */ protected $sHtml = '';
/** * protected (string) sXml * XML menu string */ protected $sXml = '';
/** * public function __construct * constructor * @Param (string) sVersion : xml version * @Param (string) sEncoding : xml encoding */
public function __construct ($sVersion = null, $sEncoding= null) { $aTypes = scandir ('xsl'); foreach ($aTypes as $type) { $this -> aTypes[$type] = strtoupper (substr ($type, 0, -4)); } }
/** * public function defineNode * method defining a node * @Param (string) sText : text of the node * @Param (array) aAttr : array of attributes for the node * @Param (int) iId : id of the parent node * @Return (int) new node's id */ public function defineNode ($sText, $aAttr = array (), $iId=0) { $newElem = $this -> doc -> createElement ('node', $sText); $dump = $this -> root -> getElementsByTagName('node'); $iNewId = $dump -> length + 1; if($iId===0){ $newElem = $this -> root -> appendChild ($newElem); } else { $parent = $this -> doc -> getElementById($iId); $newElem = $parent -> appendChild ($newElem); } $newElem -> setAttribute ('xml:id', $iNewId); if (!empty ($aAttr) && is_array ($aAttr)) { $this -> defineAttributes ($aAttr, $iNewId); } return $iNewId; }
/** * public function defineLink * method defining a link on a given node * @Param (string) sLink : url of the link * @Param (int) iId : id of the node */ public function defineLink ($sLink, $iId) { $elem = $this -> doc -> getElementById($iId); $elem -> setAttribute ('link', $sLink); }
/** * public function defineAttributes * method defining attributes for a given node * @Param (array) aAttr : array of attributes * @Param (int) iId : id of the node */ public function defineAttributes ($aAttr, $iId) { $elem = $this -> doc -> getElementById($iId); foreach ($aAttr as $attrName => $attrValue) { $elem -> setAttribute ($attrName, $attrValue); } }
/** * public function __toString * method to return the XML of a menu * @return (string) iId : XML string */ public function __toString () { return htmlentities ($this -> doc -> saveXML ()); }
/** * public function xmlToFile * method to save the xml to a file * @Param (string) sFileName :filename */ public function xmlToFile ($sFileName) { $this -> doc -> save ('xml/'.$sFileName.'.xml'); }
/** * public function fileToXml * method to load an xml from a file * @Param (string) sFileName :filename */ public function fileToXml ($sFileName) { $this -> doc -> load ('xml/'.$sFileName.'.xml'); }
/** * public function htmlToFile * method to save the html to a file. * cannot be done if XSLTProcessor is not enabled (see comments in the xmlmenu::toHTML () method to learn how to save the HTML file) * @Param (string) sFileName :filename */ public function htmlToFile ($sFileName) { if (empty ($this -> sHtml)) { return false; } $fp = fopen ('html/'.$sFileName.'.html', 'w+'); fwrite ($fp, $this -> sHtml); fclose ($fp); return true; }
/** * public function toHTML * method to transform the xml to html * @Param (string) sType : XSL file to be used * @Return (string) sHtml : transformed HTML string */ public function toHTML ($sType) { if (false === ( $type = array_search($sType, $this -> aTypes))) { return false; } $xsl = new XSLTProcessor (); $xsl -> importStyleSheet (DOMDocument::load ('xsl/'.$type)); $this -> sHtml = $xsl -> transformToXML ($this -> doc);
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.