dolibarr 19.0.3
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
25// Put here all includes required by your class file
26require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
27
28
32class Cregion extends CommonDict
33{
34 //public $element = 'cregion'; //!< Id that identify managed objects
35 //public $table_element = 'c_regions'; //!< Name of table without prefix where object is stored
36
37 public $code_region;
38 public $fk_pays;
39
43 public $name;
44
48 public $cheflieu;
49
50
56 public function __construct($db)
57 {
58 $this->db = $db;
59 }
60
61
69 public function create($user, $notrigger = 0)
70 {
71 global $conf, $langs;
72 $error = 0;
73
74 // Clean parameters
75 if (isset($this->code_region)) {
76 $this->code_region = trim($this->code_region);
77 }
78 if (isset($this->fk_pays)) {
79 $this->fk_pays = trim($this->fk_pays);
80 }
81 if (isset($this->nom)) {
82 $this->nom = trim($this->nom);
83 }
84 if (isset($this->cheflieu)) {
85 $this->cheflieu = trim($this->cheflieu);
86 }
87 if (isset($this->active)) {
88 $this->active = trim($this->active);
89 }
90
91 // Check parameters
92 // Put here code to add control on parameters values
93
94 // Insert request
95 $sql = "INSERT INTO ".$this->db->prefix()."c_regions(";
96 $sql .= "rowid,";
97 $sql .= "code_region,";
98 $sql .= "fk_pays,";
99 $sql .= "nom,";
100 $sql .= "cheflieu,";
101 $sql .= "active";
102 $sql .= ") VALUES (";
103 $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
104 $sql .= " ".(!isset($this->code_region) ? 'NULL' : "'".$this->db->escape($this->code_region)."'").",";
105 $sql .= " ".(!isset($this->fk_pays) ? 'NULL' : "'".$this->db->escape($this->fk_pays)."'").",";
106 $sql .= " ".(!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name)."'").",";
107 $sql .= " ".(!isset($this->cheflieu) ? 'NULL' : "'".$this->db->escape($this->cheflieu)."'").",";
108 $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
109 $sql .= ")";
110
111 $this->db->begin();
112
113 dol_syslog(get_class($this)."::create", LOG_DEBUG);
114 $resql = $this->db->query($sql);
115 if (!$resql) {
116 $error++;
117 $this->errors[] = "Error ".$this->db->lasterror();
118 }
119
120 if (!$error) {
121 $this->id = $this->db->last_insert_id($this->db->prefix()."c_regions");
122 }
123
124 // Commit or rollback
125 if ($error) {
126 foreach ($this->errors as $errmsg) {
127 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
128 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
129 }
130 $this->db->rollback();
131 return -1 * $error;
132 } else {
133 $this->db->commit();
134 return $this->id;
135 }
136 }
137
138
147 public function fetch($id, $code_region = '', $fk_pays = '')
148 {
149 $sql = "SELECT";
150 $sql .= " t.rowid,";
151 $sql .= " t.code_region,";
152 $sql .= " t.fk_pays,";
153 $sql .= " t.nom,";
154 $sql .= " t.cheflieu,";
155 $sql .= " t.active";
156 $sql .= " FROM ".$this->db->prefix()."c_regions as t";
157 if ($id) {
158 $sql .= " WHERE t.rowid = ".((int) $id);
159 } elseif ($code_region) {
160 $sql .= " WHERE t.code_region = '".$this->db->escape(strtoupper($code_region))."'";
161 } elseif ($fk_pays) {
162 $sql .= " WHERE t.fk_pays = '".$this->db->escape(strtoupper($fk_pays))."'";
163 }
164
165 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
166 $resql = $this->db->query($sql);
167 if ($resql) {
168 if ($this->db->num_rows($resql)) {
169 $obj = $this->db->fetch_object($resql);
170
171 if ($obj) {
172 $this->id = $obj->rowid;
173 $this->code_region = $obj->code_region;
174 $this->fk_pays = $obj->fk_pays;
175 $this->name = $obj->nom;
176 $this->cheflieu = $obj->cheflieu;
177 $this->active = $obj->active;
178 }
179
180 $this->db->free($resql);
181 return 1;
182 } else {
183 return 0;
184 }
185 } else {
186 $this->error = "Error ".$this->db->lasterror();
187 return -1;
188 }
189 }
190
191
199 public function update($user = null, $notrigger = 0)
200 {
201 global $conf, $langs;
202 $error = 0;
203
204 // Clean parameters
205 if (isset($this->code_region)) {
206 $this->code_region = trim($this->code_region);
207 }
208 if (isset($this->fk_pays)) {
209 $this->fk_pays = trim($this->fk_pays);
210 }
211 if (isset($this->name)) {
212 $this->name = trim($this->name);
213 }
214 if (isset($this->cheflieu)) {
215 $this->cheflieu = trim($this->cheflieu);
216 }
217 if (isset($this->active)) {
218 $this->active = trim($this->active);
219 }
220
221
222 // Check parameters
223 // Put here code to add control on parameters values
224
225 // Update request
226 $sql = "UPDATE ".$this->db->prefix()."c_regions SET";
227 $sql .= " code_region=".(isset($this->code_region) ? "'".$this->db->escape($this->code_region)."'" : "null").",";
228 $sql .= " fk_pays=".(isset($this->fk_pays) ? "'".$this->db->escape($this->fk_pays)."'" : "null").",";
229 $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
230 $sql .= " cheflieu=".(isset($this->cheflieu) ? "'".$this->db->escape($this->cheflieu)."'" : "null").",";
231 $sql .= " active=".(isset($this->active) ? $this->active : "null");
232 $sql .= " WHERE rowid=".((int) $this->id);
233
234 $this->db->begin();
235
236 dol_syslog(get_class($this)."::update", LOG_DEBUG);
237 $resql = $this->db->query($sql);
238 if (!$resql) {
239 $error++;
240 $this->errors[] = "Error ".$this->db->lasterror();
241 }
242
243 // Commit or rollback
244 if ($error) {
245 foreach ($this->errors as $errmsg) {
246 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
247 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
248 }
249 $this->db->rollback();
250 return -1 * $error;
251 } else {
252 $this->db->commit();
253 return 1;
254 }
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_regions";
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}
Parent class of all other dictionary classes.
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:1907
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124