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