dolibarr 21.0.0-alpha
price_expression.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
56 public $title;
57
61 public $expression;
62
66 public $table_element = "c_price_expression";
67
73 public function __construct($db)
74 {
75 $this->db = $db;
76 }
77
78
86 public function create(User $user, $notrigger = 0)
87 {
88 $error = 0;
89
90 // Clean parameters
91 if (isset($this->title)) {
92 $this->title = trim($this->title);
93 }
94 if (isset($this->expression)) {
95 $this->expression = trim($this->expression);
96 }
97
98 // Insert request
99 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
100 $sql .= "title, expression";
101 $sql .= ") VALUES (";
102 $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
103 $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
104 $sql .= ")";
105
106 $this->db->begin();
107
108 dol_syslog(__METHOD__, LOG_DEBUG);
109 $resql = $this->db->query($sql);
110 if (!$resql) {
111 $error++;
112 $this->errors[] = "Error ".$this->db->lasterror();
113 }
114
115 if (!$error) {
116 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
117 }
118
119 // Commit or rollback
120 if ($error) {
121 foreach ($this->errors as $errmsg) {
122 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
123 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
124 }
125 $this->db->rollback();
126 return -1 * $error;
127 } else {
128 $this->db->commit();
129 return $this->id;
130 }
131 }
132
133
140 public function fetch($id)
141 {
142 // Check parameters
143 if (empty($id)) {
144 $this->error = 'ErrorWrongParameters';
145 return -1;
146 }
147
148 $sql = "SELECT title, expression";
149 $sql .= " FROM ".$this->db->prefix().$this->table_element;
150 $sql .= " WHERE rowid = ".((int) $id);
151
152 dol_syslog(__METHOD__);
153 $resql = $this->db->query($sql);
154 if ($resql) {
155 $obj = $this->db->fetch_object($resql);
156 if ($obj) {
157 $this->id = $id;
158 $this->title = $obj->title;
159 $this->expression = $obj->expression;
160 return 1;
161 } else {
162 return 0;
163 }
164 } else {
165 $this->error = "Error ".$this->db->lasterror();
166 return -1;
167 }
168 }
169
170 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
176 public function list_price_expression()
177 {
178 // phpcs:enable
179 $sql = "SELECT rowid, title, expression";
180 $sql .= " FROM ".$this->db->prefix().$this->table_element;
181 $sql .= " ORDER BY title";
182
183 dol_syslog(__METHOD__, LOG_DEBUG);
184 $resql = $this->db->query($sql);
185 if ($resql) {
186 $retarray = array();
187
188 while ($record = $this->db->fetch_array($resql)) {
189 $price_expression_obj = new PriceExpression($this->db);
190 $price_expression_obj->id = $record["rowid"];
191 $price_expression_obj->title = $record["title"];
192 $price_expression_obj->expression = $record["expression"];
193 $retarray[] = $price_expression_obj;
194 }
195
196 $this->db->free($resql);
197 return $retarray;
198 } else {
199 $this->error = $this->db->error();
200 return -1;
201 }
202 }
203
204
205 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
212 public function find_title($title)
213 {
214 // phpcs:enable
215 $sql = "SELECT rowid";
216 $sql .= " FROM ".$this->db->prefix().$this->table_element;
217 $sql .= " WHERE title = '".$this->db->escape($title)."'";
218
219 dol_syslog(__METHOD__, LOG_DEBUG);
220 $resql = $this->db->query($sql);
221 if ($resql) {
222 $obj = $this->db->fetch_object($resql);
223 if ($obj) {
224 return (int) $obj->rowid;
225 } else {
226 return 0;
227 }
228 } else {
229 $this->error = "Error ".$this->db->lasterror();
230 return -1;
231 }
232 }
233
234
242 public function update(User $user, $notrigger = 0)
243 {
244 $error = 0;
245
246 // Clean parameters
247 if (isset($this->title)) {
248 $this->title = trim($this->title);
249 }
250 if (isset($this->expression)) {
251 $this->expression = trim($this->expression);
252 }
253
254 // Update request
255 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
256 $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
257 $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
258 $sql .= " WHERE rowid = ".((int) $this->id);
259
260 $this->db->begin();
261
262 dol_syslog(__METHOD__);
263 $resql = $this->db->query($sql);
264 if (!$resql) {
265 $error++;
266 $this->errors[] = "Error ".$this->db->lasterror();
267 }
268
269 // Commit or rollback
270 if ($error) {
271 foreach ($this->errors as $errmsg) {
272 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
273 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
274 }
275 $this->db->rollback();
276 return -1 * $error;
277 } else {
278 $this->db->commit();
279 return 1;
280 }
281 }
282
283
291 public function delete(User $user, $notrigger = 0)
292 {
293 $error = 0;
294
295 $rowid = $this->id;
296
297 $this->db->begin();
298
299 if (!$error) {
300 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
301 $sql .= " WHERE rowid = ".((int) $rowid);
302
303 dol_syslog(__METHOD__);
304 $resql = $this->db->query($sql);
305 if (!$resql) {
306 $error++;
307 $this->errors[] = "Error ".$this->db->lasterror();
308 }
309 }
310
311 // Commit or rollback
312 if ($error) {
313 foreach ($this->errors as $errmsg) {
314 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
315 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
316 }
317 $this->db->rollback();
318 return -1 * $error;
319 } else {
320 $this->db->commit();
321 return 1;
322 }
323 }
324
331 public function initAsSpecimen()
332 {
333 $this->id = 0;
334 $this->expression = '';
335
336 return 1;
337 }
338}
Class for accessing price expression table.
find_title($title)
Returns any existing rowid with specified title.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
create(User $user, $notrigger=0)
Create object into database.
fetch($id)
Load object in memory from the database.
update(User $user, $notrigger=0)
Update object into database.
list_price_expression()
List all price expressions.
__construct($db)
Constructor.
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.