dolibarr  7.0.0-beta
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  }
80 
88  private function checkFieldType($field, $type)
89  {
90  if (isset($this->fields[$field]) && method_exists($this, 'is_'.$type))
91  {
92  return $this->{'is_'.$type}($this->fields[$field]);
93  }
94  else
95  {
96  return false;
97  }
98  }
99 
107  public function fetch($id, $loadChild = true)
108  {
109  $res = $this->fetchCommon($id);
110  if($res>0) {
111  if ($loadChild) $this->fetchChild();
112  }
113 
114  return $res;
115  }
116 
117 
127  public function addChild($tabName, $id=0, $key='id', $try_to_load = false)
128  {
129  if(!empty($id))
130  {
131  foreach($this->{$tabName} as $k=>&$object)
132  {
133  if($object->{$key} === $id) return $k;
134  }
135  }
136 
137  $k = count($this->{$tabName});
138 
139  $className = ucfirst($tabName);
140  $this->{$tabName}[$k] = new $className($this->db);
141  if($id>0 && $key==='id' && $try_to_load)
142  {
143  $this->{$tabName}[$k]->fetch($id);
144  }
145 
146  return $k;
147  }
148 
149 
158  public function removeChild($tabName, $id, $key='id')
159  {
160  foreach ($this->{$tabName} as &$object)
161  {
162  if ($object->{$key} == $id)
163  {
164  $object->to_delete = true;
165  return true;
166  }
167  }
168  return false;
169  }
170 
171 
175  public function fetchChild()
176  {
177  if($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
178  {
179  foreach($this->childtables as &$childTable)
180  {
181  $className = ucfirst($childTable);
182 
183  $this->{$className}=array();
184 
185  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$childTable.' WHERE '.$this->fk_element.' = '.$this->id;
186  $res = $this->db->query($sql);
187 
188  if($res)
189  {
190  while($obj = $this->db->fetch_object($res))
191  {
192  $o=new $className($this->db);
193  $o->fetch($obj->rowid);
194 
195  $this->{$className}[] = $o;
196  }
197  }
198  else
199  {
200  $this->errors[] = $this->db->lasterror();
201  }
202  }
203  }
204  }
205 
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  }
254  else
255  {
256  $error++;
257  $this->error = $this->db->lasterror();
258  $this->errors[] = $this->error;
259  }
260 
261  if (empty($error))
262  {
263  $this->db->commit();
264  return $this->id;
265  }
266  else
267  {
268  $this->db->rollback();
269  return -1;
270  }
271 
272  }
273 
280  public function create(User &$user)
281  {
282  if($this->id > 0) return $this->update($user);
283 
284  $error = 0;
285  $this->db->begin();
286 
287  $res = $this->createCommon($user);
288  if($res)
289  {
290  $this->id = $this->db->last_insert_id($this->table_element);
291 
292  $result = $this->call_trigger(strtoupper($this->element). '_CREATE', $user);
293  if ($result < 0) $error++;
294  else $this->saveChild($user);
295  }
296  else
297  {
298  $error++;
299  $this->error = $this->db->lasterror();
300  $this->errors[] = $this->error;
301  }
302 
303  if (empty($error))
304  {
305  $this->db->commit();
306  return $this->id;
307  }
308  else
309  {
310  $this->db->rollback();
311  return -1;
312  }
313  }
314 
321  public function delete(User &$user)
322  {
323  if ($this->id <= 0) return 0;
324 
325  $error = 0;
326  $this->db->begin();
327 
328  $result = $this->call_trigger(strtoupper($this->element). '_DELETE', $user);
329  if ($result < 0) $error++;
330 
331  if (!$error)
332  {
333  $this->deleteCommon($user);
334  if($this->withChild && !empty($this->childtables))
335  {
336  foreach($this->childtables as &$childTable)
337  {
338  $className = ucfirst($childTable);
339  if (!empty($this->{$className}))
340  {
341  foreach($this->{$className} as &$object)
342  {
343  $object->delete($user);
344  }
345  }
346  }
347  }
348  }
349 
350  if (empty($error))
351  {
352  $this->db->commit();
353  return 1;
354  }
355  else
356  {
357  $this->error = $this->db->lasterror();
358  $this->errors[] = $this->error;
359  $this->db->rollback();
360  return -1;
361  }
362  }
363 
364 
372  public function getDate($field, $format='')
373  {
374  if(empty($this->{$field})) return '';
375  else
376  {
377  return dol_print_date($this->{$field}, $format);
378  }
379  }
380 
388  public function setDate($field, $date)
389  {
390  if (empty($date))
391  {
392  $this->{$field} = 0;
393  }
394  else
395  {
396  require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
397  $this->{$field} = dol_stringtotime($date);
398  }
399 
400  return $this->{$field};
401  }
402 
403 
410  public function setValues(&$Tab)
411  {
412  foreach ($Tab as $key => $value)
413  {
414  if($this->checkFieldType($key, 'date'))
415  {
416  $this->setDate($key, $value);
417  }
418  else if( $this->checkFieldType($key, 'array'))
419  {
420  $this->{$key} = $value;
421  }
422  else if( $this->checkFieldType($key, 'float') )
423  {
424  $this->{$key} = (double) price2num($value);
425  }
426  else if( $this->checkFieldType($key, 'int') ) {
427  $this->{$key} = (int) price2num($value);
428  }
429  else
430  {
431  $this->{$key} = $value;
432  }
433  }
434 
435  return 1;
436  }
437 
438 }
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.
Class to manage Dolibarr users.
Definition: user.class.php:39
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:286
update(User &$user)
Function to update object or create or delete if needed.
fetchCommon($id, $ref=null)
Load object in memory from the database.
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.
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.
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 '...
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)