Login   Register  
PHP Classes
elePHPant
Icontem

File: class/xmlmenuhasnone.cls.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Johan Barbier  >  XML to HTML menu  >  class/xmlmenuhasnone.cls.php  
File: class/xmlmenuhasnone.cls.php
Role: Class source
Content type: text/plain
Description: xml menu class when no extension are enabled
Class: XML to HTML menu
Generate menus in XML and HTML
 

Contents

Class file image Download
<?php
/**
* xmlmenuhasnone
* DOMDocument and XSLTProcessor are disabled
*
* @author: johan <barbier_johan@hotmail.com>
*/
class xmlmenuhasnone extends abstractxmlmenu {

    
/**
    * public function __construct
    * constructor
    * @Param (string) sVersion : xml version
    * @Param (string) sEncoding : xml encoding
    */
    
public function __construct ($sVersion null$sEncodingnull) {
        
$sVersion = (is_null ($sVersion)?'1.0':$sVersion);
        
$sEncoding = (is_null ($sEncoding)?'iso-8859-1':$sEncoding);
        
$this -> sXml .= '<?xml version="'.$sVersion.'" encoding="'.$sEncoding.'"?>';
        
$this -> sXml .= '<menu></menu>';
        
parent::__construct ($sVersion$sEncoding);
    }

    
/**
    * 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) {
        
$iNewId substr_count ($this -> sXml'xml:id') + 1;
        
$sAttr '';
        if (!empty (
$aAttr) && is_array ($aAttr)) {
            foreach (
$aAttr as $attrName => $attrValue) {
                
$sAttr .= $attrName.'="'.$attrValue.'" ';
            }
        }
        
$sTempXml '<node xml:id="'.$iNewId.'" '.$sAttr.'>'.$sText.'</node>';
        if (
$iId === 0) {
            
$iLastPos strpos ($this -> sXml'</menu>');
            
$this -> sXml substr_replace ($this -> sXml$sTempXml$iLastPos0);
        } else {
            
$iIdPos strpos ($this -> sXml'xml:id="'.$iId.'"');
            
preg_match_all ('@(\<node|node>)+@im'$this -> sXml$aNodesPREG_OFFSET_CAPTURE$iIdPos);
            
$iOpened 1;
            
$iClosed 0;
            foreach (
$aNodes[0] as $clef => $val) {
                if (
$val[0] === '<node') {
                    
$iOpened ++;
                } else {
                    
$iClosed ++;
                    
$iTmpPos $val[1];
                }
                if (
$iOpened === $iClosed) {
                    
$iLastPos $iTmpPos;
                    break;
                }
            }
            
$this -> sXml substr_replace ($this -> sXml$sTempXml$iLastPos 20);
        }
        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) {
        
$iTempPos strpos ($this -> sXml'xml:id="'.$iId.'"');
        
$iLastPos strpos ($this -> sXml'>'$iTempPos);
        
$sString 'link="'.$sLink.'"';
        
$this -> sXml substr_replace ($this -> sXml$sString$iLastPos0);
    }

    
/**
    * 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) {
        
$iTempPos strpos ($this -> sXml'xml:id="'.$iId.'"');
        
$iLastPos strpos ($this -> sXml'>'$iTempPos);
        
$sAttr '';
        foreach (
$aAttr as $attrName => $attrValue) {
            
$sAttr .= $attrName.'="'.$attrValue.'" ';
        }
        
$this -> sXml substr_replace ($this -> sXml$sAttr$iLastPos0);
    }

    
/**
    * public function __toString
    * method to return the XML of a menu
    * @return (string) iId : XML string
    */
    
public function __toString () {
        return 
htmlentities ($this -> sXml);
    }

    
/**
    * public function xmlToFile
    * method to save the xml to a file
    * @Param (string) sFileName :filename
    */
    
public function xmlToFile ($sFileName) {
        
$fp fopen ('xml/'.$sFileName.'.xml''w+');
        
fwrite ($fp$this -> sXml);
        
fclose ($fp);
    }

    
/**
    * public function fileToXml
    * method to load an xml from a file
    * @Param (string) sFileName :filename
    */
    
public function fileToXml ($sFileName) {
        
$this -> sXml file_get_contents ('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) {
        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 -> sXml;
        
$iPos strpos ($this -> sHtml'?>');
        
$this -> sHtml substr_replace ($this -> sHtml$sXsl$iPos 20);
        
$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;
    }
}
?>