File: class/xmlmenuhasdom.cls.php

Recommend this page to a friend!
  Classes of Johan Barbier  >  XML to HTML menu  >  class/xmlmenuhasdom.cls.php  >  Download  
File: class/xmlmenuhasdom.cls.php
Role: Class source
Content type: text/plain
Description: xml menu class when only DOMDocument is enabled
Class: XML to HTML menu
Generate menus in XML and HTML
Author: By
Last change: Modification : added abstract class
Date: 14 years ago
Size: 2,301 bytes
 

 

Contents

Class file image Download
<?php
/**
* class xmlmenuhasdom
* DOMDocument is enabled, XSLTProcessor is not
*
* @author: johan <barbier_johan@hotmail.com>
*/
class xmlmenuhasdom extends abstractxmlmenu {

   
/**
    * public function __construct
    * constructor
    * @Param (string) sVersion : xml version
    * @Param (string) sEncoding : xml encoding
    */

   
public function __construct ($sVersion = null, $sEncoding= null) {
       
$this -> doc = new DOMDocument ($sVersion, $sEncoding);
       
$root = $this -> doc -> createElement ('menu');
       
$this -> root = $this -> doc -> appendChild ($root);
       
parent::__construct ($sVersion, $sEncoding);
    }

   
/**
    * 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) {
        return
false;
    }

   
/**
    * 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;
        }
       
/**
        * Difficult bit here that needs a bit of explanation
        * We do not have XSLTProcessor availaible.
        * So, we cannot use it to apply the stylesheet.
        * What we do is :
        * we take the generated xml.
        * We add a stylesheet import by parsing the text.
        * we save it to a temporary folder
        * we return only a link to the newly created file.
        * now, to have the html version of the xml , user will need to save it via his web browser...
        */
       
$sXsl = '<?xml-stylesheet type="text/xsl" href="../xsl/'.$type.'"?>';
       
$this -> sHtml = $this -> doc -> saveXML ();
       
$iPos = strpos ($this -> sHtml, '?>');
       
$this -> sHtml = substr_replace ($this -> sHtml, $sXsl, $iPos + 2, 0);
       
$iCpt = count (scandir ('tmp_xml'));
       
$fp = fopen ('tmp_xml/tmp_xmllfile_'.$iCpt.'.xml', 'w+');
       
fwrite ($fp, $this -> sHtml);
       
fclose ($fp);
       
$this -> sHtml = '<a href="tmp_xml/tmp_xmllfile_'.$iCpt.'.xml" title="Cliquer pour voir le html généré" target="_blank">Cliquer pour voir le html généré</a>';

        return
$this -> sHtml;
    }
}
?>


For more information send a message to info at phpclasses dot org.