dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
18  */
19 
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 
28 class CoreObject extends CommonObject
29 {
30  public $withChild = true;
31 
35  protected $fields=array();
36 
42  function __construct(DoliDB &$db)
43  {
44  $this->db = $db;
45  }
46 
52  protected function init()
53  {
54  $this->id = 0;
55  $this->datec = 0;
56  $this->tms = 0;
57 
58  if (!empty($this->fields))
59  {
60  foreach ($this->fields as $field=>$info)
61  {
62  if ($this->isDate($info)) $this->{$field} = time();
63  elseif ($this->isArray($info)) $this->{$field} = array();
64  elseif ($this->isInt($info)) $this->{$field} = (int) 0;
65  elseif ($this->isFloat($info)) $this->{$field} = (double) 0;
66  else $this->{$field} = '';
67  }
68 
69  $this->to_delete=false;
70  $this->is_clone=false;
71 
72  return true;
73  }
74  else
75  {
76  return false;
77  }
78  }
79 
87  private function checkFieldType($field, $type)
88  {
89  if (isset($this->fields[$field]) && method_exists($this, 'is_'.$type))
90  {
91  return $this->{'is_'.$type}($this->fields[$field]);
92  }
93  else
94  {
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  }
199  else
200  {
201  $this->errors[] = $this->db->lasterror();
202  }
203  }
204  }
205  }
206 
213  public function saveChild(User &$user)
214  {
215  if($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
216  {
217  foreach($this->childtables as &$childTable)
218  {
219  $className = ucfirst($childTable);
220  if(!empty($this->{$className}))
221  {
222  foreach($this->{$className} as $i => &$object)
223  {
224  $object->{$this->fk_element} = $this->id;
225 
226  $object->update($user);
227  if($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete==true) unset($this->{$className}[$i]);
228  }
229  }
230  }
231  }
232  }
233 
234 
241  public function update(User &$user)
242  {
243  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
244  elseif (isset($this->to_delete) && $this->to_delete==true) return $this->delete($user);
245 
246  $error = 0;
247  $this->db->begin();
248 
249  $res = $this->updateCommon($user);
250  if ($res)
251  {
252  $result = $this->call_trigger(strtoupper($this->element). '_UPDATE', $user);
253  if ($result < 0) $error++;
254  else $this->saveChild($user);
255  }
256  else
257  {
258  $error++;
259  $this->error = $this->db->lasterror();
260  $this->errors[] = $this->error;
261  }
262 
263  if (empty($error))
264  {
265  $this->db->commit();
266  return $this->id;
267  }
268  else
269  {
270  $this->db->rollback();
271  return -1;
272  }
273  }
274 
281  public function create(User &$user)
282  {
283  if($this->id > 0) return $this->update($user);
284 
285  $error = 0;
286  $this->db->begin();
287 
288  $res = $this->createCommon($user);
289  if($res)
290  {
291  $this->id = $this->db->last_insert_id($this->table_element);
292 
293  $result = $this->call_trigger(strtoupper($this->element). '_CREATE', $user);
294  if ($result < 0) $error++;
295  else $this->saveChild($user);
296  }
297  else
298  {
299  $error++;
300  $this->error = $this->db->lasterror();
301  $this->errors[] = $this->error;
302  }
303 
304  if (empty($error))
305  {
306  $this->db->commit();
307  return $this->id;
308  }
309  else
310  {
311  $this->db->rollback();
312  return -1;
313  }
314  }
315 
322  public function delete(User &$user)
323  {
324  if ($this->id <= 0) return 0;
325 
326  $error = 0;
327  $this->db->begin();
328 
329  $result = $this->call_trigger(strtoupper($this->element). '_DELETE', $user);
330  if ($result < 0) $error++;
331 
332  if (!$error)
333  {
334  $this->deleteCommon($user);
335  if($this->withChild && !empty($this->childtables))
336  {
337  foreach($this->childtables as &$childTable)
338  {
339  $className = ucfirst($childTable);
340  if (!empty($this->{$className}))
341  {
342  foreach($this->{$className} as &$object)
343  {
344  $object->delete($user);
345  }
346  }
347  }
348  }
349  }
350 
351  if (empty($error))
352  {
353  $this->db->commit();
354  return 1;
355  }
356  else
357  {
358  $this->error = $this->db->lasterror();
359  $this->errors[] = $this->error;
360  $this->db->rollback();
361  return -1;
362  }
363  }
364 
365 
373  public function getDate($field, $format='')
374  {
375  if(empty($this->{$field})) return '';
376  else
377  {
378  return dol_print_date($this->{$field}, $format);
379  }
380  }
381 
389  public function setDate($field, $date)
390  {
391  if (empty($date))
392  {
393  $this->{$field} = 0;
394  }
395  else
396  {
397  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
398  $this->{$field} = dol_stringtotime($date);
399  }
400 
401  return $this->{$field};
402  }
403 
404 
411  public function setValues(&$Tab)
412  {
413  foreach ($Tab as $key => $value)
414  {
415  if($this->checkFieldType($key, 'date'))
416  {
417  $this->setDate($key, $value);
418  }
419  else if( $this->checkFieldType($key, 'array'))
420  {
421  $this->{$key} = $value;
422  }
423  else if( $this->checkFieldType($key, 'float') )
424  {
425  $this->{$key} = (double) price2num($value);
426  }
427  else if( $this->checkFieldType($key, 'int') ) {
428  $this->{$key} = (int) price2num($value);
429  }
430  else
431  {
432  $this->{$key} = $value;
433  }
434  }
435 
436  return 1;
437  }
438 }
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
create(User &$user)
Function to create object in database.
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
isFloat($info)
Function test if type is float.
isInt($info)
Function test if type is integer.
Class to manage Dolibarr users.
Definition: user.class.php:41
Class to manage Dolibarr database access.
saveChild(User &$user)
Function to update children data.
isDate($info)
Function test if type is date.
createCommon(User $user, $notrigger=false)
Create object into database.
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:295
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.
addChild($tabName, $id=0, $key='id', $try_to_load=false)
Function to instantiate a new child.
__construct(DoliDB &$db)
Constructor.
updateCommon(User $user, $notrigger=false)
Update object into database.
checkFieldType($field, $type)
Test type of field.
fetchChild()
Function to fetch children objects.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
init()
Function to init fields.
fetch($id, $loadChild=true)
Get object and children from database.
removeChild($tabName, $id, $key='id')
Function to set a child as to delete.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
setValues(&$Tab)
Function to update current object.
getDate($field, $format='')
Function to get a formatted date.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)