Logo classes.scriptsphp.org PHP

go to nav bar

HelpML

Manipulation de documents HelpML

Cette classe permet de manipuler (créer, modifier, afficher) des documents au format HelpML 0.1 (basé sur XML). Ce format permet essentiellement la création de FAQ.

Les Méthodes

Exemples

  1. Exemple de fichier HelpML
  2. Création d'un document HelpML

La Source

<?php 

class HelpML {

    public 
$HelpMLVersion '0.1';

    private 
$XMLEncoding 'ISO-8859-1';
    private 
$document = array();

    private 
$XMLReturn '';


    public function 
__construct($file null) {
        if(
$file) {
            if(!
$this->xml = @simplexml_load_file($file)) {
                throw new 
Exception('XML''Wrong XML File');
            }
            
$this->buildArrayFromFile();
        }
    }

    public function 
setEncoding($encoding) {
        
$this->XMLEncoding $encoding;
    }

    public function 
addTitle($title) {
        
$this->document['title'] = $title;
    }

    public function 
addURL($URL) {
        
$this->document['URL'] = $URL;
    }

    public function 
addDescription($description) {
        
$this->document['description'] = $description;
    }

    public function 
addTopic($title$keywords$answer$catid null) {
        
$this->document['topics'][md5($title)] = array (
        
'title' => $title,
        
'keywords' => $keywords,
        
'answer' => $answer,
        
'catid' => $catid);
    }

    public function 
unsetTopic($title) {
        unset(
$this->document['topics'][md5($title)]);
    }

    public function 
addCatId($catid$name) {
        
$this->document['category'][(string)$catid] = $name;
    }

    public function 
addInfo($info) {
        
$this->document['info'] = $info;
    }

    public function 
toXML($intoFile null) {
        
$r '';
        if(
$intoFile) {
            
$r '<?xml version="1.0" encoding="'.$this->XMLEncoding.'" ?>';
        }
        
$r .= '<helpml version="'.$this->HelpMLVersion.'">';
        
$r .= $this->makeDocument();
        
$r .= $this->makeInfo();
        
$r .= $this->makeTopics();
        
$r .= $this->makeCatId();
        
$r .= '</helpml>';

        if(
$intoFile) {
            
$this->writeToFile($intoFile$r);
        }
        return 
$this->XMLReturn $r;
    }

    public function 
toHTML($intoFile null$xsl null) {
        
$xslt = new XSLTProcessor();

        
// Chargement du fichier XML
        
$xml = new domDocument();
        if(!
$this->XMLReturn) {
            
$this->XMLReturn $this->toXML();
        }
        
$xml -> loadXML($this->XMLReturn);

        
// Chargement du fichier XSL
        
$xsl = new domDocument();
        
$xsl -> load('http://csp.biophyse.net/helpml.xsl');

        
// Import de la feuille XSL
        
$xslt -> importStylesheet($xsl);

        
// Transformation et affichage du résultat
        
return $xslt -> transformToXml($xml);
    }

    private function 
buildArrayFromFile() {

        
// bloc document
        
$document $this->xml->xpath('document');
        if(isset(
$document[0]->title)) {
            
$this->document['title'] = $document[0]->title;
        }
        if(isset(
$document[0]->url)) {
            
$this->document['URL'] = $document[0]->url;
        }
        if(isset(
$document[0]->description)) {
            
$this->document['description'] = $document[0]->description;
        }

        
// bloc topics
        
$topics $this->xml->xpath('topics/topic');
        foreach(
$topics as $topic) {
            
$this->document['topics'][md5($topic->title)] = array (
            
'title' => $topic->title,
            
'keywords' => $topic->keywords,
            
'answer' => $topic->answer,
            
'catid' => $topic->catid);
        }

        
// bloc categories
        
$cats $this->xml->xpath('categories/category');
        foreach(
$cats as $cat) {
            
$att $cat->attributes();
            
$this->addCatId($att[0], $cat);
        }

    }

    private function 
makeDocument() {
        
$r =  '<document>';
        
$r .= $this->makeDocumentTitle();
        
$r .= $this->makeDocumentURL();
        
$r .= $this->makeDocuementDescription();
        
$r .= '</document>';
        return 
$r;
    }

    private function 
makeDocumentTitle() {
        if(isset(
$this->document['title'])) {
            return 
'<title>' $this->document['title'] . '</title>';
        }
        return 
'';
    }

    private function 
makeDocumentURL() {
        if(isset(
$this->document['URL'])) {
            return 
'<url>' $this->document['URL'] . '</url>';
        }
        return 
'';
    }

    private function 
makeDocuementDescription() {
        if(isset(
$this->document['description'])) {
            return 
'<description>' $this->document['description'] . '</description>';
        }
        return 
'';
    }

    private function 
makeInfo() {
        if(!empty(
$this->document['info'])) {
            return 
'<info>'.$info.'</info>';
        }
        return 
'';
    }

    private function 
makeTopics() {
        
$r '';
        if(!empty(
$this->document['topics'])) {
            
$r .= '<topics>';
            foreach(
$this->document['topics'] as $topic) {
                
$r .= '<topic>';
                
$r .= '<title>'.$topic['title'].'</title>';
                
$r .= '<keywords>'.$topic['keywords'].'</keywords>';
                
$r .= '<answer>'.$topic['answer'].'</answer>';
                if(!empty(
$topic['catid'])) {
                    
$r .= '<catid>'.$topic['catid'].'</catid>';
                }
                
$r .= '</topic>';
            }
            
$r .= '</topics>';
        }
        return 
$r;
    }

    private function 
makeCatId() {
        
$r '';
        if(!empty(
$this->document['category'])) {
            
$r .= '<categories>';
            foreach(
$this->document['category'] as $name => $value) {
                
$r .= '<category id="'.$name.'">'.$value.'</category>';
            }
            
$r .= '</categories>';
        }
        return 
$r;
    }

    private function 
writeToFile($filename$content) {
        return @
file_put_contents($filename$content);
    }
}

?>

Merci de ne pas suivre ce lien emails.

0.0477s | «»
PHP powered