dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
33{
37 public $db;
38
42 public $error = '';
43
47 public $errors = array();
48
52 public $id;
53
57 public $code;
58
62 public $description;
63
67 public $value;
68
72 public $table_element = "c_price_global_variable";
73
79 public function __construct($db)
80 {
81 $this->db = $db;
82 }
83
84
92 public function create($user, $notrigger = 0)
93 {
94 $error = 0;
95
96 $this->checkParameters();
97
98 // Insert request
99 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
100 $sql .= "code, description, value";
101 $sql .= ") VALUES (";
102 $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
103 $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
104 $sql .= " ".((float) $this->value);
105 $sql .= ")";
106
107 $this->db->begin();
108
109 dol_syslog(__METHOD__);
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().$this->table_element);
118
119 /*
120 if (!$notrigger) {
121 // Uncomment this and change MYOBJECT to your own tag if you
122 // want this action calls a trigger.
123
125 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
126 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
128 }
129 */
130 }
131
132 // Commit or rollback
133 if ($error) {
134 foreach ($this->errors as $errmsg) {
135 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
136 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
137 }
138 $this->db->rollback();
139 return -1 * $error;
140 } else {
141 $this->db->commit();
142 return $this->id;
143 }
144 }
145
146
153 public function fetch($id)
154 {
155 $sql = "SELECT code, description, value";
156 $sql .= " FROM ".$this->db->prefix().$this->table_element;
157 $sql .= " WHERE rowid = ".((int) $id);
158
159 dol_syslog(__METHOD__);
160 $resql = $this->db->query($sql);
161 if ($resql) {
162 $obj = $this->db->fetch_object($resql);
163 if ($obj) {
164 $this->id = $id;
165 $this->code = $obj->code;
166 $this->description = $obj->description;
167 $this->value = $obj->value;
168 $this->checkParameters();
169 return 1;
170 } else {
171 return 0;
172 }
173 } else {
174 $this->error = "Error ".$this->db->lasterror();
175 return -1;
176 }
177 }
178
186 public function update($user = null, $notrigger = 0)
187 {
188 $error = 0;
189
190 $this->checkParameters();
191
192 // Update request
193 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
194 $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
195 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
196 $sql .= " value = ".((float) $this->value);
197 $sql .= " WHERE rowid = ".((int) $this->id);
198
199 $this->db->begin();
200
201 dol_syslog(__METHOD__);
202 $resql = $this->db->query($sql);
203 if (!$resql) {
204 $error++;
205 $this->errors[] = "Error ".$this->db->lasterror();
206 }
207
208 // if (! $error)
209 // {
210 // if (! $notrigger)
211 // {
212 // // Uncomment this and change MYOBJECT to your own tag if you
213 // // want this action calls a trigger.
214
215 // //// Call triggers
216 // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
217 // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
218 // //// End call triggers
219 // }
220 // }
221
222 // Commit or rollback
223 if ($error) {
224 foreach ($this->errors as $errmsg) {
225 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
226 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
227 }
228 $this->db->rollback();
229 return -1 * $error;
230 } else {
231 $this->db->commit();
232 return 1;
233 }
234 }
235
236
245 public function delete($rowid, $user, $notrigger = 0)
246 {
247 $error = 0;
248
249 $this->db->begin();
250
251 /*
252 if (!$error) {
253 if (!$notrigger) {
254 // Uncomment this and change MYOBJECT to your own tag if you
255 // want this action calls a trigger.
256
258 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
259 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
261 }
262 }
263 */
264
265 if (!$error) {
266 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
267 $sql .= " WHERE rowid = ".((int) $rowid);
268
269 dol_syslog(__METHOD__);
270 $resql = $this->db->query($sql);
271 if (!$resql) {
272 $error++;
273 $this->errors[] = "Error ".$this->db->lasterror();
274 }
275 }
276
277 // Commit or rollback
278 if ($error) {
279 foreach ($this->errors as $errmsg) {
280 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
281 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
282 }
283 $this->db->rollback();
284 return -1 * $error;
285 } else {
286 $this->db->commit();
287 return 1;
288 }
289 }
290
297 public function initAsSpecimen()
298 {
299 $this->id = 0;
300 $this->code = '';
301 $this->description = '';
302 $this->value = '';
303
304 return 1;
305 }
306
312 public function checkParameters()
313 {
314 // Clean parameters
315 if (isset($this->code)) {
316 $this->code = trim($this->code);
317 }
318 if (isset($this->description)) {
319 $this->description = trim($this->description);
320 }
321
322 // Check parameters
323 if (empty($this->value) || !is_numeric($this->value)) {
324 $this->value = 0;
325 }
326 }
327
333 public function listGlobalVariables()
334 {
335 $sql = "SELECT rowid, code, description, value";
336 $sql .= " FROM ".$this->db->prefix().$this->table_element;
337 $sql .= " ORDER BY code";
338
339 dol_syslog(__METHOD__, LOG_DEBUG);
340 $resql = $this->db->query($sql);
341 if ($resql) {
342 $retarray = array();
343
344 while ($record = $this->db->fetch_array($resql)) {
345 $variable_obj = new PriceGlobalVariable($this->db);
346 $variable_obj->id = $record["rowid"];
347 $variable_obj->code = $record["code"];
348 $variable_obj->description = $record["description"];
349 $variable_obj->value = $record["value"];
350 $variable_obj->checkParameters();
351 $retarray[] = $variable_obj;
352 }
353
354 $this->db->free($resql);
355 return $retarray;
356 } else {
357 $this->error = $this->db->error();
358 return -1;
359 }
360 }
361}
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.