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