dolibarr 18.0.6
ctypent.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2011 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
28class Ctypent // extends CommonObject
29{
33 public $db;
34
38 public $error = '';
39
43 public $errors = array();
44
45 // public $element = 'ctypent'; //!< Id that identify managed objects
46 // public $table_element = 'ctypent'; //!< Name of table without prefix where object is stored
47
51 public $id;
52
56 public $country_id;
57
58 public $code;
59 public $libelle;
60 public $active;
61 public $module;
62
68 public function __construct($db)
69 {
70 $this->db = $db;
71 }
72
73
81 public function create($user, $notrigger = 0)
82 {
83 global $conf, $langs;
84 $error = 0;
85
86 // Clean parameters
87
88 if (isset($this->id)) {
89 $this->id = trim($this->id);
90 }
91 if (isset($this->code)) {
92 $this->code = trim($this->code);
93 }
94 if (isset($this->libelle)) {
95 $this->libelle = trim($this->libelle);
96 }
97 if (isset($this->active)) {
98 $this->active = trim($this->active);
99 }
100 if (isset($this->module)) {
101 $this->module = trim($this->module);
102 }
103
104 // Check parameters
105 // Put here code to add control on parameters values
106
107 // Insert request
108 $sql = "INSERT INTO ".$this->db->prefix()."c_typent(";
109 $sql .= "id,";
110 $sql .= "code,";
111 $sql .= "libelle,";
112 $sql .= "active,";
113 $sql .= "module";
114 $sql .= ") VALUES (";
115 $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
116 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
117 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
118 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
119 $sql .= " ".(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'");
120 $sql .= ")";
121
122 $this->db->begin();
123
124 dol_syslog(get_class($this)."::create", LOG_DEBUG);
125 $resql = $this->db->query($sql);
126 if (!$resql) {
127 $error++;
128 $this->errors[] = "Error ".$this->db->lasterror();
129 }
130
131 if (!$error) {
132 $this->id = $this->db->last_insert_id($this->db->prefix()."c_typent");
133 }
134
135 // Commit or rollback
136 if ($error) {
137 foreach ($this->errors as $errmsg) {
138 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
139 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
140 }
141 $this->db->rollback();
142 return -1 * $error;
143 } else {
144 $this->db->commit();
145 return $this->id;
146 }
147 }
148
149
158 public function fetch($id, $code = '', $label = '')
159 {
160 $sql = "SELECT";
161 $sql .= " t.id,";
162 $sql .= " t.code,";
163 $sql .= " t.libelle as label,";
164 $sql .= " t.fk_country as country_id,";
165 $sql .= " t.active,";
166 $sql .= " t.module";
167 $sql .= " FROM ".$this->db->prefix()."c_typent as t";
168 if ($id) {
169 $sql .= " WHERE t.id = ".((int) $id);
170 } elseif ($code) {
171 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
172 } elseif ($label) {
173 $sql .= " WHERE t.libelle = '".$this->db->escape($label)."'";
174 }
175
176 $resql = $this->db->query($sql);
177 if ($resql) {
178 if ($this->db->num_rows($resql)) {
179 $obj = $this->db->fetch_object($resql);
180
181 $this->id = $obj->id;
182 $this->code = $obj->code;
183 $this->libelle = $obj->label;
184 $this->country_id = $obj->country_id;
185 $this->active = $obj->active;
186 $this->module = $obj->module;
187 }
188 $this->db->free($resql);
189
190 return 1;
191 } else {
192 $this->error = "Error ".$this->db->lasterror();
193 return -1;
194 }
195 }
196
197
205 public function update($user = null, $notrigger = 0)
206 {
207 global $conf, $langs;
208 $error = 0;
209
210 // Clean parameters
211 if (isset($this->code)) {
212 $this->code = trim($this->code);
213 }
214 if (isset($this->libelle)) {
215 $this->libelle = trim($this->libelle);
216 }
217 if (isset($this->active)) {
218 $this->active = trim($this->active);
219 }
220 if (isset($this->module)) {
221 $this->module = trim($this->module);
222 }
223
224
225 // Check parameters
226 // Put here code to add control on parameters values
227
228 // Update request
229 $sql = "UPDATE ".$this->db->prefix()."c_typent SET";
230 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
231 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
232 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null").",";
233 $sql .= " module=".(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null");
234 $sql .= " WHERE id=".$this->id;
235
236 $this->db->begin();
237
238 dol_syslog(get_class($this)."::update", LOG_DEBUG);
239 $resql = $this->db->query($sql);
240 if (!$resql) {
241 $error++;
242 $this->errors[] = "Error ".$this->db->lasterror();
243 }
244
245 // Commit or rollback
246 if ($error) {
247 foreach ($this->errors as $errmsg) {
248 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
249 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
250 }
251 $this->db->rollback();
252 return -1 * $error;
253 } else {
254 $this->db->commit();
255 return 1;
256 }
257 }
258
259
267 public function delete($user, $notrigger = 0)
268 {
269 global $conf, $langs;
270 $error = 0;
271
272 $sql = "DELETE FROM ".$this->db->prefix()."c_typent";
273 $sql .= " WHERE id = ".$this->id;
274
275 $this->db->begin();
276
277 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
278 $resql = $this->db->query($sql);
279 if (!$resql) {
280 $error++;
281 $this->errors[] = "Error ".$this->db->lasterror();
282 }
283
284 // Commit or rollback
285 if ($error) {
286 foreach ($this->errors as $errmsg) {
287 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
288 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
289 }
290 $this->db->rollback();
291 return -1 * $error;
292 } else {
293 $this->db->commit();
294 return 1;
295 }
296 }
297}
Class of dictionary type of thirdparty (used by imports)
create($user, $notrigger=0)
Create object into database.
__construct($db)
Constructor.
update($user=null, $notrigger=0)
Update object into database.
fetch($id, $code='', $label='')
Load object in memory from database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.