dolibarr 18.0.6
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/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
33class Ccountry // extends CommonObject
34{
38 public $db;
39
43 public $error = '';
44
48 public $errors = array();
49
50 public $element = 'ccountry';
51 public $table_element = 'c_country';
52
56 public $id;
57
58 public $code;
59 public $code_iso;
60
64 public $label;
65
66 public $active;
67
68 public $fields = array(
69 'label' => array('type'=>'varchar(250)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>15, 'notnull'=>-1, 'showoncombobox'=>'1')
70 );
71
72
78 public function __construct($db)
79 {
80 $this->db = $db;
81 }
82
83
91 public function create($user, $notrigger = 0)
92 {
93 global $conf, $langs;
94 $error = 0;
95
96 // Clean parameters
97 if (isset($this->code)) {
98 $this->code = trim($this->code);
99 }
100 if (isset($this->code_iso)) {
101 $this->code_iso = trim($this->code_iso);
102 }
103 if (isset($this->label)) {
104 $this->label = trim($this->label);
105 }
106 if (isset($this->active)) {
107 $this->active = trim($this->active);
108 }
109
110 // Check parameters
111 // Put here code to add control on parameters values
112
113 // Insert request
114 $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
115 $sql .= "rowid,";
116 $sql .= "code,";
117 $sql .= "code_iso,";
118 $sql .= "label,";
119 $sql .= "active";
120 $sql .= ") VALUES (";
121 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
122 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
123 $sql .= " ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'").",";
124 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
125 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
126 $sql .= ")";
127
128 $this->db->begin();
129
130 dol_syslog(get_class($this)."::create", LOG_DEBUG);
131 $resql = $this->db->query($sql);
132 if (!$resql) {
133 $error++;
134 $this->errors[] = "Error ".$this->db->lasterror();
135 }
136
137 if (!$error) {
138 $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
139 }
140
141 // Commit or rollback
142 if ($error) {
143 foreach ($this->errors as $errmsg) {
144 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
145 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
146 }
147 $this->db->rollback();
148 return -1 * $error;
149 } else {
150 $this->db->commit();
151 return $this->id;
152 }
153 }
154
155
164 public function fetch($id, $code = '', $code_iso = '')
165 {
166 $sql = "SELECT";
167 $sql .= " t.rowid,";
168 $sql .= " t.code,";
169 $sql .= " t.code_iso,";
170 $sql .= " t.label,";
171 $sql .= " t.active";
172 $sql .= " FROM ".$this->db->prefix()."c_country as t";
173 if ($id) {
174 $sql .= " WHERE t.rowid = ".((int) $id);
175 } elseif ($code) {
176 $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
177 } elseif ($code_iso) {
178 $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
179 }
180
181 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
182 $resql = $this->db->query($sql);
183 if ($resql) {
184 if ($this->db->num_rows($resql)) {
185 $obj = $this->db->fetch_object($resql);
186
187 if ($obj) {
188 $this->id = $obj->rowid;
189 $this->code = $obj->code;
190 $this->code_iso = $obj->code_iso;
191 $this->label = $obj->label;
192 $this->active = $obj->active;
193 }
194
195 $this->db->free($resql);
196 return 1;
197 } else {
198 return 0;
199 }
200 } else {
201 $this->error = "Error ".$this->db->lasterror();
202 return -1;
203 }
204 }
205
206
214 public function update($user = null, $notrigger = 0)
215 {
216 global $conf, $langs;
217 $error = 0;
218
219 // Clean parameters
220 if (isset($this->code)) {
221 $this->code = trim($this->code);
222 }
223 if (isset($this->code_iso)) {
224 $this->code_iso = trim($this->code_iso);
225 }
226 if (isset($this->label)) {
227 $this->label = trim($this->label);
228 }
229 if (isset($this->active)) {
230 $this->active = trim($this->active);
231 }
232
233
234 // Check parameters
235 // Put here code to add control on parameters values
236
237 // Update request
238 $sql = "UPDATE ".$this->db->prefix()."c_country SET";
239 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
240 $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
241 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
242 $sql .= " active=".(isset($this->active) ? $this->active : "null");
243 $sql .= " WHERE rowid=".((int) $this->id);
244
245 $this->db->begin();
246
247 dol_syslog(get_class($this)."::update", LOG_DEBUG);
248 $resql = $this->db->query($sql);
249 if (!$resql) {
250 $error++;
251 $this->errors[] = "Error ".$this->db->lasterror();
252 }
253
254 // Commit or rollback
255 if ($error) {
256 foreach ($this->errors as $errmsg) {
257 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
258 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
259 }
260 $this->db->rollback();
261 return -1 * $error;
262 } else {
263 $this->db->commit();
264 return 1;
265 }
266 }
267
268
276 public function delete($user, $notrigger = 0)
277 {
278 global $conf, $langs;
279 $error = 0;
280
281 $sql = "DELETE FROM ".$this->db->prefix()."c_country";
282 $sql .= " WHERE rowid=".((int) $this->id);
283
284 $this->db->begin();
285
286 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
287 $resql = $this->db->query($sql);
288 if (!$resql) {
289 $error++;
290 $this->errors[] = "Error ".$this->db->lasterror();
291 }
292
293 // Commit or rollback
294 if ($error) {
295 foreach ($this->errors as $errmsg) {
296 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
297 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
298 }
299 $this->db->rollback();
300 return -1 * $error;
301 } else {
302 $this->db->commit();
303 return 1;
304 }
305 }
306
317 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
318 {
319 global $langs;
320 return $langs->trans($this->label);
321 }
322}
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 optionaly 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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e rowid
Definition invoice.php:1632