Subversion Repositories Sites.tela-botanica.org

Rev

Rev 609 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*vim: set expandtab tabstop=4 shiftwidth=4: */ 
// +------------------------------------------------------------------------------------------------------+
// | PHP version 4.1                                                                                      |
// +------------------------------------------------------------------------------------------------------+
// | Copyright (C) 2005 Tela Botanica (accueil@tela-botanica.org)                                         |
// +------------------------------------------------------------------------------------------------------+
// | This file is part of API - Formulaire.                                                               |
// |                                                                                                      |
// | Foobar is free software; you can redistribute it and/or modify                                       |
// | it under the terms of the GNU General Public License as published by                                 |
// | the Free Software Foundation; either version 2 of the License, or                                    |
// | (at your option) any later version.                                                                  |
// |                                                                                                      |
// | Foobar is distributed in the hope that it will be useful,                                            |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
// | GNU General Public License for more details.                                                         |
// |                                                                                                      |
// | You should have received a copy of the GNU General Public License                                    |
// | along with Foobar; if not, write to the Free Software                                                |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
// +------------------------------------------------------------------------------------------------------+
// CVS : $Id: FORM_formulaire.class.php,v 1.4 2005/04/06 13:22:17 jpm Exp $
/**
* Classe form
*
* Classe permettant l'édition d'une base donnée MySQL de façon
* simplifié.
*
*@package Formulaire
//Auteur original :
*@author        Alexandre GRANIER <alexandre@tela-botanica.org>
//Autres auteurs :
*@author        Jean-Pascal MILCENT <jpm@clapas.org>
*@copyright     Tela-Botanica 2000-2005
*@version       $Revision: 1.4 $ $Date: 2005/04/06 13:22:17 $
// +------------------------------------------------------------------------------------------------------+
*/

// +------------------------------------------------------------------------------------------------------+
// |                                            ENTETE du PROGRAMME                                       |
// +------------------------------------------------------------------------------------------------------+

// +------------------------------------------------------------------------------------------------------+
// |                                            CORPS du PROGRAMME                                        |
// +------------------------------------------------------------------------------------------------------+
class form {
        // propriétés
        var $link;
        var $style_general;
        var $style_label;
        var $style_champ;
        var $style_button;
        var $style_radiocheckbox;
        var $style_commentaire;
        
        // constructeur
        function form($identifiant_de_connection)
        {
            $this->link = $identifiant_de_connection;
        }
        
        function select_db($db)
        {
            mysql_select_db($db, $this->link) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $db));
        }
        
        function selectFromEnum($table, $champs_enum, $selected = '')
        {
            // recup des différentes occurences
            $query = 'DESCRIBE '.$table;
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
                
            while ($row = mysql_fetch_object($result)) {
                if ($row->Field == $champs_enum) {
                    break;
                }
            }
            $tableau_enum = form::_extraction('enum', $row->Type);
            
            // construction du select
            $res = '<select name="'.$champs_enum.'" ';
            if ($this->style_general != '') {
                $res .= 'class="'.$this->style_general.'"';
            }
            $res .= '>'."\n";
            
            for ($i = 0 ; $i < count ($tableau_enum) ; $i++) {
                $res .= "\t".'<option value="'.$tableau_enum[$i].'"';
                if ($selected != '') {
                    if ($tableau_enum[$i] == $selected) {
                        $res .= 'selected';
                    }
                }
                $res .= '>'.$tableau_enum[$i].'</option>'."\n";
            }
            $res .= '</select>'."\n";
            mysql_free_result($result);
            return $res;
        }
        
        // Pour une table ne comportant que 2 champs ID et Label
        function selectFromTable($table, $selected = '',$champs1 = '',$champs2 = '', $autre = '')
        {
            // la requete, sur la table
            if ($champs1 != '') {
                $query =    'SELECT '.$table.'.'.$champs1.', '.$table.'.'.$champs2.' '.
                            'FROM '.$table;
            } else {
                $query =    'SELECT * '.
                            'FROM '.$table;
            }
            if (!($result = mysql_query($query))) {
                return false ;
            }
            
            $res = '<select name="'.$table.'" ';
            if ($this->style_general != '') {
                $res .= 'class="'.$this->style_general.'"';
            }
            if ($autre != '') {
                $res .= ' '.$autre;
            }
            $res .= '>'."\n";
            while ($row = mysql_fetch_row($result)) {
                $res .= "\t".'<option value="'.$row[0].'"';
                if ($row[0] == $selected) {
                    $res .= ' selected="selected"';
                }
                $res .= '>'.$row[1].'</option>'."\n";
            }
            $res .= '</select>'."\n";
            mysql_free_result($result) ;
            return $res ;
        }
        
        // Génère un <select> avec les elements d'un tableau
        function selectFromTableau($nom, $tableau, $selected = '', $javascript = array())
        {
            $res = '<select name="'.$nom.'" ';
            if ($this->style_general != '') {
                $res .= 'class="'.$this->style_general.'" ';
            }
            if (! empty($javascript)) {
                $res .= $javascript['nom_evenement'].'="'.$javascript['valeur_evenement'].'"';
            }
            $res .= '>'."\n";
            for ($i = 0 ; $i < count ($tableau) ; $i++) {
                $res .= "\t".'<option value="'.$tableau[$i].'" ';
                if ($tableau[$i] == $selected) {
                    $res .= 'selected="selected"';
                }
                $i++;
                $res .= '>'.$tableau[$i].'</option>'."\n";
            }
            $res .= '</select>'."\n";
            return $res;
        }
        
        function radioFromEnum($table, $champs_enum, $enum_checked = '')
        {
        // Récupération des différentes occurences
            $query = 'DESCRIBE '.$table;
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
            
            while ($row = mysql_fetch_object($result)) {
                if ($row->Field == $champs_enum) {
                    break;
                }
            }
            $tableau_enum = form::_extraction('enum', $row->Type);
            
            // Construction du select
            $res = '';
            for ($i = 0 ; $i < count ($tableau_enum) ; $i++) {
                $res .= '<input ';
                if ($this->style_radiocheckbox != '') {
                    $res .= 'class="'.$this->style_radiocheckbox.'" ';
                }
                $res .= 'type="radio" name="'.$champs_enum.'" value="'.$tableau_enum[$i].'" ';
                if ($tableau_enum[$i] == $enum_checked) {
                    $res .= ' checked="checked"';
                }
                $res .= ' />'."\n";
                $res .= "\t\t".$tableau_enum[$i]."\n";
                $res .= "\t".'&nbsp;&nbsp;'."\n";
            }
            mysql_free_result($result);
            return $res;
        }
        
        // pour une table ne comportant que 2 champs ID et Label
        function radioFromTable($table, $idChecked = '', $champs1 = '', $champs2 = '', $name = '')
        {
            // La requete, sur la table
            $requete =  'SELECT * '.
                        'FROM '.$table;
            $resultat = mysql_query($requete) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $requete));
            if (!($resultat)) {
                return false;
            }
            
            $retour = '';
            while ($ligne = mysql_fetch_row($resultat)) {
                $retour .= '<input ';
                if ($this->style_radiocheckbox != '') {
                    $retour .= 'class="'.$this->style_radiocheckbox.'" ';
                }
                $retour .= 'type="radio" name="';
                if ($name != '') {
                    $retour .= $name;
                } else {
                    $retour .= $table;
                }
                $retour .= '" value="'.$ligne[0].'" ';
                if ($ligne[0]== $idChecked) {
                    $retour .= 'checked="checked" ';
                }
                $retour .= ' />'."\n";
                $retour .= "\t".'&nbsp;'."\t".$ligne[1]."\n";
                $retour .= "\t".'&nbsp;&nbsp;<br />'."\n";
            }
            
            mysql_free_result($resultat);
            return $retour;
        }
        
        function checkboxFromTable($table_jointure, $table_label, $idChecked, $presentation = 'LIGNE')
        {
            // la requete sur les tables
            if (empty($idChecked)) {
                $idChecked = array();
            }
            $query =    'SELECT * '.
                        'FROM '.$table_label;
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
            if (!($result)) {
                return false;
            }
            $res = '';
            while ($row = mysql_fetch_row($result)) {
                $res .= "\n".'<input type="checkbox" name="'.$table_label.$row[0].'" value="'.$row[0].'"';
                if (in_array($row[0], $idChecked)) {
                    $res .= ' checked="checked"';
                }
                $res .= ' />'.$row[1];
                if ($presentation == 'LIGNE') {
                    $res .= '<br />';
                }
            }
            return $res;
        }
        
        function checkboxFromSet($table, $champs, $set_checked = '')
        {
            if (empty($set_checked)) {
                $set_checked = array();
            }
            
            // Récupération des différentes occurences
            $query = 'DESCRIBE '.$table;
            $result = mysql_query($query) or die (BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));
            
            while ($row = mysql_fetch_object($result)) {
                if ($row->Field == $champs) {
                    break;
                }
            }
            $tableau_enum = form::_extraction('set', $row->Type);
            
            for ($i = 0 ; $i < count($tableau_enum) ; $i++) {
                // Construction du des checkbox
                $res = '<input type="checkbox" name="'.$champs.'" ';
                if ($this->style_general != '') {
                    $res .= 'class="'.$this->style_general.'" ';
                }
                $res .= 'value="'.$tableau_enum[$i].'" ';
                if (in_array($tableau_enum[$i], $set_checked)) {
                    $res .= 'selected="selected"';
                }
                $res .= ' />'.$tableau_enum[$i].'&nbsp;&nbsp;';
            }
            mysql_free_result($result);
            return $res;
        }
    
    function submit($value, $name = '', $autre = '')
    {
        $res = '<input ';
        if ($name != '') {
            $res .= 'name="'.$name.'" ';
        }
        $res .= 'type="submit" value="'.$value.'" class="'.$this->style_button.'" ';
        if ($autre != '') {
            $res .= $autre;
        }
        $res .= ' />'."\n";
        return $res;
    }
    
    function bouton($label, $style = '', $name = 'bouton', $autre = '')
    {
        $res = "\t".'<input type="submit" name="'.$name.'" value="'.$label.'" ';
        if ($style != '') {
            $res .= 'class="'.$style.'"';
        }
        if ($autre != '') {
            $res .= ' '.$autre;
        }
        $res .= ' />'."\n";
        return $res;
    }
    
    function _extraction($action, $chaine)
    {
        $regexp = '^'.$action.'\(';
        $chaine = ereg_replace($regexp, '', $chaine);
        $chaine = ereg_replace("\)$", '', $chaine);
        $chaine = ereg_replace("'", '', $chaine);
        $tableau_enum = explode(',', $chaine);
        return $tableau_enum;
    }
}

// +------------------------------------------------------------------------------------------------------+
// |                                            PIED du PROGRAMME                                         |
// +------------------------------------------------------------------------------------------------------+

/* +--Fin du code ----------------------------------------------------------------------------------------+
*
* $Log: FORM_formulaire.class.php,v $
* Revision 1.4  2005/04/06 13:22:17  jpm
* Corrections et ajout d'id aux champs des formulaires.
*
* Revision 1.3  2005/03/30 09:06:26  jpm
* Mise en conformité du code.
*
* Revision 1.2  2005/03/08 11:29:51  jpm
* Amélioration du code.
*
* Revision 1.1  2005/03/03 17:44:44  jpm
* Ajout des fichiers anciennement contenu dans le fichier lib.form.php.
*
*
* +-- Fin du code ----------------------------------------------------------------------------------------+
*/
?>