dolibarr 20.0.0
cleadstatus.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2020 Florian HENRY <florian.henry@scopen.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.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
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
28
29
34{
38 public $records = array();
39
43 public $element = 'cleadstatus';
44
48 public $table_element = 'c_lead_status';
49
53 public $position;
54
58 public $percent;
59
60
66 public function __construct($db)
67 {
68 $this->db = $db;
69 }
70
71
79 public function create($user, $notrigger = 0)
80 {
81 // Insert request
82 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element."(";
83 $sql .= "rowid,";
84 $sql .= "code,";
85 $sql .= "label,";
86 $sql .= "position,";
87 $sql .= "percent,";
88 $sql .= "active";
89 $sql .= ") VALUES (";
90 $sql .= " ".(!isset($this->id) ? 'NULL' : ((int) $this->id)).",";
91 $sql .= " ".(!isset($this->code) ? 'NULL' : ((int) $this->code)).",";
92 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape(trim($this->label))."'").",";
93 $sql .= " ".(!isset($this->position) ? 'NULL' : (int) $this->position).",";
94 $sql .= " ".(!isset($this->percent) ? 'NULL' : (float) $this->percent).",";
95 $sql .= " ".(!isset($this->active) ? 'NULL' : ((int) $this->active)).",";
96 $sql .= ")";
97
98 $this->db->begin();
99
100 dol_syslog(get_class($this)."::create", LOG_DEBUG);
101 $resql = $this->db->query($sql);
102 // Commit or rollback
103 if (!$resql) {
104 dol_syslog(get_class($this)."::create ".$this->db->lasterror(), LOG_ERR);
105 $this->error = "Error ".$this->db->lasterror();
106 $this->db->rollback();
107 return -1;
108 } else {
109 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
110 $this->db->commit();
111 return $this->id;
112 }
113 }
114
115
123 public function fetch($id, $code = '')
124 {
125 $sql = "SELECT";
126 $sql .= " t.rowid,";
127 $sql .= " t.code,";
128 $sql .= " t.label,";
129 $sql .= " t.position,";
130 $sql .= " t.percent,";
131 $sql .= " t.active";
132 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
133 $sql_where = array();
134 if ($id) {
135 $sql_where[] = " t.rowid = ".((int) $id);
136 }
137 if ($code >= 0) {
138 $sql_where[] = " t.code = ".((int) $code);
139 }
140 if (count($sql_where) > 0) {
141 $sql .= ' WHERE '.implode(' AND ', $sql_where);
142 }
143
144 $resql = $this->db->query($sql);
145 if ($resql) {
146 if ($this->db->num_rows($resql)) {
147 $obj = $this->db->fetch_object($resql);
148
149 $this->id = $obj->rowid;
150 $this->code = $obj->code;
151 $this->label = $obj->label;
152 $this->position = $obj->position;
153 $this->percent = $obj->percent;
154 $this->active = $obj->active;
155 }
156 $this->db->free($resql);
157
158 return 1;
159 } else {
160 $this->error = "Error ".$this->db->lasterror();
161 return -1;
162 }
163 }
164
165
177 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
178 {
179 dol_syslog(__METHOD__, LOG_DEBUG);
180
181 $sql = "SELECT";
182 $sql .= " t.rowid,";
183 $sql .= " t.code,";
184 $sql .= " t.label,";
185 $sql .= " t.position,";
186 $sql .= " t.percent,";
187 $sql .= " t.active";
188 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
189 $sql .= " WHERE 1 = 1";
190
191 // Manage filter
192 if (is_array($filter)) {
193 $sqlwhere = array();
194 if (count($filter) > 0) {
195 foreach ($filter as $key => $value) {
196 if ($key == 't.rowid' || $key == 't.active' || $key == 't.code') {
197 $sqlwhere[] = $this->db->sanitize($key)." = ".((int) $value);
198 } elseif (strpos($key, 'date') !== false) {
199 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->idate($value)."'";
200 } elseif ($key == 't.label') {
201 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
202 } else {
203 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
204 }
205 }
206 }
207 if (count($sqlwhere) > 0) {
208 $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
209 }
210
211 $filter = '';
212 }
213
214 // Manage filter
215 $errormessage = '';
216 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
217 if ($errormessage) {
218 $this->errors[] = $errormessage;
219 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
220 return -1;
221 }
222
223 if (!empty($sortfield)) {
224 $sql .= $this->db->order($sortfield, $sortorder);
225 }
226 if (!empty($limit)) {
227 $sql .= $this->db->plimit($limit, $offset);
228 }
229
230 $resql = $this->db->query($sql);
231 if ($resql) {
232 $this->records = array();
233 $num = $this->db->num_rows($resql);
234 if ($num > 0) {
235 while ($obj = $this->db->fetch_object($resql)) {
236 $record = new self($this->db);
237
238 $record->id = $obj->rowid;
239 $record->code = $obj->code;
240 $record->label = $obj->label;
241 $record->position = $obj->position;
242 $record->percent = $obj->percent;
243 $record->active = $obj->active;
244 $this->records[$record->id] = $record;
245 }
246 }
247 $this->db->free($resql);
248
249 return $this->records;
250 } else {
251 $this->errors[] = 'Error '.$this->db->lasterror();
252 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
253
254 return -1;
255 }
256 }
257
258
266 public function update($user = null, $notrigger = 0)
267 {
268 // Update request
269 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
270 $sql .= " code=".(isset($this->code) ? ((int) $this->code) : "null").",";
271 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape(trim($this->label))."'" : "null").",";
272 $sql .= " position=".(isset($this->position) ? (int) $this->position : "null").",";
273 $sql .= " percent=".(isset($this->label) ? (float) $this->percent : "null").",";
274 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
275 $sql .= " WHERE rowid=".(int) $this->id;
276
277 $this->db->begin();
278
279 dol_syslog(get_class($this)."::update", LOG_DEBUG);
280 $resql = $this->db->query($sql);
281 // Commit or rollback
282 if (!$resql) {
283 dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
284 $this->error = "Error ".$this->db->lasterror();
285 $this->db->rollback();
286 return -1;
287 } else {
288 $this->db->commit();
289 return 1;
290 }
291 }
292
293
301 public function delete($user, $notrigger = 0)
302 {
303 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
304 $sql .= " WHERE rowid=".(int) $this->id;
305
306 $this->db->begin();
307
308 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
309 $resql = $this->db->query($sql);
310 // Commit or rollback
311 if (!$resql) {
312 dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
313 $this->error = "Error ".$this->db->lasterror();
314 $this->db->rollback();
315 return -1;
316 } else {
317 $this->db->commit();
318 return 1;
319 }
320 }
321}
print $object position
Definition edit.php:195
Class of dictionary of opportunity status.
create($user, $notrigger=0)
Create object into database.
fetch($id, $code='')
Load object in memory from database.
update($user=null, $notrigger=0)
Update object into database.
__construct($db)
Constructor.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load list of objects in memory from the database.
Parent class of all other dictionary classes.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.