dolibarr  16.0.5
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 $element = 'undefined'; // Will be defined into constructor
37 
41  public $table_element = 'undefined'; // Will be defined into constructor
42 
46  public $lines = array();
47 
48  public $code;
49 
53  public $label;
54 
55  public $active;
56 
57 
63  public function __construct(DoliDB $db)
64  {
65  $this->db = $db;
66 
67  // Don't forget to set this->element and this->table_element after the construction
68  }
69 
78  public function create(User $user, $notrigger = false)
79  {
80  dol_syslog(__METHOD__, LOG_DEBUG);
81 
82  $fieldlabel = 'label';
83  if ($this->table_element == 'c_stcomm') {
84  $fieldlabel = 'libelle';
85  } elseif ($this->table_element == 'c_type_fees') {
86  $fieldrowid = 'id';
87  }
88 
89  $error = 0;
90 
91  // Clean parameters
92 
93  if (isset($this->code)) {
94  $this->code = trim($this->code);
95  }
96  if (isset($this->label)) {
97  $this->label = trim($this->label);
98  }
99  if (isset($this->active)) {
100  $this->active = trim($this->active);
101  }
102 
103  // Insert request
104  $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
105  $sql .= 'code,';
106  $sql .= $fieldlabel;
107  $sql .= 'active';
108  $sql .= ') VALUES (';
109  $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
110  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
111  $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
112  $sql .= ')';
113 
114  $this->db->begin();
115 
116  $resql = $this->db->query($sql);
117  if (!$resql) {
118  $error++;
119  $this->errors[] = 'Error '.$this->db->lasterror();
120  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
121  }
122 
123  if (!$error) {
124  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
125 
126  // Uncomment this and change CTYPERESOURCE to your own tag if you
127  // want this action to call a trigger.
128  //if (!$notrigger) {
129 
130  // // Call triggers
131  // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
132  // if ($result < 0) $error++;
133  // // End call triggers
134  //}
135  }
136 
137  // Commit or rollback
138  if ($error) {
139  $this->db->rollback();
140 
141  return -1 * $error;
142  } else {
143  $this->db->commit();
144 
145  return $this->id;
146  }
147  }
148 
158  public function fetch($id, $code = '', $label = '')
159  {
160  dol_syslog(__METHOD__, LOG_DEBUG);
161 
162  $fieldrowid = 'rowid';
163  $fieldlabel = 'label';
164  if ($this->table_element == 'c_stcomm') {
165  $fieldrowid = 'id';
166  $fieldlabel = 'libelle';
167  } elseif ($this->table_element == 'c_type_fees') {
168  $fieldrowid = 'id';
169  }
170 
171  $sql = "SELECT";
172  $sql .= " t.".$fieldrowid.",";
173  $sql .= " t.code,";
174  $sql .= " t.".$fieldlabel." as label,";
175  $sql .= " t.active";
176  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
177  if ($id) {
178  $sql .= " WHERE t.".$fieldrowid." = ".((int) $id);
179  } elseif ($code) {
180  $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
181  } elseif ($label) {
182  $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
183  }
184 
185  $resql = $this->db->query($sql);
186  if ($resql) {
187  $numrows = $this->db->num_rows($resql);
188  if ($numrows) {
189  $obj = $this->db->fetch_object($resql);
190 
191  $this->id = $obj->$fieldrowid;
192 
193  $this->code = $obj->code;
194  $this->label = $obj->label;
195  $this->active = $obj->active;
196  }
197 
198  // Retrieve all extrafields for invoice
199  // fetch optionals attributes and labels
200  // $this->fetch_optionals();
201 
202  // $this->fetch_lines();
203 
204  $this->db->free($resql);
205 
206  if ($numrows) {
207  return 1;
208  } else {
209  return 0;
210  }
211  } else {
212  $this->errors[] = 'Error '.$this->db->lasterror();
213  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
214 
215  return -1;
216  }
217  }
218 
231  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
232  {
233  dol_syslog(__METHOD__, LOG_DEBUG);
234 
235  $fieldrowid = 'rowid';
236  $fieldlabel = 'label';
237  if ($this->table_element == 'c_stcomm') {
238  $fieldrowid = 'id';
239  $fieldlabel = 'libelle';
240  } elseif ($this->table_element == 'c_type_fees') {
241  $fieldrowid = 'id';
242  }
243 
244  $sql = "SELECT";
245  $sql .= " t.".$fieldrowid.",";
246  $sql .= " t.code,";
247  $sql .= " t.".$fieldlabel." as label,";
248  $sql .= " t.active";
249  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
250 
251  // Manage filter
252  $sqlwhere = array();
253  if (count($filter) > 0) {
254  foreach ($filter as $key => $value) {
255  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
256  }
257  }
258 
259  if (count($sqlwhere) > 0) {
260  $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
261  }
262  if (!empty($sortfield)) {
263  $sql .= $this->db->order($sortfield, $sortorder);
264  }
265  if (!empty($limit)) {
266  $sql .= $this->db->plimit($limit, $offset);
267  }
268 
269  $resql = $this->db->query($sql);
270  if ($resql) {
271  $num = $this->db->num_rows($resql);
272 
273  while ($obj = $this->db->fetch_object($resql)) {
274  $line = new self($this->db);
275 
276  $line->id = $obj->$fieldrowid;
277 
278  $line->code = $obj->code;
279  $line->label = $obj->label;
280  $line->active = $obj->active;
281  }
282  $this->db->free($resql);
283 
284  return $num;
285  } else {
286  $this->errors[] = 'Error '.$this->db->lasterror();
287  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
288 
289  return -1;
290  }
291  }
292 
301  public function update(User $user, $notrigger = false)
302  {
303  $error = 0;
304 
305  dol_syslog(__METHOD__, LOG_DEBUG);
306 
307  $fieldrowid = 'rowid';
308  $fieldlabel = 'label';
309  if ($this->table_element == 'c_stcomm') {
310  $fieldrowid = 'id';
311  $fieldlabel = 'libelle';
312  } elseif ($this->table_element == 'c_type_fees') {
313  $fieldrowid = 'id';
314  }
315 
316  // Clean parameters
317 
318  if (isset($this->code)) {
319  $this->code = trim($this->code);
320  }
321  if (isset($this->label)) {
322  $this->label = trim($this->label);
323  }
324  if (isset($this->active)) {
325  $this->active = trim($this->active);
326  }
327 
328  // Check parameters
329  // Put here code to add a control on parameters values
330 
331  // Update request
332  $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET';
333  $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
334  $sql .= " ".$fieldlabel." = ".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
335  $sql .= " active = ".(isset($this->active) ? $this->active : "null");
336  $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
337 
338  $this->db->begin();
339 
340  $resql = $this->db->query($sql);
341  if (!$resql) {
342  $error++;
343  $this->errors[] = 'Error '.$this->db->lasterror();
344  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
345  }
346 
347  // Uncomment this and change CTYPERESOURCE to your own tag if you
348  // want this action calls a trigger.
349  //if (!$error && !$notrigger) {
350 
351  // // Call triggers
352  // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
353  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
354  // // End call triggers
355  //}
356 
357  // Commit or rollback
358  if ($error) {
359  $this->db->rollback();
360 
361  return -1 * $error;
362  } else {
363  $this->db->commit();
364 
365  return 1;
366  }
367  }
368 
377  public function delete(User $user, $notrigger = false)
378  {
379  dol_syslog(__METHOD__, LOG_DEBUG);
380 
381  $fieldrowid = 'rowid';
382 
383  $error = 0;
384 
385  $this->db->begin();
386 
387  // Uncomment this and change CTYPERESOURCE to your own tag if you
388  // want this action calls a trigger.
389  //if (!$error && !$notrigger) {
390 
391  // // Call triggers
392  // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
393  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
394  // // End call triggers
395  //}
396 
397  // If you need to delete child tables to, you can insert them here
398 
399  if (!$error) {
400  $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
401  $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
402 
403  $resql = $this->db->query($sql);
404  if (!$resql) {
405  $error++;
406  $this->errors[] = 'Error '.$this->db->lasterror();
407  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
408  }
409  }
410 
411  // Commit or rollback
412  if ($error) {
413  $this->db->rollback();
414 
415  return -1 * $error;
416  } else {
417  $this->db->commit();
418 
419  return 1;
420  }
421  }
422 
430  public function createFromClone(User $user, $fromid)
431  {
432  dol_syslog(__METHOD__, LOG_DEBUG);
433 
434  $error = 0;
435  $object = new Ctyperesource($this->db);
436 
437  $this->db->begin();
438 
439  // Load source object
440  $object->fetch($fromid);
441  // Reset object
442  $object->id = 0;
443 
444  // Clear fields
445  // ...
446 
447  // Create clone
448  $object->context['createfromclone'] = 'createfromclone';
449  $result = $object->create($user);
450 
451  // Other options
452  if ($result < 0) {
453  $error++;
454  $this->errors = $object->errors;
455  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
456  }
457 
458  unset($object->context['createfromclone']);
459 
460  // End
461  if (!$error) {
462  $this->db->commit();
463 
464  return $object->id;
465  } else {
466  $this->db->rollback();
467 
468  return -1;
469  }
470  }
471 
478  public function initAsSpecimen()
479  {
480  $this->id = 0;
481 
482  $this->code = 'CODE';
483  $this->label = 'Label';
484  $this->active = 1;
485  }
486 }
db
$conf db
API class for accounts.
Definition: inc.php:41
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
CGenericDic\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: cgenericdic.class.php:478
CGenericDic\update
update(User $user, $notrigger=false)
Update object into database.
Definition: cgenericdic.class.php:301
CGenericDic\fetch
fetch($id, $code='', $label='')
Load object in memory from the database.
Definition: cgenericdic.class.php:158
CGenericDic\fetchAll
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
Definition: cgenericdic.class.php:231
Ctyperesource
Class Ctyperesource.
Definition: ctyperesource.class.php:31
CGenericDic\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: cgenericdic.class.php:430
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
CGenericDic
Class CGenericDic.
Definition: cgenericdic.class.php:31
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
User
Class to manage Dolibarr users.
Definition: user.class.php:44
CGenericDic\create
create(User $user, $notrigger=false)
Create object into database.
Definition: cgenericdic.class.php:78
CGenericDic\__construct
__construct(DoliDB $db)
Constructor.
Definition: cgenericdic.class.php:63
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742