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