dolibarr  x.y.z
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  }
86 
87  $error = 0;
88 
89  // Clean parameters
90 
91  if (isset($this->code)) {
92  $this->code = trim($this->code);
93  }
94  if (isset($this->label)) {
95  $this->label = trim($this->label);
96  }
97  if (isset($this->active)) {
98  $this->active = trim($this->active);
99  }
100 
101  // Insert request
102  $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
103  $sql .= 'code,';
104  $sql .= $fieldlabel;
105  $sql .= 'active';
106  $sql .= ') VALUES (';
107  $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
108  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
109  $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
110  $sql .= ')';
111 
112  $this->db->begin();
113 
114  $resql = $this->db->query($sql);
115  if (!$resql) {
116  $error++;
117  $this->errors[] = 'Error '.$this->db->lasterror();
118  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
119  }
120 
121  if (!$error) {
122  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
123 
124  // Uncomment this and change CTYPERESOURCE to your own tag if you
125  // want this action to call a trigger.
126  //if (!$notrigger) {
127 
128  // // Call triggers
129  // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
130  // if ($result < 0) $error++;
131  // // End call triggers
132  //}
133  }
134 
135  // Commit or rollback
136  if ($error) {
137  $this->db->rollback();
138 
139  return -1 * $error;
140  } else {
141  $this->db->commit();
142 
143  return $this->id;
144  }
145  }
146 
156  public function fetch($id, $code = '', $label = '')
157  {
158  dol_syslog(__METHOD__, LOG_DEBUG);
159 
160  $fieldrowid = 'rowid';
161  $fieldlabel = 'label';
162  if ($this->table_element == 'c_stcomm') {
163  $fieldrowid = 'id';
164  $fieldlabel = 'libelle';
165  }
166 
167  $sql = "SELECT";
168  $sql .= " t.".$fieldrowid.",";
169  $sql .= " t.code,";
170  $sql .= " t.".$fieldlabel." as label,";
171  $sql .= " t.active";
172  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
173  if ($id) {
174  $sql .= " WHERE t.".$fieldrowid." = ".((int) $id);
175  } elseif ($code) {
176  $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
177  } elseif ($label) {
178  $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
179  }
180 
181  $resql = $this->db->query($sql);
182  if ($resql) {
183  $numrows = $this->db->num_rows($resql);
184  if ($numrows) {
185  $obj = $this->db->fetch_object($resql);
186 
187  $this->id = $obj->$fieldrowid;
188 
189  $this->code = $obj->code;
190  $this->label = $obj->label;
191  $this->active = $obj->active;
192  }
193 
194  // Retrieve all extrafields for invoice
195  // fetch optionals attributes and labels
196  // $this->fetch_optionals();
197 
198  // $this->fetch_lines();
199 
200  $this->db->free($resql);
201 
202  if ($numrows) {
203  return 1;
204  } else {
205  return 0;
206  }
207  } else {
208  $this->errors[] = 'Error '.$this->db->lasterror();
209  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
210 
211  return -1;
212  }
213  }
214 
227  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
228  {
229  dol_syslog(__METHOD__, LOG_DEBUG);
230 
231  $fieldrowid = 'rowid';
232  $fieldlabel = 'label';
233  if ($this->table_element == 'c_stcomm') {
234  $fieldrowid = 'id';
235  $fieldlabel = 'libelle';
236  }
237 
238  $sql = "SELECT";
239  $sql .= " t.".$fieldrowid.",";
240  $sql .= " t.code,";
241  $sql .= " t.".$fieldlabel." as label,";
242  $sql .= " t.active";
243  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
244 
245  // Manage filter
246  $sqlwhere = array();
247  if (count($filter) > 0) {
248  foreach ($filter as $key => $value) {
249  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
250  }
251  }
252 
253  if (count($sqlwhere) > 0) {
254  $sql .= " WHERE ".implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
255  }
256  if (!empty($sortfield)) {
257  $sql .= $this->db->order($sortfield, $sortorder);
258  }
259  if (!empty($limit)) {
260  $sql .= $this->db->plimit($limit, $offset);
261  }
262 
263  $resql = $this->db->query($sql);
264  if ($resql) {
265  $num = $this->db->num_rows($resql);
266 
267  while ($obj = $this->db->fetch_object($resql)) {
268  $line = new self($this->db);
269 
270  $line->id = $obj->$fieldrowid;
271 
272  $line->code = $obj->code;
273  $line->label = $obj->label;
274  $line->active = $obj->active;
275  }
276  $this->db->free($resql);
277 
278  return $num;
279  } else {
280  $this->errors[] = 'Error '.$this->db->lasterror();
281  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
282 
283  return -1;
284  }
285  }
286 
295  public function update(User $user, $notrigger = false)
296  {
297  $error = 0;
298 
299  dol_syslog(__METHOD__, LOG_DEBUG);
300 
301  $fieldrowid = 'rowid';
302  $fieldlabel = 'label';
303  if ($this->table_element == 'c_stcomm') {
304  $fieldrowid = 'id';
305  $fieldlabel = 'libelle';
306  }
307 
308  // Clean parameters
309 
310  if (isset($this->code)) {
311  $this->code = trim($this->code);
312  }
313  if (isset($this->label)) {
314  $this->label = trim($this->label);
315  }
316  if (isset($this->active)) {
317  $this->active = trim($this->active);
318  }
319 
320  // Check parameters
321  // Put here code to add a control on parameters values
322 
323  // Update request
324  $sql = "UPDATE ".$this->db->prefix().$this->table_element.' SET';
325  $sql .= " code = ".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
326  $sql .= " ".$fieldlabel." = ".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
327  $sql .= " active = ".(isset($this->active) ? $this->active : "null");
328  $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
329 
330  $this->db->begin();
331 
332  $resql = $this->db->query($sql);
333  if (!$resql) {
334  $error++;
335  $this->errors[] = 'Error '.$this->db->lasterror();
336  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
337  }
338 
339  // Uncomment this and change CTYPERESOURCE to your own tag if you
340  // want this action calls a trigger.
341  //if (!$error && !$notrigger) {
342 
343  // // Call triggers
344  // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
345  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
346  // // End call triggers
347  //}
348 
349  // Commit or rollback
350  if ($error) {
351  $this->db->rollback();
352 
353  return -1 * $error;
354  } else {
355  $this->db->commit();
356 
357  return 1;
358  }
359  }
360 
369  public function delete(User $user, $notrigger = false)
370  {
371  dol_syslog(__METHOD__, LOG_DEBUG);
372 
373  $fieldrowid = 'rowid';
374 
375  $error = 0;
376 
377  $this->db->begin();
378 
379  // Uncomment this and change CTYPERESOURCE to your own tag if you
380  // want this action calls a trigger.
381  //if (!$error && !$notrigger) {
382 
383  // // Call triggers
384  // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
385  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
386  // // End call triggers
387  //}
388 
389  // If you need to delete child tables to, you can insert them here
390 
391  if (!$error) {
392  $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
393  $sql .= " WHERE ".$fieldrowid." = ".((int) $this->id);
394 
395  $resql = $this->db->query($sql);
396  if (!$resql) {
397  $error++;
398  $this->errors[] = 'Error '.$this->db->lasterror();
399  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
400  }
401  }
402 
403  // Commit or rollback
404  if ($error) {
405  $this->db->rollback();
406 
407  return -1 * $error;
408  } else {
409  $this->db->commit();
410 
411  return 1;
412  }
413  }
414 
422  public function createFromClone(User $user, $fromid)
423  {
424  dol_syslog(__METHOD__, LOG_DEBUG);
425 
426  $error = 0;
427  $object = new Ctyperesource($this->db);
428 
429  $this->db->begin();
430 
431  // Load source object
432  $object->fetch($fromid);
433  // Reset object
434  $object->id = 0;
435 
436  // Clear fields
437  // ...
438 
439  // Create clone
440  $object->context['createfromclone'] = 'createfromclone';
441  $result = $object->create($user);
442 
443  // Other options
444  if ($result < 0) {
445  $error++;
446  $this->errors = $object->errors;
447  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
448  }
449 
450  unset($object->context['createfromclone']);
451 
452  // End
453  if (!$error) {
454  $this->db->commit();
455 
456  return $object->id;
457  } else {
458  $this->db->rollback();
459 
460  return -1;
461  }
462  }
463 
470  public function initAsSpecimen()
471  {
472  $this->id = 0;
473 
474  $this->code = 'CODE';
475  $this->label = 'Label';
476  $this->active = 1;
477  }
478 }
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.
Definition: user.class.php:45
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...