dolibarr 19.0.3
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 *
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 $title;
53 public $expression;
54
58 public $table_element = "c_price_expression";
59
65 public function __construct($db)
66 {
67 $this->db = $db;
68 }
69
70
78 public function create($user, $notrigger = 0)
79 {
80 $error = 0;
81
82 // Clean parameters
83 if (isset($this->title)) {
84 $this->title = trim($this->title);
85 }
86 if (isset($this->expression)) {
87 $this->expression = trim($this->expression);
88 }
89
90 // Insert request
91 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
92 $sql .= "title, expression";
93 $sql .= ") VALUES (";
94 $sql .= " ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
95 $sql .= " ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
96 $sql .= ")";
97
98 $this->db->begin();
99
100 dol_syslog(__METHOD__, LOG_DEBUG);
101 $resql = $this->db->query($sql);
102 if (!$resql) {
103 $error++;
104 $this->errors[] = "Error ".$this->db->lasterror();
105 }
106
107 if (!$error) {
108 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
109 }
110
111 // Commit or rollback
112 if ($error) {
113 foreach ($this->errors as $errmsg) {
114 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
115 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
116 }
117 $this->db->rollback();
118 return -1 * $error;
119 } else {
120 $this->db->commit();
121 return $this->id;
122 }
123 }
124
125
132 public function fetch($id)
133 {
134 // Check parameters
135 if (empty($id)) {
136 $this->error = 'ErrorWrongParameters';
137 return -1;
138 }
139
140 $sql = "SELECT title, expression";
141 $sql .= " FROM ".$this->db->prefix().$this->table_element;
142 $sql .= " WHERE rowid = ".((int) $id);
143
144 dol_syslog(__METHOD__);
145 $resql = $this->db->query($sql);
146 if ($resql) {
147 $obj = $this->db->fetch_object($resql);
148 if ($obj) {
149 $this->id = $id;
150 $this->title = $obj->title;
151 $this->expression = $obj->expression;
152 return 1;
153 } else {
154 return 0;
155 }
156 } else {
157 $this->error = "Error ".$this->db->lasterror();
158 return -1;
159 }
160 }
161
162 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
168 public function list_price_expression()
169 {
170 // phpcs:enable
171 $sql = "SELECT rowid, title, expression";
172 $sql .= " FROM ".$this->db->prefix().$this->table_element;
173 $sql .= " ORDER BY title";
174
175 dol_syslog(__METHOD__, LOG_DEBUG);
176 $resql = $this->db->query($sql);
177 if ($resql) {
178 $retarray = array();
179
180 while ($record = $this->db->fetch_array($resql)) {
181 $price_expression_obj = new PriceExpression($this->db);
182 $price_expression_obj->id = $record["rowid"];
183 $price_expression_obj->title = $record["title"];
184 $price_expression_obj->expression = $record["expression"];
185 $retarray[] = $price_expression_obj;
186 }
187
188 $this->db->free($resql);
189 return $retarray;
190 } else {
191 $this->error = $this->db->error();
192 return -1;
193 }
194 }
195
196
197 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
204 public function find_title($title)
205 {
206 // phpcs:enable
207 $sql = "SELECT rowid";
208 $sql .= " FROM ".$this->db->prefix().$this->table_element;
209 $sql .= " WHERE title = '".$this->db->escape($title)."'";
210
211 dol_syslog(__METHOD__, LOG_DEBUG);
212 $resql = $this->db->query($sql);
213 if ($resql) {
214 $obj = $this->db->fetch_object($resql);
215 if ($obj) {
216 return (int) $obj->rowid;
217 } else {
218 return 0;
219 }
220 } else {
221 $this->error = "Error ".$this->db->lasterror();
222 return -1;
223 }
224 }
225
226
234 public function update($user = 0, $notrigger = 0)
235 {
236 $error = 0;
237
238 // Clean parameters
239 if (isset($this->title)) {
240 $this->title = trim($this->title);
241 }
242 if (isset($this->expression)) {
243 $this->expression = trim($this->expression);
244 }
245
246 // Update request
247 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
248 $sql .= " title = ".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "''").",";
249 $sql .= " expression = ".(isset($this->expression) ? "'".$this->db->escape($this->expression)."'" : "''");
250 $sql .= " WHERE rowid = ".((int) $this->id);
251
252 $this->db->begin();
253
254 dol_syslog(__METHOD__);
255 $resql = $this->db->query($sql);
256 if (!$resql) {
257 $error++;
258 $this->errors[] = "Error ".$this->db->lasterror();
259 }
260
261 // Commit or rollback
262 if ($error) {
263 foreach ($this->errors as $errmsg) {
264 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
265 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
266 }
267 $this->db->rollback();
268 return -1 * $error;
269 } else {
270 $this->db->commit();
271 return 1;
272 }
273 }
274
275
283 public function delete(User $user, $notrigger = 0)
284 {
285 $error = 0;
286
287 $rowid = $this->id;
288
289 $this->db->begin();
290
291 if (!$error) {
292 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
293 $sql .= " WHERE rowid = ".((int) $rowid);
294
295 dol_syslog(__METHOD__);
296 $resql = $this->db->query($sql);
297 if (!$resql) {
298 $error++;
299 $this->errors[] = "Error ".$this->db->lasterror();
300 }
301 }
302
303 // Commit or rollback
304 if ($error) {
305 foreach ($this->errors as $errmsg) {
306 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
307 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
308 }
309 $this->db->rollback();
310 return -1 * $error;
311 } else {
312 $this->db->commit();
313 return 1;
314 }
315 }
316
323 public function initAsSpecimen()
324 {
325 $this->id = 0;
326 $this->expression = '';
327 }
328}
Class for accesing price expression table.
update($user=0, $notrigger=0)
Update object into database.
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.
fetch($id)
Load object in memory from the database.
create($user, $notrigger=0)
Create 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.