dolibarr 21.0.0-beta
ctypent.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 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 Ctypent extends CommonDict
33{
37 public $country_id;
38
42 public $libelle;
46 public $module;
47
53 public function __construct($db)
54 {
55 $this->db = $db;
56 }
57
58
66 public function create($user, $notrigger = 0)
67 {
68 global $conf, $langs;
69 $error = 0;
70
71 // Clean parameters
72
73 if (isset($this->id)) {
74 $this->id = (int) $this->id;
75 }
76 if (isset($this->code)) {
77 $this->code = trim($this->code);
78 }
79 if (isset($this->libelle)) {
80 $this->libelle = trim($this->libelle);
81 }
82 if (isset($this->active)) {
83 $this->active = (int) $this->active;
84 }
85 if (isset($this->module)) {
86 $this->module = trim($this->module);
87 }
88
89 // Check parameters
90 // Put here code to add control on parameters values
91
92 // Insert request
93 $sql = "INSERT INTO ".$this->db->prefix()."c_typent(";
94 $sql .= "id,";
95 $sql .= "code,";
96 $sql .= "libelle,";
97 $sql .= "active,";
98 $sql .= "module";
99 $sql .= ") VALUES (";
100 $sql .= " ".(!isset($this->id) ? 'NULL' : "'".$this->db->escape($this->id)."'").",";
101 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
102 $sql .= " ".(!isset($this->libelle) ? 'NULL' : "'".$this->db->escape($this->libelle)."'").",";
103 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'").",";
104 $sql .= " ".(!isset($this->module) ? 'NULL' : "'".$this->db->escape($this->module)."'");
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_typent");
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
143 public function fetch($id, $code = '', $label = '')
144 {
145 $sql = "SELECT";
146 $sql .= " t.id,";
147 $sql .= " t.code,";
148 $sql .= " t.libelle as label,";
149 $sql .= " t.fk_country as country_id,";
150 $sql .= " t.active,";
151 $sql .= " t.module";
152 $sql .= " FROM ".$this->db->prefix()."c_typent as t";
153 if ($id) {
154 $sql .= " WHERE t.id = ".((int) $id);
155 } elseif ($code) {
156 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
157 } elseif ($label) {
158 $sql .= " WHERE t.libelle = '".$this->db->escape($label)."'";
159 }
160
161 $resql = $this->db->query($sql);
162 if ($resql) {
163 if ($this->db->num_rows($resql)) {
164 $obj = $this->db->fetch_object($resql);
165
166 $this->id = $obj->id;
167 $this->code = $obj->code;
168 $this->libelle = $obj->label;
169 $this->country_id = $obj->country_id;
170 $this->active = $obj->active;
171 $this->module = $obj->module;
172 }
173 $this->db->free($resql);
174
175 return 1;
176 } else {
177 $this->error = "Error ".$this->db->lasterror();
178 return -1;
179 }
180 }
181
182
190 public function update($user = null, $notrigger = 0)
191 {
192 global $conf, $langs;
193 $error = 0;
194
195 // Clean parameters
196 if (isset($this->code)) {
197 $this->code = trim($this->code);
198 }
199 if (isset($this->libelle)) {
200 $this->libelle = trim($this->libelle);
201 }
202 if (isset($this->active)) {
203 $this->active = (int) $this->active;
204 }
205 if (isset($this->module)) {
206 $this->module = trim($this->module);
207 }
208
209
210 // Check parameters
211 // Put here code to add control on parameters values
212
213 // Update request
214 $sql = "UPDATE ".$this->db->prefix()."c_typent SET";
215 $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
216 $sql .= " libelle=".(isset($this->libelle) ? "'".$this->db->escape($this->libelle)."'" : "null").",";
217 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null").",";
218 $sql .= " module=".(isset($this->module) ? "'".$this->db->escape($this->module)."'" : "null");
219 $sql .= " WHERE id=".$this->id;
220
221 $this->db->begin();
222
223 dol_syslog(get_class($this)."::update", LOG_DEBUG);
224 $resql = $this->db->query($sql);
225 if (!$resql) {
226 $error++;
227 $this->errors[] = "Error ".$this->db->lasterror();
228 }
229
230 // Commit or rollback
231 if ($error) {
232 foreach ($this->errors as $errmsg) {
233 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
234 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
235 }
236 $this->db->rollback();
237 return -1 * $error;
238 } else {
239 $this->db->commit();
240 return 1;
241 }
242 }
243
244
252 public function delete($user, $notrigger = 0)
253 {
254 global $conf, $langs;
255 $error = 0;
256
257 $sql = "DELETE FROM ".$this->db->prefix()."c_typent";
258 $sql .= " WHERE id = ".$this->id;
259
260 $this->db->begin();
261
262 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
263 $resql = $this->db->query($sql);
264 if (!$resql) {
265 $error++;
266 $this->errors[] = "Error ".$this->db->lasterror();
267 }
268
269 // Commit or rollback
270 if ($error) {
271 foreach ($this->errors as $errmsg) {
272 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
273 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
274 }
275 $this->db->rollback();
276 return -1 * $error;
277 } else {
278 $this->db->commit();
279 return 1;
280 }
281 }
282}
Parent class of all other dictionary classes.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79