dolibarr  9.0.0
cstate.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
24 // Put here all includes required by your class file
25 //require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26 //require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
27 //require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
28 
29 
33 class Cstate // extends CommonObject
34 {
38  public $db;
39 
43  public $error='';
44 
48  public $errors = array();
49 
50  //var $element='cstate'; //!< Id that identify managed objects
51  //var $table_element='cstate'; //!< Name of table without prefix where object is stored
52 
56  public $id;
57 
58  public $code_departement;
59 
65  public $nom='';
66 
70  public $name='';
71 
72  public $active;
73 
74 
75 
76 
82  function __construct($db)
83  {
84  $this->db = $db;
85  }
86 
87 
95  function create($user, $notrigger=0)
96  {
97  global $conf, $langs;
98  $error=0;
99 
100  // Clean parameters
101  if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
102  if (isset($this->nom)) $this->nom=trim($this->nom);
103  if (isset($this->active)) $this->active=trim($this->active);
104 
105  // Check parameters
106  // Put here code to add control on parameters values
107 
108  // Insert request
109  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_departements(";
110  $sql.= "rowid,";
111  $sql.= "code_departement,";
112  $sql.= "nom,";
113  $sql.= "active";
114  $sql.= ") VALUES (";
115  $sql.= " ".(! isset($this->rowid)?'NULL':"'".$this->db->escape($this->rowid)."'").",";
116  $sql.= " ".(! isset($this->code_departement)?'NULL':"'".$this->db->escape($this->code_departement)."'").",";
117  $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
118  $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->escape($this->active)."'")."";
119  $sql.= ")";
120 
121  $this->db->begin();
122 
123  dol_syslog(get_class($this)."::create", LOG_DEBUG);
124  $resql=$this->db->query($sql);
125  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
126 
127  if (! $error)
128  {
129  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_departements");
130 
131  if (! $notrigger)
132  {
133  // Uncomment this and change MYOBJECT to your own tag if you
134  // want this action call a trigger.
135 
137  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
138  //$interface=new Interfaces($this->db);
139  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
140  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
142  }
143  }
144 
145  // Commit or rollback
146  if ($error)
147  {
148  foreach($this->errors as $errmsg)
149  {
150  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
151  $this->error.=($this->error?', '.$errmsg:$errmsg);
152  }
153  $this->db->rollback();
154  return -1*$error;
155  }
156  else
157  {
158  $this->db->commit();
159  return $this->id;
160  }
161  }
162 
163 
171  function fetch($id,$code='')
172  {
173  global $langs;
174  $sql = "SELECT";
175  $sql.= " t.rowid,";
176  $sql.= " t.code_departement,";
177  $sql.= " t.nom,";
178  $sql.= " t.active";
179  $sql.= " FROM ".MAIN_DB_PREFIX."c_departements as t";
180  if ($id) $sql.= " WHERE t.rowid = ".$id;
181  elseif ($code) $sql.= " WHERE t.code_departement = '".$this->db->escape($code)."'";
182 
183  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
184  $resql=$this->db->query($sql);
185  if ($resql)
186  {
187  if ($this->db->num_rows($resql))
188  {
189  $obj = $this->db->fetch_object($resql);
190 
191  $this->id = $obj->rowid;
192  $this->code_departement = $obj->code_departement;
193  $this->nom = $obj->nom;
194  $this->active = $obj->active;
195  }
196  $this->db->free($resql);
197 
198  return 1;
199  }
200  else
201  {
202  $this->error="Error ".$this->db->lasterror();
203  return -1;
204  }
205  }
206 
207 
215  function update($user=null, $notrigger=0)
216  {
217  global $conf, $langs;
218  $error=0;
219 
220  // Clean parameters
221  if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
222  if (isset($this->nom)) $this->nom=trim($this->nom);
223  if (isset($this->active)) $this->active=trim($this->active);
224 
225 
226  // Check parameters
227  // Put here code to add control on parameters values
228 
229  // Update request
230  $sql = "UPDATE ".MAIN_DB_PREFIX."c_departements SET";
231  $sql.= " code_departement=".(isset($this->code_departement)?"'".$this->db->escape($this->code_departement)."'":"null").",";
232  $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
233  $sql.= " active=".(isset($this->active)?$this->active:"null")."";
234  $sql.= " WHERE rowid=".$this->id;
235 
236  $this->db->begin();
237 
238  dol_syslog(get_class($this)."::update", LOG_DEBUG);
239  $resql = $this->db->query($sql);
240  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
241 
242  if (! $error)
243  {
244  if (! $notrigger)
245  {
246  // Uncomment this and change MYOBJECT to your own tag if you
247  // want this action call a trigger.
248 
250  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
251  //$interface=new Interfaces($this->db);
252  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
253  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
255  }
256  }
257 
258  // Commit or rollback
259  if ($error)
260  {
261  foreach($this->errors as $errmsg)
262  {
263  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
264  $this->error.=($this->error?', '.$errmsg:$errmsg);
265  }
266  $this->db->rollback();
267  return -1*$error;
268  }
269  else
270  {
271  $this->db->commit();
272  return 1;
273  }
274  }
275 
276 
284  function delete($user, $notrigger=0)
285  {
286  global $conf, $langs;
287  $error=0;
288 
289  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_departements";
290  $sql.= " WHERE rowid=".$this->id;
291 
292  $this->db->begin();
293 
294  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
295  $resql = $this->db->query($sql);
296  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
297 
298  if (! $error)
299  {
300  if (! $notrigger)
301  {
302  // Uncomment this and change MYOBJECT to your own tag if you
303  // want this action call a trigger.
304 
306  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
307  //$interface=new Interfaces($this->db);
308  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
309  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
311  }
312  }
313 
314  // Commit or rollback
315  if ($error)
316  {
317  foreach($this->errors as $errmsg)
318  {
319  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
320  $this->error.=($this->error?', '.$errmsg:$errmsg);
321  }
322  $this->db->rollback();
323  return -1*$error;
324  }
325  else
326  {
327  $this->db->commit();
328  return 1;
329  }
330  }
331 }
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
update($user=null, $notrigger=0)
Update object into database.
Class to manage dictionary States (used by imports)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
create($user, $notrigger=0)
Create object into database.
if(! empty($arrayfields['s.nom']['checked'])) print_liste_field_titre($arrayfields['s.nom']['label'] s nom
Definition: list.php:573
fetch($id, $code='')
Load object in memory from database.
__construct($db)
Constructor.