dolibarr 18.0.6
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
27class Cstate // extends CommonObject
28{
32 public $db;
33
37 public $error = '';
38
42 public $errors = array();
43
44 //var $element='cstate'; //!< Id that identify managed objects
45 //var $table_element='cstate'; //!< Name of table without prefix where object is stored
46
50 public $id;
51
55 public $rowid;
56
57 public $code_departement;
58 public $code;
59
63 public $name = '';
64
70 public $nom = '';
71
72 public $label;
73
74 public $active;
75
76
77
78
84 public function __construct($db)
85 {
86 $this->db = $db;
87 }
88
89
97 public function create($user, $notrigger = 0)
98 {
99 $error = 0;
100
101 // Clean parameters
102 if (isset($this->code_departement)) {
103 $this->code_departement = trim($this->code_departement);
104 }
105 if (isset($this->nom)) {
106 $this->nom = trim($this->nom);
107 }
108 if (isset($this->active)) {
109 $this->active = trim($this->active);
110 }
111
112 // Check parameters
113 // Put here code to add control on parameters values
114
115 // Insert request
116 $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
117 $sql .= "rowid,";
118 $sql .= "code_departement,";
119 $sql .= "nom,";
120 $sql .= "active";
121 $sql .= ") VALUES (";
122 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
123 $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
124 $sql .= " ".(!isset($this->nom) ? 'NULL' : "'".$this->db->escape($this->nom)."'").",";
125 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
126 $sql .= ")";
127
128 $this->db->begin();
129
130 dol_syslog(get_class($this)."::create", LOG_DEBUG);
131 $resql = $this->db->query($sql);
132 if (!$resql) {
133 $error++;
134 $this->errors[] = "Error ".$this->db->lasterror();
135 }
136
137 if (!$error) {
138 $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
139 }
140
141 // Commit or rollback
142 if ($error) {
143 foreach ($this->errors as $errmsg) {
144 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
145 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
146 }
147 $this->db->rollback();
148 return -1 * $error;
149 } else {
150 $this->db->commit();
151 return $this->id;
152 }
153 }
154
155
163 public function fetch($id, $code = '')
164 {
165 $sql = "SELECT";
166 $sql .= " t.rowid,";
167 $sql .= " t.code_departement,";
168 $sql .= " t.nom,";
169 $sql .= " t.active";
170 $sql .= " FROM ".$this->db->prefix()."c_departements as t";
171 if ($id) {
172 $sql .= " WHERE t.rowid = ".((int) $id);
173 } elseif ($code) {
174 $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
175 }
176
177 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
178 $resql = $this->db->query($sql);
179 if ($resql) {
180 if ($this->db->num_rows($resql)) {
181 $obj = $this->db->fetch_object($resql);
182
183 $this->id = $obj->rowid;
184 $this->code_departement = $obj->code_departement; //deprecated
185 $this->code = $obj->code_departement;
186 $this->nom = $obj->nom; //deprecated
187 $this->name = $obj->nom;
188 $this->active = $obj->active;
189 }
190 $this->db->free($resql);
191
192 return 1;
193 } else {
194 $this->error = "Error ".$this->db->lasterror();
195 return -1;
196 }
197 }
198
199
207 public function update($user = null, $notrigger = 0)
208 {
209 global $conf, $langs;
210 $error = 0;
211
212 // Clean parameters
213 if (isset($this->code_departement)) {
214 $this->code_departement = trim($this->code_departement);
215 }
216 if (isset($this->name)) {
217 $this->name = trim($this->name);
218 }
219 if (isset($this->active)) {
220 $this->active = trim($this->active);
221 }
222
223 // Check parameters
224 if (empty($this->name) && !empty($this->nom)) {
225 $this->name = $this->nom;
226 }
227
228 // Update request
229 $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
230 $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
231 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
232 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
233 $sql .= " WHERE rowid=".((int) $this->id);
234
235 $this->db->begin();
236
237 dol_syslog(get_class($this)."::update", LOG_DEBUG);
238 $resql = $this->db->query($sql);
239 if (!$resql) {
240 $error++;
241 $this->errors[] = "Error ".$this->db->lasterror();
242 }
243
244 // Commit or rollback
245 if ($error) {
246 foreach ($this->errors as $errmsg) {
247 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
248 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
249 }
250 $this->db->rollback();
251 return -1 * $error;
252 } else {
253 $this->db->commit();
254 return 1;
255 }
256 }
257
265 public function delete($user, $notrigger = 0)
266 {
267 global $conf, $langs;
268 $error = 0;
269
270 $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
271 $sql .= " WHERE rowid=".((int) $this->id);
272
273 $this->db->begin();
274
275 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
276 $resql = $this->db->query($sql);
277 if (!$resql) {
278 $error++;
279 $this->errors[] = "Error ".$this->db->lasterror();
280 }
281
282 // Commit or rollback
283 if ($error) {
284 foreach ($this->errors as $errmsg) {
285 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
286 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
287 }
288 $this->db->rollback();
289 return -1 * $error;
290 } else {
291 $this->db->commit();
292 return 1;
293 }
294 }
295
306 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
307 {
308 global $langs;
309 return $langs->trans($this->name);
310 }
311}
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 optionaly 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.
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall right right takeposterminal SELECT e rowid
Definition invoice.php:1632
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:123