Subversion Repositories eFlore/Archives.eflore-consultation-v2

Rev

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)
    {
        $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) {
                $blocks_de_donnees = $collection->recupererBlocksDeDonnees();
                foreach ($blocks_de_donnees as $cle => $val) {
                    if ($cle == $id) {
                        return $blocks_de_donnees[$cle]->ajouterCollectionBlockFils($this->collectionblockdedonnees);
                    }
                }
            } else if (is_object($this->blockdedonneesParent->recupererCollectionBlockFils())) {
                return $this->attribuerCollectionBlockFils($un_block_donnees->recupererCollectionBlockFils());
            }
        }
    }
    
    function construire()
    {
        if (is_null($this->identifiant_parent)) {
            $sql = $this->getSQL();
            if(!is_null($sql)) {
                $this->openCursor($sql);
                while ($this->fetch());
                $this->closeCursor();
            }
        } else {
            $this->contexteRefTmp = $this->contexteRef;
            for($i = 0; $i < count($this->contexteRefTmp); $i++) {
                $this->contexteRef = $this->contexteRefTmp[$i]->recupererDonnees();
                $sql = $this->getSQL();
                if(!is_null($sql)) {
                    $this->openCursor($sql, $i);
                    while ($this->fetch());
                    $this->closeCursor();
                }
            }
            //$this->contexteRef = $this->contexteRefTmp;
        }
        //echo '<pre>'.print_r($this->contexteRef, true).'</pre>';
    }
    
    function contruireParRecursivitePlate()
    {
        construire();
        
        $this->$contexteRef = $this->blockdedonnees;
        
        construire();
    }
    
    function contruireParRecursivite()
    {
        
    }
    
    function openCursor($sql, $id = null)
    {
        $res = mysql_query($sql, $this->connexion);
        if (!$res) echo mysql_errno().': '.mysql_error()."\n";
        $this->idRes = $res;
        $this->collectionblockdedonnees = new collectionblockdedonnees($this->identifiantblockdedonnees);
        if (is_null($this->identifiant_parent)) {
            $this->blockdedonneesParent->ajouterCollectionBlockFils($this->collectionblockdedonnees);
        } else {
            $this->attribuerCollectionBlockFils($this->blockdedonneesParent, $id);
        }
    }
    
    function fetch()
    {
        $res = mysql_fetch_assoc($this->idRes);
        if (!$res) {
            
        } else {
            //$this->blockdedonnees = new blockdedonnees($this->identifiantblockdedonnees);
            //$this->blockdedonnees->affecterDonnees($res);
            $this->collectionblockdedonnees->ajouterDonnees($res);
        }
        return $res;
    }
    
    function closeCursor()
    {
        mysql_free_result($this->idRes);
    }
}
?>