Blame | 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_table.class.php,v 1.4 2005/06/29 16:59:05 jpm Exp $/*** Classe formFromTable étendant form** Classe permettant l'édition d'une table 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/06/29 16:59:05 $// +------------------------------------------------------------------------------------------------------+*/// +------------------------------------------------------------------------------------------------------+// | ENTETE du PROGRAMME |// +------------------------------------------------------------------------------------------------------+define ('NOUVELLE_LIGNE', 1);define ('MEME_LIGNE', 2);// +------------------------------------------------------------------------------------------------------+// | CORPS du PROGRAMME |// +------------------------------------------------------------------------------------------------------+class formFromTable extends form {// Le nom de la tablevar $table;var $tableau_type;var $ligne;var $champs;var $mode;// L'identifiant de la ligne en cas de modificationvar $id;var $cle;// Les valeurs par defautvar $default_value;// Tableau contenant les jointuresvar $jointure;var $champs1;var $champs2;var $champs3;// Un compteur pour différencier les jointures portant sur une meme tablevar $compteur;function formFromTable($link, $table, $mode = 'NOUV', $id = '', $cle = ''){$this->link = $link;$this->table = $table;$this->form = '';$this->ligne = array();$this->champs = array();$this->jointure = array();$this->mode = $mode;$this->id = $id;$this->cle = $cle;$this->default_value = array();$this->champs1 = array();$this->champs2 = array();$this->champs3 = array();$this->compteur = 0;// Style$this->style_general = 'formulaire';$this->style_label = 'label';$this->style_champ = 'champ';$this->style_button = 'bouton';$this->style_commentaire = 'commentaire';$this->style_radiocheckbox = 'radio';// requete pour recuperer les types de champs dans le tableau ;$query = 'DESCRIBE '.$this->table;if (!($result = mysql_query($query))) {return false ;}while ($row = mysql_fetch_object($result)) {$this->tableau_type[$row->Field] = $row->Type;// on definit les index pour eviter les notices...$this->default_value[$row->Field] = $row->Default;$this->jointure[$row->Field] = '';}foreach ($this->tableau_type as $key => $value) {$radical = $this->_extraitRadical($value);switch ($radical) {case 'char' :case 'varchar' :case 'smallint' :case 'int' :$this->tableau_type[$key] = $this->_estimation_taille($this->_extraitTaille($value));break;case 'text' :$this->tableau_type[$key] = 'text';break;case 'enum' :$this->tableau_type[$key] = 'enum';break;case 'date' :$this->tableau_type[$key] = 'date';break;case 'set' :$this->tableau_type[$key] = 'set';break;}}// on recupere les infos dans le cas d'une modification// notons que les valeurs par défaut initialisé plus haut à partir de la table SQL// sont écrasées.if ($this->mode == 'MOD') {$query = 'SELECT * '.'FROM '.$this->table.' '.'WHERE '.$this->cle.' = "'.$this->id.'"';$result = mysql_query($query) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));$this->default_value = mysql_fetch_array($result);}}// Ajouter un champ pour le formumaire nom_du_champs, label_a_mettre_devantfunction addChamps($name, $label, $default = '', $type = 'text', $presentation = NOUVELLE_LIGNE){array_push($this->ligne, $label);array_push($this->champs, $name);switch($this->tableau_type[$name]) {case 'text' :$this->textarea($name);break;case 'enum' :$this->enum($name);break;case 'date' :$this->date($name, $presentation);break;case 'set' :$this->set($name);break;default :$this->input($name, $default, $type, $presentation);}}function textarea($name){$code = '<textarea id="'.$name.'" name="'.$name.'" class="'.$this->style_general.'" cols="60" rows="5">';$code .= $this->default_value[$name];$code .= '</textarea>'."\n";array_push($this->ligne, $code);}function input($name,$default, $type = 'text', $presentation = NOUVELLE_LIGNE) {if (!empty($default) and $this->mode != 'MOD') {$this->default_value[$name] = $default;}$code = '<input type="'.$type.'" id="'.$name.'" name="'.$name.'" size="'.$this->tableau_type[$name].'" class="'.$this->style_general.'" ';$code .= 'value="';if ($this->mode == 'MOD' and $type == 'password') {} else {$code .= htmlentities($this->default_value[$name]);}$code .= '" />'."\n";if ($presentation == MEME_LIGNE) {// cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->lignearray_push($this->ligne, '!'.array_pop($this->ligne));}array_push($this->ligne, $code);}function date($name, $presentation = NOUVELLE_LIGNE) {if ($this->default_value[$name] != '') {$this->default_value[$name] = preg_replace("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", "\\3-\\2-\\1", $this->default_value[$name]);}$code = '<input type="text" id="'.$name.'" name="'.$name.'" size="12" class="'.$this->style_general.'" ';$code .= 'value="'.$this->default_value[$name].'" />';if ($presentation == MEME_LIGNE) {// cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->lignearray_push($this->ligne, '!'.array_pop($this->ligne));}array_push($this->ligne, $code);}function submit($label,$name = '', $autre = ''){array_push($this->ligne, form::submit($label, $name, $autre)) ;}function annuler($label, $fonction = ''){$code = '<input type="button" id="annu" name="annu" class="'.$this->style_button.'" value="'.$label.'" ';$code .= 'onclick="javascript:window.location.href=\''.$fonction.'\';" />'."\n";array_push($this->ligne, $code) ;}function printForm($action, $autre = '', $format = 'table'){//Création du formulaire et de sa structure$res = '<form id="'.$this->table.'" action="'.$action.'" method="post" ';if ($autre != '') {$res .= ' '.$autre;}$res .= '>'."\n";if ($format == 'table') {$res .= '<table class="'.$this->style_general.'">'."\n";$colspan = $this->getMaxColonne() * 2;} elseif ($format == 'liste') {$res .= '<ul class="'.$this->style_general.'">'."\n";}// Ajout des champs du formulairefor ($i = 0; $i < count($this->ligne); $i = $i + 2) {// Ajout du bouton de validation du formulaireif (ereg('submit', $this->ligne[$i])) {if ($format == 'table') {$res .= '<tr><td> </td><td>';} elseif ($format == 'liste') {$res .= '<li>';}$res .= $this->ligne[$i]."\n";if (isset($this->ligne[$i+1])) {$res .= $this->ligne[$i+1];}if ($format == 'table') {$res .= '</td></tr>';} elseif ($format == 'liste') {$res .= '</li>'."\n";}$i--;break;}// Ajout de commentaireif (ereg('^#', $this->ligne[$i])) {if ($format == 'table') {$res .= '<tr><td colspan="'.($colspan + 2).'" ';} elseif ($format == 'liste') {$res .= '<li ';}// Suppression du premier #$this->ligne[$i] = ereg_replace('^#', '', $this->ligne[$i]);if (ereg('#', $this->ligne[$i])) {$tableau = explode('#', $this->ligne[$i]);$style = $tableau[1];$this->ligne[$i] = $tableau[0];} else {$style = $this->style_commentaire;}$res .= 'class="'.$style.'">';$res .= $this->ligne[$i];if ($format == 'table') {$res .= '</td></tr>'."\n";} elseif ($format == 'liste') {$res .= '</li>'."\n";}$i--;continue;}if (ereg('^!', $this->ligne[$i])) {if ($format == 'table') {$res .= '<tr>'."\n";} elseif ($format == 'liste') {$res .= '<li>'."\n";}do {if ($format == 'table') {$res .= '<td class="'.$this->style_label.'">'.ereg_replace("^!", '', $this->ligne[$i]).'</td>'."\n";$res .= '<td class="'.$this->style_commentaire.'">'.$this->ligne[$i + 1].'</td>'."\n";} elseif ($format == 'liste') {$res .= '<td class="'.$this->style_label.'">'.ereg_replace("^!", '', $this->ligne[$i]).'</td>'."\n";$res .= '<td class="'.$this->style_commentaire.'">'.$this->ligne[$i + 1].'</td>'."\n";}$i += 2;} while (ereg('^!', $this->ligne[$i - 2]));if ($format == 'table') {$res .= '</tr>'."\n";} elseif ($format == 'liste') {$res .= '</li>'."\n";}$i -= 2;// on est allé trop loin donc un revient sur nos pascontinue;}// Ajout du labelif ($format == 'table') {$res .= '<tr><td ';if (isset($this->ligne[$i + 1])) {if (ereg('textarea', $this->ligne[$i + 1])) {$res .= 'valign="top" ';}}$res .= 'class="'.$this->style_label.'">';} elseif ($format == 'liste') {$res .= '<li>'."\n";}$res .= '<label>'.$this->ligne[$i].'</label>';// Ajout champif ($format == 'table') {$res .= '</td>'."\n";$res .= '<td class="'.$this->style_radiocheckbox.'" colspan="';$res .= $colspan + 1;$res .= '">';}if (isset($this->ligne[$i + 1])) {$res .= $this->ligne[$i + 1];}if ($format == 'table') {$res .= '</td>'."\n".'</tr>'."\n";} elseif ($format == 'liste') {$res .= '</li>'."\n";}}// Affichage du champ caché contenant l'objet Formif ($format == 'table') {$res .= '<tr>'."\n".'<td> </td><td>'."\n";} elseif ($format == 'liste') {$res .= '<li>'."\n";}$res .= $this->finalise($this->table);if ($format == 'table') {$res .= '</td>'."\n".'</tr>'."\n";} elseif ($format == 'liste') {$res .= '</li>'."\n";}// Fin de la structure du formulaireif ($format == 'table') {$res .= '</table>'."\n";} elseif ($format == 'liste') {$res .= '</ul>'."\n";}$res .= '</form>'."\n";return $res;}function addCommentaire($Commentaire, $style = ''){$ligne = '#'.$Commentaire;if ($style != '') {$ligne .= '#'.$style;}array_push($this->ligne, $ligne);}function enum($name){array_push($this->ligne, form::selectFromEnum($this->table, $name, $this->default_value[$name]));}function set($name){array_push($this->ligne, form::checkboxFromSet($this->table, $name, explode(',', $this->default_value[$name])));}function radio($name, $tableau, $label, $checked = ''){array_push($this->ligne, $label);array_push($this->champs, $name);$code = '';for ($i = 0; $i < count($tableau); $i = $i + 2) {$code .= '<input type="radio" id="'.$name.'" name="'.$name.'" value="'.$tableau[$i].'"';if ($tableau[$i] == $this->default_value[$name]) {$code .= ' checked';}$code .= ' />';$code .= ' '.$tableau[$i + 1];}array_push($this->ligne, $code);}function radioEnum($name, $label, $checked = ''){array_push($this->ligne, $label);array_push($this->champs, $name);$code = form::radioFromEnum($this->table, $name, $this->default_value[$name]);array_push($this->ligne, $code);}function selectFromTable($table, $selected = '',$champs1 = '',$champs2 = '', $commentaire = ''){if (ereg('(.*) par (.*)', $table, $str)) {$this->jointure[$str[2]] = $str[1];$table = $str[2];}if ($this->default_value[$str[1]] != '' and $selected == '') {$selected = $this->default_value[$str[1]];}array_push($this->ligne, $commentaire);array_push($this->champs, $table);array_push($this->ligne,form::selectFromTable($table, $selected, $champs1, $champs2));}function radioFromTable($table, $selected="", $commentaire="", $presentation = NOUVELLE_LIGNE){if (ereg('(.*) par (.*)', $table, $str)) {$this->jointure[$str[2].$this->compteur] = $str[1];$table = $str[2];}if ($this->default_value[$str[1]] != '') {$selected = $this->default_value[$str[1]];}array_push($this->ligne, $commentaire);array_push($this->champs, $table.$this->compteur);if ($presentation == MEME_LIGNE) {// Cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->lignearray_push($this->ligne, '!'.array_pop($this->ligne));}array_push($this->ligne, form::radioFromTable($table, $selected, '', '', $table.$this->compteur));$this->compteur++;}function checkboxFromTable($table, $commentaire, $idcheck, $presentation = 'LIGNE', $champs1 = '', $champs2 = '', $champs3 = ''){if (ereg("(.*) par ((.*) et (.*))", $table, $str)) {$table = $str[1] ;$table_label = $str[4] ;$table_jointure = $str[3] ;$this->jointure[$table_label] = $str[2] ;$this->champs1[$table_label] = $champs1 ;$this->champs2[$table_label] = $champs2 ;$this->champs3[$table_label] = $champs3 ;}// tableau des valeurs par defaultif ($this->mode == 'MOD') {$idcheck = array() ;$query = 'SELECT * '.'FROM '.$table_jointure.' '.'WHERE '.$champs1.' = "'.$this->id.'"';$result = mysql_query($query) or die ("Echec de la requête sur $table_jointure");while ($row = mysql_fetch_array($result)) {array_push($idcheck, $row[$champs2]) ;}}array_push($this->ligne, $commentaire);array_push($this->ligne, form::checkboxFromTable('', $table_label, $idcheck, $presentation));array_push($this->champs, $table_label) ;}// Cette methode serialize l'objet et l'ecrit dans un fichier temporairefunction finalise($nom){$retour = '<input type="hidden" id="'.$nom.'" name="'.$nom.'" value="'.urlencode(serialize($this)).'" />'."\n";return $retour;}function acquerir($nom){$retour = unserialize(urldecode($_POST[$nom]));return $retour;}/** Fonction insertMySQL () Insere les données d'un objet formFromTable dans la base associée** @param string le fin d'une requete SQL d'insertion, du type CHAMPS="valeur",....* @return integer L'identifiant d'insertion renvoyé par mysql_insert_id()*/function insertMySQL($sql = ''){$jointure_multiple = array();$query = 'INSERT INTO '.$this->table.' '.'SET ';for($i = 0 ; $i < count($this->champs) ; $i++) {// Traitement des jointuresif ($this->jointure[$this->champs[$i]] != '') {// Traitement des jointures a forte cardinaliteif (ereg('(.*) et (.*)', $this->jointure[$this->champs[$i]], $str)) {array_push($jointure_multiple, $str[0]);// Suppression de la virgule finale//$query = ereg_replace (",$", "", $query) ;continue;} else {$val = $_POST[$this->champs[$i]];$this->champs[$i] = $this->jointure[$this->champs[$i]];$_POST[$this->champs[$i]] = $val;}}// Cas des datesif ($this->tableau_type[$this->champs[$i]] == 'date') {$_POST[$this->champs[$i]] = preg_replace("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", "\\3-\\2-\\1", $_POST[$this->champs[$i]]);}// Cas simple$champs = $this->champs[$i];$query .= $this->champs[$i];$query .= ' = ';if (empty($_POST[$champs])) {$_POST[$champs] = '';}$query .= ' "'.$_POST[$champs].'" ';if ($i < (count($this->champs) - 1)) {$query .= ',';}}if ($sql != '') {$query .= ', '.$sql;}$query = ereg_replace(',,', ',', $query);mysql_query($query) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));$insert_id = mysql_insert_id();// Traitement des jointures multiplesif (count($jointure_multiple) > 0) {for ($i = 0 ; $i < count($jointure_multiple) ; $i++) {ereg('(.*) et (.*)', $jointure_multiple[$i], $str);$table_jointure = $str[1];$table_label = $str[2];$queryN = 'SELECT COUNT('.$this->champs3[$table_label].') AS nombre '.'FROM '.$table_label;$resultN = mysql_query($queryN) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryN));$rowN = mysql_fetch_object($resultN);for ($j = 0 ; $j < $rowN->nombre ; $j++) {$nom = $table_label.$j;if (isset($_POST[$nom]) && $_POST[$nom] != '') {$queryM = 'INSERT INTO '.$table_jointure.' '.'SET '.$this->champs1[$table_label].' = '.$insert_id.', '.$this->champs2[$table_label].' = "'.$_POST[$nom].'"';mysql_query($queryM) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryM));}}}}return $insert_id;}/** Fonction updateMySQL() - Met à jour une table sélectionné dans l'objet formFromTable*** @access public* @param string le nom du champs identifiant de la ligne à modifier.* @param integer l'identifiant numérique de la ligne à modifier.* @param string code SQL à rajouter à la fin de la requete, avant le WHERE* @return boolean retourne true si l'opération réussi.*/function updateMySQL($champs_key, $id, $sql = ''){$jointure_multiple = array();$query = 'UPDATE '.$this->table.' SET ';for($i = 0 ; $i < count($this->champs) ; $i++) {// Traitement des jointuresif ($this->jointure[$this->champs[$i]] != '') {// Traitement des jointures a forte cardinaliteif (ereg('(.*) et (.*)', $this->jointure[$this->champs[$i]], $str)) {array_push($jointure_multiple, $str[0]);// on enleve la virgule finale//$query = ereg_replace ("", "", $query) ;continue;} else {$val = $_POST[$this->champs[$i]];$this->champs[$i] = $this->jointure[$this->champs[$i]];$_POST[$this->champs[$i]] = $val;}}// Cas des datesif ($this->tableau_type[$this->champs[$i]] == 'date') {$_POST[$this->champs[$i]] = preg_replace("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", "\\3-\\2-\\1", $_POST[$this->champs[$i]]);}// Cas simple$champs = $this->champs[$i];$query .= $this->champs[$i];$query .= ' = ';if (empty($_POST[$champs])) {$_POST[$champs] = '';}$query .= '"'.$_POST[$champs].'"';if ($i < (count($this->champs) - 1)) {$query .= ', ';}}// Avant d'ajouter la clause WHERE, on vérifie qu'il ne reste pas une virgule à la fin// si tel est le cas, on la supprime// signifie 'remplace une virgule à la fin de la chaine par rien.'$query = preg_replace("/(.*),$/", "\\1", $query);if ($sql != '') {$query .= ', '.$sql;}$query .= ' WHERE '.$champs_key.' = '.$id;$query = ereg_replace(', ,', ',', $query);mysql_query($query) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $query));// Traitement des jointures multiplesif (count($jointure_multiple) > 0) {for ($i = 0 ; $i < count($jointure_multiple) ; $i++) {ereg('(.*) et (.*)', $jointure_multiple[$i], $str);$table_jointure = $str[1];$table_label = $str[2];// Pour la mise a jour, on supprime puis on réinsère$queryDel = 'DELETE FROM '.$table_jointure.' '.'WHERE '.$this->champs1[$table_label].' = '.$id;mysql_query($queryDel) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryDel));$queryN = 'SELECT COUNT('.$this->champs3[$table_label].') AS nombre '.'FROM '.$table_label;$resultN = mysql_query($queryN) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryN));$rowN = mysql_fetch_object($resultN) ;// Il faut trouver le premier index.$requete_index = 'SELECT '.$this->champs3[$table_label].' AS _index '.'FROM '.$table_label;$resultat_index = mysql_query($requete_index) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $requete_index));$ligne_index = mysql_fetch_object($resultat_index);for ($j = $ligne_index->_index ; $j <= $rowN->nombre ; $j++) {$nom = $table_label.$j;if (empty($_POST[$nom])) {$_POST[$nom] = '';}if ($_POST[$nom] != '') {$queryM = 'INSERT INTO '.$table_jointure.' '.'SET '.$this->champs1[$table_label].' = '.$id.', '.$this->champs2[$table_label].' = "'.$_POST[$nom].'"';mysql_query($queryM) or die(BOG_afficherErreurSql(__FILE__, __LINE__, mysql_error(), $queryM));}}}}return true;}// reprend les intitules avec les valeursfunction ConfirmResult(){}function setSize($champs, $size){$this->tableau_type[$champs] = $size;}function getMaxColonne(){$maxCol = 0;$currentMax = 0;for ($i = 0; $i < count ($this->ligne); ) {if (ereg("^!", $this->ligne[$i])) {$start = true;if ($start) $currentMax++;$i += 2;} else {$start = false ;if ($currentMax > $maxCol) $maxCol = $currentMax;$currentMax = 0;$i++;}}return $maxCol;}// ajouter un formulaire sans lien avec un champsfunction addFormOnly($string, $label, $presentation = NOUVELLE_LIGNE){array_push ($this->ligne, $label);array_push ($this->ligne, $string);if ($presentation == MEME_LIGNE) {// cette feinte ajoute le ! au début de la chaine du dernier élément du tableau $this->lignearray_push($this->ligne, '!'.array_pop($this->ligne));}}// fonctions utiles a la classe formFromTablefunction _extraitTaille($type){return eregi_replace('[^[0-9]]*', '', $type);}function _extraitRadical($type){ereg('^[a-zA-Z]*', $type, $tableau_retour);return $tableau_retour[0];}function _estimation_taille($taille){if ($taille < 8) {return $taille + 2;}if ($taille <= 16) {return 20;}if ($taille <= 64) {return 32;}return 60;}}// +------------------------------------------------------------------------------------------------------+// | PIED du PROGRAMME |// +------------------------------------------------------------------------------------------------------+/* +--Fin du code ----------------------------------------------------------------------------------------+** $Log: FORM_formulaire_table.class.php,v $* Revision 1.4 2005/06/29 16:59:05 jpm* Correction d'un bogue.** Revision 1.3 2005/04/06 13:22:17 jpm* Corrections et ajout d'id aux champs des formulaires.** Revision 1.2 2005/03/30 09:06:26 jpm* Mise en conformité 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 ----------------------------------------------------------------------------------------+*/?>