Subversion Repositories eFlore/Archives.eflore-consultation-v2

Rev

Rev 140 | 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 $contexteChemin;
    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));
        $this->identifiantblockdedonnees = $tab_etape_chemin[0];
        $this->identifiant_parent = NULL;//$this->blockdedonneesParent->recupererIdentifiant();
        $this->contexteRef = $this->blockdedonneesParent->recupererDonnees();
        $this->contexteMultiple = FALSE;
        if (count($tab_etape_chemin) > 1) {
            $this->contexteMultiple = TRUE;
            $this->identifiant_parent = $tab_etape_chemin[1];
        }
    }
    
    function attribuerContexteRef($un_block_donnees, $id)
    {
        $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
        for($i = 0; $i < count($collection_block_fils); $i++) {
            $collection = $collection_block_fils[$i];
            if ($collection->recupererIdentifiant() == $id) {
                return $collection->recupererBlocksDeDonnees();
            } else if (is_object($un_block_donnees->recupererCollectionBlockFils())) {
                $this->attribuerContexteRef($un_block_donnees->recupererCollectionBlockFils(), $id);
            }
        }
    }
    
    function attribuerCollectionBlockFils($un_block_donnees, $id, $num = null, $num_courant = null)
    {
        if ($un_block_donnees->recupererIdentifiant() == $id AND is_null($num)) {
            return $un_block_donnees->ajouterCollectionBlockFils($this->collectionblockdedonnees);
        } else if ($un_block_donnees->recupererIdentifiant() == $id AND !is_null($num) AND $num == $num_courant) {
            //echo '<pre>'.print_r($this->contexteRefTmp, true).'</pre>';
            //echo $un_block_donnees->recupererIdentifiant().'----'.$num.'-'.$num_courant.'<br />';
            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 ($j = 0; $j < count($blocks_de_donnees); $j++) {
                    //echo $blocks_de_donnees[$j]->recupererIdentifiant().'-'.$id.'----'.$num.'-'.$j.'<br />';
                    $this->attribuerCollectionBlockFils($blocks_de_donnees[$j], $id, $num, $j);
                }
            }
        }
    }
    
    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 ($this->contexteMultiple == FALSE) {
            $sql = $this->getSQL();
            if (!is_null($sql)) {
                $this->openCursor($sql);
                while ($this->fetch());
                $this->closeCursor();
            }
        } else {
            $this->contexteRefTmp = $this->attribuerContexteRef($this->blockdedonneesParent, $this->identifiant_parent);
            //echo '<pre>'.print_r($this->contexteRefTmp, true).'</pre>';
            for($i = 0; $i < count($this->contexteRefTmp); $i++) {
                $this->contexteRef = $this->contexteRefTmp[$i]->recupererDonnees();
                $sql = $this->getSQL();
                if (!is_null($sql)) {
                    $this->openCursor($sql);
                    while ($this->fetch());
                    $this->closeCursor($this->identifiant_parent, $i);
                }
            }
        }
    }
    
    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();
        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 contruireParRecursivitePlate()
    {
        construire();
        
        $this->$contexteRef = $this->blockdedonnees;
        
        construire();
    }
    
    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, $num = 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, $num);
            }
        }
    }
}
?>