Subversion Repositories eFlore/Archives.eflore-consultation-v2

Rev

Rev 140 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 jpm 1
<?php
2
 
3
class macroElement implements iMacroElement{
4
 
5
    protected $identifiantblockdedonnees;
6
    protected $identifiant_parent;
7
    protected $connexion;
8
    protected $contexteRef;
9
    protected $contexteRefTmp;
145 jpm 10
    protected $contexteChemin;
87 jpm 11
    protected $contexteSQL;
12
    protected $blockdedonneesParent;
13
    protected $collectionblockdedonnees;
14
    protected $idRes;
15
 
16
    function __construct($une_connexion, $un_block_donnees_parent, $chemin_macro_element)
17
    {
18
        $this->connexion = $une_connexion;
19
        $this->blockdedonneesParent = $un_block_donnees_parent;
20
        $tab_etape_chemin = array_reverse(explode('>', $chemin_macro_element));
145 jpm 21
        $this->identifiantblockdedonnees = $tab_etape_chemin[0];
22
        $this->identifiant_parent = NULL;//$this->blockdedonneesParent->recupererIdentifiant();
23
        $this->contexteRef = $this->blockdedonneesParent->recupererDonnees();
24
        $this->contexteMultiple = FALSE;
87 jpm 25
        if (count($tab_etape_chemin) > 1) {
145 jpm 26
            $this->contexteMultiple = TRUE;
87 jpm 27
            $this->identifiant_parent = $tab_etape_chemin[1];
28
        }
29
    }
30
 
145 jpm 31
    function attribuerContexteRef($un_block_donnees, $id)
87 jpm 32
    {
33
        $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
34
        for($i = 0; $i < count($collection_block_fils); $i++) {
35
            $collection = $collection_block_fils[$i];
145 jpm 36
            if ($collection->recupererIdentifiant() == $id) {
87 jpm 37
                return $collection->recupererBlocksDeDonnees();
38
            } else if (is_object($un_block_donnees->recupererCollectionBlockFils())) {
145 jpm 39
                $this->attribuerContexteRef($un_block_donnees->recupererCollectionBlockFils(), $id);
87 jpm 40
            }
41
        }
42
    }
43
 
145 jpm 44
    function attribuerCollectionBlockFils($un_block_donnees, $id, $num = null, $num_courant = null)
87 jpm 45
    {
145 jpm 46
        if ($un_block_donnees->recupererIdentifiant() == $id AND is_null($num)) {
139 jpm 47
            return $un_block_donnees->ajouterCollectionBlockFils($this->collectionblockdedonnees);
145 jpm 48
        } else if ($un_block_donnees->recupererIdentifiant() == $id AND !is_null($num) AND $num == $num_courant) {
49
            //echo '<pre>'.print_r($this->contexteRefTmp, true).'</pre>';
50
            //echo $un_block_donnees->recupererIdentifiant().'----'.$num.'-'.$num_courant.'<br />';
51
            return $un_block_donnees->ajouterCollectionBlockFils($this->collectionblockdedonnees);
139 jpm 52
        } else if (is_array($un_block_donnees->recupererCollectionBlockFils()) AND count($un_block_donnees->recupererCollectionBlockFils()) > 0) {
53
            $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
54
            for($i = 0; $i < count($collection_block_fils); $i++) {
55
                $blocks_de_donnees = $collection_block_fils[$i]->recupererBlocksDeDonnees();
145 jpm 56
                for ($j = 0; $j < count($blocks_de_donnees); $j++) {
57
                    //echo $blocks_de_donnees[$j]->recupererIdentifiant().'-'.$id.'----'.$num.'-'.$j.'<br />';
58
                    $this->attribuerCollectionBlockFils($blocks_de_donnees[$j], $id, $num, $j);
139 jpm 59
                }
60
            }
61
        }
62
    }
63
 
64
    function recupererCollectionBlockFils($un_block_donnees, $id)
65
    {
87 jpm 66
        $collection_block_fils = $un_block_donnees->recupererCollectionBlockFils();
67
        for($i = 0; $i < count($collection_block_fils); $i++) {
139 jpm 68
            $une_collection = $collection_block_fils[$i];
69
            if ($une_collection->recupererIdentifiant() == $id ) {
70
                return $une_collection->recupererBlocksDeDonnees();
71
            } else {
72
                $blocks_de_donnees = $une_collection->recupererBlocksDeDonnees();
73
                for ($i = 0; $i < count($blocks_de_donnees); $i++) {
74
                    $block = $blocks_de_donnees[$i];
75
                    if (is_array($block->recupererCollectionBlockFils()) AND count($block->recupererCollectionBlockFils()) > 0) {
76
                        return $this->recupererCollectionBlockFils($block, $id);
87 jpm 77
                    }
78
                }
79
            }
80
        }
139 jpm 81
        return null;
87 jpm 82
    }
83
 
84
    function construire()
85
    {
145 jpm 86
        if ($this->contexteMultiple == FALSE) {
87
            $sql = $this->getSQL();
88
            if (!is_null($sql)) {
89
                $this->openCursor($sql);
90
                while ($this->fetch());
91
                $this->closeCursor();
92
            }
93
        } else {
94
            $this->contexteRefTmp = $this->attribuerContexteRef($this->blockdedonneesParent, $this->identifiant_parent);
95
            //echo '<pre>'.print_r($this->contexteRefTmp, true).'</pre>';
96
            for($i = 0; $i < count($this->contexteRefTmp); $i++) {
97
                $this->contexteRef = $this->contexteRefTmp[$i]->recupererDonnees();
98
                $sql = $this->getSQL();
99
                if (!is_null($sql)) {
100
                    $this->openCursor($sql);
101
                    while ($this->fetch());
102
                    $this->closeCursor($this->identifiant_parent, $i);
103
                }
104
            }
140 jpm 105
        }
87 jpm 106
    }
107
 
139 jpm 108
    function construireParRecursivite($niveau_max, $aso_contexte = null, $id = null)
109
    {
110
        static $niveau_courant = 0;
111
        if ($niveau_courant > $niveau_max) {
112
            return null;
113
        }
114
 
115
        if (!is_null($aso_contexte)) {
116
            $this->contexteRef = $aso_contexte;
117
        }
118
        if (is_null($id)) {
119
            $id = $this->identifiantblockdedonnees;
120
            $this->identifiant_parent = $this->blockdedonneesParent->recupererIdentifiant();
121
        } else {
122
            $this->identifiant_parent = $id;
123
        }
124
 
125
        $sql = $this->getSQL();
126
        if (!is_null($sql)) {
127
            $this->openCursor($sql, $id);
128
            for ($i = 0; $this->fetch($id.'-'.$i); $i++);
129
            $this->closeCursor($this->identifiant_parent);
130
 
131
            $blocks_de_donnees = $this->recupererCollectionBlockFils($this->blockdedonneesParent, $id);
132
            if (count($blocks_de_donnees) > 0) {
133
                $niveau_courant++;
134
            }
135
            for ($i = 0; $i < count($blocks_de_donnees); $i++) {
136
                $block = $blocks_de_donnees[$i];
137
                $this->construireParRecursivite($niveau_max, $block->recupererDonnees(), $block->recupererIdentifiant());
138
            }
139
        }
87 jpm 140
    }
140 jpm 141
 
142
    function contruireParRecursivitePlate()
143
    {
144
        construire();
145
 
146
        $this->$contexteRef = $this->blockdedonnees;
147
 
148
        construire();
149
    }
87 jpm 150
 
151
    function openCursor($sql, $id = null)
152
    {
139 jpm 153
        $resultat = mysql_query($sql, $this->connexion);
154
        if (!$resultat) {
155
            echo 'Numéro erreur Mysql : '.mysql_errno().'<br />'."\n";
156
            echo 'Erreur Mysql : '.mysql_error().'<br />'."\n";
157
            echo 'Requête : '.'<pre>'.$sql.'</pre>';
158
        }
159
        $this->idRes = $resultat;
160
        if (is_null($id)) {
161
            $this->collectionblockdedonnees = new collectionblockdedonnees($this->identifiantblockdedonnees);
87 jpm 162
        } else {
139 jpm 163
            $this->collectionblockdedonnees = new collectionblockdedonnees($id);
87 jpm 164
        }
165
    }
166
 
139 jpm 167
    function fetch($identifiant = null)
87 jpm 168
    {
139 jpm 169
        $resultat = mysql_fetch_assoc($this->idRes);
170
        if (!$resultat) {
171
            // Nous ne faisons rien car aucune donnée n'est collectée.
172
        } else {
173
            $this->collectionblockdedonnees->ajouterDonnees($resultat, $identifiant);
87 jpm 174
        }
139 jpm 175
        return $resultat;
87 jpm 176
    }
177
 
145 jpm 178
    function closeCursor($id = null, $num = null)
87 jpm 179
    {
139 jpm 180
        mysql_free_result($this->idRes);
181
        // Nous ajoutons les données de la requêtes seulement si elle a ramenée des données
182
        if (count($this->collectionblockdedonnees->recupererBlocksDeDonnees()) > 0) {
183
            if (is_null($id)) {
184
                $this->blockdedonneesParent->ajouterCollectionBlockFils($this->collectionblockdedonnees);
185
            } else {
145 jpm 186
                $this->attribuerCollectionBlockFils($this->blockdedonneesParent, $id, $num);
139 jpm 187
            }
188
        }
87 jpm 189
    }
190
}
191
?>