noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_profile_menu_sql.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 /**
00021  *@file
00022  *@brief Manage the table public.profile_menu
00023  *
00024  *
00025 Example
00026 @code
00027 
00028 @endcode
00029  */
00030 require_once('class_database.php');
00031 require_once('ac_common.php');
00032 
00033 
00034 /**
00035  *@brief Manage the table public.profile_menu
00036 */
00037 class Profile_Menu_sql  
00038 {
00039   /* example private $variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0); */
00040   
00041   protected $variable=array("pm_id"=>"pm_id","me_code"=>"me_code"
00042 ,"me_code_dep"=>"me_code_dep"
00043 ,"p_id"=>"p_id"
00044 ,"p_order"=>"p_order"
00045 ,"p_type_display"=>"p_type_display"
00046 ,"pm_default"=>"pm_default"
00047 );
00048   function __construct ( & $p_cn,$p_id=-1) {
00049         $this->cn=$p_cn;
00050         $this->pm_id=$p_id;
00051         
00052         if ( $p_id == -1 ) {
00053         /* Initialize an empty object */
00054             foreach ($this->variable as $key=>$value) $this->$value=null;
00055             $this->pm_id=$p_id;
00056         } else {
00057          /* load it */
00058 
00059          $this->load();
00060       }
00061   }
00062   public function get_parameter($p_string) {
00063     if ( array_key_exists($p_string,$this->variable) ) {
00064       $idx=$this->variable[$p_string];
00065       return $this->$idx;
00066     }
00067     else 
00068       throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00069   }
00070   public function set_parameter($p_string,$p_value) {
00071     if ( array_key_exists($p_string,$this->variable) ) {
00072       $idx=$this->variable[$p_string];
00073       $this->$idx=$p_value;
00074     }
00075     else 
00076       throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
00077   }
00078   public function get_info() {    return var_export($this,true);  }
00079   public function verify() {
00080     // Verify that the elt we want to add is correct
00081     /* verify only the datatype */
00082      if ( trim($this->me_code) == '') $this->me_code=null;
00083  if ( trim($this->me_code_dep) == '') $this->me_code_dep=null;
00084  if ( trim($this->p_id) == '') $this->p_id=null;
00085 if ( $this->p_id!== null && settype($this->p_id,'float') == false )
00086              throw new Exception('DATATYPE p_id $this->p_id non numerique');
00087  if ( trim($this->p_order) == '') $this->p_order=null;
00088 if ( $this->p_order!== null && settype($this->p_order,'float') == false )
00089              throw new Exception('DATATYPE p_order $this->p_order non numerique');
00090  if ( trim($this->p_type_display) == '') $this->p_type_display=null;
00091  if ( trim($this->pm_default) == '') $this->pm_default=null;
00092 if ( $this->pm_default!== null && settype($this->pm_default,'float') == false )
00093              throw new Exception('DATATYPE pm_default $this->pm_default non numerique');
00094 
00095     
00096   }
00097   public function save() {
00098   /* please adapt */
00099     if (  $this->pm_id == -1 ) 
00100       $this->insert();
00101     else
00102       $this->update();
00103   }
00104   /**
00105    *@brief retrieve array of object thanks a condition
00106    *@param $cond condition (where clause) (optional by default all the rows are fetched)
00107    * you can use this parameter for the order or subselect
00108    *@param $p_array array for the SQL stmt
00109    *@see Database::exec_sql get_object  Database::num_row
00110    *@return the return value of exec_sql
00111    */
00112    public function seek($cond='',$p_array=null) 
00113    {
00114      $sql="select * from public.profile_menu  $cond";
00115      $aobj=array();
00116      $ret= $this->cn->exec_sql($sql,$p_array);
00117      return $ret;
00118    }
00119    /**
00120     *get_seek return the next object, the return of the query must have all the column
00121     * of the object
00122     *@param $p_ret is the return value of an exec_sql
00123     *@param $idx is the index
00124     *@see seek
00125     *@return object 
00126     */
00127    public function get_object($p_ret,$idx)
00128     {
00129      // map each row in a object
00130      $oobj=new Profile_Menu_sql ($this->cn);
00131      $array=Database::fetch_array($p_ret,$idx);
00132      foreach ($array as $idx=>$value) { $oobj->$idx=$value; }
00133      return $oobj;
00134    }
00135   public function insert() {
00136     if ( $this->verify() != 0 ) return;
00137       if( $this->pm_id==-1 ){
00138     /*  please adapt */
00139     $sql="insert into public.profile_menu(me_code
00140 ,me_code_dep
00141 ,p_id
00142 ,p_order
00143 ,p_type_display
00144 ,pm_default
00145 ) values ($1
00146 ,$2
00147 ,$3
00148 ,$4
00149 ,$5
00150 ,$6
00151 ) returning pm_id";
00152     
00153     $this->pm_id=$this->cn->get_value(
00154                  $sql,
00155                  array( $this->me_code
00156 ,$this->me_code_dep
00157 ,$this->p_id
00158 ,$this->p_order
00159 ,$this->p_type_display
00160 ,$this->pm_default
00161 )
00162                  );
00163           } else {
00164               $sql="insert into public.profile_menu(me_code
00165 ,me_code_dep
00166 ,p_id
00167 ,p_order
00168 ,p_type_display
00169 ,pm_default
00170 ,pm_id) values ($1
00171 ,$2
00172 ,$3
00173 ,$4
00174 ,$5
00175 ,$6
00176 ,$7
00177 ) returning pm_id";
00178     
00179     $this->pm_id=$this->cn->get_value(
00180                  $sql,
00181                  array( $this->me_code
00182 ,$this->me_code_dep
00183 ,$this->p_id
00184 ,$this->p_order
00185 ,$this->p_type_display
00186 ,$this->pm_default
00187 ,$this->pm_id)
00188                  );
00189 
00190           }
00191    
00192   }
00193 
00194   public function update() {
00195     if ( $this->verify() != 0 ) return;
00196     /*   please adapt */
00197     $sql=" update public.profile_menu set me_code = $1
00198 ,me_code_dep = $2
00199 ,p_id = $3
00200 ,p_order = $4
00201 ,p_type_display = $5
00202 ,pm_default = $6
00203  where pm_id= $7";
00204     $res=$this->cn->exec_sql(
00205                  $sql,
00206                  array($this->me_code
00207 ,$this->me_code_dep
00208 ,$this->p_id
00209 ,$this->p_order
00210 ,$this->p_type_display
00211 ,$this->pm_default
00212 ,$this->pm_id)
00213                  );
00214                  
00215   }
00216 /**
00217  *@brief load a object
00218  *@return 0 on success -1 the object is not found
00219  */
00220   public function load() {
00221 
00222    $sql="select me_code
00223 ,me_code_dep
00224 ,p_id
00225 ,p_order
00226 ,p_type_display
00227 ,pm_default
00228  from public.profile_menu where pm_id=$1"; 
00229     /* please adapt */
00230     $res=$this->cn->get_array(
00231                  $sql,
00232                  array($this->pm_id)
00233                  );
00234                  
00235     if ( count($res) == 0 ) {
00236           /* Initialize an empty object */
00237           foreach ($this->variable as $key=>$value) $this->$key='';
00238 
00239           return -1;
00240           }
00241     foreach ($res[0] as $idx=>$value) { $this->$idx=$value; }
00242     return 0;
00243   }
00244 
00245   public function delete() {
00246     $sql="delete from public.profile_menu where pm_id=$1"; 
00247     $res=$this->cn->exec_sql($sql,array($this->pm_id));
00248   }
00249   /**
00250    * Unit test for the class
00251    */   
00252   static function test_me() {
00253       $cn=new Database(25);
00254 $cn->start();
00255     echo h2info('Test object vide');
00256     $obj=new Profile_Menu_sql($cn);
00257     var_dump($obj);
00258 
00259     echo h2info('Test object NON vide');
00260     $obj->set_parameter('j_id',3);
00261     $obj->load();
00262     var_dump($obj);
00263 
00264     echo h2info('Update');
00265     $obj->set_parameter('j_qcode','NOUVEAU CODE');
00266     $obj->save();
00267     $obj->load();
00268     var_dump($obj);
00269 
00270     echo h2info('Insert');
00271     $obj->set_parameter('j_id',0);
00272     $obj->save();
00273     $obj->load();
00274     var_dump($obj);
00275 
00276     echo h2info('Delete');
00277     $obj->delete();
00278     echo (($obj->load()==0)?'Trouve':'non trouve');
00279     var_dump($obj);
00280 $cn->rollback();
00281 
00282   }
00283   
00284 }
00285 // Profile_Menu_sql::test_me();
00286 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations