dolibarr  13.0.2
coreobject.class.php
Go to the documentation of this file.
1 <?php
2 /* EXPERIMENTAL
3  *
4  * Copyright (C) 2016 ATM Consulting <support@atm-consulting.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
28 // TODO Remove this class (used in Expensereportik and ExpenseReportRule
32 class CoreObject extends CommonObject
33 {
34  public $withChild = true;
35 
39  protected $fields = array();
40 
46  public function __construct(DoliDB &$db)
47  {
48  $this->db = $db;
49  }
50 
56  protected function init()
57  {
58  $this->id = 0;
59  $this->datec = 0;
60  $this->tms = 0;
61 
62  if (!empty($this->fields))
63  {
64  foreach ($this->fields as $field=>$info)
65  {
66  if ($this->isDate($info)) $this->{$field} = time();
67  elseif ($this->isArray($info)) $this->{$field} = array();
68  elseif ($this->isInt($info)) $this->{$field} = (int) 0;
69  elseif ($this->isFloat($info)) $this->{$field} = (double) 0;
70  else $this->{$field} = '';
71  }
72 
73  $this->to_delete = false;
74  $this->is_clone = false;
75 
76  return true;
77  } else {
78  return false;
79  }
80  }
81 
89  private function checkFieldType($field, $type)
90  {
91  if (isset($this->fields[$field]) && method_exists($this, 'is_'.$type))
92  {
93  return $this->{'is_'.$type}($this->fields[$field]);
94  } else {
95  return false;
96  }
97  }
98 
106  public function fetch($id, $loadChild = true)
107  {
108  $res = $this->fetchCommon($id);
109  if ($res > 0) {
110  if ($loadChild) $this->fetchChild();
111  }
112 
113  return $res;
114  }
115 
116 
126  public function addChild($tabName, $id = 0, $key = 'id', $try_to_load = false)
127  {
128  if (!empty($id))
129  {
130  foreach ($this->{$tabName} as $k=>&$object)
131  {
132  if ($object->{$key} === $id) return $k;
133  }
134  }
135 
136  $k = count($this->{$tabName});
137 
138  $className = ucfirst($tabName);
139  $this->{$tabName}[$k] = new $className($this->db);
140  if ($id > 0 && $key === 'id' && $try_to_load)
141  {
142  $this->{$tabName}[$k]->fetch($id);
143  }
144 
145  return $k;
146  }
147 
148 
157  public function removeChild($tabName, $id, $key = 'id')
158  {
159  foreach ($this->{$tabName} as &$object)
160  {
161  if ($object->{$key} == $id)
162  {
163  $object->to_delete = true;
164  return true;
165  }
166  }
167  return false;
168  }
169 
170 
176  public function fetchChild()
177  {
178  if ($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
179  {
180  foreach ($this->childtables as &$childTable)
181  {
182  $className = ucfirst($childTable);
183 
184  $this->{$className} = array();
185 
186  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$childTable.' WHERE '.$this->fk_element.' = '.$this->id;
187  $res = $this->db->query($sql);
188 
189  if ($res)
190  {
191  while ($obj = $this->db->fetch_object($res))
192  {
193  $o = new $className($this->db);
194  $o->fetch($obj->rowid);
195 
196  $this->{$className}[] = $o;
197  }
198  } else {
199  $this->errors[] = $this->db->lasterror();
200  }
201  }
202  }
203  }
204 
211  public function saveChild(User &$user)
212  {
213  if ($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
214  {
215  foreach ($this->childtables as &$childTable)
216  {
217  $className = ucfirst($childTable);
218  if (!empty($this->{$className}))
219  {
220  foreach ($this->{$className} as $i => &$object)
221  {
222  $object->{$this->fk_element} = $this->id;
223 
224  $object->update($user);
225  if ($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete == true) unset($this->{$className}[$i]);
226  }
227  }
228  }
229  }
230  }
231 
232 
239  public function update(User &$user)
240  {
241  if (empty($this->id)) return $this->create($user); // To test, with that, no need to test on high level object, the core decide it, update just needed
242  elseif (isset($this->to_delete) && $this->to_delete == true) return $this->delete($user);
243 
244  $error = 0;
245  $this->db->begin();
246 
247  $res = $this->updateCommon($user);
248  if ($res)
249  {
250  $result = $this->call_trigger(strtoupper($this->element).'_UPDATE', $user);
251  if ($result < 0) $error++;
252  else $this->saveChild($user);
253  } else {
254  $error++;
255  $this->error = $this->db->lasterror();
256  $this->errors[] = $this->error;
257  }
258 
259  if (empty($error))
260  {
261  $this->db->commit();
262  return $this->id;
263  } else {
264  $this->db->rollback();
265  return -1;
266  }
267  }
268 
275  public function create(User $user)
276  {
277  if ($this->id > 0) return $this->update($user);
278 
279  $error = 0;
280  $this->db->begin();
281 
282  $res = $this->createCommon($user);
283  if ($res)
284  {
285  $this->id = $this->db->last_insert_id($this->table_element);
286 
287  $result = $this->call_trigger(strtoupper($this->element).'_CREATE', $user);
288  if ($result < 0) $error++;
289  else $this->saveChild($user);
290  } else {
291  $error++;
292  $this->error = $this->db->lasterror();
293  $this->errors[] = $this->error;
294  }
295 
296  if (empty($error))
297  {
298  $this->db->commit();
299  return $this->id;
300  } else {
301  $this->db->rollback();
302  return -1;
303  }
304  }
305 
312  public function delete(User &$user)
313  {
314  if ($this->id <= 0) return 0;
315 
316  $error = 0;
317  $this->db->begin();
318 
319  $result = $this->call_trigger(strtoupper($this->element).'_DELETE', $user);
320  if ($result < 0) $error++;
321 
322  if (!$error)
323  {
324  $this->deleteCommon($user);
325  if ($this->withChild && !empty($this->childtables))
326  {
327  foreach ($this->childtables as &$childTable)
328  {
329  $className = ucfirst($childTable);
330  if (!empty($this->{$className}))
331  {
332  foreach ($this->{$className} as &$object)
333  {
334  $object->delete($user);
335  }
336  }
337  }
338  }
339  }
340 
341  if (empty($error))
342  {
343  $this->db->commit();
344  return 1;
345  } else {
346  $this->error = $this->db->lasterror();
347  $this->errors[] = $this->error;
348  $this->db->rollback();
349  return -1;
350  }
351  }
352 
353 
361  public function getDate($field, $format = '')
362  {
363  if (empty($this->{$field})) return '';
364  else {
365  return dol_print_date($this->{$field}, $format);
366  }
367  }
368 
376  public function setDate($field, $date)
377  {
378  if (empty($date))
379  {
380  $this->{$field} = 0;
381  } else {
382  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
383  $this->{$field} = dol_stringtotime($date);
384  }
385 
386  return $this->{$field};
387  }
388 
389 
396  public function setValues(&$Tab)
397  {
398  foreach ($Tab as $key => $value)
399  {
400  if ($this->checkFieldType($key, 'date'))
401  {
402  $this->setDate($key, $value);
403  } elseif ($this->checkFieldType($key, 'float'))
404  {
405  $this->{$key} = (double) price2num($value);
406  } elseif ($this->checkFieldType($key, 'int')) {
407  $this->{$key} = (int) price2num($value);
408  } else {
409  $this->{$key} = dol_string_nohtmltag($value);
410  }
411  }
412 
413  return 1;
414  }
415 }
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
create(User $user)
Function to create object in database.
isFloat($info)
Function test if type is float.
isInt($info)
Function test if type is integer.
foreach($object->fields as $key=> $val) if(is_array($extrafields->attributes[$object->table_element]['label'])&&count($extrafields->attributes[$object->table_element]['label']) > 0) $object fields
removeChild($tabName, $id, $key= 'id')
Function to set a child as to delete.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
saveChild(User &$user)
Function to update children data.
isDate($info)
Function test if type is date.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:319
update(User &$user)
Function to update object or create or delete if needed.
setDate($field, $date)
Function to set date in field.
isArray($info)
Function test if type is array.
$conf db
API class for accounts.
Definition: inc.php:54
__construct(DoliDB &$db)
Constructor.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
checkFieldType($field, $type)
Test type of field.
fetchChild()
Function to fetch children objects.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDate($field, $format= '')
Function to get a formatted date.
init()
Function to init fields.
CoreObject.
fetch($id, $loadChild=true)
Get object and children from database.
setValues(&$Tab)
Function to update current object.
addChild($tabName, $id=0, $key= 'id', $try_to_load=false)
Function to instantiate a new child.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetchCommon($id, $ref=null, $morewhere= '')
Load object in memory from the database.