1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| <?
//array_keys
define("CONSULT", 0);
define("MODIFY", 1);
define("ADD", 2);
class row{
/*
*/
var $label = ""; //
var $fieldName = ""; //nom du champs en db
var $fieldLen = "";
var $fieldType = "";
var $fieldValue ;//sera envoyé a la database
var $visibleValue="";//sera affiché
var $fieldErrMsg= "";
var $visible = true;/*Un champ peut etre invisible
- pour permettre des tests
- car rempli automatiquement et pour le design */
var $protected = false;
var $key = false;
var $list = array();
var $auto = "";
var $emptyisNULL = true;
function row($fieldName, $fieldLen, $fieldType ){
$this->fieldName = $fieldName;
$this->label = $fieldName;
$this->fieldLen = $fieldLen;
$this->fieldType = $fieldType;
}
function toDB( ){
$rtn = $this->fieldValue;
switch($this->fieldType){
case 'text' :
case 'varchar' :
$rtn = addslashes($rtn);
$rtn = "'".$rtn."'";
break;
case 'int' :
case 'datetime' :
break;
}
return $rtn;
}
function show( $mode ){
$rtn = "";
if ($mode && !$this->protected ){
$class = ($this->fieldErrMsg)? "class=incorrecte":"";
switch($this->fieldType){
case 'text':
$rtn = "<TEXTAREA ".$class." name='".$this->fieldName."' rows=4 cols=40>".nl2br($this->fieldValue)."</TEXTAREA>";
break;
default:
$rtn = "<input type=text ".$class." name='".$this->fieldName."' value='".$this->fieldValue."'>";
break;
}
}else{//consult
/*if ($this->fieldName == $this->primaryKey){
$form .= "<input name=".$this->rows[$i]->fieldName." type=hidden value=".$this->rows[$i]->fieldValue.">";
if ( ! $this->fieldValue ) $form .= " automatique ";
}*/
$rtn = ($this->fieldValue == '' && $this->auto )?'automatique':$this->fieldValue;
}
return $rtn;
}//end show
function test( ){
$rtn = true;
switch($this->fieldType){
case 'text':
case 'varchar':
$rtn = true;
break;
case 'int':
$rtn = is_numeric($this->fieldValue);
if ($rtn) $this->fieldErrMsg = "Ceci doit être un nombre";
break;
case 'datetime' :
$this->fieldErrMsg = "ceci est une date";
$rtn = false;
break;
}
return $rtn;
}
}//end class
class formabs{
var $mode = CONSULT;
var $table = "";
var $SQL = "";
var $rows = array();
var $numfields = 0 ; //nombre de rows
var $ErrMsg = "";
var $autocommit = false;
function formabs( ){
if (count($_POST)>0){
$this->autocommit = !$this->mode;
foreach ($_POST as $key => $val) if(isset($this->rows[$key])) $this->rows[$key]->fieldValue = $val;
}
}//end _construct
function nextSequence($col, $tab){}//end abstract fct
function mode($mode ){
$this->mode = $mode;
}//end mode
function protect($field ){
$this->rows[$field]->protected = true;
}//end protect
function setAuto($field, $fct ){
$this->protect($field);
$this->rows[$field]->auto = $fct;
}//end setAuto
function primary($field ){
$this->rows[$field]->key = true;
$this->setAuto($field, 'nextSequence');
}//end primary
function traduction($dico ){
foreach ($dico as $key => $val) if (isset($this->rows[$key]) && $val != "")$this->rows[$key]->label = $val;
}//end traduction
function getValue($field){
return (isset($this->rows[$field]))? $this->rows[$field]->fieldValue: O;
}//end getValue
function show(){
if ($this->autocommit) $this->commit();
$form = "<form method=POST action=".$_SERVER['PHP_SELF']."?mode=".$this->mode."> ";
$form .= "<table width=500 cellSpacing=0 cellPadding=0><TBODY>";
$i = 0;
foreach ($this->rows as $key => $row) {
if ($row->visible){
//libellé
$i++;
$form .= "<tr class=data".(($i % 2) + 1) ."><td>".$row->label."</td>";
//icone
$form .= "<td width=16>";
if ( $row->fieldErrMsg != "" ) $form .= "<img src='"._HOMEPAGE."/Img/b_drop.png' alt='".$row->fieldErrMsg."'>";
$form .= "</td>";
//champ
$form .= "<td>";
$form .= $row->show($this->mode) ;
$form .= "</td><td> </td></tr>";
}
}
$form .= "<TBODY></table>";
if ( $this->mode == MODIFY || $this->mode == ADD ){
$form .= " <table width=100%><tr><td>";
if ( $this->ErrMsg ) $form .= "<marquee>".$this->ErrMsg."</marquee>" ;
$form .= "</td><td width=86 height=29><input type=image src='"._HOMEPAGE."OO/form_bval.gif'></td></tr></table>";
}
$form .= "</form>";
return $form;
}// end show
function test( ){
$test = true;
foreach ($this->rows as $key => $row) {
$test = $test & $this->rows[$key]->test();
}
if (!$test) $this->ErrMsg = "<font color=red><b>Il a des champs incorrects</b></font>";
return $test;
}//end test
function fillAutoField(){
foreach ($this->rows as $key => $row) {
if ($row->auto){
switch($row->auto){
case 'nextSequence':
$temp = '$temp = $this->nextSequence("'.$key.'");';
eval($temp);
$this->rows[$key]->fieldValue = $temp;
break;
}
}
}
}//end fillAutoField
function getSQLSyntax(){
$rtn = "";
$into = "";
$values = "";
$set = "";
$where = "";
foreach ($this->rows as $key => $row) {
if ($row->key){
$where .= ($where == "" )? "" :" and ";
$where .= $key."=".$row->toDB()." ";
}else{
$set = "";
}
$into .= ','.$key;
$values .= ','.$row->toDB();
}
//retirer la premiere ','
$into = substr($into,1);
$values = substr($values,1);
switch ($this->mode){
case MODIFY:
$rtn = "update ".$this->table." set ".$set." where ".$where.";";
break;
case ADD:
$rtn = "insert into ".$this->table."(".$into.") values(".$values.");";
break;
}
return $rtn;
}//end getSQLSyntax
function sql_exec($SQL ){
return false; //abstract
}
function commit( ){
$this->fillAutoField();
echo "commit";
$this->test();
$SQL = $this->getSQLSyntax();
if ($this->sql_exec($SQL)){
switch ($this->mode){
case MODIFY:
$this->ErrMsg = "<font color=green><b>Enregistrement modifié</b></font>";
break;
case ADD:
$this->ErrMsg = "<font color=green><b>Enregistrement ajouté</b></font>";
break;
}
$this->mode = CONSULT;
}else{
$this->ErrMsg = "<font color=red><b>Modification annulée</b></font>";
}
}//end commit
}//end class
?> |