dolibarr 22.0.5
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 *
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 Cstate extends CommonDict
33{
37 public $rowid;
38
43 public $code_departement;
44
48 public $name = '';
49
55 public $nom = '';
56
57
63 public function __construct($db)
64 {
65 $this->db = $db;
66 }
67
68
76 public function create($user, $notrigger = 0)
77 {
78 $error = 0;
79
80 // Clean parameters
81 if (isset($this->code_departement)) {
82 $this->code_departement = trim($this->code_departement);
83 }
84 if (isset($this->nom)) {
85 $this->nom = trim($this->nom);
86 }
87 if (isset($this->active)) {
88 $this->active = (int) $this->active;
89 }
90
91 // Check parameters
92 // Put here code to add control on parameters values
93
94 // Insert request
95 $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
96 $sql .= "rowid,";
97 $sql .= "code_departement,";
98 $sql .= "nom,";
99 $sql .= "active";
100 $sql .= ") VALUES (";
101 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape((string) $this->rowid)."'").",";
102 $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
103 $sql .= " ".(!isset($this->nom) ? 'NULL' : "'".$this->db->escape($this->nom)."'").",";
104 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape((string) $this->active)."'");
105 $sql .= ")";
106
107 $this->db->begin();
108
109 dol_syslog(get_class($this)."::create", LOG_DEBUG);
110 $resql = $this->db->query($sql);
111 if (!$resql) {
112 $error++;
113 $this->errors[] = "Error ".$this->db->lasterror();
114 }
115
116 if (!$error) {
117 $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
118 }
119
120 // Commit or rollback
121 if ($error) {
122 foreach ($this->errors as $errmsg) {
123 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
124 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
125 }
126 $this->db->rollback();
127 return -1 * $error;
128 } else {
129 $this->db->commit();
130 return $this->id;
131 }
132 }
133
134
142 public function fetch($id, $code = '')
143 {
144 $sql = "SELECT";
145 $sql .= " t.rowid,";
146 $sql .= " t.code_departement,";
147 $sql .= " t.nom,";
148 $sql .= " t.active";
149 $sql .= " FROM ".$this->db->prefix()."c_departements as t";
150 if ($id) {
151 $sql .= " WHERE t.rowid = ".((int) $id);
152 } elseif ($code) {
153 $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
154 }
155
156 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
157 $resql = $this->db->query($sql);
158 if ($resql) {
159 if ($this->db->num_rows($resql)) {
160 $obj = $this->db->fetch_object($resql);
161
162 $this->id = $obj->rowid;
163 $this->code_departement = $obj->code_departement; //deprecated
164 $this->code = $obj->code_departement;
165 $this->nom = $obj->nom; //deprecated
166 $this->name = $obj->nom;
167 $this->active = $obj->active;
168 }
169 $this->db->free($resql);
170
171 return 1;
172 } else {
173 $this->error = "Error ".$this->db->lasterror();
174 return -1;
175 }
176 }
177
178
186 public function update($user = null, $notrigger = 0)
187 {
188 $error = 0;
189
190 // Clean parameters
191 if (isset($this->code_departement)) {
192 $this->code_departement = trim($this->code_departement);
193 }
194 if (isset($this->name)) {
195 $this->name = trim($this->name);
196 }
197 if (isset($this->active)) {
198 $this->active = (int) $this->active;
199 }
200
201 // Check parameters
202 if (empty($this->name) && !empty($this->nom)) {
203 $this->name = $this->nom;
204 }
205
206 // Update request
207 $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
208 $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
209 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
210 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
211 $sql .= " WHERE rowid=".((int) $this->id);
212
213 $this->db->begin();
214
215 dol_syslog(get_class($this)."::update", LOG_DEBUG);
216 $resql = $this->db->query($sql);
217 if (!$resql) {
218 $error++;
219 $this->errors[] = "Error ".$this->db->lasterror();
220 }
221
222 // Commit or rollback
223 if ($error) {
224 foreach ($this->errors as $errmsg) {
225 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
226 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
227 }
228 $this->db->rollback();
229 return -1 * $error;
230 } else {
231 $this->db->commit();
232 return 1;
233 }
234 }
235
243 public function delete($user, $notrigger = 0)
244 {
245 $error = 0;
246
247 $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
248 $sql .= " WHERE rowid=".((int) $this->id);
249
250 $this->db->begin();
251
252 dol_syslog(get_class($this)."::delete", 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)."::delete ".$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
283 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
284 {
285 global $langs;
286 return $langs->trans($this->name);
287 }
288}
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.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:161