00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009 require_once('class_database.php');
00010 require_once('ac_common.php');
00011
00012 class Fiche_Attr
00013 {
00014
00015
00016 protected $variable=array("id"=>"ad_id","desc"=>"ad_text","type"=>"ad_type","size"=>"ad_size","extra"=>"ad_extra");
00017 function __construct ($p_cn,$p_id=0)
00018 {
00019 $this->cn=$p_cn;
00020 if ( $p_id == 0 )
00021 {
00022
00023 foreach ($this->variable as $key=>$value) $this->$value='';
00024 }
00025 else
00026 {
00027
00028 $this->ad_id=$p_id;
00029 $this->load();
00030 }
00031 }
00032 public function get_parameter($p_string)
00033 {
00034 if ( array_key_exists($p_string,$this->variable) )
00035 {
00036 $idx=$this->variable[$p_string];
00037 return $this->$idx;
00038 }
00039 else
00040 throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00041 }
00042 public function set_parameter($p_string,$p_value)
00043 {
00044 if ( array_key_exists($p_string,$this->variable) )
00045 {
00046 $idx=$this->variable[$p_string];
00047 $this->$idx=$p_value;
00048 }
00049 else
00050 throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00051 }
00052 public function get_info()
00053 {
00054 return var_export($this,true);
00055 }
00056 public function verify()
00057 {
00058
00059
00060 if ( strlen(trim($this->ad_text))==0)
00061 throw new Exception('La description ne peut pas être vide',1);
00062 if ( strlen(trim($this->ad_type))==0)
00063 throw new Exception('Le type ne peut pas être vide',1);
00064 $this->ad_type=strtolower($this->ad_type);
00065 if ( in_array($this->ad_type,array('date','text','numeric','zone','poste','card','select'))==false)
00066 throw new Exception('Le type doit être text, numeric,poste, card, select ou date',1);
00067 if ( trim($this->ad_size)=='' || isNumber($this->ad_size)==0||$this->ad_size>22)
00068 {
00069 switch ($this->ad_type)
00070 {
00071 case 'text':
00072 $this->ad_size=22;
00073 break;
00074 case 'numeric':
00075 $this->ad_size=9;
00076 break;
00077 case 'date':
00078 $this->ad_size=8;
00079 break;
00080 case 'zone':
00081 $this->ad_size=22;
00082 break;
00083
00084 default:
00085 $this->ad_size=22;
00086 }
00087 }
00088 if ( $this->ad_type == 'numeric' ) {
00089 $this->ad_extra=(trim($this->ad_extra)=='')?'2':$this->ad_extra;
00090 if (isNumber($this->ad_extra) == 0) throw new Exception ("La précision doit être un chiffre");
00091
00092 }
00093 if ( $this->ad_type == 'select')
00094 {
00095 if (trim($this->ad_extra)=="") throw new Exception ("La requête SQL est vide ");
00096 if ( preg_match('/^\h*select/i',$this->ad_extra) == 0) throw new Exception ("La requête SQL doit commencer par SELECT ");
00097 try{
00098
00099 $this->cn->exec_sql($this->ad_extra);
00100 }catch (Exception $e)
00101 {
00102 throw new Exception ("La requête SQL ".h($this->ad_extra)." est invalide ");
00103 }
00104 }
00105 }
00106 public function save()
00107 {
00108
00109
00110 if ( $this->ad_id == 0 )
00111 $this->insert();
00112 else
00113 $this->update();
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 public function seek($cond='',$p_array=null)
00123 {
00124 if ( $cond != '')
00125 $sql="select * from attr_def where $cond order by ad_text";
00126 else
00127 $sql="select * from attr_def order by ad_text";
00128
00129 $aobj=array();
00130 $array= $this->cn->get_array($sql,$p_array);
00131
00132 $size=$this->cn->count();
00133 if ( $size == 0 ) return $aobj;
00134 for ($i=0;$i<$size;$i++)
00135 {
00136 $oobj=new Fiche_Attr ($this->cn);
00137 foreach ($array[$i] as $idx=>$value)
00138 {
00139 $oobj->$idx=$value;
00140 }
00141 $aobj[]=clone $oobj;
00142 }
00143 return $aobj;
00144 }
00145 public function insert()
00146 {
00147 try{
00148 $this->verify();
00149
00150 $sql="insert into attr_def(ad_text
00151 ,ad_type,ad_size,ad_extra
00152 ) values ($1
00153 ,$2,$3,$4
00154 ) returning ad_id";
00155
00156 $this->ad_id=$this->cn->get_value(
00157 $sql,
00158 array( $this->ad_text,$this->ad_type,$this->ad_size,$this->ad_extra
00159 )
00160 );
00161 } catch (Exception $e)
00162 {
00163 throw $e;
00164 }
00165
00166 }
00167
00168 public function update()
00169 {
00170 try
00171 {
00172 $this->verify();
00173 if ( $this->ad_id < 9000) return;
00174
00175 $sql=" update attr_def set ad_text = $1
00176 ,ad_type = $2,ad_size=$4,ad_extra=$5
00177 where ad_id= $3";
00178 $res=$this->cn->exec_sql(
00179 $sql,
00180 array($this->ad_text
00181 ,$this->ad_type
00182 ,$this->ad_id,$this->ad_size,$this->ad_extra)
00183 );
00184 }catch (Exception $e)
00185 {
00186 throw $e;
00187 }
00188
00189
00190 }
00191
00192
00193
00194
00195 public function load()
00196 {
00197
00198 $sql="select ad_text
00199 ,ad_type,ad_size,ad_extra
00200 from attr_def where ad_id=$1";
00201
00202 $res=$this->cn->get_array(
00203 $sql,
00204 array($this->ad_id)
00205 );
00206
00207 if ( count($res) == 0 )
00208 {
00209
00210 foreach ($this->variable as $key=>$value) $this->$key='';
00211
00212 return -1;
00213 }
00214 foreach ($res[0] as $idx=>$value)
00215 {
00216 $this->$idx=$value;
00217 }
00218 return 0;
00219 }
00220
00221 public function delete()
00222 {
00223 if ($this->ad_id < 9000) return;
00224 $sql=$this->cn->exec_sql("delete from fiche_detail where ad_id=$1 ",
00225 array($this->ad_id));
00226
00227 $sql="delete from jnt_fic_attr where ad_id=$1";
00228 $res=$this->cn->exec_sql($sql,array($this->ad_id));
00229
00230 $sql="delete from attr_def where ad_id=$1";
00231 $res=$this->cn->exec_sql($sql,array($this->ad_id));
00232
00233 }
00234
00235
00236
00237 static function test_me()
00238 {
00239 $cn=new Database(25);
00240 $cn->start();
00241 echo h2info('Test object vide');
00242 $obj=new Fiche_Attr($cn);
00243 var_dump($obj);
00244
00245 echo h2info('Test object NON vide');
00246 $obj->set_parameter('j_id',3);
00247 $obj->load();
00248 var_dump($obj);
00249
00250 echo h2info('Update');
00251 $obj->set_parameter('j_qcode','NOUVEAU CODE');
00252 $obj->save();
00253 $obj->load();
00254 var_dump($obj);
00255
00256 echo h2info('Insert');
00257 $obj->set_parameter('j_id',0);
00258 $obj->save();
00259 $obj->load();
00260 var_dump($obj);
00261
00262 echo h2info('Delete');
00263 $obj->delete();
00264 echo (($obj->load()==0)?'Trouve':'non trouve');
00265 var_dump($obj);
00266 $cn->rollback();
00267
00268 }
00269
00270
00271
00272 static function sort_by_id($o1,$o2)
00273 {
00274 if ( $o1->ad_id > $o2->ad_id ) return 1;
00275 if ( $o1->ad_id == $o2->ad_id ) return 0;
00276 return -1;
00277 }
00278
00279
00280 }
00281
00282
00283
00284