dolibarr 19.0.4
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
31{
35 public $db;
36
40 public $error = '';
41
45 public $errors = array();
46
50 public $id;
51
52 public $code;
53
57 public $description;
58
59 public $value;
60
64 public $table_element = "c_price_global_variable";
65
71 public function __construct($db)
72 {
73 $this->db = $db;
74 }
75
76
84 public function create($user, $notrigger = 0)
85 {
86 $error = 0;
87
88 $this->checkParameters();
89
90 // Insert request
91 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
92 $sql .= "code, description, value";
93 $sql .= ") VALUES (";
94 $sql .= " ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
95 $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
96 $sql .= " ".((float) $this->value);
97 $sql .= ")";
98
99 $this->db->begin();
100
101 dol_syslog(__METHOD__);
102 $resql = $this->db->query($sql);
103 if (!$resql) {
104 $error++;
105 $this->errors[] = "Error ".$this->db->lasterror();
106 }
107
108 if (!$error) {
109 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
110
111 if (!$notrigger) {
112 // Uncomment this and change MYOBJECT to your own tag if you
113 // want this action calls a trigger.
114
116 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
117 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
119 }
120 }
121
122 // Commit or rollback
123 if ($error) {
124 foreach ($this->errors as $errmsg) {
125 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
126 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
127 }
128 $this->db->rollback();
129 return -1 * $error;
130 } else {
131 $this->db->commit();
132 return $this->id;
133 }
134 }
135
136
143 public function fetch($id)
144 {
145 $sql = "SELECT code, description, value";
146 $sql .= " FROM ".$this->db->prefix().$this->table_element;
147 $sql .= " WHERE rowid = ".((int) $id);
148
149 dol_syslog(__METHOD__);
150 $resql = $this->db->query($sql);
151 if ($resql) {
152 $obj = $this->db->fetch_object($resql);
153 if ($obj) {
154 $this->id = $id;
155 $this->code = $obj->code;
156 $this->description = $obj->description;
157 $this->value = $obj->value;
158 $this->checkParameters();
159 return 1;
160 } else {
161 return 0;
162 }
163 } else {
164 $this->error = "Error ".$this->db->lasterror();
165 return -1;
166 }
167 }
168
176 public function update($user = 0, $notrigger = 0)
177 {
178 $error = 0;
179
180 $this->checkParameters();
181
182 // Update request
183 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
184 $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "''").",";
185 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
186 $sql .= " value = ".((float) $this->value);
187 $sql .= " WHERE rowid = ".((int) $this->id);
188
189 $this->db->begin();
190
191 dol_syslog(__METHOD__);
192 $resql = $this->db->query($sql);
193 if (!$resql) {
194 $error++;
195 $this->errors[] = "Error ".$this->db->lasterror();
196 }
197
198 // if (! $error)
199 // {
200 // if (! $notrigger)
201 // {
202 // // Uncomment this and change MYOBJECT to your own tag if you
203 // // want this action calls a trigger.
204
205 // //// Call triggers
206 // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
207 // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
208 // //// End call triggers
209 // }
210 // }
211
212 // Commit or rollback
213 if ($error) {
214 foreach ($this->errors as $errmsg) {
215 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
216 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
217 }
218 $this->db->rollback();
219 return -1 * $error;
220 } else {
221 $this->db->commit();
222 return 1;
223 }
224 }
225
226
235 public function delete($rowid, $user, $notrigger = 0)
236 {
237 $error = 0;
238
239 $this->db->begin();
240
241 if (!$error) {
242 if (!$notrigger) {
243 // Uncomment this and change MYOBJECT to your own tag if you
244 // want this action calls a trigger.
245
247 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
248 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
250 }
251 }
252
253 if (!$error) {
254 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
255 $sql .= " WHERE rowid = ".((int) $rowid);
256
257 dol_syslog(__METHOD__);
258 $resql = $this->db->query($sql);
259 if (!$resql) {
260 $error++;
261 $this->errors[] = "Error ".$this->db->lasterror();
262 }
263 }
264
265 // Commit or rollback
266 if ($error) {
267 foreach ($this->errors as $errmsg) {
268 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
269 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
270 }
271 $this->db->rollback();
272 return -1 * $error;
273 } else {
274 $this->db->commit();
275 return 1;
276 }
277 }
278
285 public function initAsSpecimen()
286 {
287 $this->id = 0;
288 $this->code = '';
289 $this->description = '';
290 $this->value = '';
291 }
292
298 public function checkParameters()
299 {
300 // Clean parameters
301 if (isset($this->code)) {
302 $this->code = trim($this->code);
303 }
304 if (isset($this->description)) {
305 $this->description = trim($this->description);
306 }
307
308 // Check parameters
309 if (empty($this->value) || !is_numeric($this->value)) {
310 $this->value = 0;
311 }
312 }
313
319 public function listGlobalVariables()
320 {
321 $sql = "SELECT rowid, code, description, value";
322 $sql .= " FROM ".$this->db->prefix().$this->table_element;
323 $sql .= " ORDER BY code";
324
325 dol_syslog(__METHOD__, LOG_DEBUG);
326 $resql = $this->db->query($sql);
327 if ($resql) {
328 $retarray = array();
329
330 while ($record = $this->db->fetch_array($resql)) {
331 $variable_obj = new PriceGlobalVariable($this->db);
332 $variable_obj->id = $record["rowid"];
333 $variable_obj->code = $record["code"];
334 $variable_obj->description = $record["description"];
335 $variable_obj->value = $record["value"];
336 $variable_obj->checkParameters();
337 $retarray[] = $variable_obj;
338 }
339
340 $this->db->free($resql);
341 return $retarray;
342 } else {
343 $this->error = $this->db->error();
344 return -1;
345 }
346 }
347}
Class for accesing price global variables table.
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.
update($user=0, $notrigger=0)
Update object into database.
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.