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