Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
<?php
2
/*vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +------------------------------------------------------------------------------------------------------+
4
// | PHP version 4.1                                                                                      |
5
// +------------------------------------------------------------------------------------------------------+
6
// | Copyright (C) 2005 Tela Botanica (accueil@tela-botanica.org)                                         |
7
// +------------------------------------------------------------------------------------------------------+
8
// | This file is part of API - Formulaire.                                                               |
9
// |                                                                                                      |
10
// | Foobar is free software; you can redistribute it and/or modify                                       |
11
// | it under the terms of the GNU General Public License as published by                                 |
12
// | the Free Software Foundation; either version 2 of the License, or                                    |
13
// | (at your option) any later version.                                                                  |
14
// |                                                                                                      |
15
// | Foobar is distributed in the hope that it will be useful,                                            |
16
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
17
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
18
// | GNU General Public License for more details.                                                         |
19
// |                                                                                                      |
20
// | You should have received a copy of the GNU General Public License                                    |
21
// | along with Foobar; if not, write to the Free Software                                                |
22
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
23
// +------------------------------------------------------------------------------------------------------+
24
// CVS : $Id: FORM_formulaire.class.php,v 1.4 2005/04/06 13:22:17 jpm Exp $
25
/**
26
* Classe form
27
*
28
* Classe permettant l'édition d'une base donnée MySQL de façon
29
* simplifié.
30
*
31
*@package Formulaire
32
//Auteur original :
33
*@author        Alexandre GRANIER <alexandre@tela-botanica.org>
34
//Autres auteurs :
35
*@author        Jean-Pascal MILCENT <jpm@clapas.org>
36
*@copyright     Tela-Botanica 2000-2005
37
*@version       $Revision: 1.4 $ $Date: 2005/04/06 13:22:17 $
38
// +------------------------------------------------------------------------------------------------------+
39
*/
40
 
41
// +------------------------------------------------------------------------------------------------------+
42
// |                                            ENTETE du PROGRAMME                                       |
43
// +------------------------------------------------------------------------------------------------------+
44
 
45
// +------------------------------------------------------------------------------------------------------+
46
// |                                            CORPS du PROGRAMME                                        |
47
// +------------------------------------------------------------------------------------------------------+
48
class form {
49
        // propriétés
50
        var $link;
51
        var $style_general;
52
        var $style_label;
53
        var $style_champ;
54
        var $style_button;
55
        var $style_radiocheckbox;
56
        var $style_commentaire;
57
 
58
        // constructeur
59
        function form($identifiant_de_connection)
60
        {
61
            $this->link = $identifiant_de_connection;
62
        }
63
 
64
        function select_db($db)
65
        {
66
            mysql_select_db($db, $this->link) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $db));
67
        }
68
 
69
        function selectFromEnum($table, $champs_enum, $selected = '')
70
        {
71
            // recup des différentes occurences
72
            $query = 'DESCRIBE '.$table;
73
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
74
 
75
            while ($row = mysql_fetch_object($result)) {
76
                if ($row->Field == $champs_enum) {
77
                    break;
78
                }
79
            }
80
            $tableau_enum = form::_extraction('enum', $row->Type);
81
 
82
            // construction du select
83
            $res = '<select name="'.$champs_enum.'" ';
84
            if ($this->style_general != '') {
85
                $res .= 'class="'.$this->style_general.'"';
86
            }
87
            $res .= '>'."\n";
88
 
89
            for ($i = 0 ; $i < count ($tableau_enum) ; $i++) {
90
                $res .= "\t".'<option value="'.$tableau_enum[$i].'"';
91
                if ($selected != '') {
92
                    if ($tableau_enum[$i] == $selected) {
93
                        $res .= 'selected';
94
                    }
95
                }
96
                $res .= '>'.$tableau_enum[$i].'</option>'."\n";
97
            }
98
            $res .= '</select>'."\n";
99
            mysql_free_result($result);
100
            return $res;
101
        }
102
 
103
        // Pour une table ne comportant que 2 champs ID et Label
104
        function selectFromTable($table, $selected = '',$champs1 = '',$champs2 = '', $autre = '')
105
        {
106
            // la requete, sur la table
107
            if ($champs1 != '') {
108
                $query =    'SELECT '.$table.'.'.$champs1.', '.$table.'.'.$champs2.' '.
109
                            'FROM '.$table;
110
            } else {
111
                $query =    'SELECT * '.
112
                            'FROM '.$table;
113
            }
114
            if (!($result = mysql_query($query))) {
115
                return false ;
116
            }
117
 
118
            $res = '<select name="'.$table.'" ';
119
            if ($this->style_general != '') {
120
                $res .= 'class="'.$this->style_general.'"';
121
            }
122
            if ($autre != '') {
123
                $res .= ' '.$autre;
124
            }
125
            $res .= '>'."\n";
126
            while ($row = mysql_fetch_row($result)) {
127
                $res .= "\t".'<option value="'.$row[0].'"';
128
                if ($row[0] == $selected) {
129
                    $res .= ' selected="selected"';
130
                }
131
                $res .= '>'.$row[1].'</option>'."\n";
132
            }
133
            $res .= '</select>'."\n";
134
            mysql_free_result($result) ;
135
            return $res ;
136
        }
137
 
138
        // Génère un <select> avec les elements d'un tableau
139
        function selectFromTableau($nom, $tableau, $selected = '', $javascript = array())
140
        {
141
            $res = '<select name="'.$nom.'" ';
142
            if ($this->style_general != '') {
143
                $res .= 'class="'.$this->style_general.'" ';
144
            }
145
            if (! empty($javascript)) {
146
                $res .= $javascript['nom_evenement'].'="'.$javascript['valeur_evenement'].'"';
147
            }
148
            $res .= '>'."\n";
149
            for ($i = 0 ; $i < count ($tableau) ; $i++) {
150
                $res .= "\t".'<option value="'.$tableau[$i].'" ';
151
                if ($tableau[$i] == $selected) {
152
                    $res .= 'selected="selected"';
153
                }
154
                $i++;
155
                $res .= '>'.$tableau[$i].'</option>'."\n";
156
            }
157
            $res .= '</select>'."\n";
158
            return $res;
159
        }
160
 
161
        function radioFromEnum($table, $champs_enum, $enum_checked = '')
162
        {
163
        // Récupération des différentes occurences
164
            $query = 'DESCRIBE '.$table;
165
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
166
 
167
            while ($row = mysql_fetch_object($result)) {
168
                if ($row->Field == $champs_enum) {
169
                    break;
170
                }
171
            }
172
            $tableau_enum = form::_extraction('enum', $row->Type);
173
 
174
            // Construction du select
175
            $res = '';
176
            for ($i = 0 ; $i < count ($tableau_enum) ; $i++) {
177
                $res .= '<input ';
178
                if ($this->style_radiocheckbox != '') {
179
                    $res .= 'class="'.$this->style_radiocheckbox.'" ';
180
                }
181
                $res .= 'type="radio" name="'.$champs_enum.'" value="'.$tableau_enum[$i].'" ';
182
                if ($tableau_enum[$i] == $enum_checked) {
183
                    $res .= ' checked="checked"';
184
                }
185
                $res .= ' />'."\n";
186
                $res .= "\t\t".$tableau_enum[$i]."\n";
187
                $res .= "\t".'&nbsp;&nbsp;'."\n";
188
            }
189
            mysql_free_result($result);
190
            return $res;
191
        }
192
 
193
        // pour une table ne comportant que 2 champs ID et Label
194
        function radioFromTable($table, $idChecked = '', $champs1 = '', $champs2 = '', $name = '')
195
        {
196
            // La requete, sur la table
197
            $requete =  'SELECT * '.
198
                        'FROM '.$table;
199
            $resultat = mysql_query($requete) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $requete));
200
            if (!($resultat)) {
201
                return false;
202
            }
203
 
204
            $retour = '';
205
            while ($ligne = mysql_fetch_row($resultat)) {
206
                $retour .= '<input ';
207
                if ($this->style_radiocheckbox != '') {
208
                    $retour .= 'class="'.$this->style_radiocheckbox.'" ';
209
                }
210
                $retour .= 'type="radio" name="';
211
                if ($name != '') {
212
                    $retour .= $name;
213
                } else {
214
                    $retour .= $table;
215
                }
216
                $retour .= '" value="'.$ligne[0].'" ';
217
                if ($ligne[0]== $idChecked) {
218
                    $retour .= 'checked="checked" ';
219
                }
220
                $retour .= ' />'."\n";
221
                $retour .= "\t".'&nbsp;'."\t".$ligne[1]."\n";
222
                $retour .= "\t".'&nbsp;&nbsp;<br />'."\n";
223
            }
224
 
225
            mysql_free_result($resultat);
226
            return $retour;
227
        }
228
 
229
        function checkboxFromTable($table_jointure, $table_label, $idChecked, $presentation = 'LIGNE')
230
        {
231
            // la requete sur les tables
232
            if (empty($idChecked)) {
233
                $idChecked = array();
234
            }
235
            $query =    'SELECT * '.
236
                        'FROM '.$table_label;
237
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
238
            if (!($result)) {
239
                return false;
240
            }
241
            $res = '';
242
            while ($row = mysql_fetch_row($result)) {
243
                $res .= "\n".'<input type="checkbox" name="'.$table_label.$row[0].'" value="'.$row[0].'"';
244
                if (in_array($row[0], $idChecked)) {
245
                    $res .= ' checked="checked"';
246
                }
247
                $res .= ' />'.$row[1];
248
                if ($presentation == 'LIGNE') {
249
                    $res .= '<br />';
250
                }
251
            }
252
            return $res;
253
        }
254
 
255
        function checkboxFromSet($table, $champs, $set_checked = '')
256
        {
257
            if (empty($set_checked)) {
258
                $set_checked = array();
259
            }
260
 
261
            // Récupération des différentes occurences
262
            $query = 'DESCRIBE '.$table;
263
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
264
 
265
            while ($row = mysql_fetch_object($result)) {
266
                if ($row->Field == $champs) {
267
                    break;
268
                }
269
            }
270
            $tableau_enum = form::_extraction('set', $row->Type);
271
 
272
            for ($i = 0 ; $i < count($tableau_enum) ; $i++) {
273
                // Construction du des checkbox
274
                $res = '<input type="checkbox" name="'.$champs.'" ';
275
                if ($this->style_general != '') {
276
                    $res .= 'class="'.$this->style_general.'" ';
277
                }
278
                $res .= 'value="'.$tableau_enum[$i].'" ';
279
                if (in_array($tableau_enum[$i], $set_checked)) {
280
                    $res .= 'selected="selected"';
281
                }
282
                $res .= ' />'.$tableau_enum[$i].'&nbsp;&nbsp;';
283
            }
284
            mysql_free_result($result);
285
            return $res;
286
        }
287
 
288
    function submit($value, $name = '', $autre = '')
289
    {
290
        $res = '<input ';
291
        if ($name != '') {
292
            $res .= 'name="'.$name.'" ';
293
        }
294
        $res .= 'type="submit" value="'.$value.'" class="'.$this->style_button.'" ';
295
        if ($autre != '') {
296
            $res .= $autre;
297
        }
298
        $res .= ' />'."\n";
299
        return $res;
300
    }
301
 
302
    function bouton($label, $style = '', $name = 'bouton', $autre = '')
303
    {
304
        $res = "\t".'<input type="submit" name="'.$name.'" value="'.$label.'" ';
305
        if ($style != '') {
306
            $res .= 'class="'.$style.'"';
307
        }
308
        if ($autre != '') {
309
            $res .= ' '.$autre;
310
        }
311
        $res .= ' />'."\n";
312
        return $res;
313
    }
314
 
315
    function _extraction($action, $chaine)
316
    {
317
        $regexp = '^'.$action.'\(';
318
        $chaine = ereg_replace($regexp, '', $chaine);
319
        $chaine = ereg_replace("\)$", '', $chaine);
320
        $chaine = ereg_replace("'", '', $chaine);
321
        $tableau_enum = explode(',', $chaine);
322
        return $tableau_enum;
323
    }
324
}
325
 
326
// +------------------------------------------------------------------------------------------------------+
327
// |                                            PIED du PROGRAMME                                         |
328
// +------------------------------------------------------------------------------------------------------+
329
 
330
/* +--Fin du code ----------------------------------------------------------------------------------------+
331
*
332
* $Log: FORM_formulaire.class.php,v $
333
* Revision 1.4  2005/04/06 13:22:17  jpm
334
* Corrections et ajout d'id aux champs des formulaires.
335
*
336
* Revision 1.3  2005/03/30 09:06:26  jpm
337
* Mise en conformité du code.
338
*
339
* Revision 1.2  2005/03/08 11:29:51  jpm
340
* Amélioration du code.
341
*
342
* Revision 1.1  2005/03/03 17:44:44  jpm
343
* Ajout des fichiers anciennement contenu dans le fichier lib.form.php.
344
*
345
*
346
* +-- Fin du code ----------------------------------------------------------------------------------------+
347
*/
348
?>