dolibarr 19.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 *
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
38 public $code_departement;
39
43 public $name = '';
44
50 public $nom = '';
51
52
58 public function __construct($db)
59 {
60 $this->db = $db;
61 }
62
63
71 public function create($user, $notrigger = 0)
72 {
73 $error = 0;
74
75 // Clean parameters
76 if (isset($this->code_departement)) {
77 $this->code_departement = trim($this->code_departement);
78 }
79 if (isset($this->nom)) {
80 $this->nom = trim($this->nom);
81 }
82 if (isset($this->active)) {
83 $this->active = trim($this->active);
84 }
85
86 // Check parameters
87 // Put here code to add control on parameters values
88
89 // Insert request
90 $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
91 $sql .= "rowid,";
92 $sql .= "code_departement,";
93 $sql .= "nom,";
94 $sql .= "active";
95 $sql .= ") VALUES (";
96 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
97 $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
98 $sql .= " ".(!isset($this->nom) ? 'NULL' : "'".$this->db->escape($this->nom)."'").",";
99 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
100 $sql .= ")";
101
102 $this->db->begin();
103
104 dol_syslog(get_class($this)."::create", LOG_DEBUG);
105 $resql = $this->db->query($sql);
106 if (!$resql) {
107 $error++;
108 $this->errors[] = "Error ".$this->db->lasterror();
109 }
110
111 if (!$error) {
112 $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
113 }
114
115 // Commit or rollback
116 if ($error) {
117 foreach ($this->errors as $errmsg) {
118 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
119 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
120 }
121 $this->db->rollback();
122 return -1 * $error;
123 } else {
124 $this->db->commit();
125 return $this->id;
126 }
127 }
128
129
137 public function fetch($id, $code = '')
138 {
139 $sql = "SELECT";
140 $sql .= " t.rowid,";
141 $sql .= " t.code_departement,";
142 $sql .= " t.nom,";
143 $sql .= " t.active";
144 $sql .= " FROM ".$this->db->prefix()."c_departements as t";
145 if ($id) {
146 $sql .= " WHERE t.rowid = ".((int) $id);
147 } elseif ($code) {
148 $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
149 }
150
151 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
152 $resql = $this->db->query($sql);
153 if ($resql) {
154 if ($this->db->num_rows($resql)) {
155 $obj = $this->db->fetch_object($resql);
156
157 $this->id = $obj->rowid;
158 $this->code_departement = $obj->code_departement; //deprecated
159 $this->code = $obj->code_departement;
160 $this->nom = $obj->nom; //deprecated
161 $this->name = $obj->nom;
162 $this->active = $obj->active;
163 }
164 $this->db->free($resql);
165
166 return 1;
167 } else {
168 $this->error = "Error ".$this->db->lasterror();
169 return -1;
170 }
171 }
172
173
181 public function update($user = null, $notrigger = 0)
182 {
183 $error = 0;
184
185 // Clean parameters
186 if (isset($this->code_departement)) {
187 $this->code_departement = trim($this->code_departement);
188 }
189 if (isset($this->name)) {
190 $this->name = trim($this->name);
191 }
192 if (isset($this->active)) {
193 $this->active = trim($this->active);
194 }
195
196 // Check parameters
197 if (empty($this->name) && !empty($this->nom)) {
198 $this->name = $this->nom;
199 }
200
201 // Update request
202 $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
203 $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
204 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
205 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
206 $sql .= " WHERE rowid=".((int) $this->id);
207
208 $this->db->begin();
209
210 dol_syslog(get_class($this)."::update", LOG_DEBUG);
211 $resql = $this->db->query($sql);
212 if (!$resql) {
213 $error++;
214 $this->errors[] = "Error ".$this->db->lasterror();
215 }
216
217 // Commit or rollback
218 if ($error) {
219 foreach ($this->errors as $errmsg) {
220 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
221 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
222 }
223 $this->db->rollback();
224 return -1 * $error;
225 } else {
226 $this->db->commit();
227 return 1;
228 }
229 }
230
238 public function delete($user, $notrigger = 0)
239 {
240 $error = 0;
241
242 $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
243 $sql .= " WHERE rowid=".((int) $this->id);
244
245 $this->db->begin();
246
247 dol_syslog(get_class($this)."::delete", 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)."::delete ".$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
278 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
279 {
280 global $langs;
281 return $langs->trans($this->name);
282 }
283}
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 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:1907
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124