dolibarr 20.0.0
price_global_variable.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2015 Ion Agorria <ion@agorria.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
32{
36 public $db;
37
41 public $error = '';
42
46 public $errors = array();
47
51 public $id;
52
53 public $code;
54
58 public $description;
59
60 public $value;
61
65 public $table_element = "c_price_global_variable";
66
72 public function __construct($db)
73 {
74 $this->db = $db;
75 }
76
77
85 public function create($user, $notrigger = 0)
86 {
87 $error = 0;
88
89 $this->checkParameters();
90
91 // Insert request
92 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
93 $sql .= "code, description, value";
94 $sql .= ") VALUES (";
95 $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
96 $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
97 $sql .= " ".((float) $this->value);
98 $sql .= ")";
99
100 $this->db->begin();
101
102 dol_syslog(__METHOD__);
103 $resql = $this->db->query($sql);
104 if (!$resql) {
105 $error++;
106 $this->errors[] = "Error ".$this->db->lasterror();
107 }
108
109 if (!$error) {
110 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
111
112 if (!$notrigger) {
113 // Uncomment this and change MYOBJECT to your own tag if you
114 // want this action calls a trigger.
115
117 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
118 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
120 }
121 }
122
123 // Commit or rollback
124 if ($error) {
125 foreach ($this->errors as $errmsg) {
126 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
127 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
128 }
129 $this->db->rollback();
130 return -1 * $error;
131 } else {
132 $this->db->commit();
133 return $this->id;
134 }
135 }
136
137
144 public function fetch($id)
145 {
146 $sql = "SELECT code, description, value";
147 $sql .= " FROM ".$this->db->prefix().$this->table_element;
148 $sql .= " WHERE rowid = ".((int) $id);
149
150 dol_syslog(__METHOD__);
151 $resql = $this->db->query($sql);
152 if ($resql) {
153 $obj = $this->db->fetch_object($resql);
154 if ($obj) {
155 $this->id = $id;
156 $this->code = $obj->code;
157 $this->description = $obj->description;
158 $this->value = $obj->value;
159 $this->checkParameters();
160 return 1;
161 } else {
162 return 0;
163 }
164 } else {
165 $this->error = "Error ".$this->db->lasterror();
166 return -1;
167 }
168 }
169
177 public function update($user = null, $notrigger = 0)
178 {
179 $error = 0;
180
181 $this->checkParameters();
182
183 // Update request
184 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
185 $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
186 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
187 $sql .= " value = ".((float) $this->value);
188 $sql .= " WHERE rowid = ".((int) $this->id);
189
190 $this->db->begin();
191
192 dol_syslog(__METHOD__);
193 $resql = $this->db->query($sql);
194 if (!$resql) {
195 $error++;
196 $this->errors[] = "Error ".$this->db->lasterror();
197 }
198
199 // if (! $error)
200 // {
201 // if (! $notrigger)
202 // {
203 // // Uncomment this and change MYOBJECT to your own tag if you
204 // // want this action calls a trigger.
205
206 // //// Call triggers
207 // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
208 // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
209 // //// End call triggers
210 // }
211 // }
212
213 // Commit or rollback
214 if ($error) {
215 foreach ($this->errors as $errmsg) {
216 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
217 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
218 }
219 $this->db->rollback();
220 return -1 * $error;
221 } else {
222 $this->db->commit();
223 return 1;
224 }
225 }
226
227
236 public function delete($rowid, $user, $notrigger = 0)
237 {
238 $error = 0;
239
240 $this->db->begin();
241
242 if (!$error) {
243 if (!$notrigger) {
244 // Uncomment this and change MYOBJECT to your own tag if you
245 // want this action calls a trigger.
246
248 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
249 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
251 }
252 }
253
254 if (!$error) {
255 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
256 $sql .= " WHERE rowid = ".((int) $rowid);
257
258 dol_syslog(__METHOD__);
259 $resql = $this->db->query($sql);
260 if (!$resql) {
261 $error++;
262 $this->errors[] = "Error ".$this->db->lasterror();
263 }
264 }
265
266 // Commit or rollback
267 if ($error) {
268 foreach ($this->errors as $errmsg) {
269 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
270 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
271 }
272 $this->db->rollback();
273 return -1 * $error;
274 } else {
275 $this->db->commit();
276 return 1;
277 }
278 }
279
286 public function initAsSpecimen()
287 {
288 $this->id = 0;
289 $this->code = '';
290 $this->description = '';
291 $this->value = '';
292
293 return 1;
294 }
295
301 public function checkParameters()
302 {
303 // Clean parameters
304 if (isset($this->code)) {
305 $this->code = trim($this->code);
306 }
307 if (isset($this->description)) {
308 $this->description = trim($this->description);
309 }
310
311 // Check parameters
312 if (empty($this->value) || !is_numeric($this->value)) {
313 $this->value = 0;
314 }
315 }
316
322 public function listGlobalVariables()
323 {
324 $sql = "SELECT rowid, code, description, value";
325 $sql .= " FROM ".$this->db->prefix().$this->table_element;
326 $sql .= " ORDER BY code";
327
328 dol_syslog(__METHOD__, LOG_DEBUG);
329 $resql = $this->db->query($sql);
330 if ($resql) {
331 $retarray = array();
332
333 while ($record = $this->db->fetch_array($resql)) {
334 $variable_obj = new PriceGlobalVariable($this->db);
335 $variable_obj->id = $record["rowid"];
336 $variable_obj->code = $record["code"];
337 $variable_obj->description = $record["description"];
338 $variable_obj->value = $record["value"];
339 $variable_obj->checkParameters();
340 $retarray[] = $variable_obj;
341 }
342
343 $this->db->free($resql);
344 return $retarray;
345 } else {
346 $this->error = $this->db->error();
347 return -1;
348 }
349 }
350}
Class for accessing price global variables table.
update($user=null, $notrigger=0)
Update object into database.
listGlobalVariables()
List all price global variables.
checkParameters()
Checks if all parameters are in order.
create($user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetch($id)
Load object in memory from the database.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.