dolibarr 25.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 * Copyright (C) 2024-2026 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 * Copyright (C) 2026 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Put here all includes required by your class file
29require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/cstate.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/cregion.class.php';
32
36class Ccountry extends CommonDict
37{
41 public $element = 'ccountry';
45 public $table_element = 'c_country';
46
50 public $code_iso;
51
55 public $eec;
56
60 public $favorite;
61
65 public $numeric_code;
66
70 public $phone_code;
71
75 public $trunk_prefix;
76
77
81 public $fields = array(
82 'code' => array('type' => 'varchar(2)', 'label' => 'CountryCode', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => 1),
83 'label' => array('type' => 'varchar(250)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 15, 'notnull' => -1, 'showoncombobox' => 1)
84 );
85
86
92 public function __construct($db)
93 {
94 $this->db = $db;
95 }
96
97
105 public function create($user, $notrigger = 0)
106 {
107 $error = 0;
108 if (empty($this->id)) {
109 return -1;
110 }
111 // Clean parameters
112 if (isset($this->code)) {
113 $this->code = trim($this->code);
114 }
115 if (isset($this->code_iso)) {
116 $this->code_iso = trim($this->code_iso);
117 }
118 if (isset($this->label)) {
119 $this->label = trim($this->label);
120 }
121 if (isset($this->active)) {
122 $this->active = (int) $this->active;
123 }
124
125 // Check parameters
126 // Put here code to add control on parameters values
127
128 // Insert request
129 $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
130 $sql .= "rowid,";
131 $sql .= "code,";
132 $sql .= "code_iso,";
133 $sql .= "label,";
134 $sql .= "active";
135 $sql .= ") VALUES (";
136 $sql .= (int) $this->id;
137 $sql .= ", ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'");
138 $sql .= ", ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'");
139 $sql .= ", ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'");
140 $sql .= ", ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape((string) $this->active)."'");
141 $sql .= ")";
142
143 $this->db->begin();
144
145 dol_syslog(get_class($this)."::create", LOG_DEBUG);
146 $resql = $this->db->query($sql);
147 if (!$resql) {
148 $error++;
149 $this->errors[] = "Error ".$this->db->lasterror();
150 }
151
152 // Note: the rowid of c_country is not auto incremented, $this->id is the id given
153
154 // Commit or rollback
155 if ($error) {
156 foreach ($this->errors as $errmsg) {
157 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
158 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
159 }
160 $this->db->rollback();
161 return -1 * $error;
162 } else {
163 $this->db->commit();
164 return $this->id;
165 }
166 }
167
168
177 public function fetch($id, $code = '', $code_iso = '')
178 {
179 $sql = "SELECT";
180 $sql .= " t.rowid,";
181 $sql .= " t.code,";
182 $sql .= " t.code_iso,";
183 $sql .= " t.label,";
184 $sql .= " t.eec,";
185 $sql .= " t.active,";
186 $sql .= " t.favorite,";
187 $sql .= " t.numeric_code,";
188 $sql .= " t.phone_code,";
189 $sql .= " t.trunk_prefix";
190 $sql .= " FROM ".$this->db->prefix()."c_country as t";
191 if ($id) {
192 $sql .= " WHERE t.rowid = ".((int) $id);
193 } elseif ($code) {
194 $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
195 } elseif ($code_iso) {
196 $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
197 }
198
199 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
200
201 $resql = $this->db->query($sql);
202 if ($resql) {
203 if ($this->db->num_rows($resql)) {
204 $obj = $this->db->fetch_object($resql);
205
206 if ($obj) {
207 $this->id = (int) $obj->rowid;
208 $this->code = $obj->code;
209 $this->code_iso = $obj->code_iso;
210 $this->label = $obj->label;
211 $this->eec = $obj->eec;
212 $this->active = (int) $obj->active;
213 $this->favorite = $obj->favorite;
214 $this->numeric_code = $obj->numeric_code;
215 $this->phone_code = $obj->phone_code;
216 $this->trunk_prefix = $obj->trunk_prefix;
217 }
218
219 $this->db->free($resql);
220 return 1;
221 } else {
222 return 0;
223 }
224 } else {
225 $this->error = "Error ".$this->db->lasterror();
226 return -1;
227 }
228 }
229
230
238 public function update($user = null, $notrigger = 0)
239 {
240 $error = 0;
241
242 // Clean parameters
243 if (isset($this->code)) {
244 $this->code = trim($this->code);
245 }
246 if (isset($this->code_iso)) {
247 $this->code_iso = trim($this->code_iso);
248 }
249 if (isset($this->label)) {
250 $this->label = trim($this->label);
251 }
252 if (isset($this->active)) {
253 $this->active = (int) $this->active;
254 }
255
256
257 // Check parameters
258 // Put here code to add control on parameters values
259
260 // Update request
261 $sql = "UPDATE ".$this->db->prefix()."c_country SET";
262 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
263 $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
264 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
265 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
266 $sql .= " WHERE rowid=".((int) $this->id);
267
268 $this->db->begin();
269
270 dol_syslog(get_class($this)."::update", LOG_DEBUG);
271 $resql = $this->db->query($sql);
272 if (!$resql) {
273 $error++;
274 $this->errors[] = "Error ".$this->db->lasterror();
275 }
276
277 // Commit or rollback
278 if ($error) {
279 foreach ($this->errors as $errmsg) {
280 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
281 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
282 }
283 $this->db->rollback();
284 return -1 * $error;
285 } else {
286 $this->db->commit();
287 return 1;
288 }
289 }
290
291
299 public function delete($user, $notrigger = 0)
300 {
301 $error = 0;
302
303 $sql = "DELETE FROM ".$this->db->prefix()."c_country";
304 $sql .= " WHERE rowid=".((int) $this->id);
305
306 $this->db->begin();
307
308 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
309 $resql = $this->db->query($sql);
310 if (!$resql) {
311 $error++;
312 $this->errors[] = "Error ".$this->db->lasterror();
313 }
314
315 // Commit or rollback
316 if ($error) {
317 foreach ($this->errors as $errmsg) {
318 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
319 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
320 }
321 $this->db->rollback();
322 return -1 * $error;
323 } else {
324 $this->db->commit();
325 return 1;
326 }
327 }
328
339 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
340 {
341 global $langs;
342 return $langs->trans($this->label);
343 }
344}
345
350{
354 public $states;
355
359 public $regions;
360
366 public function __construct($db)
367 {
368 $this->db = $db;
369 }
370}
Class to manage dictionary Countries (used by imports)
__construct($db)
Constructor.
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