dolibarr  7.0.0-beta
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 {
35  var $db;
36  var $error;
37  var $errors=array();
38  //var $element='cstate'; //!< Id that identify managed objects
39  //var $table_element='cstate'; //!< Name of table without prefix where object is stored
40 
41  var $id;
42  var $code_departement;
43  var $nom;
44  var $active;
45 
46 
47 
48 
54  function __construct($db)
55  {
56  $this->db = $db;
57  return 1;
58  }
59 
60 
68  function create($user, $notrigger=0)
69  {
70  global $conf, $langs;
71  $error=0;
72 
73  // Clean parameters
74  if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
75  if (isset($this->nom)) $this->nom=trim($this->nom);
76  if (isset($this->active)) $this->active=trim($this->active);
77 
78  // Check parameters
79  // Put here code to add control on parameters values
80 
81  // Insert request
82  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_departements(";
83  $sql.= "rowid,";
84  $sql.= "code_departement,";
85  $sql.= "nom,";
86  $sql.= "active";
87  $sql.= ") VALUES (";
88  $sql.= " ".(! isset($this->rowid)?'NULL':"'".$this->db->escape($this->rowid)."'").",";
89  $sql.= " ".(! isset($this->code_departement)?'NULL':"'".$this->db->escape($this->code_departement)."'").",";
90  $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
91  $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->escape($this->active)."'")."";
92  $sql.= ")";
93 
94  $this->db->begin();
95 
96  dol_syslog(get_class($this)."::create", LOG_DEBUG);
97  $resql=$this->db->query($sql);
98  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
99 
100  if (! $error)
101  {
102  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_departements");
103 
104  if (! $notrigger)
105  {
106  // Uncomment this and change MYOBJECT to your own tag if you
107  // want this action call a trigger.
108 
110  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
111  //$interface=new Interfaces($this->db);
112  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
113  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
115  }
116  }
117 
118  // Commit or rollback
119  if ($error)
120  {
121  foreach($this->errors as $errmsg)
122  {
123  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
124  $this->error.=($this->error?', '.$errmsg:$errmsg);
125  }
126  $this->db->rollback();
127  return -1*$error;
128  }
129  else
130  {
131  $this->db->commit();
132  return $this->id;
133  }
134  }
135 
136 
144  function fetch($id,$code='')
145  {
146  global $langs;
147  $sql = "SELECT";
148  $sql.= " t.rowid,";
149  $sql.= " t.code_departement,";
150  $sql.= " t.nom,";
151  $sql.= " t.active";
152  $sql.= " FROM ".MAIN_DB_PREFIX."c_departements as t";
153  if ($id) $sql.= " WHERE t.rowid = ".$id;
154  elseif ($code) $sql.= " WHERE t.code_departement = '".$this->db->escape($code)."'";
155 
156  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
157  $resql=$this->db->query($sql);
158  if ($resql)
159  {
160  if ($this->db->num_rows($resql))
161  {
162  $obj = $this->db->fetch_object($resql);
163 
164  $this->id = $obj->rowid;
165  $this->code_departement = $obj->code_departement;
166  $this->nom = $obj->nom;
167  $this->active = $obj->active;
168  }
169  $this->db->free($resql);
170 
171  return 1;
172  }
173  else
174  {
175  $this->error="Error ".$this->db->lasterror();
176  return -1;
177  }
178  }
179 
180 
188  function update($user=null, $notrigger=0)
189  {
190  global $conf, $langs;
191  $error=0;
192 
193  // Clean parameters
194  if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
195  if (isset($this->nom)) $this->nom=trim($this->nom);
196  if (isset($this->active)) $this->active=trim($this->active);
197 
198 
199  // Check parameters
200  // Put here code to add control on parameters values
201 
202  // Update request
203  $sql = "UPDATE ".MAIN_DB_PREFIX."c_departements SET";
204  $sql.= " code_departement=".(isset($this->code_departement)?"'".$this->db->escape($this->code_departement)."'":"null").",";
205  $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
206  $sql.= " active=".(isset($this->active)?$this->active:"null")."";
207  $sql.= " WHERE rowid=".$this->id;
208 
209  $this->db->begin();
210 
211  dol_syslog(get_class($this)."::update", LOG_DEBUG);
212  $resql = $this->db->query($sql);
213  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
214 
215  if (! $error)
216  {
217  if (! $notrigger)
218  {
219  // Uncomment this and change MYOBJECT to your own tag if you
220  // want this action call a trigger.
221 
223  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
224  //$interface=new Interfaces($this->db);
225  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
226  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
228  }
229  }
230 
231  // Commit or rollback
232  if ($error)
233  {
234  foreach($this->errors as $errmsg)
235  {
236  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
237  $this->error.=($this->error?', '.$errmsg:$errmsg);
238  }
239  $this->db->rollback();
240  return -1*$error;
241  }
242  else
243  {
244  $this->db->commit();
245  return 1;
246  }
247  }
248 
249 
257  function delete($user, $notrigger=0)
258  {
259  global $conf, $langs;
260  $error=0;
261 
262  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_departements";
263  $sql.= " WHERE rowid=".$this->id;
264 
265  $this->db->begin();
266 
267  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
268  $resql = $this->db->query($sql);
269  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
270 
271  if (! $error)
272  {
273  if (! $notrigger)
274  {
275  // Uncomment this and change MYOBJECT to your own tag if you
276  // want this action call a trigger.
277 
279  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
280  //$interface=new Interfaces($this->db);
281  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
282  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
284  }
285  }
286 
287  // Commit or rollback
288  if ($error)
289  {
290  foreach($this->errors as $errmsg)
291  {
292  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
293  $this->error.=($this->error?', '.$errmsg:$errmsg);
294  }
295  $this->db->rollback();
296  return -1*$error;
297  }
298  else
299  {
300  $this->db->commit();
301  return 1;
302  }
303  }
304 
305 }
update($user=null, $notrigger=0)
Update object into database.
Class to manage dictionary States (used by imports)
$errors
To return several error codes (or messages)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
create($user, $notrigger=0)
Create object into database.
fetch($id, $code='')
Load object in memory from database.
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:1013
$db
To store db handler.
$error
To return error code (or message)
__construct($db)
Constructor.