dolibarr  20.0.0-alpha
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 <https://www.gnu.org/licenses/>.
16  */
17 
24 // Put here all includes required by your class file
25 require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
26 
27 
31 class Ccountry extends CommonDict
32 {
33  public $element = 'ccountry';
34  public $table_element = 'c_country';
35 
36  public $code_iso;
37 
38  public $fields = array(
39  'label' => array('type'=>'varchar(250)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>15, 'notnull'=>-1, 'showoncombobox'=>'1')
40  );
41 
42 
48  public function __construct($db)
49  {
50  $this->db = $db;
51  }
52 
53 
61  public function create($user, $notrigger = 0)
62  {
63  $error = 0;
64 
65  // Clean parameters
66  if (isset($this->code)) {
67  $this->code = trim($this->code);
68  }
69  if (isset($this->code_iso)) {
70  $this->code_iso = trim($this->code_iso);
71  }
72  if (isset($this->label)) {
73  $this->label = trim($this->label);
74  }
75  if (isset($this->active)) {
76  $this->active = (int) $this->active;
77  }
78 
79  // Check parameters
80  // Put here code to add control on parameters values
81 
82  // Insert request
83  $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
84  $sql .= "rowid,";
85  $sql .= "code,";
86  $sql .= "code_iso,";
87  $sql .= "label,";
88  $sql .= "active";
89  $sql .= ") VALUES (";
90  $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
91  $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
92  $sql .= " ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'").",";
93  $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
94  $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
95  $sql .= ")";
96 
97  $this->db->begin();
98 
99  dol_syslog(get_class($this)."::create", LOG_DEBUG);
100  $resql = $this->db->query($sql);
101  if (!$resql) {
102  $error++;
103  $this->errors[] = "Error ".$this->db->lasterror();
104  }
105 
106  if (!$error) {
107  $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
108  }
109 
110  // Commit or rollback
111  if ($error) {
112  foreach ($this->errors as $errmsg) {
113  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
114  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
115  }
116  $this->db->rollback();
117  return -1 * $error;
118  } else {
119  $this->db->commit();
120  return $this->id;
121  }
122  }
123 
124 
133  public function fetch($id, $code = '', $code_iso = '')
134  {
135  $sql = "SELECT";
136  $sql .= " t.rowid,";
137  $sql .= " t.code,";
138  $sql .= " t.code_iso,";
139  $sql .= " t.label,";
140  $sql .= " t.active";
141  $sql .= " FROM ".$this->db->prefix()."c_country as t";
142  if ($id) {
143  $sql .= " WHERE t.rowid = ".((int) $id);
144  } elseif ($code) {
145  $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
146  } elseif ($code_iso) {
147  $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
148  }
149 
150  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
151  $resql = $this->db->query($sql);
152  if ($resql) {
153  if ($this->db->num_rows($resql)) {
154  $obj = $this->db->fetch_object($resql);
155 
156  if ($obj) {
157  $this->id = $obj->rowid;
158  $this->code = $obj->code;
159  $this->code_iso = $obj->code_iso;
160  $this->label = $obj->label;
161  $this->active = $obj->active;
162  }
163 
164  $this->db->free($resql);
165  return 1;
166  } else {
167  return 0;
168  }
169  } else {
170  $this->error = "Error ".$this->db->lasterror();
171  return -1;
172  }
173  }
174 
175 
183  public function update($user = null, $notrigger = 0)
184  {
185  $error = 0;
186 
187  // Clean parameters
188  if (isset($this->code)) {
189  $this->code = trim($this->code);
190  }
191  if (isset($this->code_iso)) {
192  $this->code_iso = trim($this->code_iso);
193  }
194  if (isset($this->label)) {
195  $this->label = trim($this->label);
196  }
197  if (isset($this->active)) {
198  $this->active = (int) $this->active;
199  }
200 
201 
202  // Check parameters
203  // Put here code to add control on parameters values
204 
205  // Update request
206  $sql = "UPDATE ".$this->db->prefix()."c_country SET";
207  $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
208  $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
209  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
210  $sql .= " active=".(isset($this->active) ? $this->active : "null");
211  $sql .= " WHERE rowid=".((int) $this->id);
212 
213  $this->db->begin();
214 
215  dol_syslog(get_class($this)."::update", LOG_DEBUG);
216  $resql = $this->db->query($sql);
217  if (!$resql) {
218  $error++;
219  $this->errors[] = "Error ".$this->db->lasterror();
220  }
221 
222  // Commit or rollback
223  if ($error) {
224  foreach ($this->errors as $errmsg) {
225  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
226  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
227  }
228  $this->db->rollback();
229  return -1 * $error;
230  } else {
231  $this->db->commit();
232  return 1;
233  }
234  }
235 
236 
244  public function delete($user, $notrigger = 0)
245  {
246  $error = 0;
247 
248  $sql = "DELETE FROM ".$this->db->prefix()."c_country";
249  $sql .= " WHERE rowid=".((int) $this->id);
250 
251  $this->db->begin();
252 
253  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
254  $resql = $this->db->query($sql);
255  if (!$resql) {
256  $error++;
257  $this->errors[] = "Error ".$this->db->lasterror();
258  }
259 
260  // Commit or rollback
261  if ($error) {
262  foreach ($this->errors as $errmsg) {
263  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
264  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
265  }
266  $this->db->rollback();
267  return -1 * $error;
268  } else {
269  $this->db->commit();
270  return 1;
271  }
272  }
273 
284  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
285  {
286  global $langs;
287  return $langs->trans($this->label);
288  }
289 }
Class to manage dictionary Countries (used by imports)
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
create($user, $notrigger=0)
Create object into database.
$table_element
Name of table without prefix where object is stored.
__construct($db)
Constructor.
update($user=null, $notrigger=0)
Update object into database.
$element
Id that identify managed objects.
fetch($id, $code='', $code_iso='')
Load object in memory from database.
Parent class of all other dictionary classes.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
print *****$script_file(".$version.") pid code
1: frais de port 2: ecotaxe 3: option line (when qty = 0)