dolibarr 23.0.3
cstate.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
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 Cstate extends CommonDict
34{
40 public $code_departement;
41
45 public $name = '';
46
52 public $nom = '';
53
54
60 public function __construct($db)
61 {
62 $this->db = $db;
63 }
64
65
73 public function create($user, $notrigger = 0)
74 {
75 $error = 0;
76
77 if (empty($this->id)) {
78 return -1;
79 }
80 // Clean parameters
81 if (isset($this->code_departement)) {
82 $this->code_departement = trim($this->code_departement);
83 }
84 if (isset($this->name)) {
85 $this->name = trim($this->name);
86 }
87 if (isset($this->active)) {
88 $this->active = (int) $this->active;
89 }
90
91 // Insert request
92 $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
93 $sql .= "rowid,";
94 $sql .= "code_departement,";
95 $sql .= "nom,";
96 $sql .= "active";
97 $sql .= ") VALUES (";
98 $sql .= (int) $this->id . ",";
99 $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
100 $sql .= " ".(!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name)."'").",";
101 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape((string) $this->active)."'");
102 $sql .= ")";
103
104 $this->db->begin();
105
106 dol_syslog(get_class($this)."::create", LOG_DEBUG);
107 $resql = $this->db->query($sql);
108 if (!$resql) {
109 $error++;
110 $this->errors[] = "Error ".$this->db->lasterror();
111 }
112
113 if (!$error) {
114 $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
115 }
116
117 // Commit or rollback
118 if ($error) {
119 foreach ($this->errors as $errmsg) {
120 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
121 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
122 }
123 $this->db->rollback();
124 return -1 * $error;
125 } else {
126 $this->db->commit();
127 return $this->id;
128 }
129 }
130
131
139 public function fetch($id, $code = '')
140 {
141 $sql = "SELECT";
142 $sql .= " t.rowid,";
143 $sql .= " t.code_departement,";
144 $sql .= " t.nom,";
145 $sql .= " t.active";
146 $sql .= " FROM ".$this->db->prefix()."c_departements as t";
147 if ($id) {
148 $sql .= " WHERE t.rowid = ".((int) $id);
149 } elseif ($code) {
150 $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
151 }
152
153 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
154 $resql = $this->db->query($sql);
155 if ($resql) {
156 if ($this->db->num_rows($resql)) {
157 $obj = $this->db->fetch_object($resql);
158
159 $this->id = $obj->rowid;
160 $this->code_departement = $obj->code_departement; //deprecated
161 $this->code = $obj->code_departement;
162 $this->nom = $obj->nom; //deprecated
163 $this->name = $obj->nom;
164 $this->active = $obj->active;
165 }
166 $this->db->free($resql);
167
168 return 1;
169 } else {
170 $this->error = "Error ".$this->db->lasterror();
171 return -1;
172 }
173 }
174
175
183 public function update($user = null, $notrigger = 0)
184 {
185 $error = 0;
186
187 // Clean parameters
188 if (isset($this->code_departement)) {
189 $this->code_departement = trim($this->code_departement);
190 }
191 if (isset($this->name)) {
192 $this->name = trim($this->name);
193 }
194 if (isset($this->active)) {
195 $this->active = (int) $this->active;
196 }
197
198 // Check parameters
199 if (empty($this->name) && !empty($this->nom)) {
200 $this->name = $this->nom;
201 }
202
203 // Update request
204 $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
205 $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
206 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
207 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
208 $sql .= " WHERE rowid=".((int) $this->id);
209
210 $this->db->begin();
211
212 dol_syslog(get_class($this)."::update", LOG_DEBUG);
213 $resql = $this->db->query($sql);
214 if (!$resql) {
215 $error++;
216 $this->errors[] = "Error ".$this->db->lasterror();
217 }
218
219 // Commit or rollback
220 if ($error) {
221 foreach ($this->errors as $errmsg) {
222 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
223 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
224 }
225 $this->db->rollback();
226 return -1 * $error;
227 } else {
228 $this->db->commit();
229 return 1;
230 }
231 }
232
240 public function delete($user, $notrigger = 0)
241 {
242 $error = 0;
243
244 $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
245 $sql .= " WHERE rowid=".((int) $this->id);
246
247 $this->db->begin();
248
249 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
250 $resql = $this->db->query($sql);
251 if (!$resql) {
252 $error++;
253 $this->errors[] = "Error ".$this->db->lasterror();
254 }
255
256 // Commit or rollback
257 if ($error) {
258 foreach ($this->errors as $errmsg) {
259 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
260 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
261 }
262 $this->db->rollback();
263 return -1 * $error;
264 } else {
265 $this->db->commit();
266 return 1;
267 }
268 }
269
280 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
281 {
282 global $langs;
283 return $langs->trans($this->name);
284 }
285}
Parent class of all other dictionary classes.
Class to manage dictionary States (used by imports)
fetch($id, $code='')
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)
create($user, $notrigger=0)
Create object into database.
__construct($db)
Constructor.
update($user=null, $notrigger=0)
Update object into database.
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:128