dolibarr 24.0.0-beta
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-2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Put here all includes required by your class file
28require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
29
30
34class Cregion extends CommonDict
35{
36 //public $element = 'cregion'; //!< Id that identify managed objects
37 //public $table_element = 'c_regions'; //!< Name of table without prefix where object is stored
38
42 public $code_region;
43
47 public $fk_pays;
48
52 public $name;
53
59 public $cheflieu;
60
61
67 public function __construct($db)
68 {
69 $this->db = $db;
70 }
71
72
80 public function create($user, $notrigger = 0)
81 {
82 global $conf, $langs;
83 $error = 0;
84
85 // Clean parameters
86 if (isset($this->code_region)) {
87 $this->code_region = (int) $this->code_region;
88 }
89 if (isset($this->fk_pays)) {
90 $this->fk_pays = (int) $this->fk_pays;
91 }
92 if (isset($this->name)) {
93 $this->name = trim((string) $this->name);
94 }
95 if (isset($this->cheflieu)) {
96 $this->cheflieu = trim((string) $this->cheflieu);
97 }
98 if (isset($this->active)) {
99 $this->active = (int) $this->active;
100 }
101
102 // Check parameters
103 // Put here code to add control on parameters values
104
105 // Insert request
106 $sql = "INSERT INTO ".$this->db->prefix()."c_regions(";
107 $sql .= "rowid,";
108 $sql .= "code_region,";
109 $sql .= "fk_pays,";
110 $sql .= "nom,";
111 $sql .= "cheflieu,";
112 $sql .= "active";
113 $sql .= ") VALUES (";
114 $sql .= (int) $this->id;
115 $sql .= ", " . (!isset($this->code_region) ? 'NULL' : (int) $this->code_region);
116 $sql .= ", " . (int) $this->fk_pays;
117 $sql .= ", " . (!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name) . "'");
118 $sql .= ", " . (!isset($this->cheflieu) ? 'NULL' : "'".$this->db->escape($this->cheflieu) . "'");
119 $sql .= ", " . (int) $this->active;
120 $sql .= ")";
121
122 $this->db->begin();
123
124 dol_syslog(get_class($this)."::create", LOG_DEBUG);
125 $resql = $this->db->query($sql);
126 if (!$resql) {
127 $error++;
128 $this->errors[] = "Error ".$this->db->lasterror();
129 }
130
131 if (!$error) {
132 $this->id = $this->db->last_insert_id($this->db->prefix()."c_regions");
133 }
134
135 // Commit or rollback
136 if ($error) {
137 foreach ($this->errors as $errmsg) {
138 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
139 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
140 }
141 $this->db->rollback();
142 return -1 * $error;
143 } else {
144 $this->db->commit();
145 return $this->id;
146 }
147 }
148
149
158 public function fetch($id, $code_region = 0, $fk_pays = 0)
159 {
160 $sql = "SELECT";
161 $sql .= " t.rowid,";
162 $sql .= " t.code_region,";
163 $sql .= " t.fk_pays,";
164 $sql .= " t.nom as name,";
165 $sql .= " t.cheflieu,";
166 $sql .= " t.active";
167 $sql .= " FROM ".$this->db->prefix()."c_regions as t";
168 if ($id) {
169 $sql .= " WHERE t.rowid = ".((int) $id);
170 } elseif ($code_region) {
171 $sql .= " WHERE t.code_region = ".((int) $code_region);
172 } elseif ($fk_pays) {
173 $sql .= " WHERE t.fk_pays = ".((int) $fk_pays);
174 }
175
176 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
177 $resql = $this->db->query($sql);
178 if ($resql) {
179 if ($this->db->num_rows($resql)) {
180 $obj = $this->db->fetch_object($resql);
181
182 if ($obj) {
183 $this->id = $obj->rowid;
184 $this->code_region = (int) $obj->code_region;
185 $this->fk_pays = (int) $obj->fk_pays;
186 $this->name = $obj->name;
187 $this->cheflieu = $obj->cheflieu;
188 $this->active = (int) $obj->active;
189 }
190
191 $this->db->free($resql);
192 return 1;
193 } else {
194 return 0;
195 }
196 } else {
197 $this->error = "Error ".$this->db->lasterror();
198 return -1;
199 }
200 }
201
202
210 public function update($user = null, $notrigger = 0)
211 {
212 global $conf, $langs;
213 $error = 0;
214
215 // Clean parameters
216 if (isset($this->code_region)) {
217 $this->code_region = (int) $this->code_region;
218 }
219 if (isset($this->fk_pays)) {
220 $this->fk_pays = (int) $this->fk_pays;
221 }
222 if (isset($this->name)) {
223 $this->name = trim($this->name);
224 }
225 if (isset($this->cheflieu)) {
226 $this->cheflieu = trim($this->cheflieu);
227 }
228 if (isset($this->active)) {
229 $this->active = (int) $this->active;
230 }
231
232
233 // Check parameters
234 // Put here code to add control on parameters values
235
236 // Update request
237 $sql = "UPDATE ".$this->db->prefix()."c_regions SET";
238 $sql .= " code_region=".(isset($this->code_region) ? ((int) $this->code_region) : "null").",";
239 $sql .= " fk_pays=".(isset($this->fk_pays) ? ((int) $this->fk_pays) : "null").",";
240 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
241 $sql .= " cheflieu=".(isset($this->cheflieu) ? "'".$this->db->escape($this->cheflieu)."'" : "null").",";
242 $sql .= " active=".(isset($this->active) ? $this->active : "null");
243 $sql .= " WHERE rowid=".((int) $this->id);
244
245 $this->db->begin();
246
247 dol_syslog(get_class($this)."::update", LOG_DEBUG);
248 $resql = $this->db->query($sql);
249 if (!$resql) {
250 $error++;
251 $this->errors[] = "Error ".$this->db->lasterror();
252 }
253
254 // Commit or rollback
255 if ($error) {
256 foreach ($this->errors as $errmsg) {
257 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
258 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
259 }
260 $this->db->rollback();
261 return -1 * $error;
262 } else {
263 $this->db->commit();
264 return 1;
265 }
266 }
267
268
276 public function delete($user, $notrigger = 0)
277 {
278 global $conf, $langs;
279 $error = 0;
280
281 $sql = "DELETE FROM ".$this->db->prefix()."c_regions";
282 $sql .= " WHERE rowid=".((int) $this->id);
283
284 $this->db->begin();
285
286 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
287 $resql = $this->db->query($sql);
288 if (!$resql) {
289 $error++;
290 $this->errors[] = "Error ".$this->db->lasterror();
291 }
292
293 // Commit or rollback
294 if ($error) {
295 foreach ($this->errors as $errmsg) {
296 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
297 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
298 }
299 $this->db->rollback();
300 return -1 * $error;
301 } else {
302 $this->db->commit();
303 return 1;
304 }
305 }
306
317 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
318 {
319 global $langs;
320 return $langs->trans($this->name);
321 }
322}
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
editval_textarea active
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133