dolibarr 21.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 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
55
61 public function __construct($db)
62 {
63 $this->db = $db;
64 }
65
66
74 public function create($user, $notrigger = 0)
75 {
76 $error = 0;
77
78 // Clean parameters
79 if (isset($this->code)) {
80 $this->code = trim($this->code);
81 }
82 if (isset($this->code_iso)) {
83 $this->code_iso = trim($this->code_iso);
84 }
85 if (isset($this->label)) {
86 $this->label = trim($this->label);
87 }
88 if (isset($this->active)) {
89 $this->active = (int) $this->active;
90 }
91
92 // Check parameters
93 // Put here code to add control on parameters values
94
95 // Insert request
96 $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
97 $sql .= "rowid,";
98 $sql .= "code,";
99 $sql .= "code_iso,";
100 $sql .= "label,";
101 $sql .= "active";
102 $sql .= ") VALUES (";
103 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
104 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
105 $sql .= " ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'").",";
106 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
107 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
108 $sql .= ")";
109
110 $this->db->begin();
111
112 dol_syslog(get_class($this)."::create", LOG_DEBUG);
113 $resql = $this->db->query($sql);
114 if (!$resql) {
115 $error++;
116 $this->errors[] = "Error ".$this->db->lasterror();
117 }
118
119 if (!$error) {
120 $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
121 }
122
123 // Commit or rollback
124 if ($error) {
125 foreach ($this->errors as $errmsg) {
126 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
127 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
128 }
129 $this->db->rollback();
130 return -1 * $error;
131 } else {
132 $this->db->commit();
133 return $this->id;
134 }
135 }
136
137
146 public function fetch($id, $code = '', $code_iso = '')
147 {
148 $sql = "SELECT";
149 $sql .= " t.rowid,";
150 $sql .= " t.code,";
151 $sql .= " t.code_iso,";
152 $sql .= " t.label,";
153 $sql .= " t.active";
154 $sql .= " FROM ".$this->db->prefix()."c_country as t";
155 if ($id) {
156 $sql .= " WHERE t.rowid = ".((int) $id);
157 } elseif ($code) {
158 $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
159 } elseif ($code_iso) {
160 $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
161 }
162
163 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
164 $resql = $this->db->query($sql);
165 if ($resql) {
166 if ($this->db->num_rows($resql)) {
167 $obj = $this->db->fetch_object($resql);
168
169 if ($obj) {
170 $this->id = $obj->rowid;
171 $this->code = $obj->code;
172 $this->code_iso = $obj->code_iso;
173 $this->label = $obj->label;
174 $this->active = $obj->active;
175 }
176
177 $this->db->free($resql);
178 return 1;
179 } else {
180 return 0;
181 }
182 } else {
183 $this->error = "Error ".$this->db->lasterror();
184 return -1;
185 }
186 }
187
188
196 public function update($user = null, $notrigger = 0)
197 {
198 $error = 0;
199
200 // Clean parameters
201 if (isset($this->code)) {
202 $this->code = trim($this->code);
203 }
204 if (isset($this->code_iso)) {
205 $this->code_iso = trim($this->code_iso);
206 }
207 if (isset($this->label)) {
208 $this->label = trim($this->label);
209 }
210 if (isset($this->active)) {
211 $this->active = (int) $this->active;
212 }
213
214
215 // Check parameters
216 // Put here code to add control on parameters values
217
218 // Update request
219 $sql = "UPDATE ".$this->db->prefix()."c_country SET";
220 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
221 $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
222 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
223 $sql .= " active=".(isset($this->active) ? $this->active : "null");
224 $sql .= " WHERE rowid=".((int) $this->id);
225
226 $this->db->begin();
227
228 dol_syslog(get_class($this)."::update", LOG_DEBUG);
229 $resql = $this->db->query($sql);
230 if (!$resql) {
231 $error++;
232 $this->errors[] = "Error ".$this->db->lasterror();
233 }
234
235 // Commit or rollback
236 if ($error) {
237 foreach ($this->errors as $errmsg) {
238 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
239 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
240 }
241 $this->db->rollback();
242 return -1 * $error;
243 } else {
244 $this->db->commit();
245 return 1;
246 }
247 }
248
249
257 public function delete($user, $notrigger = 0)
258 {
259 $error = 0;
260
261 $sql = "DELETE FROM ".$this->db->prefix()."c_country";
262 $sql .= " WHERE rowid=".((int) $this->id);
263
264 $this->db->begin();
265
266 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
267 $resql = $this->db->query($sql);
268 if (!$resql) {
269 $error++;
270 $this->errors[] = "Error ".$this->db->lasterror();
271 }
272
273 // Commit or rollback
274 if ($error) {
275 foreach ($this->errors as $errmsg) {
276 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
277 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
278 }
279 $this->db->rollback();
280 return -1 * $error;
281 } else {
282 $this->db->commit();
283 return 1;
284 }
285 }
286
297 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
298 {
299 global $langs;
300 return $langs->trans($this->label);
301 }
302}
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.