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