26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
34 public $withChild =
true;
39 protected $fields = array();
64 foreach ($this->
fields as $field=>$info)
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} =
'';
73 $this->to_delete =
false;
74 $this->is_clone =
false;
91 if (isset($this->
fields[$field]) && method_exists($this,
'is_'.$type))
93 return $this->{
'is_'.$type}($this->
fields[$field]);
106 public function fetch($id, $loadChild =
true)
126 public function addChild($tabName, $id = 0, $key =
'id', $try_to_load =
false)
130 foreach ($this->{$tabName} as $k=>&$object)
132 if ($object->{$key} === $id)
return $k;
136 $k = count($this->{$tabName});
138 $className = ucfirst($tabName);
139 $this->{$tabName}[$k] =
new $className($this->
db);
140 if ($id > 0 && $key ===
'id' && $try_to_load)
142 $this->{$tabName}[$k]->fetch($id);
159 foreach ($this->{$tabName} as &$object)
161 if ($object->{$key} == $id)
163 $object->to_delete =
true;
178 if ($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
180 foreach ($this->childtables as &$childTable)
182 $className = ucfirst($childTable);
184 $this->{$className} = array();
186 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.$childTable.
' WHERE '.$this->fk_element.
' = '.$this->id;
187 $res = $this->
db->query($sql);
191 while ($obj = $this->
db->fetch_object($res))
193 $o =
new $className($this->
db);
194 $o->fetch($obj->rowid);
196 $this->{$className}[] = $o;
199 $this->errors[] = $this->
db->lasterror();
213 if ($this->withChild && !empty($this->childtables) && !empty($this->fk_element))
215 foreach ($this->childtables as &$childTable)
217 $className = ucfirst($childTable);
218 if (!empty($this->{$className}))
220 foreach ($this->{$className} as $i => &$object)
222 $object->{$this->fk_element} = $this->id;
224 $object->update($user);
225 if ($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete ==
true) unset($this->{$className}[$i]);
241 if (empty($this->
id))
return $this->create($user);
242 elseif (isset($this->to_delete) && $this->to_delete ==
true)
return $this->
delete($user);
247 $res = $this->updateCommon($user);
250 $result = $this->call_trigger(strtoupper($this->element).
'_UPDATE', $user);
251 if ($result < 0) $error++;
252 else $this->saveChild($user);
255 $this->error = $this->
db->lasterror();
256 $this->errors[] = $this->error;
264 $this->
db->rollback();
277 if ($this->
id > 0)
return $this->update($user);
282 $res = $this->createCommon($user);
285 $this->
id = $this->
db->last_insert_id($this->table_element);
287 $result = $this->call_trigger(strtoupper($this->element).
'_CREATE', $user);
288 if ($result < 0) $error++;
289 else $this->saveChild($user);
292 $this->error = $this->
db->lasterror();
293 $this->errors[] = $this->error;
301 $this->
db->rollback();
312 public function delete(
User &$user)
314 if ($this->
id <= 0)
return 0;
319 $result = $this->call_trigger(strtoupper($this->element).
'_DELETE', $user);
320 if ($result < 0) $error++;
324 $this->deleteCommon($user);
325 if ($this->withChild && !empty($this->childtables))
327 foreach ($this->childtables as &$childTable)
329 $className = ucfirst($childTable);
330 if (!empty($this->{$className}))
332 foreach ($this->{$className} as &$object)
334 $object->delete($user);
346 $this->error = $this->
db->lasterror();
347 $this->errors[] = $this->error;
348 $this->
db->rollback();
363 if (empty($this->{$field}))
return '';
382 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
386 return $this->{$field};
398 foreach ($Tab as $key => $value)
400 if ($this->checkFieldType($key,
'date'))
402 $this->setDate($key, $value);
403 } elseif ($this->checkFieldType($key,
'float'))
405 $this->{$key} = (double)
price2num($value);
406 } elseif ($this->checkFieldType($key,
'int')) {
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.
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...
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.
__construct(DoliDB &$db)
Constructor.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
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.
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.