<?php

class SiteAdmin
{

    /**
     * Repertoire de stockage des elements clients
     */
    var $directory = 'c:/tmp';

    /**
     * Nom du fichier contenant la structre des div du sites
     */
    var $myFile = 'structure.xml';

    /**
     * Contenu du stream
     */
    var $sData = '';

    var $dom = null;

    var $elemBklClient = null;

    function __construct($directory)
    {
        $this->directory = $directory;
        $this->dom = new DOMDocument();
        $this->dom->preserveWhiteSpace = true;
    }

    function Save($str)
    {
        if ($fh = fopen($this->directory . "/" . $this->myFile, 'wb')) {
            $this->sData = getValue("xml");
            
            fwrite($fh, $this->sData);
            fclose($fh);
            return true;
        } else {
            return false;
        }
    }

    function Load()
    {
        $sFilename = $this->directory . "/" . $this->myFile;
        if ($rHandle = @fopen($sFilename, 'rb')) {
            $this->sData = '';
            while (! feof($rHandle))
                $this->sData .= fread($rHandle, filesize($sFilename));
            
            fclose($rHandle);
            return true;
        }
        return false;
    }

    function &getStructure()
    {
        $this->dom->loadXML($this->sData);
        return new DOMXPath($this->dom);
    }

    function RegeneXsl($xsl)
    {
        if ($this->Load()) {
            $xpath = $this->getStructure();
            
            // Traitement de la xsl
            // Sur les 2 documents on va se baser sur les noeuds id=container
            
            $this->elemBklClient = null;
            $xpathXsl = new DOMXPath($xsl);
            if ($elemBase = $xpath->query("//*[@id='container']")) {
                if ($elemXsl = $xpathXsl->query("//*[@id='container']")) {
                    // Recherche du composant box utilisateur
                    
                    $elemClientNodeList = $xpathXsl->query("//*[@name='BklClient']/div");
                    if ($elemClientNodeList->length > 0) {
                        // Recuperation du div
                        $this->elemBklClient = $elemClientNodeList->item(0);
                    }
                    // A partir de là on va replacer les elements du dom elemXsl sur la base du concept elemBase
                    if (! is_null($elemBase)) {
                        foreach ($elemBase as $element) {
                            
                            $this->TraiteArboXsl($xpathXsl, $element, $elemXsl->item(0));
                        }
                    }
                }
            }
        }
    }

    function TraiteArboXsl($xpathXsl, $element, $elemXsl)
    {
        // Le but de cette fonction est de re-ordonne chaque niveau
        if ($element->hasChildNodes()) {
            $retry = true;
            
            while ($retry) {
                $retry = false;
                $first = true;
                $child = $element->firstChild;
                $childXsl = $elemXsl->firstChild;
                
                $level = 0;
                $levelXsl = 0;
                $baseXls = null;
                $ok = true;
                
                while ($child != null) {
                    if ($child->nodeType == XML_ELEMENT_NODE) {
                        if ($child->hasAttributes()) {
                            $id = $child->getAttribute("id");
                            
                            if ($id > "") {
                                // ON recherche cet id dans la xsl
                                $trouve = $childXsl;
                                $levelXsl = $level;
                                // print $id." ($level $levelXsl) <br/>";
                                $ok = true;
                                $idXsl = "";
                                while ($trouve && $ok) {
                                    if ($trouve->nodeType == XML_ELEMENT_NODE) {
                                        if ($baseXls == null)
                                            $baseXls = $trouve;
                                        if (strtoupper($trouve->nodeName) == "DIV") {
                                            if ($trouve->getAttribute("class") != "moveDep") {
                                                $idXsl = $trouve->getAttribute("id");
                                                if ($first) {
                                                    $childXsl = $trouve;
                                                    $first = false;
                                                }
                                                
                                                if ($idXsl > "") {
                                                    if ($idXsl == $id) {
                                                        $baseXls = $trouve;
                                                        $strstyle = $child->getAttribute("style");
                                                        
                                                        if (isset($style))
                                                            unset($style);
                                                        
                                                        $info = explode(';', $strstyle);
                                                        
                                                        foreach ($info as $index => $elementStyle) {
                                                            list ($code, $valeur) = explode(":", $elementStyle);
                                                            $style[$code] = $valeur;
                                                        }
                                                        $style["float"] = $child->getAttribute("float");
                                                        $style["width"] = $child->getAttribute("width");
                                                        $style["height"] = $child->getAttribute("height");
                                                        
                                                        $url = $child->getAttribute("url");
                                                        if ($url != null && $url != "") {
                                                            // Affectation dans le tag iframe du src
                                                            if ($elemBase = $xpathXsl->query("*/iframe", $trouve)) {
                                                                if (! is_null($elemBase)) {
                                                                    foreach ($elemBase as $elementSuite) {
                                                                        $elementSuite->setAttribute("src", $url);
                                                                    }
                                                                }
                                                            }
                                                            if ($elemBase = $xpathXsl->query("*/xsl:with-param[@name='url']", $trouve)) {
                                                                if (! is_null($elemBase)) {
                                                                    foreach ($elemBase as $elementSuite) {
                                                                        if ($elementSuite->hasChildNodes())
                                                                            $elementSuite->removeChild($elementSuite->firstChild);
                                                                        $elementSuite->appendChild($newText = new DOMText($url));
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        
                                                        // On recompose le style ensuite
                                                        
                                                        $strstyle = "";
                                                        foreach ($style as $index => $elementStyle) {
                                                            if ($index != "" && $elementStyle != "") {
                                                                $strstyle .= $index . ":" . $elementStyle . ";";
                                                            }
                                                        }
                                                        if ($strstyle != "") {
                                                            $trouve->setAttribute("style", $strstyle);
                                                        }
                                                        
                                                        if ($level != $levelXsl) {
                                                            // print " --> ".$trouve->nodeName." ($level $levelXsl) ".$idXsl." (swap) <br/>";
                                                            // On doit faire une operation sur les div pour les reoordonner
                                                            // donc on vas swapper les references $childXsl et $trouve
                                                            $node1 = $childXsl->cloneNode(true);
                                                            $node2 = $trouve->cloneNode(true);
                                                            
                                                            $trouve->parentNode->replaceChild($node2, $childXsl);
                                                            $trouve->parentNode->replaceChild($node1, $trouve);
                                                            $retry = true;
                                                            $child = &$node1;
                                                            $childXsl = $child->nextSibling;
                                                        } else {
                                                            // print " --> ".$trouve->nodeName." ($levelXsl) ".$idXsl."<br/>";
                                                            $childXsl = $trouve->nextSibling;
                                                        }
                                                        // Mise a jour des attributes
                                                        $this->TraiteArboXsl($xpathXsl, $child, $trouve);
                                                        $ok = false;
                                                    }
                                                }
                                                $levelXsl ++;
                                            }
                                        }
                                    }
                                    $trouve = $trouve->nextSibling;
                                }
                                $level ++;
                            }
                            // ajout d'un nouveau noeud dans la xsl
                            
                            if (strpos($id, "box_") === 0 && ! (strpos($idXsl, "box") === 0) && $baseXls != null && $this->elemBklClient != null) {
                                /*
                                 * print $id." -- ".
                                 * ($baseXls->nodeType == XML_ELEMENT_NODE
                                 * ? $baseXls->getAttribute("id")
                                 * : "")."<br/>";
                                 */
                                // On importe sur le noeud
                                $newNode = $this->elemBklClient->cloneNode(true);
                                $newNode->setAttribute("id", $id);
                                
                                $elemXsl->ownerDocument->importNode($newNode, true);
                                if ($baseXls->nextSibling) {
                                    $baseXls->parentNode->insertBefore($newNode, $baseXls->nextSibling);
                                } else {
                                    $elemXsl->appendChild($newNode);
                                }
                                $ok = false;
                                $retry = true;
                            }
                        }
                    }
                    if (! ok)
                        break;
                    $child = $child->nextSibling;
                }
            }
        }
    }
}

$SiteAdmin = new SiteAdmin("c:/tmp");
?>
