dolibarr 19.0.3
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT.'/core/class/commondict.class.php';
28
33{
37 public $element = 'undefined'; // Will be defined into constructor
38
42 public $table_element = 'undefined'; // Will be defined into constructor
43
47 public $lines = array();
48
49 public $code;
50
54 public $label;
55
56 public $active;
57
58
64 public function __construct(DoliDB $db)
65 {
66 $this->db = $db;
67
68 // Don't forget to set this->element and this->table_element after the construction
69 }
70
79 public function create(User $user, $notrigger = false)
80 {
81 dol_syslog(__METHOD__, LOG_DEBUG);
82
83 $fieldlabel = 'label';
84 if ($this->table_element == 'c_stcomm') {
85 $fieldlabel = 'libelle';
86 } elseif ($this->table_element == 'c_type_fees') {
87 $fieldrowid = 'id';
88 }
89
90 $error = 0;
91
92 // Clean parameters
93
94 if (isset($this->code)) {
95 $this->code = trim($this->code);
96 }
97 if (isset($this->label)) {
98 $this->label = trim($this->label);
99 }
100 if (isset($this->active)) {
101 $this->active = trim($this->active);
102 }
103
104 // Insert request
105 $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
106 $sql .= 'code,';
107 $sql .= $fieldlabel;
108 $sql .= 'active';
109 $sql .= ') VALUES (';
110 $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
111 $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
112 $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
113 $sql .= ')';
114
115 $this->db->begin();
116
117 $resql = $this->db->query($sql);
118 if (!$resql) {
119 $error++;
120 $this->errors[] = 'Error '.$this->db->lasterror();
121 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
122 }
123
124 if (!$error) {
125 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
126
127 // Uncomment this and change CTYPERESOURCE to your own tag if you
128 // want this action to call a trigger.
129 //if (!$notrigger) {
130
131 // // Call triggers
132 // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
133 // if ($result < 0) $error++;
134 // // End call triggers
135 //}
136 }
137
138 // Commit or rollback
139 if ($error) {
140 $this->db->rollback();
141
142 return -1 * $error;
143 } else {
144 $this->db->commit();
145
146 return $this->id;
147 }
148 }
149
159 public function fetch($id, $code = '', $label = '')
160 {
161 dol_syslog(__METHOD__, LOG_DEBUG);
162
163 $fieldrowid = 'rowid';
164 $fieldlabel = 'label';
165 if ($this->table_element == 'c_stcomm') {
166 $fieldrowid = 'id';
167 $fieldlabel = 'libelle';
168 } elseif ($this->table_element == 'c_type_fees') {
169 $fieldrowid = 'id';
170 }
171
172 $sql = "SELECT";
173 $sql .= " t.".$fieldrowid.",";
174 $sql .= " t.code,";
175 $sql .= " t.".$fieldlabel." as label,";
176 $sql .= " t.active";
177 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
178 if ($id) {
179 $sql .= " WHERE t.".$fieldrowid." = ".((int) $id);
180 } elseif ($code) {
181 $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
182 } elseif ($label) {
183 $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
184 }
185
186 $resql = $this->db->query($sql);
187 if ($resql) {
188 $numrows = $this->db->num_rows($resql);
189 if ($numrows) {
190 $obj = $this->db->fetch_object($resql);
191
192 $this->id = $obj->$fieldrowid;
193
194 $this->code = $obj->code;
195 $this->label = $obj->label;
196 $this->active = $obj->active;
197 }
198
199 // Retrieve all extrafields for invoice
200 // fetch optionals attributes and labels
201 // $this->fetch_optionals();
202
203 // $this->fetch_lines();
204
205 $this->db->free($resql);
206
207 if ($numrows) {
208 return 1;
209 } else {
210 return 0;
211 }
212 } else {
213 $this->errors[] = 'Error '.$this->db->lasterror();
214 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
215
216 return -1;
217 }
218 }
219
232 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $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.".$fieldrowid.",";
247 $sql .= " t.code,";
248 $sql .= " t.".$fieldlabel." as label,";
249 $sql .= " t.active";
250 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
251
252 // Manage filter
253 $sqlwhere = array();
254 if (count($filter) > 0) {
255 foreach ($filter as $key => $value) {
256 $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
257 }
258 }
259
260 if (count($sqlwhere) > 0) {
261 $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
262 }
263 if (!empty($sortfield)) {
264 $sql .= $this->db->order($sortfield, $sortorder);
265 }
266 if (!empty($limit)) {
267 $sql .= $this->db->plimit($limit, $offset);
268 }
269
270 $resql = $this->db->query($sql);
271 if ($resql) {
272 $num = $this->db->num_rows($resql);
273
274 while ($obj = $this->db->fetch_object($resql)) {
275 $line = new self($this->db);
276
277 $line->id = $obj->$fieldrowid;
278
279 $line->code = $obj->code;
280 $line->label = $obj->label;
281 $line->active = $obj->active;
282 }
283 $this->db->free($resql);
284
285 return $num;
286 } else {
287 $this->errors[] = 'Error '.$this->db->lasterror();
288 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
289
290 return -1;
291 }
292 }
293
302 public function update(User $user, $notrigger = false)
303 {
304 $error = 0;
305
306 dol_syslog(__METHOD__, LOG_DEBUG);
307
308 $fieldrowid = 'rowid';
309 $fieldlabel = 'label';
310 if ($this->table_element == 'c_stcomm') {
311 $fieldrowid = 'id';
312 $fieldlabel = 'libelle';
313 } elseif ($this->table_element == 'c_type_fees') {
314 $fieldrowid = 'id';
315 }
316
317 // Clean parameters
318
319 if (isset($this->code)) {
320 $this->code = trim($this->code);
321 }
322 if (isset($this->label)) {
323 $this->label = trim($this->label);
324 }
325 if (isset($this->active)) {
326 $this->active = trim($this->active);
327 }
328
329 // Check parameters
330 // Put here code to add a control on parameters values
331
332 // Update request
333 $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET';
334 $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
335 $sql .= " ".$fieldlabel." = ".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
336 $sql .= " active = ".(isset($this->active) ? $this->active : "null");
337 $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
338
339 $this->db->begin();
340
341 $resql = $this->db->query($sql);
342 if (!$resql) {
343 $error++;
344 $this->errors[] = 'Error '.$this->db->lasterror();
345 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
346 }
347
348 // Uncomment this and change CTYPERESOURCE to your own tag if you
349 // want this action calls a trigger.
350 //if (!$error && !$notrigger) {
351
352 // // Call triggers
353 // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
354 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
355 // // End call triggers
356 //}
357
358 // Commit or rollback
359 if ($error) {
360 $this->db->rollback();
361
362 return -1 * $error;
363 } else {
364 $this->db->commit();
365
366 return 1;
367 }
368 }
369
378 public function delete(User $user, $notrigger = false)
379 {
380 dol_syslog(__METHOD__, LOG_DEBUG);
381
382 $fieldrowid = 'rowid';
383
384 $error = 0;
385
386 $this->db->begin();
387
388 // Uncomment this and change CTYPERESOURCE to your own tag if you
389 // want this action calls a trigger.
390 //if (!$error && !$notrigger) {
391
392 // // Call triggers
393 // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
394 // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
395 // // End call triggers
396 //}
397
398 // If you need to delete child tables to, you can insert them here
399
400 if (!$error) {
401 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
402 $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
403
404 $resql = $this->db->query($sql);
405 if (!$resql) {
406 $error++;
407 $this->errors[] = 'Error '.$this->db->lasterror();
408 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
409 }
410 }
411
412 // Commit or rollback
413 if ($error) {
414 $this->db->rollback();
415
416 return -1 * $error;
417 } else {
418 $this->db->commit();
419
420 return 1;
421 }
422 }
423
431 public function createFromClone(User $user, $fromid)
432 {
433 dol_syslog(__METHOD__, LOG_DEBUG);
434
435 $error = 0;
436 $object = new Ctyperesource($this->db);
437
438 $this->db->begin();
439
440 // Load source object
441 $object->fetch($fromid);
442 // Reset object
443 $object->id = 0;
444
445 // Clear fields
446 // ...
447
448 // Create clone
449 $object->context['createfromclone'] = 'createfromclone';
450 $result = $object->create($user);
451
452 // Other options
453 if ($result < 0) {
454 $error++;
455 $this->errors = $object->errors;
456 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
457 }
458
459 unset($object->context['createfromclone']);
460
461 // End
462 if (!$error) {
463 $this->db->commit();
464
465 return $object->id;
466 } else {
467 $this->db->rollback();
468
469 return -1;
470 }
471 }
472
479 public function initAsSpecimen()
480 {
481 $this->id = 0;
482
483 $this->code = 'CODE';
484 $this->label = 'Label';
485 $this->active = 1;
486 }
487}
Class CGenericDic.
create(User $user, $notrigger=false)
Create object into database.
update(User $user, $notrigger=false)
Update object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
fetch($id, $code='', $label='')
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.