bc_id=$id; $this->db=$p_cn; } /*!\brief insert a record into the table bud_card *\return return the string Sauve or throw an exception */ function add () { if ( isNumber($this->bc_price_unit) == false ) $this->bc_price_unit=0; $array=array( $this->bc_code, $this->bc_description, $this->bc_price_unit, $this->bc_unit, $this->bh_id ); $sql="insert into bud_card (bc_code,bc_description,bc_price_unit,bc_unit,bh_id) ". " values (substr($1,1,10),$2,$3,substr($4,1,20),$5) returning bc_id "; try { $a=ExecSqlParam($this->db,$sql,$array); $x=pg_fetch_array($a,0); $this->bc_id=$x['bc_id']; } catch (Exception $e) { if ( DEBUG == 'true' ) print_r($e); return 'Impossible de sauver'; } return 'Sauve'; } /*!\brief update the table bud_card *\return return the string Sauve or throw an exception */ function update() { if ( $this->bc_id == 0) return; if ( isNumber($this->bc_price_unit) == false ) $this->bc_price_unit=0; $sql="update bud_card set bc_code=substr($1,1,10),". "bc_description=$2,". "bc_price_unit=$3, ". "bc_unit=substr($4,1,20) ". " where bc_id=$5"; $array=array( $this->bc_code, $this->bc_description, $this->bc_price_unit, $this->bc_unit, $this->bc_id ); try { ExecSqlParam($this->db,$sql,$array); }catch (Exception $e) { if ( DEBUG == 'true' ) print_r($e); return 'Impossible de sauver'; } return 'Sauve'; } /*! *\brief convert an array to an object, if a idx is not set then the * corresponding property will be null * \param $p_array array to convert */ function get_from_array($p_array) { if ( empty($p_array) ) return; foreach (array ('bh_id','bc_id','bc_code','bc_description','bc_price_unit','bc_unit') as $key ){ $this->$key=(isset($p_array[$key]))?$p_array[$key]:null; } /* if ( $this->bc_id == null ) */ /* throw new Exception(__FILE__.":".__LINE__." bc ne peut pas etre nul"); */ } /*!\brief delete from bud_card the bc_id must be set * *\return Efface */ function delete() { ExecSql($this->db,"delete from bud_card where bc_id=".$this->bc_id); return 'Efface'; } /*!\brief load a bud_card from the database * * */ function load() { if ( $this->bc_id == 0 ) return ; $sql="select bc_id,bc_code,bc_description, bc_price_unit,bc_unit,bh_id ". " from bud_card ". " where ". " bc_id =".$this->bc_id; $res=ExecSql($this->db,$sql); if ( pg_NumRows($res) == 0 ) return null; $a=pg_fetch_array($res,0); $this->get_from_array($a); } /*!\brief return a list of all the record of all the budcard *\param $p_cn connection *\param $p_bh_id the hypothese id *\return an array of Bud_Card objects */ static function get_list($p_cn,$p_bh_id) { $sql="select * from bud_card where bh_id = $p_bh_id"; $r=ExecSql($p_cn,$sql); $get=pg_fetch_all($r); if (empty ($get)) return array(); $result=array(); foreach ($get as $row ) { $obj=new Bud_Card($p_cn); $obj->get_from_array($row); $result[]=clone $obj; } return $result; } /*!\brief return a string with the form * *\return string */ function form() { $wCode=new widget("text","Code","bc_code",$this->bc_code); $wDescription=new widget("text","Description","bc_description",$this->bc_description); $wPriceUnit=new widget("text","Prix/unit","bc_price_unit",$this->bc_price_unit); $wUnit=new widget("text","Unit","bc_unit",$this->bc_unit); $wCode->table=1; $wDescription->table=1; $wPriceUnit->table=1; $wUnit->table=1; $r=""; $r.=""; $r.=widget::hidden('bc_id',$this->bc_id); $r.=widget::hidden('bh_id',$this->bh_id); $r.=""; $r.=$wCode->IOValue('bc_code',$this->bc_code); $r.=""; $r.=""; $r.=$wDescription->IOValue(); $r.=""; $r.=""; $r.=$wPriceUnit->IOValue(); $r.=""; $r.=""; $r.=$wUnit->IOValue(); $r.=""; $r.="
"; return $r; } static function test_me() { $cn=DbConnect(dossier::id()); $a=new Bud_Card($cn); echo "

Ajout d'un bud_card

"; $a->bc_code='test une fois'; $a->bc_description="Juste une description"; $a->bc_unit="Euro"; $a->bc_price_unit=0.55; $a->add(); print_r($a); echo "

Mise a jour d'un bud_card

"; $a->bc_code=' --- test une fois'; $a->bc_description="--- -Juste une description"; $a->bc_unit="M2"; $a->bc_price_unit=0.50; print_r($a); echo "

Load

"; $b=new Bud_Card($cn); $b->bc_id=$a->bc_id; $b->load(); print_r($b); echo "

Effacement

"; $a->delete(); } }