dolibarr  7.0.0-beta
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 {
30  var $db;
31  var $error;
32  var $errors=array();
33  //var $element='ctypent'; //!< Id that identify managed objects
34  //var $table_element='ctypent'; //!< Name of table without prefix where object is stored
35 
36  var $id;
37  var $code;
38  var $libelle;
39  var $active;
40  var $module;
41 
42 
43 
44 
50  function __construct($db)
51  {
52  $this->db = $db;
53  return 1;
54  }
55 
56 
64  function create($user, $notrigger=0)
65  {
66  global $conf, $langs;
67  $error=0;
68 
69  // Clean parameters
70 
71  if (isset($this->id)) $this->id=trim($this->id);
72  if (isset($this->code)) $this->code=trim($this->code);
73  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
74  if (isset($this->active)) $this->active=trim($this->active);
75  if (isset($this->module)) $this->module=trim($this->module);
76 
77 
78 
79  // Check parameters
80  // Put here code to add control on parameters values
81 
82  // Insert request
83  $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_typent(";
84 
85  $sql.= "id,";
86  $sql.= "code,";
87  $sql.= "libelle,";
88  $sql.= "active,";
89  $sql.= "module";
90 
91 
92  $sql.= ") VALUES (";
93 
94  $sql.= " ".(! isset($this->id)?'NULL':"'".$this->db->escape($this->id)."'").",";
95  $sql.= " ".(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").",";
96  $sql.= " ".(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").",";
97  $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->active($this->active)."'").",";
98  $sql.= " ".(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'")."";
99 
100 
101  $sql.= ")";
102 
103  $this->db->begin();
104 
105  dol_syslog(get_class($this)."::create", LOG_DEBUG);
106  $resql=$this->db->query($sql);
107  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
108 
109  if (! $error)
110  {
111  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_typent");
112 
113  if (! $notrigger)
114  {
115  // Uncomment this and change MYOBJECT to your own tag if you
116  // want this action call a trigger.
117 
119  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
120  //$interface=new Interfaces($this->db);
121  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
122  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
124  }
125  }
126 
127  // Commit or rollback
128  if ($error)
129  {
130  foreach($this->errors as $errmsg)
131  {
132  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
133  $this->error.=($this->error?', '.$errmsg:$errmsg);
134  }
135  $this->db->rollback();
136  return -1*$error;
137  }
138  else
139  {
140  $this->db->commit();
141  return $this->id;
142  }
143  }
144 
145 
154  function fetch($id,$code='',$label='')
155  {
156  global $langs;
157  $sql = "SELECT";
158  $sql.= " t.id,";
159  $sql.= " t.code,";
160  $sql.= " t.libelle as label,";
161  $sql.= " t.fk_country as country_id,";
162  $sql.= " t.active,";
163  $sql.= " t.module";
164  $sql.= " FROM ".MAIN_DB_PREFIX."c_typent as t";
165  if ($id) $sql.= " WHERE t.id = ".$id;
166  elseif ($code) $sql.= " WHERE t.code = '".$this->db->escape($code)."'";
167  elseif ($label) $sql.= " WHERE t.libelle = '".$this->db->escape($label)."'";
168 
169  $resql=$this->db->query($sql);
170  if ($resql)
171  {
172  if ($this->db->num_rows($resql))
173  {
174  $obj = $this->db->fetch_object($resql);
175 
176  $this->id = $obj->id;
177  $this->code = $obj->code;
178  $this->libelle = $obj->label;
179  $this->country_id = $obj->country_id;
180  $this->active = $obj->active;
181  $this->module = $obj->module;
182  }
183  $this->db->free($resql);
184 
185  return 1;
186  }
187  else
188  {
189  $this->error="Error ".$this->db->lasterror();
190  return -1;
191  }
192  }
193 
194 
202  function update($user=null, $notrigger=0)
203  {
204  global $conf, $langs;
205  $error=0;
206 
207  // Clean parameters
208  if (isset($this->code)) $this->code=trim($this->code);
209  if (isset($this->libelle)) $this->libelle=trim($this->libelle);
210  if (isset($this->active)) $this->active=trim($this->active);
211  if (isset($this->module)) $this->module=trim($this->module);
212 
213 
214  // Check parameters
215  // Put here code to add control on parameters values
216 
217  // Update request
218  $sql = "UPDATE ".MAIN_DB_PREFIX."c_typent SET";
219  $sql.= " code=".(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").",";
220  $sql.= " libelle=".(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").",";
221  $sql.= " active=".(isset($this->active)?$this->active:"null").",";
222  $sql.= " module=".(isset($this->module)?"'".$this->db->escape($this->module)."'":"null")."";
223  $sql.= " WHERE id=".$this->id;
224 
225  $this->db->begin();
226 
227  dol_syslog(get_class($this)."::update", LOG_DEBUG);
228  $resql = $this->db->query($sql);
229  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
230 
231  if (! $error)
232  {
233  if (! $notrigger)
234  {
235  // Uncomment this and change MYOBJECT to your own tag if you
236  // want this action call a trigger.
237 
239  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
240  //$interface=new Interfaces($this->db);
241  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
242  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
244  }
245  }
246 
247  // Commit or rollback
248  if ($error)
249  {
250  foreach($this->errors as $errmsg)
251  {
252  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
253  $this->error.=($this->error?', '.$errmsg:$errmsg);
254  }
255  $this->db->rollback();
256  return -1*$error;
257  }
258  else
259  {
260  $this->db->commit();
261  return 1;
262  }
263  }
264 
265 
273  function delete($user, $notrigger=0)
274  {
275  global $conf, $langs;
276  $error=0;
277 
278  $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_typent";
279  $sql.= " WHERE id=".$this->id;
280 
281  $this->db->begin();
282 
283  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
284  $resql = $this->db->query($sql);
285  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
286 
287  if (! $error)
288  {
289  if (! $notrigger)
290  {
291  // Uncomment this and change MYOBJECT to your own tag if you
292  // want this action call a trigger.
293 
295  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
296  //$interface=new Interfaces($this->db);
297  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
298  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
300  }
301  }
302 
303  // Commit or rollback
304  if ($error)
305  {
306  foreach($this->errors as $errmsg)
307  {
308  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
309  $this->error.=($this->error?', '.$errmsg:$errmsg);
310  }
311  $this->db->rollback();
312  return -1*$error;
313  }
314  else
315  {
316  $this->db->commit();
317  return 1;
318  }
319  }
320 
321 }
fetch($id, $code='', $label='')
Load object in memory from database.
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.
$db
To store db handler.
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
create($user, $notrigger=0)
Create object into database.
$errors
To return several error codes (or messages)
$error
To return error code (or message)