dolibarr  16.0.5
ctyperesource.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 = 'ctyperesource';
37 
41  public $table_element = 'c_type_resource';
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 
76  public function create(User $user, $notrigger = false)
77  {
78  dol_syslog(__METHOD__, LOG_DEBUG);
79 
80  $error = 0;
81 
82  // Clean parameters
83 
84  if (isset($this->code)) {
85  $this->code = trim($this->code);
86  }
87  if (isset($this->label)) {
88  $this->label = trim($this->label);
89  }
90  if (isset($this->active)) {
91  $this->active = trim($this->active);
92  }
93 
94  // Insert request
95  $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
96  $sql .= 'code,';
97  $sql .= 'label';
98  $sql .= 'active';
99  $sql .= ') VALUES (';
100  $sql .= ' '.(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").',';
101  $sql .= ' '.(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").',';
102  $sql .= ' '.(!isset($this->active) ? 'NULL' : $this->active);
103  $sql .= ')';
104 
105  $this->db->begin();
106 
107  $resql = $this->db->query($sql);
108  if (!$resql) {
109  $error++;
110  $this->errors[] = 'Error '.$this->db->lasterror();
111  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
112  }
113 
114  if (!$error) {
115  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
116 
117  // Uncomment this and change CTYPERESOURCE to your own tag if you
118  // want this action to call a trigger.
119  //if (!$notrigger) {
120 
121  // // Call triggers
122  // $result=$this->call_trigger('CTYPERESOURCE_CREATE',$user);
123  // if ($result < 0) $error++;
124  // // End call triggers
125  //}
126  }
127 
128  // Commit or rollback
129  if ($error) {
130  $this->db->rollback();
131 
132  return -1 * $error;
133  } else {
134  $this->db->commit();
135 
136  return $this->id;
137  }
138  }
139 
149  public function fetch($id, $code = '', $label = '')
150  {
151  dol_syslog(__METHOD__, LOG_DEBUG);
152 
153  $sql = "SELECT";
154  $sql .= " t.rowid,";
155  $sql .= " t.code,";
156  $sql .= " t.label,";
157  $sql .= " t.active";
158  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
159  if ($id) {
160  $sql .= " WHERE t.id = ".((int) $id);
161  } elseif ($code) {
162  $sql .= " WHERE t.code = '".$this->db->escape($code)."'";
163  } elseif ($label) {
164  $sql .= " WHERE t.label = '".$this->db->escape($label)."'";
165  }
166 
167  $resql = $this->db->query($sql);
168  if ($resql) {
169  $numrows = $this->db->num_rows($resql);
170  if ($numrows) {
171  $obj = $this->db->fetch_object($resql);
172 
173  $this->id = $obj->rowid;
174 
175  $this->code = $obj->code;
176  $this->label = $obj->label;
177  $this->active = $obj->active;
178  }
179 
180  // Retrieve all extrafields for invoice
181  // fetch optionals attributes and labels
182  // $this->fetch_optionals();
183 
184  // $this->fetch_lines();
185 
186  $this->db->free($resql);
187 
188  if ($numrows) {
189  return 1;
190  } else {
191  return 0;
192  }
193  } else {
194  $this->errors[] = 'Error '.$this->db->lasterror();
195  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
196 
197  return -1;
198  }
199  }
200 
213  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
214  {
215  dol_syslog(__METHOD__, LOG_DEBUG);
216 
217  $sql = "SELECT";
218  $sql .= " t.rowid,";
219  $sql .= " t.code,";
220  $sql .= " t.label,";
221  $sql .= " t.active";
222  $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
223 
224  // Manage filter
225  $sqlwhere = array();
226  if (count($filter) > 0) {
227  foreach ($filter as $key => $value) {
228  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
229  }
230  }
231 
232  if (count($sqlwhere) > 0) {
233  $sql .= ' WHERE '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
234  }
235  if (!empty($sortfield)) {
236  $sql .= $this->db->order($sortfield, $sortorder);
237  }
238  if (!empty($limit)) {
239  $sql .= $this->db->plimit($limit, $offset);
240  }
241 
242  $resql = $this->db->query($sql);
243  if ($resql) {
244  $num = $this->db->num_rows($resql);
245 
246  while ($obj = $this->db->fetch_object($resql)) {
247  $line = new self($this->db);
248 
249  $line->id = $obj->rowid;
250 
251  $line->code = $obj->code;
252  $line->label = $obj->label;
253  $line->active = $obj->active;
254  }
255  $this->db->free($resql);
256 
257  return $num;
258  } else {
259  $this->errors[] = 'Error '.$this->db->lasterror();
260  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
261 
262  return -1;
263  }
264  }
265 
274  public function update(User $user, $notrigger = false)
275  {
276  $error = 0;
277 
278  dol_syslog(__METHOD__, LOG_DEBUG);
279 
280  // Clean parameters
281 
282  if (isset($this->code)) {
283  $this->code = trim($this->code);
284  }
285  if (isset($this->label)) {
286  $this->label = trim($this->label);
287  }
288  if (isset($this->active)) {
289  $this->active = trim($this->active);
290  }
291 
292  // Check parameters
293  // Put here code to add a control on parameters values
294 
295  // Update request
296  $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
297  $sql .= ' code = '.(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").',';
298  $sql .= ' label = '.(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").',';
299  $sql .= ' active = '.(isset($this->active) ? $this->active : "null");
300  $sql .= ' WHERE rowid='.((int) $this->id);
301 
302  $this->db->begin();
303 
304  $resql = $this->db->query($sql);
305  if (!$resql) {
306  $error++;
307  $this->errors[] = 'Error '.$this->db->lasterror();
308  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
309  }
310 
311  // Uncomment this and change CTYPERESOURCE to your own tag if you
312  // want this action calls a trigger.
313  //if (!$error && !$notrigger) {
314 
315  // // Call triggers
316  // $result=$this->call_trigger('CTYPERESOURCE_MODIFY',$user);
317  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
318  // // End call triggers
319  //}
320 
321  // Commit or rollback
322  if ($error) {
323  $this->db->rollback();
324 
325  return -1 * $error;
326  } else {
327  $this->db->commit();
328 
329  return 1;
330  }
331  }
332 
341  public function delete(User $user, $notrigger = false)
342  {
343  dol_syslog(__METHOD__, LOG_DEBUG);
344 
345  $error = 0;
346 
347  $this->db->begin();
348 
349  // Uncomment this and change CTYPERESOURCE to your own tag if you
350  // want this action calls a trigger.
351  //if (!$error && !$notrigger) {
352 
353  // // Call triggers
354  // $result=$this->call_trigger('CTYPERESOURCE_DELETE',$user);
355  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
356  // // End call triggers
357  //}
358 
359  // If you need to delete child tables to, you can insert them here
360 
361  if (!$error) {
362  $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
363  $sql .= ' WHERE rowid='.((int) $this->id);
364 
365  $resql = $this->db->query($sql);
366  if (!$resql) {
367  $error++;
368  $this->errors[] = 'Error '.$this->db->lasterror();
369  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
370  }
371  }
372 
373  // Commit or rollback
374  if ($error) {
375  $this->db->rollback();
376 
377  return -1 * $error;
378  } else {
379  $this->db->commit();
380 
381  return 1;
382  }
383  }
384 
392  public function createFromClone(User $user, $fromid)
393  {
394  dol_syslog(__METHOD__, LOG_DEBUG);
395 
396  $error = 0;
397  $object = new Ctyperesource($this->db);
398 
399  $this->db->begin();
400 
401  // Load source object
402  $object->fetch($fromid);
403  // Reset object
404  $object->id = 0;
405 
406  // Clear fields
407  // ...
408 
409  // Create clone
410  $object->context['createfromclone'] = 'createfromclone';
411  $result = $object->create($user);
412 
413  // Other options
414  if ($result < 0) {
415  $error++;
416  $this->errors = $object->errors;
417  dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
418  }
419 
420  unset($object->context['createfromclone']);
421 
422  // End
423  if (!$error) {
424  $this->db->commit();
425 
426  return $object->id;
427  } else {
428  $this->db->rollback();
429 
430  return -1;
431  }
432  }
433 
440  public function initAsSpecimen()
441  {
442  $this->id = 0;
443 
444  $this->code = '';
445  $this->label = '';
446  $this->active = '';
447  }
448 }
449 
454 {
458  public $id;
459 
463  public $code;
464 
468  public $label;
469 
470  public $active;
471 
475 }
Ctyperesource\create
create(User $user, $notrigger=false)
Create object into database.
Definition: ctyperesource.class.php:76
db
$conf db
API class for accounts.
Definition: inc.php:41
Ctyperesource\fetch
fetch($id, $code='', $label='')
Load object in memory from the database.
Definition: ctyperesource.class.php:149
Ctyperesource\fetchAll
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.
Definition: ctyperesource.class.php:213
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Ctyperesource
Class Ctyperesource.
Definition: ctyperesource.class.php:31
Ctyperesource\__construct
__construct(DoliDB $db)
Constructor.
Definition: ctyperesource.class.php:63
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
Ctyperesource\update
update(User $user, $notrigger=false)
Update object into database.
Definition: ctyperesource.class.php:274
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CtyperesourceLine
Class CtyperesourceLine.
Definition: ctyperesource.class.php:453
Ctyperesource\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: ctyperesource.class.php:440
User
Class to manage Dolibarr users.
Definition: user.class.php:44
$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
Ctyperesource\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: ctyperesource.class.php:392