dolibarr 18.0.6
cregion.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) Richard Rondu <rondu.richard@lainwir3d.net>
3 * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
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
29{
33 public $db;
34
38 public $error = '';
39
43 public $errors = array();
44
45 //public $element = 'cregion'; //!< Id that identify managed objects
46 //public $table_element = 'c_regions'; //!< Name of table without prefix where object is stored
47
51 public $id;
52
53 public $code_region;
54 public $fk_pays;
55
59 public $name;
60
64 public $cheflieu;
65
66 public $active;
67
73 public function __construct($db)
74 {
75 $this->db = $db;
76 }
77
78
86 public function create($user, $notrigger = 0)
87 {
88 global $conf, $langs;
89 $error = 0;
90
91 // Clean parameters
92 if (isset($this->code_region)) {
93 $this->code_region = trim($this->code_region);
94 }
95 if (isset($this->fk_pays)) {
96 $this->fk_pays = trim($this->fk_pays);
97 }
98 if (isset($this->nom)) {
99 $this->nom = trim($this->nom);
100 }
101 if (isset($this->cheflieu)) {
102 $this->cheflieu = trim($this->cheflieu);
103 }
104 if (isset($this->active)) {
105 $this->active = trim($this->active);
106 }
107
108 // Check parameters
109 // Put here code to add control on parameters values
110
111 // Insert request
112 $sql = "INSERT INTO ".$this->db->prefix()."c_regions(";
113 $sql .= "rowid,";
114 $sql .= "code_region,";
115 $sql .= "fk_pays,";
116 $sql .= "nom,";
117 $sql .= "cheflieu,";
118 $sql .= "active";
119 $sql .= ") VALUES (";
120 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
121 $sql .= " ".(!isset($this->code_region) ? 'NULL' : "'".$this->db->escape($this->code_region)."'").",";
122 $sql .= " ".(!isset($this->fk_pays) ? 'NULL' : "'".$this->db->escape($this->fk_pays)."'").",";
123 $sql .= " ".(!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name)."'").",";
124 $sql .= " ".(!isset($this->cheflieu) ? 'NULL' : "'".$this->db->escape($this->cheflieu)."'").",";
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_regions");
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
164 public function fetch($id, $code_region = '', $fk_pays = '')
165 {
166 $sql = "SELECT";
167 $sql .= " t.rowid,";
168 $sql .= " t.code_region,";
169 $sql .= " t.fk_pays,";
170 $sql .= " t.nom,";
171 $sql .= " t.cheflieu,";
172 $sql .= " t.active";
173 $sql .= " FROM ".$this->db->prefix()."c_regions as t";
174 if ($id) {
175 $sql .= " WHERE t.rowid = ".((int) $id);
176 } elseif ($code_region) {
177 $sql .= " WHERE t.code_region = '".$this->db->escape(strtoupper($code_region))."'";
178 } elseif ($fk_pays) {
179 $sql .= " WHERE t.fk_pays = '".$this->db->escape(strtoupper($fk_pays))."'";
180 }
181
182 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
183 $resql = $this->db->query($sql);
184 if ($resql) {
185 if ($this->db->num_rows($resql)) {
186 $obj = $this->db->fetch_object($resql);
187
188 if ($obj) {
189 $this->id = $obj->rowid;
190 $this->code_region = $obj->code_region;
191 $this->fk_pays = $obj->fk_pays;
192 $this->name = $obj->nom;
193 $this->cheflieu = $obj->cheflieu;
194 $this->active = $obj->active;
195 }
196
197 $this->db->free($resql);
198 return 1;
199 } else {
200 return 0;
201 }
202 } else {
203 $this->error = "Error ".$this->db->lasterror();
204 return -1;
205 }
206 }
207
208
216 public function update($user = null, $notrigger = 0)
217 {
218 global $conf, $langs;
219 $error = 0;
220
221 // Clean parameters
222 if (isset($this->code_region)) {
223 $this->code_region = trim($this->code_region);
224 }
225 if (isset($this->fk_pays)) {
226 $this->fk_pays = trim($this->fk_pays);
227 }
228 if (isset($this->name)) {
229 $this->name = trim($this->name);
230 }
231 if (isset($this->cheflieu)) {
232 $this->cheflieu = trim($this->cheflieu);
233 }
234 if (isset($this->active)) {
235 $this->active = trim($this->active);
236 }
237
238
239 // Check parameters
240 // Put here code to add control on parameters values
241
242 // Update request
243 $sql = "UPDATE ".$this->db->prefix()."c_regions SET";
244 $sql .= " code_region=".(isset($this->code_region) ? "'".$this->db->escape($this->code_region)."'" : "null").",";
245 $sql .= " fk_pays=".(isset($this->fk_pays) ? "'".$this->db->escape($this->fk_pays)."'" : "null").",";
246 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
247 $sql .= " cheflieu=".(isset($this->cheflieu) ? "'".$this->db->escape($this->cheflieu)."'" : "null").",";
248 $sql .= " active=".(isset($this->active) ? $this->active : "null");
249 $sql .= " WHERE rowid=".((int) $this->id);
250
251 $this->db->begin();
252
253 dol_syslog(get_class($this)."::update", LOG_DEBUG);
254 $resql = $this->db->query($sql);
255 if (!$resql) {
256 $error++;
257 $this->errors[] = "Error ".$this->db->lasterror();
258 }
259
260 // Commit or rollback
261 if ($error) {
262 foreach ($this->errors as $errmsg) {
263 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
264 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
265 }
266 $this->db->rollback();
267 return -1 * $error;
268 } else {
269 $this->db->commit();
270 return 1;
271 }
272 }
273
274
282 public function delete($user, $notrigger = 0)
283 {
284 global $conf, $langs;
285 $error = 0;
286
287 $sql = "DELETE FROM ".$this->db->prefix()."c_regions";
288 $sql .= " WHERE rowid=".((int) $this->id);
289
290 $this->db->begin();
291
292 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
293 $resql = $this->db->query($sql);
294 if (!$resql) {
295 $error++;
296 $this->errors[] = "Error ".$this->db->lasterror();
297 }
298
299 // Commit or rollback
300 if ($error) {
301 foreach ($this->errors as $errmsg) {
302 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
303 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
304 }
305 $this->db->rollback();
306 return -1 * $error;
307 } else {
308 $this->db->commit();
309 return 1;
310 }
311 }
312
323 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
324 {
325 global $langs;
326 return $langs->trans($this->name);
327 }
328}
Class to manage dictionary Regions.
create($user, $notrigger=0)
Create object into database.
update($user=null, $notrigger=0)
Update object into database.
fetch($id, $code_region='', $fk_pays='')
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)
__construct($db)
Constructor.
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