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