dolibarr  7.0.0-beta
ctyperesource.class.php
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';
42  public $table_element = 'c_type_resource';
43 
47  public $lines = array();
48 
52  public $code;
53  public $label;
54  public $active;
55 
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  if (!$notrigger) {
131  // Uncomment this and change MYOBJECT to your own tag if you
132  // want this action to call a trigger.
133 
135  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
136  //if ($result < 0) $error++;
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  }
194 
195  // Retrieve all extrafields for invoice
196  // fetch optionals attributes and labels
197  /*
198  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
199  $extrafields=new ExtraFields($this->db);
200  $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
201  $this->fetch_optionals($this->id,$extralabels);
202  */
203 
204  // $this->fetch_lines();
205 
206  $this->db->free($resql);
207 
208  if ($numrows) {
209  return 1;
210  } else {
211  return 0;
212  }
213  } else {
214  $this->errors[] = 'Error ' . $this->db->lasterror();
215  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
216 
217  return - 1;
218  }
219  }
220 
233  public function fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter = array(), $filtermode='AND')
234  {
235  dol_syslog(__METHOD__, LOG_DEBUG);
236 
237  $sql = 'SELECT';
238  $sql .= ' t.rowid,';
239 
240  $sql .= " t.code,";
241  $sql .= " t.label,";
242  $sql .= " t.active";
243 
244 
245  $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element. ' as t';
246 
247  // Manage filter
248  $sqlwhere = array();
249  if (count($filter) > 0) {
250  foreach ($filter as $key => $value) {
251  $sqlwhere [] = $key . ' LIKE \'%' . $this->db->escape($value) . '%\'';
252  }
253  }
254 
255  if (count($sqlwhere) > 0) {
256  $sql .= ' WHERE ' . implode(' '.$filtermode.' ', $sqlwhere);
257  }
258  if (!empty($sortfield)) {
259  $sql .= $this->db->order($sortfield,$sortorder);
260  }
261  if (!empty($limit)) {
262  $sql .= ' ' . $this->db->plimit($limit, $offset);
263  }
264 
265  $resql = $this->db->query($sql);
266  if ($resql) {
267  $num = $this->db->num_rows($resql);
268 
269  while ($obj = $this->db->fetch_object($resql)) {
270  $line = new self($this->db);
271 
272  $line->id = $obj->rowid;
273 
274  $line->code = $obj->code;
275  $line->label = $obj->label;
276  $line->active = $obj->active;
277 
278 
279  }
280  $this->db->free($resql);
281 
282  return $num;
283  } else {
284  $this->errors[] = 'Error ' . $this->db->lasterror();
285  dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
286 
287  return - 1;
288  }
289  }
290 
299  public function update(User $user, $notrigger = false)
300  {
301  $error = 0;
302 
303  dol_syslog(__METHOD__, LOG_DEBUG);
304 
305  // Clean parameters
306 
307  if (isset($this->code)) {
308  $this->code = trim($this->code);
309  }
310  if (isset($this->label)) {
311  $this->label = trim($this->label);
312  }
313  if (isset($this->active)) {
314  $this->active = trim($this->active);
315  }
316 
317  // Check parameters
318  // Put here code to add a control on parameters values
319 
320  // Update request
321  $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
322 
323  $sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").',';
324  $sql .= ' label = '.(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").',';
325  $sql .= ' active = '.(isset($this->active)?$this->active:"null");
326 
327 
328  $sql .= ' WHERE rowid=' . $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  if (!$error && !$notrigger) {
340  // Uncomment this and change MYOBJECT to your own tag if you
341  // want this action calls a trigger.
342 
344  //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
345  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
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  $error = 0;
374 
375  $this->db->begin();
376 
377  if (!$error) {
378  if (!$notrigger) {
379  // Uncomment this and change MYOBJECT to your own tag if you
380  // want this action calls a trigger.
381 
383  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
384  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
386  }
387  }
388 
389  // If you need to delete child tables to, you can insert them here
390 
391  if (!$error) {
392  $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
393  $sql .= ' WHERE rowid=' . $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($fromid)
423  {
424  dol_syslog(__METHOD__, LOG_DEBUG);
425 
426  global $user;
427  $error = 0;
428  $object = new Ctyperesource($this->db);
429 
430  $this->db->begin();
431 
432  // Load source object
433  $object->fetch($fromid);
434  // Reset object
435  $object->id = 0;
436 
437  // Clear fields
438  // ...
439 
440  // Create clone
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  // End
451  if (!$error) {
452  $this->db->commit();
453 
454  return $object->id;
455  } else {
456  $this->db->rollback();
457 
458  return - 1;
459  }
460  }
461 
468  public function initAsSpecimen()
469  {
470  $this->id = 0;
471 
472  $this->code = '';
473  $this->label = '';
474  $this->active = '';
475  }
476 
477 }
478 
483 {
487  public $id;
492  public $code;
493  public $label;
494  public $active;
495 
500 }
update(User $user, $notrigger=false)
Update object into database.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr users.
Definition: user.class.php:39
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.
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:1013
Class Ctyperesource.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load object in memory from the database.