dolibarr 25.0.0-alpha
cgenericdic.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-2016 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2016 Florian Henry <florian.henry@atm-consulting.fr>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28// Put here all includes required by your class file
29require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
30
35{
39 public $element = 'undefined'; // Will be defined into constructor
40
44 public $table_element = 'undefined'; // Will be defined into constructor
45
49 public $lines = array();
50
54 public $code;
55
59 public $label;
60
66 public function __construct(DoliDB $db)
67 {
68 $this->db = $db;
69
70 // Don't forget to set this->element and this->table_element after the construction
71 }
72
80 public function create(User $user, $notrigger = 0)
81 {
82 dol_syslog(__METHOD__, LOG_DEBUG);
83
84 $fieldrowid = 'rowid';
85 $fieldlabel = 'label';
86 if ($this->table_element == 'c_stcomm') {
87 $fieldrowid = 'id';
88 $fieldlabel = 'libelle';
89 } elseif ($this->table_element == 'c_type_fees') {
90 $fieldrowid = 'id';
91 }
92
93 $error = 0;
94
95 // Clean parameters
96
97 if (isset($this->code)) {
98 $this->code = trim($this->code);
99 }
100 if (isset($this->label)) {
101 $this->label = trim($this->label);
102 }
103 if (isset($this->active)) {
104 $this->active = (int) $this->active;
105 }
106
107 // Insert request
108 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
109 if (!empty($this->id)) {
110 $sql .= $this->db->sanitize($fieldrowid).','; // Some dictionaries have no auto incremented id (c_stcomm)
111 }
112 $sql .= 'code,';
113 $sql .= $this->db->sanitize($fieldlabel).',';
114 $sql .= 'active';
115 $sql .= ') VALUES (';
116 if (!empty($this->id)) {
117 $sql .= ((int) $this->id).',';
118 }
119 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
120 $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
121 $sql .= ' '.(!isset($this->active) ? 'NULL' : ((int) $this->active));
122 $sql .= ')';
123
124 $this->db->begin();
125
126 $resql = $this->db->query($sql);
127 if (!$resql) {
128 $error++;
129 $this->errors[] = 'Error '.$this->db->lasterror();
130 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
131 }
132
133 if (!$error && empty($this->id)) {
134 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element, $fieldrowid);
135 }
136 if (!$error) {
137 // Uncomment this and change CTYPERESOURCE to your own tag if you
138 // want this action to call a trigger.
139 //if (!$notrigger) {
140
141 // // Call triggers
142 // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
143 // if ($result < 0) $error++;
144 // // End call triggers
145 //}
146 }
147
148 // Commit or rollback
149 if ($error) {
150 $this->db->rollback();
151
152 return -1 * $error;
153 } else {
154 $this->db->commit();
155
156 return $this->id;
157 }
158 }
159
169 public function fetch($id, $code = '', $label = '')
170 {
171 dol_syslog(__METHOD__, LOG_DEBUG);
172
173 $fieldrowid = 'rowid';
174 $fieldlabel = 'label';
175 if ($this->table_element == 'c_stcomm') {
176 $fieldrowid = 'id';
177 $fieldlabel = 'libelle';
178 } elseif ($this->table_element == 'c_type_fees') {
179 $fieldrowid = 'id';
180 }
181
182 $sql = "SELECT";
183 $sql .= " ".$this->db->sanitize("t.".$fieldrowid).",";
184 $sql .= " t.code,";
185 $sql .= " ".$this->db->sanitize("t.".$fieldlabel)." as label,";
186 $sql .= " t.active";
187 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
188 if ($id) {
189 $sql .= " WHERE ".$this->db->sanitize("t.".$fieldrowid)." = ".((int) $id);
190 } elseif ($code) {
191 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
192 } elseif ($label) {
193 $sql .= " WHERE ".$this->db->sanitize("t.".$fieldlabel)." = '".$this->db->escape($label)."'";
194 }
195
196 $resql = $this->db->query($sql);
197 if ($resql) {
198 $numrows = $this->db->num_rows($resql);
199 if ($numrows) {
200 $obj = $this->db->fetch_object($resql);
201
202 $this->id = $obj->$fieldrowid;
203
204 $this->code = $obj->code;
205 $this->label = $obj->label;
206 $this->active = $obj->active;
207 }
208
209 // Retrieve all extrafields for invoice
210 // fetch optionals attributes and labels
211 // $this->fetch_optionals();
212
213 // $this->fetch_lines();
214
215 $this->db->free($resql);
216
217 if ($numrows) {
218 return 1;
219 } else {
220 return 0;
221 }
222 } else {
223 $this->errors[] = 'Error '.$this->db->lasterror();
224 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
225
226 return -1;
227 }
228 }
229
241 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
242 {
243 dol_syslog(__METHOD__, LOG_DEBUG);
244
245 $fieldrowid = 'rowid';
246 $fieldlabel = 'label';
247 if ($this->table_element == 'c_stcomm') {
248 $fieldrowid = 'id';
249 $fieldlabel = 'libelle';
250 } elseif ($this->table_element == 'c_type_fees') {
251 $fieldrowid = 'id';
252 }
253
254 $sql = "SELECT";
255 $sql .= " t.".$this->db->sanitize($fieldrowid).",";
256 $sql .= " t.code,";
257 $sql .= " t.".$this->db->sanitize($fieldlabel)." as label,";
258 $sql .= " t.active";
259 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
260
261 // Manage filter
262 if (is_array($filter)) {
263 $sqlwhere = array();
264 if (count($filter) > 0) {
265 foreach ($filter as $key => $value) {
266 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
267 }
268 }
269 if (count($sqlwhere) > 0) {
270 $sql .= " WHERE ".implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
271 }
272
273 $filter = '';
274 }
275
276 // Manage filter
277 $errormessage = '';
278 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
279 if ($errormessage) {
280 $this->errors[] = $errormessage;
281 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
282 return -1;
283 }
284
285 if (!empty($sortfield)) {
286 $sql .= $this->db->order($sortfield, $sortorder);
287 }
288 if (!empty($limit)) {
289 $sql .= $this->db->plimit($limit, $offset);
290 }
291
292 $resql = $this->db->query($sql);
293 if ($resql) {
294 $num = $this->db->num_rows($resql);
295
296 $this->lines = array();
297 while ($obj = $this->db->fetch_object($resql)) {
298 $line = new self($this->db);
299
300 $line->id = $obj->$fieldrowid;
301
302 $line->code = $obj->code;
303 $line->label = $obj->label;
304 $line->active = $obj->active;
305
306 $this->lines[] = $line;
307 }
308 $this->db->free($resql);
309
310 return $num;
311 } else {
312 $this->errors[] = 'Error '.$this->db->lasterror();
313 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
314
315 return -1;
316 }
317 }
318
326 public function update(User $user, $notrigger = 0)
327 {
328 $error = 0;
329
330 dol_syslog(__METHOD__, LOG_DEBUG);
331
332 $fieldrowid = 'rowid';
333 $fieldlabel = 'label';
334 if ($this->table_element == 'c_stcomm') {
335 $fieldrowid = 'id';
336 $fieldlabel = 'libelle';
337 } elseif ($this->table_element == 'c_type_fees') {
338 $fieldrowid = 'id';
339 }
340
341 // Clean parameters
342
343 if (isset($this->code)) {
344 $this->code = trim($this->code);
345 }
346 if (isset($this->label)) {
347 $this->label = trim($this->label);
348 }
349 if (isset($this->active)) {
350 $this->active = (int) $this->active;
351 }
352
353 // Check parameters
354 // Put here code to add a control on parameters values
355
356 // Update request
357 $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET';
358 $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
359 $sql .= " ".$this->db->sanitize($fieldlabel)." = ".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
360 $sql .= " active = ".(isset($this->active) ? ((int) $this->active) : "null");
361 $sql .= " WHERE ".$this->db->sanitize($fieldrowid)." = ".((int) $this->id);
362
363 $this->db->begin();
364
365 $resql = $this->db->query($sql);
366 if (!$resql) {
367 $error++;
368 $this->errors[] = 'Error '.$this->db->lasterror();
369 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
370 }
371
372 // Uncomment this and change CTYPERESOURCE to your own tag if you
373 // want this action calls a trigger.
374 //if (!$error && !$notrigger) {
375
376 // // Call triggers
377 // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
378 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
379 // // End call triggers
380 //}
381
382 // Commit or rollback
383 if ($error) {
384 $this->db->rollback();
385
386 return -1 * $error;
387 } else {
388 $this->db->commit();
389
390 return 1;
391 }
392 }
393
401 public function delete(User $user, $notrigger = 0)
402 {
403 dol_syslog(__METHOD__, LOG_DEBUG);
404
405 $fieldrowid = 'rowid';
406 if ($this->table_element == 'c_stcomm' || $this->table_element == 'c_type_fees') {
407 $fieldrowid = 'id';
408 }
409
410 $error = 0;
411
412 $this->db->begin();
413
414 // Uncomment this and change CTYPERESOURCE to your own tag if you
415 // want this action calls a trigger.
416 //if (!$error && !$notrigger) {
417
418 // // Call triggers
419 // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
420 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
421 // // End call triggers
422 //}
423
424 // If you need to delete child tables to, you can insert them here
425
426 if (!$error) {
427 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
428 $sql .= " WHERE ".$this->db->sanitize($fieldrowid)." = ".((int) $this->id);
429
430 $resql = $this->db->query($sql);
431 if (!$resql) {
432 $error++;
433 $this->errors[] = 'Error '.$this->db->lasterror();
434 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
435 }
436 }
437
438 // Commit or rollback
439 if ($error) {
440 $this->db->rollback();
441
442 return -1 * $error;
443 } else {
444 $this->db->commit();
445
446 return 1;
447 }
448 }
449
457 public function createFromClone(User $user, $fromid)
458 {
459 dol_syslog(__METHOD__, LOG_DEBUG);
460
461 $error = 0;
462 $object = new CGenericDic($this->db);
463
464 $this->db->begin();
465
466 // Load source object
467 $object->fetch($fromid);
468 // Reset object
469 $object->id = 0;
470
471 // Clear fields
472 // ...
473
474 // Create clone
475 $object->context['createfromclone'] = 'createfromclone';
476 $result = $object->create($user);
477
478 // Other options
479 if ($result < 0) {
480 $error++;
481 $this->errors = $object->errors;
482 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
483 }
484
485 unset($object->context['createfromclone']);
486
487 // End
488 if (!$error) {
489 $this->db->commit();
490
491 return $object->id;
492 } else {
493 $this->db->rollback();
494
495 return -1;
496 }
497 }
498
505 public function initAsSpecimen()
506 {
507 $this->id = 0;
508
509 $this->code = 'CODE';
510 $this->label = 'Label';
511 $this->active = 1;
512
513 return 1;
514 }
515}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class CGenericDic.
create(User $user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
update(User $user, $notrigger=0)
Update object into database.
fetch($id, $code='', $label='')
Load object in memory from the database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load object in memory from the database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
__construct(DoliDB $db)
Constructor.
Parent class of all other dictionary classes.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
editval_textarea active