dolibarr 21.0.0-alpha
cregion.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) Richard Rondu <rondu.richard@lainwir3d.net>
3 * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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 Cregion extends CommonDict
34{
35 //public $element = 'cregion'; //!< Id that identify managed objects
36 //public $table_element = 'c_regions'; //!< Name of table without prefix where object is stored
37
41 public $code_region;
42
46 public $fk_pays;
47
51 public $name;
52
58 public $cheflieu;
59
60
66 public function __construct($db)
67 {
68 $this->db = $db;
69 }
70
71
79 public function create($user, $notrigger = 0)
80 {
81 global $conf, $langs;
82 $error = 0;
83
84 // Clean parameters
85 if (isset($this->code_region)) {
86 $this->code_region = (int) $this->code_region;
87 }
88 if (isset($this->fk_pays)) {
89 $this->fk_pays = (int) $this->fk_pays;
90 }
91 if (isset($this->name)) {
92 $this->name = trim($this->name);
93 }
94 if (isset($this->cheflieu)) {
95 $this->cheflieu = trim($this->cheflieu);
96 }
97 if (isset($this->active)) {
98 $this->active = (int) $this->active;
99 }
100
101 // Check parameters
102 // Put here code to add control on parameters values
103
104 // Insert request
105 $sql = "INSERT INTO ".$this->db->prefix()."c_regions(";
106 $sql .= "rowid,";
107 $sql .= "code_region,";
108 $sql .= "fk_pays,";
109 $sql .= "nom,";
110 $sql .= "cheflieu,";
111 $sql .= "active";
112 $sql .= ") VALUES (";
113 $sql .= " ".(!isset($this->id) ? 'NULL' : (int) $this->id).",";
114 $sql .= " ".(!isset($this->code_region) ? 'NULL' : (int) $this->code_region).",";
115 $sql .= " ".(!isset($this->fk_pays) ? 'NULL' : (int) $this->fk_pays).",";
116 $sql .= " ".(!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name)."'").",";
117 $sql .= " ".(!isset($this->cheflieu) ? 'NULL' : "'".$this->db->escape($this->cheflieu)."'").",";
118 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
119 $sql .= ")";
120
121 $this->db->begin();
122
123 dol_syslog(get_class($this)."::create", LOG_DEBUG);
124 $resql = $this->db->query($sql);
125 if (!$resql) {
126 $error++;
127 $this->errors[] = "Error ".$this->db->lasterror();
128 }
129
130 if (!$error) {
131 $this->id = $this->db->last_insert_id($this->db->prefix()."c_regions");
132 }
133
134 // Commit or rollback
135 if ($error) {
136 foreach ($this->errors as $errmsg) {
137 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
138 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
139 }
140 $this->db->rollback();
141 return -1 * $error;
142 } else {
143 $this->db->commit();
144 return $this->id;
145 }
146 }
147
148
157 public function fetch($id, $code_region = 0, $fk_pays = 0)
158 {
159 $sql = "SELECT";
160 $sql .= " t.rowid,";
161 $sql .= " t.code_region,";
162 $sql .= " t.fk_pays,";
163 $sql .= " t.nom,";
164 $sql .= " t.cheflieu,";
165 $sql .= " t.active";
166 $sql .= " FROM ".$this->db->prefix()."c_regions as t";
167 if ($id) {
168 $sql .= " WHERE t.rowid = ".((int) $id);
169 } elseif ($code_region) {
170 $sql .= " WHERE t.code_region = ".((int) $code_region);
171 } elseif ($fk_pays) {
172 $sql .= " WHERE t.fk_pays = ".((int) $fk_pays);
173 }
174
175 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
176 $resql = $this->db->query($sql);
177 if ($resql) {
178 if ($this->db->num_rows($resql)) {
179 $obj = $this->db->fetch_object($resql);
180
181 if ($obj) {
182 $this->id = $obj->rowid;
183 $this->code_region = (int) $obj->code_region;
184 $this->fk_pays = (int) $obj->fk_pays;
185 $this->name = $obj->nom;
186 $this->cheflieu = $obj->cheflieu;
187 $this->active = $obj->active;
188 }
189
190 $this->db->free($resql);
191 return 1;
192 } else {
193 return 0;
194 }
195 } else {
196 $this->error = "Error ".$this->db->lasterror();
197 return -1;
198 }
199 }
200
201
209 public function update($user = null, $notrigger = 0)
210 {
211 global $conf, $langs;
212 $error = 0;
213
214 // Clean parameters
215 if (isset($this->code_region)) {
216 $this->code_region = (int) $this->code_region;
217 }
218 if (isset($this->fk_pays)) {
219 $this->fk_pays = (int) $this->fk_pays;
220 }
221 if (isset($this->name)) {
222 $this->name = trim($this->name);
223 }
224 if (isset($this->cheflieu)) {
225 $this->cheflieu = trim($this->cheflieu);
226 }
227 if (isset($this->active)) {
228 $this->active = (int) $this->active;
229 }
230
231
232 // Check parameters
233 // Put here code to add control on parameters values
234
235 // Update request
236 $sql = "UPDATE ".$this->db->prefix()."c_regions SET";
237 $sql .= " code_region=".(isset($this->code_region) ? ((int) $this->code_region) : "null").",";
238 $sql .= " fk_pays=".(isset($this->fk_pays) ? ((int) $this->fk_pays) : "null").",";
239 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
240 $sql .= " cheflieu=".(isset($this->cheflieu) ? "'".$this->db->escape($this->cheflieu)."'" : "null").",";
241 $sql .= " active=".(isset($this->active) ? $this->active : "null");
242 $sql .= " WHERE rowid=".((int) $this->id);
243
244 $this->db->begin();
245
246 dol_syslog(get_class($this)."::update", LOG_DEBUG);
247 $resql = $this->db->query($sql);
248 if (!$resql) {
249 $error++;
250 $this->errors[] = "Error ".$this->db->lasterror();
251 }
252
253 // Commit or rollback
254 if ($error) {
255 foreach ($this->errors as $errmsg) {
256 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
257 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
258 }
259 $this->db->rollback();
260 return -1 * $error;
261 } else {
262 $this->db->commit();
263 return 1;
264 }
265 }
266
267
275 public function delete($user, $notrigger = 0)
276 {
277 global $conf, $langs;
278 $error = 0;
279
280 $sql = "DELETE FROM ".$this->db->prefix()."c_regions";
281 $sql .= " WHERE rowid=".((int) $this->id);
282
283 $this->db->begin();
284
285 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
286 $resql = $this->db->query($sql);
287 if (!$resql) {
288 $error++;
289 $this->errors[] = "Error ".$this->db->lasterror();
290 }
291
292 // Commit or rollback
293 if ($error) {
294 foreach ($this->errors as $errmsg) {
295 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
296 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
297 }
298 $this->db->rollback();
299 return -1 * $error;
300 } else {
301 $this->db->commit();
302 return 1;
303 }
304 }
305
316 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
317 {
318 global $langs;
319 return $langs->trans($this->name);
320 }
321}
Parent class of all other dictionary classes.
Class to manage dictionary Regions.
create($user, $notrigger=0)
Create object into database.
update($user=null, $notrigger=0)
Update object into database.
fetch($id, $code_region=0, $fk_pays=0)
Load object in memory from database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:140