Subversion Repositories eFlore/Archives.eflore-consultation-v2

Rev

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