<?
//$obj->addCSS("f.css");
//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 = "";
function row($fieldName, $fieldLen, $fieldType ){
$this->fieldName = $fieldName;
$this->label = $fieldName;
$this->fieldLen = $fieldLen;
$this->fieldType = $fieldType;
}
function toDB( ){
$rtn = $this->fieldValue;
$rtn = addslashes($rtn);
return $rtn;
}
function show( $mode ){
$rtn = "";
if ($mode && !$this->protected ){
switch($this->fieldType){
case 'text':
$rtn = "<TEXTAREA name='".$this->fieldName."' rows=4 cols=40>".nl2br($this->fieldValue)."</TEXTAREA>";
break;
default:
$rtn = "<input type=text 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( ){
}
}//end class
class formabs{
var $mode = CONSULT;
var $table = "";
var $SQL = "";
var $rows = array();
var $numfields = 0 ; //nombre de rows
var $ErrMsg = "";
function formabs( ){
if (isset($_POST)){
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(){
$count = $this->numfields;
$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'>";
$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;
for ($i=1;$i< $this->numfields ; $i++){
$this->fieldErrMsg[$i] = eval ($this->test[$i]);
if ( $this->fieldErrMsg[$i] != "" ) $test = false;
}
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();
$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
?>
Partager