dolibarr 23.0.3
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 * Copyright (C) 2025 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
27// Put here all includes required by your class file
28require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
29
30
35{
39 public $records = array();
40
44 public $element = 'cleadstatus';
45
49 public $table_element = 'c_lead_status';
50
54 public $position;
55
59 public $percent;
60
64 public $fields = array(
65 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'visible' => 0, 'noteditable' => 1, 'index' => 1, 'css' => 'left', 'comment' => "Id"),
66 'label' => array('type' => 'varchar(128)', 'label' => 'Label', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => 1, 'index' => 1, 'searchall' => 1, 'showoncombobox' => 1, 'comment' => "Label of status"),
67 );
68
74 public function __construct($db)
75 {
76 $this->db = $db;
77 }
78
79
87 public function create($user, $notrigger = 0)
88 {
89 if (empty($this->id)) {
90 return -1;
91 }
92 // Insert request
93 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element."(";
94 $sql .= "rowid,";
95 $sql .= "code,";
96 $sql .= "label,";
97 $sql .= "position,";
98 $sql .= "percent,";
99 $sql .= "active";
100 $sql .= ") VALUES (";
101 $sql .= (int) $this->id . ",";
102 $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape(trim($this->code))."'").",";
103 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape(trim($this->label))."'").",";
104 $sql .= " ".(!isset($this->position) ? 'NULL' : (int) $this->position).",";
105 $sql .= " ".(!isset($this->percent) ? 'NULL' : (float) $this->percent).",";
106 $sql .= " ".(!isset($this->active) ? 'NULL' : ((int) $this->active)).",";
107 $sql .= ")";
108
109 $this->db->begin();
110
111 dol_syslog(get_class($this)."::create", LOG_DEBUG);
112 $resql = $this->db->query($sql);
113 // Commit or rollback
114 if (!$resql) {
115 dol_syslog(get_class($this)."::create ".$this->db->lasterror(), LOG_ERR);
116 $this->error = "Error ".$this->db->lasterror();
117 $this->db->rollback();
118 return -1;
119 } else {
120 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
121 $this->db->commit();
122 return $this->id;
123 }
124 }
125
126
134 public function fetch($id, $code = '')
135 {
136 $sql = "SELECT";
137 $sql .= " t.rowid,";
138 $sql .= " t.code,";
139 $sql .= " t.label,";
140 $sql .= " t.position,";
141 $sql .= " t.percent,";
142 $sql .= " t.active";
143 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
144 $sql_where = array();
145 if ($id) {
146 $sql_where[] = " t.rowid = ".((int) $id);
147 }
148 if ($code) {
149 $sql_where[] = " t.code = '".$this->db->escape($code)."'";
150 }
151 if (count($sql_where) > 0) {
152 $sql .= ' WHERE '.implode(' AND ', $sql_where);
153 }
154
155 $resql = $this->db->query($sql);
156 if ($resql) {
157 if ($this->db->num_rows($resql)) {
158 $obj = $this->db->fetch_object($resql);
159
160 $this->id = (int) $obj->rowid;
161 $this->code = $obj->code;
162 $this->label = $obj->label;
163 $this->position = $obj->position;
164 $this->percent = $obj->percent;
165 $this->active = (int) $obj->active;
166 }
167 $this->db->free($resql);
168
169 return 1;
170 } else {
171 $this->error = "Error ".$this->db->lasterror();
172 return -1;
173 }
174 }
175
176
188 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
189 {
190 dol_syslog(__METHOD__, LOG_DEBUG);
191
192 $sql = "SELECT";
193 $sql .= " t.rowid,";
194 $sql .= " t.code,";
195 $sql .= " t.label,";
196 $sql .= " t.position,";
197 $sql .= " t.percent,";
198 $sql .= " t.active";
199 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
200 $sql .= " WHERE 1 = 1";
201
202 // Manage filter
203 if (is_array($filter)) {
204 $sqlwhere = array();
205 if (count($filter) > 0) {
206 foreach ($filter as $key => $value) {
207 if ($key == 't.rowid' || $key == 't.active' || $key == 't.code') {
208 $sqlwhere[] = $this->db->sanitize($key)." = ".((int) $value);
209 } elseif (strpos($key, 'date') !== false) {
210 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->idate($value)."'";
211 } elseif ($key == 't.label') {
212 $sqlwhere[] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
213 } else {
214 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
215 }
216 }
217 }
218 if (count($sqlwhere) > 0) {
219 $sql .= " AND ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
220 }
221
222 $filter = '';
223 }
224
225 // Manage filter
226 $errormessage = '';
227 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
228 if ($errormessage) {
229 $this->errors[] = $errormessage;
230 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
231 return -1;
232 }
233
234 if (!empty($sortfield)) {
235 $sql .= $this->db->order($sortfield, $sortorder);
236 }
237 if (!empty($limit)) {
238 $sql .= $this->db->plimit($limit, $offset);
239 }
240
241 $resql = $this->db->query($sql);
242 if ($resql) {
243 $this->records = array();
244 $num = $this->db->num_rows($resql);
245 if ($num > 0) {
246 while ($obj = $this->db->fetch_object($resql)) {
247 $record = new self($this->db);
248
249 $record->id = $obj->rowid;
250 $record->code = $obj->code;
251 $record->label = $obj->label;
252 $record->position = $obj->position;
253 $record->percent = $obj->percent;
254 $record->active = $obj->active;
255 $this->records[$record->id] = $record;
256 }
257 }
258 $this->db->free($resql);
259
260 return $this->records;
261 } else {
262 $this->errors[] = 'Error '.$this->db->lasterror();
263 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
264
265 return -1;
266 }
267 }
268
269
277 public function update($user = null, $notrigger = 0)
278 {
279 // Update request
280 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
281 $sql .= " code=".(isset($this->code) ? ((int) $this->code) : "null").",";
282 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape(trim($this->label))."'" : "null").",";
283 $sql .= " position=".(isset($this->position) ? (int) $this->position : "null").",";
284 $sql .= " percent=".(isset($this->label) ? (float) $this->percent : "null").",";
285 $sql .= " active=".(isset($this->active) ? ((int) $this->active) : "null");
286 $sql .= " WHERE rowid=".(int) $this->id;
287
288 $this->db->begin();
289
290 dol_syslog(get_class($this)."::update", LOG_DEBUG);
291 $resql = $this->db->query($sql);
292 // Commit or rollback
293 if (!$resql) {
294 dol_syslog(get_class($this)."::update Error ".$this->db->lasterror(), LOG_ERR);
295 $this->error = "Error ".$this->db->lasterror();
296 $this->db->rollback();
297 return -1;
298 } else {
299 $this->db->commit();
300 return 1;
301 }
302 }
303
304
312 public function delete($user, $notrigger = 0)
313 {
314 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
315 $sql .= " WHERE rowid=".(int) $this->id;
316
317 $this->db->begin();
318
319 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
320 $resql = $this->db->query($sql);
321 // Commit or rollback
322 if (!$resql) {
323 dol_syslog(get_class($this)."::delete Error ".$this->db->lasterror(), LOG_ERR);
324 $this->error = "Error ".$this->db->lasterror();
325 $this->db->rollback();
326 return -1;
327 } else {
328 $this->db->commit();
329 return 1;
330 }
331 }
332}
print $object position
Definition edit.php:207
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.
editval_textarea active