Subversion Repositories eFlore/Archives.eflore-consultation-v2

Rev

Rev 139 | 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())) {
140 jpm 39
                $this->attribuerContexteRef($un_block_donnees->recupererCollectionBlockFils());
87 jpm 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
    {
140 jpm 81
        $sql = $this->getSQL();
82
        if (!is_null($sql)) {
83
            $this->openCursor($sql);
84
            while ($this->fetch());
85
            $this->closeCursor();
86
        }
87 jpm 87
    }
88
 
139 jpm 89
    function construireParRecursivite($niveau_max, $aso_contexte = null, $id = null)
90
    {
91
        static $niveau_courant = 0;
92
        if ($niveau_courant > $niveau_max) {
93
            return null;
94
        }
95
 
96
        if (!is_null($aso_contexte)) {
97
            $this->contexteRef = $aso_contexte;
98
        }
99
        if (is_null($id)) {
100
            $id = $this->identifiantblockdedonnees;
101
            $this->identifiant_parent = $this->blockdedonneesParent->recupererIdentifiant();
102
        } else {
103
            $this->identifiant_parent = $id;
104
        }
105
 
106
        $sql = $this->getSQL();
107
        //echo '<pre>'.print_r($sql, true).'</pre>';
108
        if (!is_null($sql)) {
109
            $this->openCursor($sql, $id);
110
            for ($i = 0; $this->fetch($id.'-'.$i); $i++);
111
            $this->closeCursor($this->identifiant_parent);
112
 
113
            $blocks_de_donnees = $this->recupererCollectionBlockFils($this->blockdedonneesParent, $id);
114
            if (count($blocks_de_donnees) > 0) {
115
                $niveau_courant++;
116
            }
117
            for ($i = 0; $i < count($blocks_de_donnees); $i++) {
118
                $block = $blocks_de_donnees[$i];
119
                $this->construireParRecursivite($niveau_max, $block->recupererDonnees(), $block->recupererIdentifiant());
120
            }
121
        }
87 jpm 122
    }
140 jpm 123
 
124
    function contruireParRecursivitePlate()
125
    {
126
        construire();
127
 
128
        $this->$contexteRef = $this->blockdedonnees;
129
 
130
        construire();
131
    }
87 jpm 132
 
133
    function openCursor($sql, $id = null)
134
    {
139 jpm 135
        $resultat = mysql_query($sql, $this->connexion);
136
        if (!$resultat) {
137
            echo 'Numéro erreur Mysql : '.mysql_errno().'<br />'."\n";
138
            echo 'Erreur Mysql : '.mysql_error().'<br />'."\n";
139
            echo 'Requête : '.'<pre>'.$sql.'</pre>';
140
        }
141
        $this->idRes = $resultat;
142
        if (is_null($id)) {
143
            $this->collectionblockdedonnees = new collectionblockdedonnees($this->identifiantblockdedonnees);
87 jpm 144
        } else {
139 jpm 145
            $this->collectionblockdedonnees = new collectionblockdedonnees($id);
87 jpm 146
        }
147
    }
148
 
139 jpm 149
    function fetch($identifiant = null)
87 jpm 150
    {
139 jpm 151
        $resultat = mysql_fetch_assoc($this->idRes);
152
        if (!$resultat) {
153
            // Nous ne faisons rien car aucune donnée n'est collectée.
154
        } else {
155
            $this->collectionblockdedonnees->ajouterDonnees($resultat, $identifiant);
87 jpm 156
        }
139 jpm 157
        return $resultat;
87 jpm 158
    }
159
 
139 jpm 160
    function closeCursor($id = null)
87 jpm 161
    {
139 jpm 162
        mysql_free_result($this->idRes);
163
        // Nous ajoutons les données de la requêtes seulement si elle a ramenée des données
164
        if (count($this->collectionblockdedonnees->recupererBlocksDeDonnees()) > 0) {
165
            if (is_null($id)) {
166
                $this->blockdedonneesParent->ajouterCollectionBlockFils($this->collectionblockdedonnees);
167
            } else {
168
                $this->attribuerCollectionBlockFils($this->blockdedonneesParent, $id);
169
            }
170
        }
87 jpm 171
    }
172
}
173
?>