dolibarr  9.0.0
ctypent.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2011 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 
28 class Ctypent // extends CommonObject
29 {
33  public $db;
34 
38  public $error='';
39 
43  public $errors = array();
44 
45  //var $element='ctypent'; //!< Id that identify managed objects
46  //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
47 
51  public $id;
52 
53  public $code;
54  public $libelle;
55  public $active;
56  public $module;
57 
58 
59 
60 
66  function __construct($db)
67  {
68  $this->db = $db;
69  }
70 
71 
79  function create($user, $notrigger=0)
80  {
81  global $conf, $langs;
82  $error=0;
83 
84  // Clean parameters
85 
86  if (isset($this->id)) $this->id=trim($this->id);
87  if (isset($this->code)) $this->code=trim($this->code);
88  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
89  if (isset($this->active)) $this->active=trim($this->active);
90  if (isset($this->module)) $this->module=trim($this->module);
91 
92 
93 
94  // Check parameters
95  // Put here code to add control on parameters values
96 
97  // Insert request
98  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_typent(";
99 
100  $sql.= "id,";
101  $sql.= "code,";
102  $sql.= "libelle,";
103  $sql.= "active,";
104  $sql.= "module";
105 
106 
107  $sql.= ") VALUES (";
108 
109  $sql.= " ".(! isset($this->id)?'NULL':"'".$this->db->escape($this->id)."'").",";
110  $sql.= " ".(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").",";
111  $sql.= " ".(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").",";
112  $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->active($this->active)."'").",";
113  $sql.= " ".(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'")."";
114 
115 
116  $sql.= ")";
117 
118  $this->db->begin();
119 
120  dol_syslog(get_class($this)."::create", LOG_DEBUG);
121  $resql=$this->db->query($sql);
122  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
123 
124  if (! $error)
125  {
126  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_typent");
127 
128  if (! $notrigger)
129  {
130  // Uncomment this and change MYOBJECT to your own tag if you
131  // want this action call a trigger.
132 
134  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
135  //$interface=new Interfaces($this->db);
136  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
137  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
139  }
140  }
141 
142  // Commit or rollback
143  if ($error)
144  {
145  foreach($this->errors as $errmsg)
146  {
147  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
148  $this->error.=($this->error?', '.$errmsg:$errmsg);
149  }
150  $this->db->rollback();
151  return -1*$error;
152  }
153  else
154  {
155  $this->db->commit();
156  return $this->id;
157  }
158  }
159 
160 
169  function fetch($id,$code='',$label='')
170  {
171  global $langs;
172  $sql = "SELECT";
173  $sql.= " t.id,";
174  $sql.= " t.code,";
175  $sql.= " t.libelle as label,";
176  $sql.= " t.fk_country as country_id,";
177  $sql.= " t.active,";
178  $sql.= " t.module";
179  $sql.= " FROM ".MAIN_DB_PREFIX."c_typent as t";
180  if ($id) $sql.= " WHERE t.id = ".$id;
181  elseif ($code) $sql.= " WHERE t.code = '".$this->db->escape($code)."'";
182  elseif ($label) $sql.= " WHERE t.libelle = '".$this->db->escape($label)."'";
183 
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->id;
192  $this->code = $obj->code;
193  $this->libelle = $obj->label;
194  $this->country_id = $obj->country_id;
195  $this->active = $obj->active;
196  $this->module = $obj->module;
197  }
198  $this->db->free($resql);
199 
200  return 1;
201  }
202  else
203  {
204  $this->error="Error ".$this->db->lasterror();
205  return -1;
206  }
207  }
208 
209 
217  function update($user=null, $notrigger=0)
218  {
219  global $conf, $langs;
220  $error=0;
221 
222  // Clean parameters
223  if (isset($this->code)) $this->code=trim($this->code);
224  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
225  if (isset($this->active)) $this->active=trim($this->active);
226  if (isset($this->module)) $this->module=trim($this->module);
227 
228 
229  // Check parameters
230  // Put here code to add control on parameters values
231 
232  // Update request
233  $sql = "UPDATE ".MAIN_DB_PREFIX."c_typent SET";
234  $sql.= " code=".(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").",";
235  $sql.= " libelle=".(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").",";
236  $sql.= " active=".(isset($this->active)?$this->active:"null").",";
237  $sql.= " module=".(isset($this->module)?"'".$this->db->escape($this->module)."'":"null")."";
238  $sql.= " WHERE id=".$this->id;
239 
240  $this->db->begin();
241 
242  dol_syslog(get_class($this)."::update", LOG_DEBUG);
243  $resql = $this->db->query($sql);
244  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
245 
246  if (! $error)
247  {
248  if (! $notrigger)
249  {
250  // Uncomment this and change MYOBJECT to your own tag if you
251  // want this action call a trigger.
252 
254  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
255  //$interface=new Interfaces($this->db);
256  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
257  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
259  }
260  }
261 
262  // Commit or rollback
263  if ($error)
264  {
265  foreach($this->errors as $errmsg)
266  {
267  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
268  $this->error.=($this->error?', '.$errmsg:$errmsg);
269  }
270  $this->db->rollback();
271  return -1*$error;
272  }
273  else
274  {
275  $this->db->commit();
276  return 1;
277  }
278  }
279 
280 
288  function delete($user, $notrigger=0)
289  {
290  global $conf, $langs;
291  $error=0;
292 
293  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_typent";
294  $sql.= " WHERE id=".$this->id;
295 
296  $this->db->begin();
297 
298  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
299  $resql = $this->db->query($sql);
300  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
301 
302  if (! $error)
303  {
304  if (! $notrigger)
305  {
306  // Uncomment this and change MYOBJECT to your own tag if you
307  // want this action call a trigger.
308 
310  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
311  //$interface=new Interfaces($this->db);
312  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
313  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
315  }
316  }
317 
318  // Commit or rollback
319  if ($error)
320  {
321  foreach($this->errors as $errmsg)
322  {
323  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
324  $this->error.=($this->error?', '.$errmsg:$errmsg);
325  }
326  $this->db->rollback();
327  return -1*$error;
328  }
329  else
330  {
331  $this->db->commit();
332  return 1;
333  }
334  }
335 }
fetch($id, $code='', $label='')
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:1053
update($user=null, $notrigger=0)
Update object into database.
__construct($db)
Constructor.
Class of dictionary type of thirdparty (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.