dolibarr 24.0.0-beta
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 'label' => array('type' => 'varchar(250)', 'label' => 'Label', 'enabled' => 1, 'visible' => 1, 'position' => 15, 'notnull' => -1, 'showoncombobox' => 1)
83 );
84
85
91 public function __construct($db)
92 {
93 $this->db = $db;
94 }
95
96
104 public function create($user, $notrigger = 0)
105 {
106 $error = 0;
107 if (empty($this->id)) {
108 return -1;
109 }
110 // Clean parameters
111 if (isset($this->code)) {
112 $this->code = trim($this->code);
113 }
114 if (isset($this->code_iso)) {
115 $this->code_iso = trim($this->code_iso);
116 }
117 if (isset($this->label)) {
118 $this->label = trim($this->label);
119 }
120 if (isset($this->active)) {
121 $this->active = (int) $this->active;
122 }
123
124 // Check parameters
125 // Put here code to add control on parameters values
126
127 // Insert request
128 $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
129 $sql .= "rowid,";
130 $sql .= "code,";
131 $sql .= "code_iso,";
132 $sql .= "label,";
133 $sql .= "active";
134 $sql .= ") VALUES (";
135 $sql .= (int) $this->id;
136 $sql .= ", ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'");
137 $sql .= ", ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'");
138 $sql .= ", ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'");
139 $sql .= ", ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape((string) $this->active)."'");
140 $sql .= ")";
141
142 $this->db->begin();
143
144 dol_syslog(get_class($this)."::create", LOG_DEBUG);
145 $resql = $this->db->query($sql);
146 if (!$resql) {
147 $error++;
148 $this->errors[] = "Error ".$this->db->lasterror();
149 }
150
151 if (!$error) {
152 $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
153 }
154
155 // Commit or rollback
156 if ($error) {
157 foreach ($this->errors as $errmsg) {
158 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
159 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
160 }
161 $this->db->rollback();
162 return -1 * $error;
163 } else {
164 $this->db->commit();
165 return $this->id;
166 }
167 }
168
169
178 public function fetch($id, $code = '', $code_iso = '')
179 {
180 $sql = "SELECT";
181 $sql .= " t.rowid,";
182 $sql .= " t.code,";
183 $sql .= " t.code_iso,";
184 $sql .= " t.label,";
185 $sql .= " t.eec,";
186 $sql .= " t.active,";
187 $sql .= " t.favorite,";
188 $sql .= " t.numeric_code,";
189 $sql .= " t.phone_code,";
190 $sql .= " t.trunk_prefix";
191 $sql .= " FROM ".$this->db->prefix()."c_country as t";
192 if ($id) {
193 $sql .= " WHERE t.rowid = ".((int) $id);
194 } elseif ($code) {
195 $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
196 } elseif ($code_iso) {
197 $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
198 }
199
200 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
201
202 $resql = $this->db->query($sql);
203 if ($resql) {
204 if ($this->db->num_rows($resql)) {
205 $obj = $this->db->fetch_object($resql);
206
207 if ($obj) {
208 $this->id = (int) $obj->rowid;
209 $this->code = $obj->code;
210 $this->code_iso = $obj->code_iso;
211 $this->label = $obj->label;
212 $this->eec = $obj->eec;
213 $this->active = (int) $obj->active;
214 $this->favorite = $obj->favorite;
215 $this->numeric_code = $obj->numeric_code;
216 $this->phone_code = $obj->phone_code;
217 $this->trunk_prefix = $obj->trunk_prefix;
218 }
219
220 $this->db->free($resql);
221 return 1;
222 } else {
223 return 0;
224 }
225 } else {
226 $this->error = "Error ".$this->db->lasterror();
227 return -1;
228 }
229 }
230
231
239 public function update($user = null, $notrigger = 0)
240 {
241 $error = 0;
242
243 // Clean parameters
244 if (isset($this->code)) {
245 $this->code = trim($this->code);
246 }
247 if (isset($this->code_iso)) {
248 $this->code_iso = trim($this->code_iso);
249 }
250 if (isset($this->label)) {
251 $this->label = trim($this->label);
252 }
253 if (isset($this->active)) {
254 $this->active = (int) $this->active;
255 }
256
257
258 // Check parameters
259 // Put here code to add control on parameters values
260
261 // Update request
262 $sql = "UPDATE ".$this->db->prefix()."c_country SET";
263 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
264 $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
265 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
266 $sql .= " active=".(isset($this->active) ? $this->active : "null");
267 $sql .= " WHERE rowid=".((int) $this->id);
268
269 $this->db->begin();
270
271 dol_syslog(get_class($this)."::update", LOG_DEBUG);
272 $resql = $this->db->query($sql);
273 if (!$resql) {
274 $error++;
275 $this->errors[] = "Error ".$this->db->lasterror();
276 }
277
278 // Commit or rollback
279 if ($error) {
280 foreach ($this->errors as $errmsg) {
281 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
282 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
283 }
284 $this->db->rollback();
285 return -1 * $error;
286 } else {
287 $this->db->commit();
288 return 1;
289 }
290 }
291
292
300 public function delete($user, $notrigger = 0)
301 {
302 $error = 0;
303
304 $sql = "DELETE FROM ".$this->db->prefix()."c_country";
305 $sql .= " WHERE rowid=".((int) $this->id);
306
307 $this->db->begin();
308
309 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
310 $resql = $this->db->query($sql);
311 if (!$resql) {
312 $error++;
313 $this->errors[] = "Error ".$this->db->lasterror();
314 }
315
316 // Commit or rollback
317 if ($error) {
318 foreach ($this->errors as $errmsg) {
319 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
320 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
321 }
322 $this->db->rollback();
323 return -1 * $error;
324 } else {
325 $this->db->commit();
326 return 1;
327 }
328 }
329
340 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
341 {
342 global $langs;
343 return $langs->trans($this->label);
344 }
345}
346
351{
355 public $states;
356
360 public $regions;
361
367 public function __construct($db)
368 {
369 $this->db = $db;
370 }
371}
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