dolibarr 21.0.3
ccountry.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25// Put here all includes required by your class file
26require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
27
28
32class Ccountry extends CommonDict
33{
37 public $element = 'ccountry';
41 public $table_element = 'c_country';
42
46 public $code_iso;
47
51 public $fields = array(
52 'label' => array('type' => 'varchar(250)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 15, 'notnull' => -1, 'showoncombobox' => 1)
53 );
54
58 public $favorite;
59
63 public $eec;
64
68 public $numeric_code;
69
70
76 public function __construct($db)
77 {
78 $this->db = $db;
79 }
80
81
89 public function create($user, $notrigger = 0)
90 {
91 $error = 0;
92
93 // Clean parameters
94 if (isset($this->code)) {
95 $this->code = trim($this->code);
96 }
97 if (isset($this->code_iso)) {
98 $this->code_iso = trim($this->code_iso);
99 }
100 if (isset($this->label)) {
101 $this->label = trim($this->label);
102 }
103 if (isset($this->active)) {
104 $this->active = (int) $this->active;
105 }
106
107 // Check parameters
108 // Put here code to add control on parameters values
109
110 // Insert request
111 $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
112 $sql .= "rowid,";
113 $sql .= "code,";
114 $sql .= "code_iso,";
115 $sql .= "label,";
116 $sql .= "active";
117 $sql .= ") VALUES (";
118 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
119 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
120 $sql .= " ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'").",";
121 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
122 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
123 $sql .= ")";
124
125 $this->db->begin();
126
127 dol_syslog(get_class($this)."::create", LOG_DEBUG);
128 $resql = $this->db->query($sql);
129 if (!$resql) {
130 $error++;
131 $this->errors[] = "Error ".$this->db->lasterror();
132 }
133
134 if (!$error) {
135 $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
136 }
137
138 // Commit or rollback
139 if ($error) {
140 foreach ($this->errors as $errmsg) {
141 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
142 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
143 }
144 $this->db->rollback();
145 return -1 * $error;
146 } else {
147 $this->db->commit();
148 return $this->id;
149 }
150 }
151
152
161 public function fetch($id, $code = '', $code_iso = '')
162 {
163 $sql = "SELECT";
164 $sql .= " t.rowid,";
165 $sql .= " t.code,";
166 $sql .= " t.code_iso,";
167 $sql .= " t.label,";
168 $sql .= " t.eec,";
169 $sql .= " t.active,";
170 $sql .= " t.favorite,";
171 $sql .= " t.numeric_code";
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
183 $resql = $this->db->query($sql);
184 if ($resql) {
185 if ($this->db->num_rows($resql)) {
186 $obj = $this->db->fetch_object($resql);
187
188 if ($obj) {
189 $this->id = $obj->rowid;
190 $this->code = $obj->code;
191 $this->code_iso = $obj->code_iso;
192 $this->label = $obj->label;
193 $this->active = $obj->active;
194 $this->favorite = $obj->favorite;
195 $this->eec = $obj->eec;
196 $this->numeric_code = $obj->numeric_code;
197 }
198
199 $this->db->free($resql);
200 return 1;
201 } else {
202 return 0;
203 }
204 } else {
205 $this->error = "Error ".$this->db->lasterror();
206 return -1;
207 }
208 }
209
210
218 public function update($user = null, $notrigger = 0)
219 {
220 $error = 0;
221
222 // Clean parameters
223 if (isset($this->code)) {
224 $this->code = trim($this->code);
225 }
226 if (isset($this->code_iso)) {
227 $this->code_iso = trim($this->code_iso);
228 }
229 if (isset($this->label)) {
230 $this->label = trim($this->label);
231 }
232 if (isset($this->active)) {
233 $this->active = (int) $this->active;
234 }
235
236
237 // Check parameters
238 // Put here code to add control on parameters values
239
240 // Update request
241 $sql = "UPDATE ".$this->db->prefix()."c_country SET";
242 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
243 $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
244 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
245 $sql .= " active=".(isset($this->active) ? $this->active : "null");
246 $sql .= " WHERE rowid=".((int) $this->id);
247
248 $this->db->begin();
249
250 dol_syslog(get_class($this)."::update", LOG_DEBUG);
251 $resql = $this->db->query($sql);
252 if (!$resql) {
253 $error++;
254 $this->errors[] = "Error ".$this->db->lasterror();
255 }
256
257 // Commit or rollback
258 if ($error) {
259 foreach ($this->errors as $errmsg) {
260 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
261 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
262 }
263 $this->db->rollback();
264 return -1 * $error;
265 } else {
266 $this->db->commit();
267 return 1;
268 }
269 }
270
271
279 public function delete($user, $notrigger = 0)
280 {
281 $error = 0;
282
283 $sql = "DELETE FROM ".$this->db->prefix()."c_country";
284 $sql .= " WHERE rowid=".((int) $this->id);
285
286 $this->db->begin();
287
288 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
289 $resql = $this->db->query($sql);
290 if (!$resql) {
291 $error++;
292 $this->errors[] = "Error ".$this->db->lasterror();
293 }
294
295 // Commit or rollback
296 if ($error) {
297 foreach ($this->errors as $errmsg) {
298 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
299 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
300 }
301 $this->db->rollback();
302 return -1 * $error;
303 } else {
304 $this->db->commit();
305 return 1;
306 }
307 }
308
319 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
320 {
321 global $langs;
322 return $langs->trans($this->label);
323 }
324}
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.