PHP Classes

File: class/abstractxmlmenu.cls.php

Recommend this page to a friend!
  Classes of Johan Barbier   XML to HTML menu   class/abstractxmlmenu.cls.php   Download  
File: class/abstractxmlmenu.cls.php
Role: Class source
Content type: text/plain
Description: abstract xml menu class
Class: XML to HTML menu
Generate menus in XML and HTML
Author: By
Last change:
Date: 17 years ago
Size: 4,032 bytes
 

Contents

Class file image Download
<?php
class abstractxmlmenu {

   
/**
    * protected (object) doc
    * DOMDocument object
    */
   
protected $doc = null;

   
/**
    * protected (object::node) root
    * DOMDocument root node
    */
   
protected $root = null;

   
/**
    * 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);

        return
$this -> sHtml;
    }
}
?>