noalyss  Version-6.9.1.8
 All Data Structures Namespaces Files Functions Variables Pages
class_fiche_attr.php
Go to the documentation of this file.
1 <?php
2 //This file is part of NOALYSS and is under GPL
3 //see licence.txt
4 /**
5  *@brief Manage the table attr_def
6  *
7  *
8  */
9 require_once NOALYSS_INCLUDE.'/lib/class_database.php';
10 require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
11 
13 {
14  /* example private $variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0); */
15 
16  protected $variable=array("id"=>"ad_id","desc"=>"ad_text","type"=>"ad_type","size"=>"ad_size","extra"=>"ad_extra");
17  function __construct ($p_cn,$p_id=0)
18  {
19  $this->cn=$p_cn;
20  if ( $p_id == 0 )
21  {
22  /* Initialize an empty object */
23  foreach ($this->variable as $key=>$value) $this->$value='';
24  }
25  else
26  {
27  /* load it */
28  $this->ad_id=$p_id;
29  $this->load();
30  }
31  }
32  public function get_parameter($p_string)
33  {
34  if ( array_key_exists($p_string,$this->variable) )
35  {
36  $idx=$this->variable[$p_string];
37  return $this->$idx;
38  }
39  else
40  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
41  }
42  public function set_parameter($p_string,$p_value)
43  {
44  if ( array_key_exists($p_string,$this->variable) )
45  {
46  $idx=$this->variable[$p_string];
47  $this->$idx=$p_value;
48  }
49  else
50  throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant');
51  }
52  public function get_info()
53  {
54  return var_export($this,true);
55  }
56  public function verify()
57  {
58  // Verify that the elt we want to add is correct
59  /* verify only the datatype */
60  if ( strlen(trim($this->ad_text))==0)
61  throw new Exception('La description ne peut pas être vide',1);
62  if ( strlen(trim($this->ad_type))==0)
63  throw new Exception('Le type ne peut pas être vide',1);
64  $this->ad_type=strtolower($this->ad_type);
65  if ( in_array($this->ad_type,array('date','text','numeric','zone','poste','card','select'))==false)
66  throw new Exception('Le type doit être text, numeric,poste, card, select ou date',1);
67  if ( trim($this->ad_size)=='' || isNumber($this->ad_size)==0||$this->ad_size>22)
68  {
69  switch ($this->ad_type)
70  {
71  case 'text':
72  $this->ad_size=22;
73  break;
74  case 'numeric':
75  $this->ad_size=9;
76  break;
77  case 'date':
78  $this->ad_size=8;
79  break;
80  case 'zone':
81  $this->ad_size=22;
82  break;
83 
84  default:
85  $this->ad_size=22;
86  }
87  }
88  if ( $this->ad_type == 'numeric' ) {
89  $this->ad_extra=(trim($this->ad_extra)=='')?'2':$this->ad_extra;
90  if (isNumber($this->ad_extra) == 0) throw new Exception ("La précision doit être un chiffre");
91 
92  }
93  if ( $this->ad_type == 'select')
94  {
95  if (trim($this->ad_extra)=="") throw new Exception ("La requête SQL est vide ");
96  if ( preg_match('/^\h*select/i',$this->ad_extra) == 0) throw new Exception ("La requête SQL doit commencer par SELECT ");
97  try{
98 
99  $this->cn->exec_sql($this->ad_extra);
100  }catch (Exception $e)
101  {
102  throw new Exception ("La requête SQL ".h($this->ad_extra)." est invalide ");
103  }
104  }
105  }
106  public function save()
107  {
108 
109  /* please adapt */
110  if ( $this->ad_id == 0 )
111  $this->insert();
112  else
113  $this->update();
114  }
115  /**
116  *@brief retrieve array of object thanks a condition
117  *@param $cond condition (where clause)
118  *@param $p_array array for the SQL stmt
119  *@see Database::get_array
120  *@return an empty array if nothing is found
121  */
122  public function seek($cond='',$p_array=null)
123  {
124  if ( $cond != '')
125  $sql="select * from attr_def where $cond order by ad_text";
126  else
127  $sql="select * from attr_def order by ad_text";
128 
129  $aobj=array();
130  $array= $this->cn->get_array($sql,$p_array);
131  // map each row in a object
132  $size=$this->cn->count();
133  if ( $size == 0 ) return $aobj;
134  for ($i=0;$i<$size;$i++)
135  {
136  $oobj=new Fiche_Attr ($this->cn);
137  foreach ($array[$i] as $idx=>$value)
138  {
139  $oobj->$idx=$value;
140  }
141  $aobj[]=clone $oobj;
142  }
143  return $aobj;
144  }
145  public function insert()
146  {
147  try{
148  $this->verify();
149  /* please adapt */
150  $sql="insert into attr_def(ad_text
151  ,ad_type,ad_size,ad_extra
152  ) values ($1
153  ,$2,$3,$4
154  ) returning ad_id";
155 
156  $this->ad_id=$this->cn->get_value(
157  $sql,
158  array( $this->ad_text,$this->ad_type,$this->ad_size,$this->ad_extra
159  )
160  );
161  } catch (Exception $e)
162  {
163  throw $e;
164  }
165 
166  }
167 
168  public function update()
169  {
170  try
171  {
172  $this->verify();
173  if ( $this->ad_id < 9000) return;
174  /* please adapt */
175  $sql=" update attr_def set ad_text = $1
176  ,ad_type = $2,ad_size=$4,ad_extra=$5
177  where ad_id= $3";
178  $res=$this->cn->exec_sql(
179  $sql,
180  array($this->ad_text
181  ,$this->ad_type
182  ,$this->ad_id,$this->ad_size,$this->ad_extra)
183  );
184  }catch (Exception $e)
185  {
186  throw $e;
187  }
188 
189 
190  }
191  /**
192  *@brief load a object
193  *@return 0 on success -1 the object is not found
194  */
195  public function load()
196  {
197 
198  $sql="select ad_text
199  ,ad_type,ad_size,ad_extra
200  from attr_def where ad_id=$1";
201  /* please adapt */
202  $res=$this->cn->get_array(
203  $sql,
204  array($this->ad_id)
205  );
206 
207  if ( count($res) == 0 )
208  {
209  /* Initialize an empty object */
210  foreach ($this->variable as $key=>$value) $this->$key='';
211 
212  return -1;
213  }
214  foreach ($res[0] as $idx=>$value)
215  {
216  $this->$idx=$value;
217  }
218  return 0;
219  }
220 
221  public function delete()
222  {
223  if ($this->ad_id < 9000) return;
224  $sql=$this->cn->exec_sql("delete from fiche_detail where ad_id=$1 ",
225  array($this->ad_id));
226 
227  $sql="delete from jnt_fic_attr where ad_id=$1";
228  $res=$this->cn->exec_sql($sql,array($this->ad_id));
229 
230  $sql="delete from attr_def where ad_id=$1";
231  $res=$this->cn->exec_sql($sql,array($this->ad_id));
232 
233  }
234  /**
235  * Unit test for the class
236  */
237  static function test_me()
238  {
239  $cn=new Database(25);
240  $cn->start();
241  echo h2info('Test object vide');
242  $obj=new Fiche_Attr($cn);
243  var_dump($obj);
244 
245  echo h2info('Test object NON vide');
246  $obj->set_parameter('j_id',3);
247  $obj->load();
248  var_dump($obj);
249 
250  echo h2info('Update');
251  $obj->set_parameter('j_qcode','NOUVEAU CODE');
252  $obj->save();
253  $obj->load();
254  var_dump($obj);
255 
256  echo h2info('Insert');
257  $obj->set_parameter('j_id',0);
258  $obj->save();
259  $obj->load();
260  var_dump($obj);
261 
262  echo h2info('Delete');
263  $obj->delete();
264  echo (($obj->load()==0)?'Trouve':'non trouve');
265  var_dump($obj);
266  $cn->rollback();
267 
268  }
269  /*!
270  *@brief used with a usort function, to sort an array of Attribut on the attribut_id (ad_id)
271  */
272  static function sort_by_id($o1,$o2)
273  {
274  if ( $o1->ad_id > $o2->ad_id ) return 1;
275  if ( $o1->ad_id == $o2->ad_id ) return 0;
276  return -1;
277  }
278 
279 
280 }
281 //Fiche_Attr::test_me();
282 
283 
284 
static test_me()
Unit test for the class.
set_parameter($p_string, $p_value)
__construct($p_cn, $p_id=0)
Manage the table attr_def.
h2info($p_string)
Definition: ac_common.php:63
seek($cond='', $p_array=null)
retrieve array of object thanks a condition
isNumber(&$p_int)
Definition: ac_common.php:202
$value
get_parameter($p_string)
$idx
$size
function clone(object)
static sort_by_id($o1, $o2)
used with a usort function, to sort an array of Attribut on the attribut_id (ad_id) ...
function trim(s)
remove trailing and heading space
Definition: scripts.js:95
$input_from cn
Definition: balance.inc.php:71
h($p_string)
to protect again bad characters which can lead to a cross scripting attack the string to be diplayed ...
Definition: ac_common.php:38
This class allow you to connect to the postgresql database, execute sql, retrieve data...
load()
load a object
$obj