dolibarr 21.0.0-alpha
cstate.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24// Put here all includes required by your class file
25require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
26
27
31class Cstate extends CommonDict
32{
36 public $rowid;
37
42 public $code_departement;
43
47 public $name = '';
48
54 public $nom = '';
55
56
62 public function __construct($db)
63 {
64 $this->db = $db;
65 }
66
67
75 public function create($user, $notrigger = 0)
76 {
77 $error = 0;
78
79 // Clean parameters
80 if (isset($this->code_departement)) {
81 $this->code_departement = trim($this->code_departement);
82 }
83 if (isset($this->nom)) {
84 $this->nom = trim($this->nom);
85 }
86 if (isset($this->active)) {
87 $this->active = (int) $this->active;
88 }
89
90 // Check parameters
91 // Put here code to add control on parameters values
92
93 // Insert request
94 $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
95 $sql .= "rowid,";
96 $sql .= "code_departement,";
97 $sql .= "nom,";
98 $sql .= "active";
99 $sql .= ") VALUES (";
100 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
101 $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
102 $sql .= " ".(!isset($this->nom) ? 'NULL' : "'".$this->db->escape($this->nom)."'").",";
103 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
104 $sql .= ")";
105
106 $this->db->begin();
107
108 dol_syslog(get_class($this)."::create", LOG_DEBUG);
109 $resql = $this->db->query($sql);
110 if (!$resql) {
111 $error++;
112 $this->errors[] = "Error ".$this->db->lasterror();
113 }
114
115 if (!$error) {
116 $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
117 }
118
119 // Commit or rollback
120 if ($error) {
121 foreach ($this->errors as $errmsg) {
122 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
123 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
124 }
125 $this->db->rollback();
126 return -1 * $error;
127 } else {
128 $this->db->commit();
129 return $this->id;
130 }
131 }
132
133
141 public function fetch($id, $code = '')
142 {
143 $sql = "SELECT";
144 $sql .= " t.rowid,";
145 $sql .= " t.code_departement,";
146 $sql .= " t.nom,";
147 $sql .= " t.active";
148 $sql .= " FROM ".$this->db->prefix()."c_departements as t";
149 if ($id) {
150 $sql .= " WHERE t.rowid = ".((int) $id);
151 } elseif ($code) {
152 $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
153 }
154
155 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
156 $resql = $this->db->query($sql);
157 if ($resql) {
158 if ($this->db->num_rows($resql)) {
159 $obj = $this->db->fetch_object($resql);
160
161 $this->id = $obj->rowid;
162 $this->code_departement = $obj->code_departement; //deprecated
163 $this->code = $obj->code_departement;
164 $this->nom = $obj->nom; //deprecated
165 $this->name = $obj->nom;
166 $this->active = $obj->active;
167 }
168 $this->db->free($resql);
169
170 return 1;
171 } else {
172 $this->error = "Error ".$this->db->lasterror();
173 return -1;
174 }
175 }
176
177
185 public function update($user = null, $notrigger = 0)
186 {
187 $error = 0;
188
189 // Clean parameters
190 if (isset($this->code_departement)) {
191 $this->code_departement = trim($this->code_departement);
192 }
193 if (isset($this->name)) {
194 $this->name = trim($this->name);
195 }
196 if (isset($this->active)) {
197 $this->active = (int) $this->active;
198 }
199
200 // Check parameters
201 if (empty($this->name) && !empty($this->nom)) {
202 $this->name = $this->nom;
203 }
204
205 // Update request
206 $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
207 $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
208 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
209 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
210 $sql .= " WHERE rowid=".((int) $this->id);
211
212 $this->db->begin();
213
214 dol_syslog(get_class($this)."::update", LOG_DEBUG);
215 $resql = $this->db->query($sql);
216 if (!$resql) {
217 $error++;
218 $this->errors[] = "Error ".$this->db->lasterror();
219 }
220
221 // Commit or rollback
222 if ($error) {
223 foreach ($this->errors as $errmsg) {
224 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
225 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
226 }
227 $this->db->rollback();
228 return -1 * $error;
229 } else {
230 $this->db->commit();
231 return 1;
232 }
233 }
234
242 public function delete($user, $notrigger = 0)
243 {
244 $error = 0;
245
246 $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
247 $sql .= " WHERE rowid=".((int) $this->id);
248
249 $this->db->begin();
250
251 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
252 $resql = $this->db->query($sql);
253 if (!$resql) {
254 $error++;
255 $this->errors[] = "Error ".$this->db->lasterror();
256 }
257
258 // Commit or rollback
259 if ($error) {
260 foreach ($this->errors as $errmsg) {
261 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
262 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
263 }
264 $this->db->rollback();
265 return -1 * $error;
266 } else {
267 $this->db->commit();
268 return 1;
269 }
270 }
271
282 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
283 {
284 global $langs;
285 return $langs->trans($this->name);
286 }
287}
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:140