dolibarr 20.0.0
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 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 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
51 public $code;
52
56 public $label;
57
58 public $active;
59
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 $fieldlabel = 'label';
85 if ($this->table_element == 'c_stcomm') {
86 $fieldlabel = 'libelle';
87 } elseif ($this->table_element == 'c_type_fees') {
88 $fieldrowid = 'id';
89 }
90
91 $error = 0;
92
93 // Clean parameters
94
95 if (isset($this->code)) {
96 $this->code = trim($this->code);
97 }
98 if (isset($this->label)) {
99 $this->label = trim($this->label);
100 }
101 if (isset($this->active)) {
102 $this->active = (int) $this->active;
103 }
104
105 // Insert request
106 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
107 $sql .= 'code,';
108 $sql .= $fieldlabel;
109 $sql .= 'active';
110 $sql .= ') VALUES (';
111 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
112 $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
113 $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
114 $sql .= ')';
115
116 $this->db->begin();
117
118 $resql = $this->db->query($sql);
119 if (!$resql) {
120 $error++;
121 $this->errors[] = 'Error '.$this->db->lasterror();
122 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
123 }
124
125 if (!$error) {
126 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
127
128 // Uncomment this and change CTYPERESOURCE to your own tag if you
129 // want this action to call a trigger.
130 //if (!$notrigger) {
131
132 // // Call triggers
133 // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
134 // if ($result < 0) $error++;
135 // // End call triggers
136 //}
137 }
138
139 // Commit or rollback
140 if ($error) {
141 $this->db->rollback();
142
143 return -1 * $error;
144 } else {
145 $this->db->commit();
146
147 return $this->id;
148 }
149 }
150
160 public function fetch($id, $code = '', $label = '')
161 {
162 dol_syslog(__METHOD__, LOG_DEBUG);
163
164 $fieldrowid = 'rowid';
165 $fieldlabel = 'label';
166 if ($this->table_element == 'c_stcomm') {
167 $fieldrowid = 'id';
168 $fieldlabel = 'libelle';
169 } elseif ($this->table_element == 'c_type_fees') {
170 $fieldrowid = 'id';
171 }
172
173 $sql = "SELECT";
174 $sql .= " t.".$fieldrowid.",";
175 $sql .= " t.code,";
176 $sql .= " t.".$fieldlabel." as label,";
177 $sql .= " t.active";
178 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
179 if ($id) {
180 $sql .= " WHERE t.".$fieldrowid." = ".((int) $id);
181 } elseif ($code) {
182 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
183 } elseif ($label) {
184 $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
185 }
186
187 $resql = $this->db->query($sql);
188 if ($resql) {
189 $numrows = $this->db->num_rows($resql);
190 if ($numrows) {
191 $obj = $this->db->fetch_object($resql);
192
193 $this->id = $obj->$fieldrowid;
194
195 $this->code = $obj->code;
196 $this->label = $obj->label;
197 $this->active = $obj->active;
198 }
199
200 // Retrieve all extrafields for invoice
201 // fetch optionals attributes and labels
202 // $this->fetch_optionals();
203
204 // $this->fetch_lines();
205
206 $this->db->free($resql);
207
208 if ($numrows) {
209 return 1;
210 } else {
211 return 0;
212 }
213 } else {
214 $this->errors[] = 'Error '.$this->db->lasterror();
215 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
216
217 return -1;
218 }
219 }
220
232 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
233 {
234 dol_syslog(__METHOD__, LOG_DEBUG);
235
236 $fieldrowid = 'rowid';
237 $fieldlabel = 'label';
238 if ($this->table_element == 'c_stcomm') {
239 $fieldrowid = 'id';
240 $fieldlabel = 'libelle';
241 } elseif ($this->table_element == 'c_type_fees') {
242 $fieldrowid = 'id';
243 }
244
245 $sql = "SELECT";
246 $sql .= " t.".$this->db->sanitize($fieldrowid).",";
247 $sql .= " t.code,";
248 $sql .= " t.".$this->db->sanitize($fieldlabel)." as label,";
249 $sql .= " t.active";
250 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
251
252 // Manage filter
253 if (is_array($filter)) {
254 $sqlwhere = array();
255 if (count($filter) > 0) {
256 foreach ($filter as $key => $value) {
257 $sqlwhere[] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($value)."%'";
258 }
259 }
260 if (count($sqlwhere) > 0) {
261 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
262 }
263
264 $filter = '';
265 }
266
267 // Manage filter
268 $errormessage = '';
269 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
270 if ($errormessage) {
271 $this->errors[] = $errormessage;
272 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
273 return -1;
274 }
275
276 if (!empty($sortfield)) {
277 $sql .= $this->db->order($sortfield, $sortorder);
278 }
279 if (!empty($limit)) {
280 $sql .= $this->db->plimit($limit, $offset);
281 }
282
283 $resql = $this->db->query($sql);
284 if ($resql) {
285 $num = $this->db->num_rows($resql);
286
287 while ($obj = $this->db->fetch_object($resql)) {
288 $line = new self($this->db);
289
290 $line->id = $obj->$fieldrowid;
291
292 $line->code = $obj->code;
293 $line->label = $obj->label;
294 $line->active = $obj->active;
295 }
296 $this->db->free($resql);
297
298 return $num;
299 } else {
300 $this->errors[] = 'Error '.$this->db->lasterror();
301 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
302
303 return -1;
304 }
305 }
306
314 public function update(User $user, $notrigger = 0)
315 {
316 $error = 0;
317
318 dol_syslog(__METHOD__, LOG_DEBUG);
319
320 $fieldrowid = 'rowid';
321 $fieldlabel = 'label';
322 if ($this->table_element == 'c_stcomm') {
323 $fieldrowid = 'id';
324 $fieldlabel = 'libelle';
325 } elseif ($this->table_element == 'c_type_fees') {
326 $fieldrowid = 'id';
327 }
328
329 // Clean parameters
330
331 if (isset($this->code)) {
332 $this->code = trim($this->code);
333 }
334 if (isset($this->label)) {
335 $this->label = trim($this->label);
336 }
337 if (isset($this->active)) {
338 $this->active = (int) $this->active;
339 }
340
341 // Check parameters
342 // Put here code to add a control on parameters values
343
344 // Update request
345 $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET';
346 $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
347 $sql .= " ".$fieldlabel." = ".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
348 $sql .= " active = ".(isset($this->active) ? $this->active : "null");
349 $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
350
351 $this->db->begin();
352
353 $resql = $this->db->query($sql);
354 if (!$resql) {
355 $error++;
356 $this->errors[] = 'Error '.$this->db->lasterror();
357 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
358 }
359
360 // Uncomment this and change CTYPERESOURCE to your own tag if you
361 // want this action calls a trigger.
362 //if (!$error && !$notrigger) {
363
364 // // Call triggers
365 // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
366 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
367 // // End call triggers
368 //}
369
370 // Commit or rollback
371 if ($error) {
372 $this->db->rollback();
373
374 return -1 * $error;
375 } else {
376 $this->db->commit();
377
378 return 1;
379 }
380 }
381
389 public function delete(User $user, $notrigger = 0)
390 {
391 dol_syslog(__METHOD__, LOG_DEBUG);
392
393 $fieldrowid = 'rowid';
394
395 $error = 0;
396
397 $this->db->begin();
398
399 // Uncomment this and change CTYPERESOURCE to your own tag if you
400 // want this action calls a trigger.
401 //if (!$error && !$notrigger) {
402
403 // // Call triggers
404 // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
405 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
406 // // End call triggers
407 //}
408
409 // If you need to delete child tables to, you can insert them here
410
411 if (!$error) {
412 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
413 $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
414
415 $resql = $this->db->query($sql);
416 if (!$resql) {
417 $error++;
418 $this->errors[] = 'Error '.$this->db->lasterror();
419 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
420 }
421 }
422
423 // Commit or rollback
424 if ($error) {
425 $this->db->rollback();
426
427 return -1 * $error;
428 } else {
429 $this->db->commit();
430
431 return 1;
432 }
433 }
434
442 public function createFromClone(User $user, $fromid)
443 {
444 dol_syslog(__METHOD__, LOG_DEBUG);
445
446 $error = 0;
447 $object = new Ctyperesource($this->db);
448
449 $this->db->begin();
450
451 // Load source object
452 $object->fetch($fromid);
453 // Reset object
454 $object->id = 0;
455
456 // Clear fields
457 // ...
458
459 // Create clone
460 $object->context['createfromclone'] = 'createfromclone';
461 $result = $object->create($user);
462
463 // Other options
464 if ($result < 0) {
465 $error++;
466 $this->errors = $object->errors;
467 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
468 }
469
470 unset($object->context['createfromclone']);
471
472 // End
473 if (!$error) {
474 $this->db->commit();
475
476 return $object->id;
477 } else {
478 $this->db->rollback();
479
480 return -1;
481 }
482 }
483
490 public function initAsSpecimen()
491 {
492 $this->id = 0;
493
494 $this->code = 'CODE';
495 $this->label = 'Label';
496 $this->active = 1;
497
498 return 1;
499 }
500}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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 Ctyperesource.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
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.