00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Forecast
00029 {
00030 private static $variable=array ("id"=>"f_id","name"=>"f_name","start_date"=>"f_start_date","end_date"=>"f_end_date");
00031 private $cn;
00032
00033
00034
00035
00036 function __construct ($p_init,$p_id=0)
00037 {
00038 $this->cn=$p_init;
00039 $this->f_id=$p_id;
00040 }
00041 public function get_parameter($p_string)
00042 {
00043 if ( array_key_exists($p_string,self::$variable) )
00044 {
00045 $idx=self::$variable[$p_string];
00046 return $this->$idx;
00047 }
00048 else
00049 exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00050 }
00051 public function set_parameter($p_string,$p_value)
00052 {
00053 if ( array_key_exists($p_string,self::$variable) )
00054 {
00055 $idx=self::$variable[$p_string];
00056 $this->$idx=$p_value;
00057 }
00058 else
00059 exit (__FILE__.":".__LINE__."[$p_string]".'Erreur attribut inexistant');
00060 }
00061 public function get_info()
00062 {
00063 return var_export(self::$variable,true);
00064 }
00065
00066 public function verify()
00067 {
00068
00069
00070 if ( strlen(trim($this->f_name))==0) throw new Exception(_('Le nom ne peut pas ĂȘtre vide'));
00071
00072 return 0;
00073 }
00074 public function save()
00075 {
00076
00077 if ( $this->get_parameter("id") == 0 )
00078 $this->insert();
00079 else
00080 $this->update();
00081 }
00082
00083 public function insert()
00084 {
00085 if ( $this->verify() != 0 ) return;
00086 $sql="insert into forecast (f_name,f_start_date,f_end_date) ".
00087 " values ($1,$2,$3) returning f_id";
00088 $res=$this->cn->exec_sql(
00089 $sql,
00090 array($this->f_name,$this->f_start_date,$this->f_end_date)
00091 );
00092 $this->f_id=Database::fetch_result($res,0,0);
00093 }
00094
00095
00096
00097
00098 public function update()
00099 {
00100 if ( $this->verify() != 0 ) return;
00101
00102 $sql="update forecast set f_name=$1,f_start_date=$2,f_end_date=$3 ".
00103 " where f_id = $4";
00104 $res=$this->cn->exec_sql(
00105 $sql,
00106 array($this->f_name,$this->f_start_date,$this->f_end_date, $this->f_id)
00107 );
00108
00109 }
00110
00111
00112
00113
00114
00115 public static function load_all($p_cn)
00116 {
00117 $sql="select f_id, f_name,f_start_date,f_end_date from forecast";
00118 $ret=$p_cn->get_array($sql);
00119 return $ret;
00120 }
00121 public function load()
00122 {
00123 $sql="select f_name,f_start_date ,f_end_date from forecast where f_id=$1";
00124 $res=$this->cn->exec_sql(
00125 $sql,
00126 array($this->f_id)
00127 );
00128 if ( Database::num_row($res) == 0 ) return -1;
00129 $row=Database::fetch_array($res,0);
00130 foreach ($row as $idx=>$value)
00131 {
00132 $this->$idx=$value;
00133 }
00134
00135 }
00136 public function delete()
00137 {
00138 $sql="delete from forecast where f_id=$1";
00139 $res=$this->cn->exec_sql($sql,array($this->f_id));
00140 }
00141 public function object_clone()
00142 {
00143 $this->load();
00144
00145 $sql="insert into forecast(f_name,f_start_date,f_end_date) select 'clone '||f_name,f_start_date,f_end_date from forecast where f_id=$1 returning f_id";
00146 $new=$this->cn->get_value($sql,array($this->f_id));
00147
00148
00149 $sql="insert into forecast_cat(fc_desc,f_id,fc_order) select fc_desc,$1,fc_order from forecast_cat where f_id=$2 returning fc_id" ;
00150 $array=$this->cn->get_array($sql,array($new,$this->f_id));
00151
00152 $old=$this->cn->get_array("select fc_id from forecast_cat where f_id=$1",array($this->f_id));
00153
00154 for ($i=0;$i<count($array);$i++)
00155 {
00156 $this->cn->exec_sql("insert into forecast_item (fi_text,fi_account,fi_card,fi_order,fc_id,fi_amount,fi_debit,fi_pid) ".
00157 " select fi_text,fi_account,fi_card,fi_order,$1,fi_amount,fi_debit,fi_pid ".
00158 " from forecast_item where fc_id=$2",array($array[$i]['fc_id'],$old[$i]['fc_id']));
00159 }
00160 }
00161
00162
00163
00164 static function test_me()
00165 {}
00166
00167 }
00168 ?>