Subversion Repositories eFlore/Archives.eflore-consultation-v2

Rev

Rev 87 | Rev 140 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

class macroElement implements iMacroElement{
    
    protected $identifiantblockdedonnees;
    protected $identifiant_parent;
    protected $connexion;
    protected $contexteRef;
    protected $contexteRefTmp;
    protected $contexteSQL;
    protected $blockdedonneesParent;
    protected $collectionblockdedonnees;
    protected $idRes;
    
    function __construct($une_connexion, $un_block_donnees_parent, $chemin_macro_element)
    {
        $this->connexion = $une_connexion;
        $this->blockdedonneesParent = $un_block_donnees_parent;
        $tab_etape_chemin = array_reverse(explode('>', $chemin_macro_element));
        $identifiant = $tab_etape_chemin[0];
        $this->identifiantblockdedonnees = $identifiant;
        if (count($tab_etape_chemin) > 1) {
            $this->identifiant_parent = $tab_etape_chemin[1];
            $this->contexteRef = $this->attribuerContexteRef($this->blockdedonneesParent);
        } else {
            $this->identifiant_parent = NULL;
            $this->contexteRef = $this->blockdedonneesParent->recupererDonnees();
        }
    }
    
    function attribuerContexteRef($un_block_donnees)
    {
        $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
        for($i = 0; $i < count($collection_block_fils); $i++) {
            $collection = $collection_block_fils[$i];
            if ($collection->recupererIdentifiant() == $this->identifiant_parent) {
                return $collection->recupererBlocksDeDonnees();
            } else if (is_object($un_block_donnees->recupererCollectionBlockFils())) {
                return $this->attribuerContexteRef($un_block_donnees->recupererCollectionBlockFils());
            }
        }
    }
    
    function attribuerCollectionBlockFils($un_block_donnees, $id)
    {
        if ($un_block_donnees->recupererIdentifiant() == $id ) {
            return $un_block_donnees->ajouterCollectionBlockFils($this->collectionblockdedonnees);
        } else if (is_array($un_block_donnees->recupererCollectionBlockFils()) AND count($un_block_donnees->recupererCollectionBlockFils()) > 0) {
            $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
            for($i = 0; $i < count($collection_block_fils); $i++) {
                $blocks_de_donnees = $collection_block_fils[$i]->recupererBlocksDeDonnees();
                for ($i = 0; $i < count($blocks_de_donnees); $i++) {
                    $this->attribuerCollectionBlockFils($blocks_de_donnees[$i], $id);
                }
            }
        }
    }
    
    function recupererCollectionBlockFils($un_block_donnees, $id)
    {
        $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
        for($i = 0; $i < count($collection_block_fils); $i++) {
            $une_collection = $collection_block_fils[$i];
            if ($une_collection->recupererIdentifiant() == $id ) {
                return $une_collection->recupererBlocksDeDonnees();
            } else {
                $blocks_de_donnees = $une_collection->recupererBlocksDeDonnees();
                for ($i = 0; $i < count($blocks_de_donnees); $i++) {
                    $block = $blocks_de_donnees[$i];
                    if (is_array($block->recupererCollectionBlockFils()) AND count($block->recupererCollectionBlockFils()) > 0) {
                        return $this->recupererCollectionBlockFils($block, $id);
                    }
                }
            }
        }
        return null;
    }
    
    function construire()
    {
        if (is_null($this->identifiant_parent)) {
            $sql = $this->getSQL();
            if (!is_null($sql)) {
                $this->openCursor($sql);
                while ($this->fetch());
                $this->closeCursor();
            }
        }
    }
    
    function contruireParRecursivitePlate()
    {
        construire();
        
        $this->$contexteRef = $this->blockdedonnees;
        
        construire();
    }
    
    function construireParRecursivite($niveau_max, $aso_contexte = null, $id = null)
    {
        static $niveau_courant = 0;
        if ($niveau_courant > $niveau_max) {
            return null;
        }
        
        if (!is_null($aso_contexte)) {
            $this->contexteRef = $aso_contexte;
        }
        if (is_null($id)) {
            $id = $this->identifiantblockdedonnees;
            $this->identifiant_parent = $this->blockdedonneesParent->recupererIdentifiant();
        } else {
            $this->identifiant_parent = $id;
        }
        
        $sql = $this->getSQL();
        //echo '<pre>'.print_r($sql, true).'</pre>';
        if (!is_null($sql)) {
            $this->openCursor($sql, $id);
            for ($i = 0; $this->fetch($id.'-'.$i); $i++);
            $this->closeCursor($this->identifiant_parent);
            
            $blocks_de_donnees = $this->recupererCollectionBlockFils($this->blockdedonneesParent, $id);
            if (count($blocks_de_donnees) > 0) {
                $niveau_courant++;
            }
            for ($i = 0; $i < count($blocks_de_donnees); $i++) {
                $block = $blocks_de_donnees[$i];
                $this->construireParRecursivite($niveau_max, $block->recupererDonnees(), $block->recupererIdentifiant());
            }
        }
    }
    
    function openCursor($sql, $id = null)
    {
        $resultat = mysql_query($sql, $this->connexion);
        if (!$resultat) {
            echo 'Numéro erreur Mysql : '.mysql_errno().'<br />'."\n";
            echo 'Erreur Mysql : '.mysql_error().'<br />'."\n";
            echo 'Requête : '.'<pre>'.$sql.'</pre>';
        }
        $this->idRes = $resultat;
        if (is_null($id)) {
            $this->collectionblockdedonnees = new collectionblockdedonnees($this->identifiantblockdedonnees);
        } else {
            $this->collectionblockdedonnees = new collectionblockdedonnees($id);
        }
    }
    
    function fetch($identifiant = null)
    {
        $resultat = mysql_fetch_assoc($this->idRes);
        if (!$resultat) {
            // Nous ne faisons rien car aucune donnée n'est collectée.
        } else {
            $this->collectionblockdedonnees->ajouterDonnees($resultat, $identifiant);
        }
        return $resultat;
    }
    
    function closeCursor($id = null)
    {
        mysql_free_result($this->idRes);
        // Nous ajoutons les données de la requêtes seulement si elle a ramenée des données
        if (count($this->collectionblockdedonnees->recupererBlocksDeDonnees()) > 0) {
            if (is_null($id)) {
                $this->blockdedonneesParent->ajouterCollectionBlockFils($this->collectionblockdedonnees);
            } else {
                $this->attribuerCollectionBlockFils($this->blockdedonneesParent, $id);
            }
        }
    }
}
?>