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_table.class.php,v 1.4 2005/06/29 16:59:05 jpm Exp $
25
/**
26
* Classe formFromTable étendant form
27
*
28
* Classe permettant l'édition d'une table 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/06/29 16:59:05 $
38
// +------------------------------------------------------------------------------------------------------+
39
*/
40
 
41
// +------------------------------------------------------------------------------------------------------+
42
// |                                            ENTETE du PROGRAMME                                       |
43
// +------------------------------------------------------------------------------------------------------+
44
define ('NOUVELLE_LIGNE', 1);
45
define ('MEME_LIGNE', 2);
46
 
47
// +------------------------------------------------------------------------------------------------------+
48
// |                                            CORPS du PROGRAMME                                        |
49
// +------------------------------------------------------------------------------------------------------+
50
class formFromTable extends form {
51
 
52
    // Le nom de la table
53
    var $table;
54
    var $tableau_type;
55
    var $ligne;
56
    var $champs;
57
 
58
    var $mode;
59
    // L'identifiant de la ligne en cas de modification
60
    var $id;
61
    var $cle;
62
    // Les valeurs par defaut
63
    var $default_value;
64
 
65
    // Tableau contenant les jointures
66
    var $jointure;
67
    var $champs1;
68
    var $champs2;
69
    var $champs3;
70
 
71
    // Un compteur pour différencier les jointures portant sur une meme table
72
    var $compteur;
73
 
74
    function formFromTable($link, $table, $mode = 'NOUV', $id = '', $cle = '')
75
    {
76
        $this->link = $link;
77
        $this->table = $table;
78
        $this->form = '';
79
        $this->ligne = array();
80
        $this->champs = array();
81
        $this->jointure = array();
82
        $this->mode = $mode;
83
        $this->id = $id;
84
        $this->cle = $cle;
85
        $this->default_value = array();
86
        $this->champs1 = array();
87
        $this->champs2 = array();
88
        $this->champs3 = array();
89
        $this->compteur = 0;
90
        // Style
91
        $this->style_general = 'formulaire';
92
        $this->style_label = 'label';
93
        $this->style_champ = 'champ';
94
        $this->style_button = 'bouton';
95
        $this->style_commentaire = 'commentaire';
96
        $this->style_radiocheckbox = 'radio';
97
 
98
        // requete pour recuperer les types de champs dans le tableau ;
99
        $query = 'DESCRIBE '.$this->table;
100
        if (!($result = mysql_query($query))) {
101
            return false ;
102
        }
103
 
104
        while ($row = mysql_fetch_object($result)) {
105
            $this->tableau_type[$row->Field] = $row->Type;
106
            // on definit les index pour eviter les notices...
107
            $this->default_value[$row->Field] = $row->Default;
108
            $this->jointure[$row->Field] = '';
109
        }
110
 
111
        foreach ($this->tableau_type as $key => $value) {
112
            $radical = $this->_extraitRadical($value);
113
 
114
            switch ($radical) {
115
                case 'char' :
116
                case 'varchar' :
117
                case 'smallint' :
118
                case 'int' :
119
                    $this->tableau_type[$key] = $this->_estimation_taille($this->_extraitTaille($value));
120
                    break;
121
                case 'text' :
122
                    $this->tableau_type[$key] = 'text';
123
                    break;
124
                case 'enum' :
125
                    $this->tableau_type[$key] = 'enum';
126
                    break;
127
                case 'date' :
128
                    $this->tableau_type[$key] = 'date';
129
                    break;
130
                case 'set' :
131
                    $this->tableau_type[$key] = 'set';
132
                    break;
133
            }
134
        }
135
        // on recupere les infos dans le cas d'une modification
136
        // notons que les valeurs par défaut initialisé plus haut à partir de la table SQL
137
        // sont écrasées.
138
        if ($this->mode == 'MOD') {
139
            $query =    'SELECT * '.
140
                        'FROM '.$this->table.' '.
141
                        'WHERE '.$this->cle.' = "'.$this->id.'"';
142
            $result = mysql_query($query) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
143
            $this->default_value = mysql_fetch_array($result);
144
        }
145
    }
146
 
147
    // Ajouter un champ pour le formumaire nom_du_champs, label_a_mettre_devant
148
    function addChamps($name, $label, $default = '', $type = 'text', $presentation = NOUVELLE_LIGNE)
149
    {
150
        array_push($this->ligne, $label);
151
        array_push($this->champs, $name);
152
        switch($this->tableau_type[$name]) {
153
            case 'text' :
154
                $this->textarea($name);
155
                break;
156
            case 'enum' :
157
                $this->enum($name);
158
                break;
159
            case 'date' :
160
                $this->date($name, $presentation);
161
                break;
162
            case 'set' :
163
                $this->set($name);
164
                break;
165
            default :
166
                $this->input($name, $default, $type, $presentation);
167
        }
168
    }
169
 
170
    function textarea($name)
171
    {
172
        $code =  '<textarea id="'.$name.'" name="'.$name.'" class="'.$this->style_general.'" cols="60" rows="5">';
173
        $code .= $this->default_value[$name];
174
        $code .= '</textarea>'."\n";
175
        array_push($this->ligne, $code);
176
    }
177
 
178
    function input($name,$default, $type = 'text', $presentation = NOUVELLE_LIGNE) {
179
        if (!empty($default) and $this->mode != 'MOD') {
180
            $this->default_value[$name] = $default;
181
        }
182
        $code = '<input type="'.$type.'" id="'.$name.'" name="'.$name.'" size="'.$this->tableau_type[$name].'" class="'.$this->style_general.'" ';
183
        $code .= 'value="';
184
        if ($this->mode == 'MOD' and $type == 'password') {
185
 
186
        } else {
187
            $code .= htmlentities($this->default_value[$name]);
188
        }
189
        $code .= '" />'."\n";
190
 
191
        if ($presentation == MEME_LIGNE) {
192
            // cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->ligne
193
            array_push($this->ligne, '!'.array_pop($this->ligne));
194
        }
195
        array_push($this->ligne, $code);
196
    }
197
 
198
    function date($name, $presentation = NOUVELLE_LIGNE) {
199
        if ($this->default_value[$name] != '') {
200
            $this->default_value[$name] = preg_replace("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", "\\3-\\2-\\1", $this->default_value[$name]);
201
        }
202
        $code = '<input type="text" id="'.$name.'" name="'.$name.'" size="12" class="'.$this->style_general.'" ';
203
        $code .= 'value="'.$this->default_value[$name].'" />';
204
        if ($presentation == MEME_LIGNE) {
205
            // cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->ligne
206
            array_push($this->ligne, '!'.array_pop($this->ligne));
207
        }
208
        array_push($this->ligne, $code);
209
    }
210
 
211
    function submit($label,$name = '', $autre = '')
212
    {
213
        array_push($this->ligne, form::submit($label, $name, $autre)) ;
214
    }
215
 
216
    function annuler($label, $fonction = '')
217
    {
218
        $code = '<input type="button" id="annu" name="annu" class="'.$this->style_button.'" value="'.$label.'" ';
219
        $code .= 'onclick="javascript:window.location.href=\''.$fonction.'\';" />'."\n";
220
        array_push($this->ligne, $code) ;
221
    }
222
 
223
    function printForm($action, $autre = '', $format = 'table')
224
    {
225
        //Création du formulaire et de sa structure
226
        $res = '<form id="'.$this->table.'" action="'.$action.'" method="post" ';
227
        if ($autre != '') {
228
            $res .= ' '.$autre;
229
        }
230
        $res .= '>'."\n";
231
        if ($format == 'table') {
232
            $res .= '<table class="'.$this->style_general.'">'."\n";
233
            $colspan = $this->getMaxColonne() * 2;
234
        } elseif ($format == 'liste') {
235
            $res .= '<ul class="'.$this->style_general.'">'."\n";
236
        }
237
 
238
        // Ajout des champs du formulaire
239
        for ($i = 0; $i < count($this->ligne); $i = $i + 2) {
240
 
241
            // Ajout du bouton de validation du formulaire
242
            if (ereg('submit', $this->ligne[$i])) {
243
                if ($format == 'table') {
244
                    $res .= '<tr><td>&nbsp;</td><td>';
245
                } elseif ($format == 'liste') {
246
                    $res .= '<li>';
247
                }
248
                $res .= $this->ligne[$i]."\n";
249
 
250
                if (isset($this->ligne[$i+1])) {
251
                    $res .= $this->ligne[$i+1];
252
                }
253
                if ($format == 'table') {
254
                    $res .= '</td></tr>';
255
                } elseif ($format == 'liste') {
256
                    $res .= '</li>'."\n";
257
                }
258
                $i--;
259
                break;
260
            }
261
            // Ajout de commentaire
262
            if (ereg('^#', $this->ligne[$i])) {
263
                if ($format == 'table') {
264
                    $res .= '<tr><td colspan="'.($colspan + 2).'" ';
265
                } elseif ($format == 'liste') {
266
                    $res .= '<li ';
267
                }
268
                // Suppression du premier #
269
                $this->ligne[$i] = ereg_replace('^#', '', $this->ligne[$i]);
270
                if (ereg('#', $this->ligne[$i])) {
271
                    $tableau = explode('#', $this->ligne[$i]);
272
                    $style = $tableau[1];
273
                    $this->ligne[$i] = $tableau[0];
274
                } else {
275
                    $style = $this->style_commentaire;
276
                }
277
                $res .= 'class="'.$style.'">';
278
                $res .= $this->ligne[$i];
279
                if ($format == 'table') {
280
                    $res .= '</td></tr>'."\n";
281
                } elseif ($format == 'liste') {
282
                    $res .= '</li>'."\n";
283
                }
284
                $i--;
285
                continue;
286
            }
287
 
288
            if (ereg('^!', $this->ligne[$i])) {
289
                if ($format == 'table') {
290
                    $res .= '<tr>'."\n";
291
                } elseif ($format == 'liste') {
292
                    $res .= '<li>'."\n";
293
                }
294
                do {
295
                    if ($format == 'table') {
296
                        $res .= '<td class="'.$this->style_label.'">'.ereg_replace("^!", '', $this->ligne[$i]).'</td>'."\n";
297
                        $res .= '<td class="'.$this->style_commentaire.'">'.$this->ligne[$i + 1].'</td>'."\n";
298
                    } elseif ($format == 'liste') {
299
                        $res .= '<td class="'.$this->style_label.'">'.ereg_replace("^!", '', $this->ligne[$i]).'</td>'."\n";
300
                        $res .= '<td class="'.$this->style_commentaire.'">'.$this->ligne[$i + 1].'</td>'."\n";
301
                    }
302
                    $i += 2;
303
                } while (ereg('^!', $this->ligne[$i - 2]));
304
                if ($format == 'table') {
305
                    $res .= '</tr>'."\n";
306
                } elseif ($format == 'liste') {
307
                    $res .= '</li>'."\n";
308
                }
309
                $i -= 2;// on est allé trop loin donc un revient sur nos pas
310
                continue;
311
            }
312
 
313
            // Ajout du label
314
            if ($format == 'table') {
315
                $res .= '<tr><td ';
316
                if (isset($this->ligne[$i + 1])) {
317
                    if (ereg('textarea', $this->ligne[$i + 1])) {
318
                        $res .= 'valign="top" ';
319
                    }
320
                }
321
                $res .= 'class="'.$this->style_label.'">';
322
            } elseif ($format == 'liste') {
323
                $res .= '<li>'."\n";
324
            }
325
            $res .= '<label>'.$this->ligne[$i].'</label>';
326
 
327
            // Ajout champ
328
            if ($format == 'table') {
329
                $res .= '</td>'."\n";
330
                $res .= '<td class="'.$this->style_radiocheckbox.'" colspan="';
331
                $res .= $colspan + 1;
332
                $res .= '">';
333
            }
334
            if (isset($this->ligne[$i + 1])) {
335
                $res .= $this->ligne[$i + 1];
336
            }
337
            if ($format == 'table') {
338
                $res .= '</td>'."\n".'</tr>'."\n";
339
            } elseif ($format == 'liste') {
340
                $res .= '</li>'."\n";
341
            }
342
 
343
        }
344
 
345
        // Affichage du champ caché contenant l'objet Form
346
        if ($format == 'table') {
347
            $res .= '<tr>'."\n".'<td>&nbsp;</td><td>'."\n";
348
        } elseif ($format == 'liste') {
349
            $res .= '<li>'."\n";
350
        }
351
        $res .= $this->finalise($this->table);
352
        if ($format == 'table') {
353
            $res .= '</td>'."\n".'</tr>'."\n";
354
        } elseif ($format == 'liste') {
355
            $res .= '</li>'."\n";
356
        }
357
 
358
        // Fin de la structure du formulaire
359
        if ($format == 'table') {
360
            $res .= '</table>'."\n";
361
        } elseif ($format == 'liste') {
362
            $res .= '</ul>'."\n";
363
        }
364
        $res .= '</form>'."\n";
365
 
366
        return $res;
367
    }
368
 
369
    function addCommentaire($Commentaire, $style = '')
370
    {
371
        $ligne = '#'.$Commentaire;
372
        if ($style != '') {
373
            $ligne .= '#'.$style;
374
        }
375
        array_push($this->ligne, $ligne);
376
    }
377
 
378
    function enum($name)
379
    {
380
        array_push($this->ligne, form::selectFromEnum($this->table, $name, $this->default_value[$name]));
381
    }
382
 
383
    function set($name)
384
    {
385
        array_push($this->ligne, form::checkboxFromSet($this->table, $name, explode(',', $this->default_value[$name])));
386
    }
387
 
388
    function radio($name, $tableau, $label, $checked = '')
389
    {
390
        array_push($this->ligne, $label);
391
        array_push($this->champs, $name);
392
        $code = '';
393
        for ($i = 0; $i < count($tableau); $i = $i + 2) {
394
            $code .= '<input type="radio" id="'.$name.'" name="'.$name.'" value="'.$tableau[$i].'"';
395
            if ($tableau[$i] == $this->default_value[$name]) {
396
                $code .= ' checked';
397
            }
398
            $code .= ' />';
399
            $code .= '&nbsp;'.$tableau[$i + 1];
400
        }
401
        array_push($this->ligne, $code);
402
    }
403
 
404
    function radioEnum($name, $label, $checked = '')
405
    {
406
        array_push($this->ligne, $label);
407
        array_push($this->champs, $name);
408
 
409
        $code = form::radioFromEnum($this->table, $name, $this->default_value[$name]);
410
 
411
        array_push($this->ligne, $code);
412
    }
413
 
414
    function selectFromTable($table, $selected = '',$champs1 = '',$champs2 = '', $commentaire = '')
415
    {
416
        if (ereg('(.*) par (.*)', $table, $str)) {
417
            $this->jointure[$str[2]] = $str[1];
418
            $table = $str[2];
419
        }
420
        if ($this->default_value[$str[1]] != '' and $selected == '') {
421
            $selected = $this->default_value[$str[1]];
422
        }
423
        array_push($this->ligne, $commentaire);
424
        array_push($this->champs, $table);
425
        array_push($this->ligne,form::selectFromTable($table, $selected, $champs1, $champs2));
426
    }
427
 
428
    function radioFromTable($table, $selected="", $commentaire="", $presentation = NOUVELLE_LIGNE)
429
    {
430
        if (ereg('(.*) par (.*)', $table, $str)) {
431
            $this->jointure[$str[2].$this->compteur] = $str[1];
432
            $table = $str[2];
433
        }
434
        if ($this->default_value[$str[1]] != '') {
435
            $selected = $this->default_value[$str[1]];
436
        }
437
        array_push($this->ligne, $commentaire);
438
        array_push($this->champs, $table.$this->compteur);
439
        if ($presentation == MEME_LIGNE) {
440
            // Cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->ligne
441
            array_push($this->ligne, '!'.array_pop($this->ligne));
442
        }
443
        array_push($this->ligne, form::radioFromTable($table, $selected, '', '', $table.$this->compteur));
444
        $this->compteur++;
445
    }
446
 
447
    function checkboxFromTable($table, $commentaire, $idcheck, $presentation = 'LIGNE', $champs1 = '', $champs2 = '', $champs3 = '')
448
    {
449
        if (ereg("(.*) par ((.*) et (.*))", $table, $str)) {
450
            $table = $str[1] ;
451
            $table_label = $str[4] ;
452
            $table_jointure = $str[3] ;
453
            $this->jointure[$table_label] = $str[2] ;
454
            $this->champs1[$table_label] = $champs1 ;
455
            $this->champs2[$table_label] = $champs2 ;
456
            $this->champs3[$table_label] = $champs3 ;
457
        }
458
 
459
        // tableau des valeurs par default
460
        if ($this->mode == 'MOD') {
461
            $idcheck = array() ;
462
            $query =    'SELECT * '.
463
                        'FROM '.$table_jointure.' '.
464
                        'WHERE '.$champs1.' = "'.$this->id.'"';
465
            $result = mysql_query($query) or die ("Echec de la requ&ecirc;te sur $table_jointure");
466
            while ($row = mysql_fetch_array($result)) {
467
                    array_push($idcheck, $row[$champs2]) ;
468
            }
469
        }
470
        array_push($this->ligne, $commentaire);
471
        array_push($this->ligne, form::checkboxFromTable('', $table_label, $idcheck, $presentation));
472
        array_push($this->champs, $table_label) ;
473
    }
474
 
475
    // Cette methode serialize l'objet et l'ecrit dans un fichier temporaire
476
    function finalise($nom)
477
    {
478
        $retour = '<input type="hidden" id="'.$nom.'" name="'.$nom.'" value="'.urlencode(serialize($this)).'" />'."\n";
479
        return $retour;
480
    }
481
 
482
    function acquerir($nom)
483
    {
484
        $retour = unserialize(urldecode($_POST[$nom]));
485
        return $retour;
486
    }
487
 
488
    /** Fonction insertMySQL () Insere les données d'un objet formFromTable dans la base associée
489
    *
490
    * @param  string  le fin d'une requete SQL d'insertion, du type CHAMPS="valeur",....
491
    * @return integer L'identifiant d'insertion renvoyé par mysql_insert_id()
492
    */
493
    function insertMySQL($sql = '')
494
    {
495
        $jointure_multiple = array();
496
        $query =    'INSERT INTO '.$this->table.' '.
497
                    'SET ';
498
        for($i = 0 ; $i < count($this->champs) ; $i++) {
499
            // Traitement des jointures
500
            if ($this->jointure[$this->champs[$i]] != '') {
501
                // Traitement des jointures a forte cardinalite
502
                if (ereg('(.*) et (.*)', $this->jointure[$this->champs[$i]], $str)) {
503
                    array_push($jointure_multiple, $str[0]);
504
                    // Suppression de la virgule finale
505
                    //$query = ereg_replace (",$", "", $query) ;
506
                    continue;
507
                } else {
508
                    $val = $_POST[$this->champs[$i]];
509
                    $this->champs[$i] = $this->jointure[$this->champs[$i]];
510
                    $_POST[$this->champs[$i]] = $val;
511
                }
512
            }
513
            // Cas des dates
514
            if ($this->tableau_type[$this->champs[$i]] == 'date') {
515
                $_POST[$this->champs[$i]] = preg_replace("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", "\\3-\\2-\\1", $_POST[$this->champs[$i]]);
516
            }
517
            // Cas simple
518
            $champs = $this->champs[$i];
519
            $query .= $this->champs[$i];
520
            $query .= ' = ';
521
            if (empty($_POST[$champs])) {
522
                $_POST[$champs] = '';
523
            }
524
            $query .= ' "'.$_POST[$champs].'" ';
525
            if ($i < (count($this->champs) - 1)) {
526
                $query .= ',';
527
            }
528
        }
529
        if ($sql != '') {
530
            $query .= ', '.$sql;
531
        }
532
        $query = ereg_replace(',,', ',', $query);
533
        mysql_query($query) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
534
        $insert_id = mysql_insert_id();
535
 
536
        // Traitement des jointures multiples
537
        if (count($jointure_multiple) > 0) {
538
            for ($i = 0 ; $i < count($jointure_multiple) ; $i++) {
539
                ereg('(.*) et (.*)', $jointure_multiple[$i], $str);
540
                $table_jointure = $str[1];
541
                $table_label = $str[2];
542
                $queryN =   'SELECT COUNT('.$this->champs3[$table_label].') AS nombre '.
543
                            'FROM '.$table_label;
544
                $resultN = mysql_query($queryN) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryN));
545
                $rowN = mysql_fetch_object($resultN);
546
                for ($j = 0 ; $j < $rowN->nombre ; $j++) {
547
                    $nom = $table_label.$j;
548
                    if (isset($_POST[$nom]) && $_POST[$nom] != '') {
549
                        $queryM =   'INSERT INTO '.$table_jointure.' '.
550
                                    'SET '.$this->champs1[$table_label].' = '.$insert_id.', '.$this->champs2[$table_label].' = "'.$_POST[$nom].'"';
551
                        mysql_query($queryM) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryM));
552
                    }
553
                }
554
            }
555
        }
556
        return $insert_id;
557
    }
558
 
559
    /** Fonction updateMySQL() - Met à jour une table sélectionné dans l'objet formFromTable
560
    *
561
    *
562
    * @access public
563
    * @param  string  le nom du champs identifiant de la ligne à modifier.
564
    * @param  integer l'identifiant numérique de la ligne à modifier.
565
    * @param  string  code SQL à rajouter à la fin de la requete, avant le WHERE
566
    * @return boolean retourne true si l'opération réussi.
567
    */
568
    function updateMySQL($champs_key, $id, $sql = '')
569
    {
570
        $jointure_multiple = array();
571
        $query = 'UPDATE '.$this->table.' SET ';
572
        for($i = 0 ; $i < count($this->champs) ; $i++) {
573
            // Traitement des jointures
574
            if ($this->jointure[$this->champs[$i]] != '') {
575
                // Traitement des jointures a forte cardinalite
576
                if (ereg('(.*) et (.*)', $this->jointure[$this->champs[$i]], $str)) {
577
                    array_push($jointure_multiple, $str[0]);
578
                    // on enleve la virgule finale
579
                    //$query = ereg_replace ("", "", $query) ;
580
                    continue;
581
                } else {
582
                    $val = $_POST[$this->champs[$i]];
583
                    $this->champs[$i] = $this->jointure[$this->champs[$i]];
584
                    $_POST[$this->champs[$i]] = $val;
585
                }
586
            }
587
            // Cas des dates
588
            if ($this->tableau_type[$this->champs[$i]] == 'date') {
589
                $_POST[$this->champs[$i]] = preg_replace("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", "\\3-\\2-\\1", $_POST[$this->champs[$i]]);
590
            }
591
            // Cas simple
592
            $champs = $this->champs[$i];
593
            $query .= $this->champs[$i];
594
            $query .= ' = ';
595
            if (empty($_POST[$champs])) {
596
                $_POST[$champs] = '';
597
            }
598
            $query .= '"'.$_POST[$champs].'"';
599
            if ($i < (count($this->champs) - 1)) {
600
                $query .= ', ';
601
            }
602
        }
603
        // Avant d'ajouter la clause WHERE, on vérifie qu'il ne reste pas une virgule à la fin
604
        // si tel est le cas, on la supprime
605
        // signifie 'remplace une virgule à la fin de la chaine par rien.'
606
        $query = preg_replace("/(.*),$/", "\\1", $query);
607
        if ($sql != '') {
608
            $query .= ', '.$sql;
609
        }
610
        $query .= ' WHERE '.$champs_key.' = '.$id;
611
        $query = ereg_replace(', ,', ',', $query);
612
        mysql_query($query) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
613
 
614
        // Traitement des jointures multiples
615
        if (count($jointure_multiple) > 0) {
616
            for ($i = 0 ; $i < count($jointure_multiple) ; $i++) {
617
                ereg('(.*) et (.*)', $jointure_multiple[$i], $str);
618
                $table_jointure = $str[1];
619
                $table_label = $str[2];
620
                // Pour la mise a jour, on supprime puis on réinsère
621
                $queryDel = 'DELETE FROM '.$table_jointure.' '.
622
                            'WHERE '.$this->champs1[$table_label].' = '.$id;
623
                mysql_query($queryDel) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryDel));
624
 
625
                $queryN =   'SELECT COUNT('.$this->champs3[$table_label].') AS nombre '.
626
                            'FROM '.$table_label;
627
                $resultN = mysql_query($queryN) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryN));
628
                $rowN = mysql_fetch_object($resultN) ;
629
                // Il faut trouver le premier index.
630
                $requete_index =    'SELECT '.$this->champs3[$table_label].' AS _index '.
631
                                    'FROM '.$table_label;
632
                $resultat_index = mysql_query($requete_index) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $requete_index));
633
                $ligne_index = mysql_fetch_object($resultat_index);
634
                for ($j = $ligne_index->_index ; $j <= $rowN->nombre ; $j++) {
635
                    $nom = $table_label.$j;
636
                    if (empty($_POST[$nom])) {
637
                        $_POST[$nom] = '';
638
                    }
639
                    if ($_POST[$nom] != '') {
640
                        $queryM =   'INSERT INTO '.$table_jointure.' '.
641
                                    'SET '.$this->champs1[$table_label].' = '.$id.', '.$this->champs2[$table_label].' = "'.$_POST[$nom].'"';
642
                        mysql_query($queryM) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryM));
643
                    }
644
                }
645
            }
646
        }
647
        return true;
648
    }
649
 
650
    // reprend les intitules avec les valeurs
651
    function ConfirmResult()
652
    {
653
 
654
    }
655
 
656
    function setSize($champs, $size)
657
    {
658
        $this->tableau_type[$champs] = $size;
659
    }
660
 
661
    function getMaxColonne()
662
    {
663
        $maxCol = 0;
664
        $currentMax = 0;
665
        for ($i = 0; $i < count ($this->ligne); ) {
666
            if (ereg("^!", $this->ligne[$i])) {
667
                $start = true;
668
                if ($start) $currentMax++;
669
                $i += 2;
670
            } else {
671
                $start = false ;
672
                if ($currentMax > $maxCol) $maxCol = $currentMax;
673
                $currentMax = 0;
674
                $i++;
675
            }
676
        }
677
        return $maxCol;
678
    }
679
 
680
    // ajouter un formulaire sans lien avec un champs
681
    function addFormOnly($string, $label, $presentation = NOUVELLE_LIGNE)
682
    {
683
        array_push ($this->ligne, $label);
684
        array_push ($this->ligne, $string);
685
        if ($presentation == MEME_LIGNE) {
686
            // cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->ligne
687
            array_push($this->ligne, '!'.array_pop($this->ligne));
688
        }
689
    }
690
 
691
    // fonctions utiles a la classe formFromTable
692
    function _extraitTaille($type)
693
    {
694
        return eregi_replace('[^[0-9]]*', '', $type);
695
    }
696
 
697
    function _extraitRadical($type)
698
    {
699
        ereg('^[a-zA-Z]*', $type, $tableau_retour);
700
        return $tableau_retour[0];
701
    }
702
 
703
    function _estimation_taille($taille)
704
    {
705
        if ($taille < 8) {
706
            return $taille + 2;
707
        }
708
        if ($taille <= 16) {
709
            return 20;
710
        }
711
        if ($taille <= 64) {
712
            return 32;
713
        }
714
        return 60;
715
    }
716
}
717
 
718
// +------------------------------------------------------------------------------------------------------+
719
// |                                            PIED du PROGRAMME                                         |
720
// +------------------------------------------------------------------------------------------------------+
721
 
722
/* +--Fin du code ----------------------------------------------------------------------------------------+
723
*
724
* $Log: FORM_formulaire_table.class.php,v $
725
* Revision 1.4  2005/06/29 16:59:05  jpm
726
* Correction d'un bogue.
727
*
728
* Revision 1.3  2005/04/06 13:22:17  jpm
729
* Corrections et ajout d'id aux champs des formulaires.
730
*
731
* Revision 1.2  2005/03/30 09:06:26  jpm
732
* Mise en conformité du code.
733
*
734
* Revision 1.1  2005/03/03 17:44:44  jpm
735
* Ajout des fichiers anciennement contenu dans le fichier lib.form.php.
736
*
737
*
738
* +-- Fin du code ----------------------------------------------------------------------------------------+
739
*/
740
?>