dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
19  */
20 
34 {
38  public $element = 'ctyperesource';
39 
43  public $table_element = 'c_type_resource';
44 
48  public $lines = array();
49 
50  public $code;
51 
55  public $label;
56 
57  public $active;
58 
59 
65  public function __construct(DoliDB $db)
66  {
67  $this->db = $db;
68  }
69 
78  public function create(User $user, $notrigger = false)
79  {
80  dol_syslog(__METHOD__, LOG_DEBUG);
81 
82  $error = 0;
83 
84  // Clean parameters
85 
86  if (isset($this->code)) {
87  $this->code = trim($this->code);
88  }
89  if (isset($this->label)) {
90  $this->label = trim($this->label);
91  }
92  if (isset($this->active)) {
93  $this->active = trim($this->active);
94  }
95 
96 
97 
98  // Check parameters
99  // Put here code to add control on parameters values
100 
101  // Insert request
102  $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
103 
104  $sql.= 'code,';
105  $sql.= 'label';
106  $sql.= 'active';
107 
108 
109  $sql .= ') VALUES (';
110 
111  $sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").',';
112  $sql .= ' '.(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").',';
113  $sql .= ' '.(! isset($this->active)?'NULL':$this->active);
114 
115 
116  $sql .= ')';
117 
118  $this->db->begin();
119 
120  $resql = $this->db->query($sql);
121  if (!$resql) {
122  $error ++;
123  $this->errors[] = 'Error ' . $this->db->lasterror();
124  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
125  }
126 
127  if (!$error) {
128  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
129 
130  // Uncomment this and change MYOBJECT to your own tag if you
131  // want this action to call a trigger.
132  //if (!$notrigger) {
133 
134  // // Call triggers
135  // $result=$this->call_trigger('MYOBJECT_CREATE',$user);
136  // if ($result < 0) $error++;
137  // // End call triggers
138  //}
139  }
140 
141  // Commit or rollback
142  if ($error) {
143  $this->db->rollback();
144 
145  return - 1 * $error;
146  } else {
147  $this->db->commit();
148 
149  return $this->id;
150  }
151  }
152 
162  public function fetch($id,$code='',$label='')
163  {
164  dol_syslog(__METHOD__, LOG_DEBUG);
165 
166  $sql = 'SELECT';
167  $sql .= ' t.rowid,';
168 
169  $sql .= " t.code,";
170  $sql .= " t.label,";
171  $sql .= " t.active";
172 
173 
174  $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
175  if ($id) $sql.= " WHERE t.id = ".$id;
176  elseif ($code) $sql.= " WHERE t.code = '".$this->db->escape($code)."'";
177  elseif ($label) $sql.= " WHERE t.label = '".$this->db->escape($label)."'";
178 
179 
180  $resql = $this->db->query($sql);
181  if ($resql) {
182  $numrows = $this->db->num_rows($resql);
183  if ($numrows) {
184  $obj = $this->db->fetch_object($resql);
185 
186  $this->id = $obj->rowid;
187 
188  $this->code = $obj->code;
189  $this->label = $obj->label;
190  $this->active = $obj->active;
191  }
192 
193  // Retrieve all extrafields for invoice
194  // fetch optionals attributes and labels
195  // $this->fetch_optionals();
196 
197  // $this->fetch_lines();
198 
199  $this->db->free($resql);
200 
201  if ($numrows) {
202  return 1;
203  } else {
204  return 0;
205  }
206  } else {
207  $this->errors[] = 'Error ' . $this->db->lasterror();
208  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
209 
210  return - 1;
211  }
212  }
213 
226  public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
227  {
228  dol_syslog(__METHOD__, LOG_DEBUG);
229 
230  $sql = 'SELECT';
231  $sql .= ' t.rowid,';
232 
233  $sql .= " t.code,";
234  $sql .= " t.label,";
235  $sql .= " t.active";
236 
237 
238  $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
239 
240  // Manage filter
241  $sqlwhere = array();
242  if (count($filter) > 0) {
243  foreach ($filter as $key => $value) {
244  $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
245  }
246  }
247 
248  if (count($sqlwhere) > 0) {
249  $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere);
250  }
251  if (!empty($sortfield)) {
252  $sql .= $this->db->order($sortfield,$sortorder);
253  }
254  if (!empty($limit)) {
255  $sql .= ' ' . $this->db->plimit($limit, $offset);
256  }
257 
258  $resql = $this->db->query($sql);
259  if ($resql) {
260  $num = $this->db->num_rows($resql);
261 
262  while ($obj = $this->db->fetch_object($resql)) {
263  $line = new self($this->db);
264 
265  $line->id = $obj->rowid;
266 
267  $line->code = $obj->code;
268  $line->label = $obj->label;
269  $line->active = $obj->active;
270  }
271  $this->db->free($resql);
272 
273  return $num;
274  } else {
275  $this->errors[] = 'Error ' . $this->db->lasterror();
276  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
277 
278  return - 1;
279  }
280  }
281 
290  public function update(User $user, $notrigger = false)
291  {
292  $error = 0;
293 
294  dol_syslog(__METHOD__, LOG_DEBUG);
295 
296  // Clean parameters
297 
298  if (isset($this->code)) {
299  $this->code = trim($this->code);
300  }
301  if (isset($this->label)) {
302  $this->label = trim($this->label);
303  }
304  if (isset($this->active)) {
305  $this->active = trim($this->active);
306  }
307 
308  // Check parameters
309  // Put here code to add a control on parameters values
310 
311  // Update request
312  $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
313 
314  $sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").',';
315  $sql .= ' label = '.(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").',';
316  $sql .= ' active = '.(isset($this->active)?$this->active:"null");
317 
318 
319  $sql .= ' WHERE rowid=' . $this->id;
320 
321  $this->db->begin();
322 
323  $resql = $this->db->query($sql);
324  if (!$resql) {
325  $error ++;
326  $this->errors[] = 'Error ' . $this->db->lasterror();
327  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
328  }
329 
330  // Uncomment this and change MYOBJECT to your own tag if you
331  // want this action calls a trigger.
332  //if (!$error && !$notrigger) {
333 
334  // // Call triggers
335  // $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
336  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
337  // // End call triggers
338  //}
339 
340  // Commit or rollback
341  if ($error) {
342  $this->db->rollback();
343 
344  return - 1 * $error;
345  } else {
346  $this->db->commit();
347 
348  return 1;
349  }
350  }
351 
360  public function delete(User $user, $notrigger = false)
361  {
362  dol_syslog(__METHOD__, LOG_DEBUG);
363 
364  $error = 0;
365 
366  $this->db->begin();
367 
368  // Uncomment this and change MYOBJECT to your own tag if you
369  // want this action calls a trigger.
370  //if (!$error && !$notrigger) {
371 
372  // // Call triggers
373  // $result=$this->call_trigger('MYOBJECT_DELETE',$user);
374  // if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
375  // // End call triggers
376  //}
377 
378  // If you need to delete child tables to, you can insert them here
379 
380  if (!$error) {
381  $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
382  $sql .= ' WHERE rowid=' . $this->id;
383 
384  $resql = $this->db->query($sql);
385  if (!$resql) {
386  $error ++;
387  $this->errors[] = 'Error ' . $this->db->lasterror();
388  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
389  }
390  }
391 
392  // Commit or rollback
393  if ($error) {
394  $this->db->rollback();
395 
396  return - 1 * $error;
397  } else {
398  $this->db->commit();
399 
400  return 1;
401  }
402  }
403 
411  public function createFromClone($fromid)
412  {
413  dol_syslog(__METHOD__, LOG_DEBUG);
414 
415  global $user;
416  $error = 0;
417  $object = new Ctyperesource($this->db);
418 
419  $this->db->begin();
420 
421  // Load source object
422  $object->fetch($fromid);
423  // Reset object
424  $object->id = 0;
425 
426  // Clear fields
427  // ...
428 
429  // Create clone
430  $object->context['createfromclone'] = 'createfromclone';
431  $result = $object->create($user);
432 
433  // Other options
434  if ($result < 0) {
435  $error ++;
436  $this->errors = $object->errors;
437  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
438  }
439 
440  unset($object->context['createfromclone']);
441 
442  // End
443  if (!$error) {
444  $this->db->commit();
445 
446  return $object->id;
447  } else {
448  $this->db->rollback();
449 
450  return -1;
451  }
452  }
453 
460  public function initAsSpecimen()
461  {
462  $this->id = 0;
463 
464  $this->code = '';
465  $this->label = '';
466  $this->active = '';
467  }
468 }
469 
474 {
478  public $id;
479 
483  public $code;
484 
488  public $label;
489 
490  public $active;
491 
495 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
update(User $user, $notrigger=false)
Update object into database.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
create(User $user, $notrigger=false)
Create object into database.
createFromClone($fromid)
Load an object from its id and create a new one in database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Class CtyperesourceLine.
fetch($id, $code='', $label='')
Load object in memory from the database.
Class Ctyperesource.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.