dolibarr 23.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-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
5 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Put here all includes required by your class file
28require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
29
30
34class Ccountry extends CommonDict
35{
39 public $element = 'ccountry';
43 public $table_element = 'c_country';
44
48 public $code_iso;
49
53 public $eec;
54
58 public $favorite;
59
63 public $numeric_code;
64
65
69 public $fields = array(
70 'label' => array('type' => 'varchar(250)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 15, 'notnull' => -1, 'showoncombobox' => 1)
71 );
72
73
79 public function __construct($db)
80 {
81 $this->db = $db;
82 }
83
84
92 public function create($user, $notrigger = 0)
93 {
94 $error = 0;
95 if (empty($this->id)) {
96 return -1;
97 }
98 // Clean parameters
99 if (isset($this->code)) {
100 $this->code = trim($this->code);
101 }
102 if (isset($this->code_iso)) {
103 $this->code_iso = trim($this->code_iso);
104 }
105 if (isset($this->label)) {
106 $this->label = trim($this->label);
107 }
108 if (isset($this->active)) {
109 $this->active = (int) $this->active;
110 }
111
112 // Check parameters
113 // Put here code to add control on parameters values
114
115 // Insert request
116 $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
117 $sql .= "rowid,";
118 $sql .= "code,";
119 $sql .= "code_iso,";
120 $sql .= "label,";
121 $sql .= "active";
122 $sql .= ") VALUES (";
123 $sql .= (int) $this->id;
124 $sql .= ", ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'");
125 $sql .= ", ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'");
126 $sql .= ", ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'");
127 $sql .= ", ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape((string) $this->active)."'");
128 $sql .= ")";
129
130 $this->db->begin();
131
132 dol_syslog(get_class($this)."::create", LOG_DEBUG);
133 $resql = $this->db->query($sql);
134 if (!$resql) {
135 $error++;
136 $this->errors[] = "Error ".$this->db->lasterror();
137 }
138
139 if (!$error) {
140 $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
141 }
142
143 // Commit or rollback
144 if ($error) {
145 foreach ($this->errors as $errmsg) {
146 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
147 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
148 }
149 $this->db->rollback();
150 return -1 * $error;
151 } else {
152 $this->db->commit();
153 return $this->id;
154 }
155 }
156
157
166 public function fetch($id, $code = '', $code_iso = '')
167 {
168 $sql = "SELECT";
169 $sql .= " t.rowid,";
170 $sql .= " t.code,";
171 $sql .= " t.code_iso,";
172 $sql .= " t.label,";
173 $sql .= " t.eec,";
174 $sql .= " t.active,";
175 $sql .= " t.favorite,";
176 $sql .= " t.numeric_code";
177 $sql .= " FROM ".$this->db->prefix()."c_country as t";
178 if ($id) {
179 $sql .= " WHERE t.rowid = ".((int) $id);
180 } elseif ($code) {
181 $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
182 } elseif ($code_iso) {
183 $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
184 }
185
186 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
187
188 $resql = $this->db->query($sql);
189 if ($resql) {
190 if ($this->db->num_rows($resql)) {
191 $obj = $this->db->fetch_object($resql);
192
193 if ($obj) {
194 $this->id = (int) $obj->rowid;
195 $this->code = $obj->code;
196 $this->code_iso = $obj->code_iso;
197 $this->label = $obj->label;
198 $this->eec = $obj->eec;
199 $this->active = (int) $obj->active;
200 $this->favorite = $obj->favorite;
201 $this->numeric_code = $obj->numeric_code;
202 }
203
204 $this->db->free($resql);
205 return 1;
206 } else {
207 return 0;
208 }
209 } else {
210 $this->error = "Error ".$this->db->lasterror();
211 return -1;
212 }
213 }
214
215
223 public function update($user = null, $notrigger = 0)
224 {
225 $error = 0;
226
227 // Clean parameters
228 if (isset($this->code)) {
229 $this->code = trim($this->code);
230 }
231 if (isset($this->code_iso)) {
232 $this->code_iso = trim($this->code_iso);
233 }
234 if (isset($this->label)) {
235 $this->label = trim($this->label);
236 }
237 if (isset($this->active)) {
238 $this->active = (int) $this->active;
239 }
240
241
242 // Check parameters
243 // Put here code to add control on parameters values
244
245 // Update request
246 $sql = "UPDATE ".$this->db->prefix()."c_country SET";
247 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
248 $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
249 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
250 $sql .= " active=".(isset($this->active) ? $this->active : "null");
251 $sql .= " WHERE rowid=".((int) $this->id);
252
253 $this->db->begin();
254
255 dol_syslog(get_class($this)."::update", LOG_DEBUG);
256 $resql = $this->db->query($sql);
257 if (!$resql) {
258 $error++;
259 $this->errors[] = "Error ".$this->db->lasterror();
260 }
261
262 // Commit or rollback
263 if ($error) {
264 foreach ($this->errors as $errmsg) {
265 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
266 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
267 }
268 $this->db->rollback();
269 return -1 * $error;
270 } else {
271 $this->db->commit();
272 return 1;
273 }
274 }
275
276
284 public function delete($user, $notrigger = 0)
285 {
286 $error = 0;
287
288 $sql = "DELETE FROM ".$this->db->prefix()."c_country";
289 $sql .= " WHERE rowid=".((int) $this->id);
290
291 $this->db->begin();
292
293 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
294 $resql = $this->db->query($sql);
295 if (!$resql) {
296 $error++;
297 $this->errors[] = "Error ".$this->db->lasterror();
298 }
299
300 // Commit or rollback
301 if ($error) {
302 foreach ($this->errors as $errmsg) {
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 } else {
309 $this->db->commit();
310 return 1;
311 }
312 }
313
324 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
325 {
326 global $langs;
327 return $langs->trans($this->label);
328 }
329}
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.
editval_textarea active