diff --git a/htdocs/core/class/coreobject.class.php b/htdocs/core/class/coreobject.class.php index f5e5b0e79a1..8f55a5bf458 100644 --- a/htdocs/core/class/coreobject.class.php +++ b/htdocs/core/class/coreobject.class.php @@ -150,7 +150,7 @@ class CoreObject extends CommonObject { foreach ($this->__fields as $field=>$info) { - if($this->_is_date($info)){ + if($this->is_date($info)){ if(empty($this->{$field})){ $query[$field] = $this->date_0; } @@ -166,11 +166,11 @@ class CoreObject extends CommonObject { $query[$field] = (int)price2num($this->{$field}); } - else if($this->_is_float($info)){ + else if($this->is_float($info)){ $query[$field] = (double)price2num($this->{$field}); } - elseif($this->_is_null($info)) { + elseif($this->is_null($info)) { $query[$field] = (is_null($this->{$field}) || (empty($this->{$field}) && $this->{$field}!==0 && $this->{$field}!=='0')?null:$this->{$field}); } else{ @@ -264,6 +264,20 @@ class CoreObject extends CommonObject { return $k; } + + public function removeChild($tabName, $id, $key='id') { + foreach($this->{$tabName} as &$object) { + + if($object->{$key} == $id) { + $object->to_delete = true; + return true; + } + + + } + return false; + } + public function fetchChild() { if($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) { @@ -295,18 +309,96 @@ class CoreObject extends CommonObject { } + public function saveChild(User &$user) { + if($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) { + foreach($this->childtables as &$childTable) { + + $className = ucfirst($childTable); + + foreach($this->{$className} as $i => &$object) { + + $object->{$this->fk_element} = $this->id; + + $object->update($user); + if($this->unsetChildDeleted && isset($object->to_delete) && $object->to_delete==true) unset($this->{$className}[$i]); + } + + } + } + } public function update(User &$user) { if(empty($this->id )) return $this->create($user); + if(isset($this->to_delete) && $this->to_delete==true) { + $this->delete(); + } + else { + + $query = array(); + + $query['id']=$this->id; + if(!isset($this->no_dt_maj))$query['tms'] = date('Y-m-d H:i:s'); + $this->set_save_query($query); + + $this->db->update($this->table_element,$query,array('id')); + + $this->id = $this->db->last_insert_id($this->table_element); + + $result = $this->call_trigger(strtoupper($this->element). '_UPDATE', $user); + + $this->saveChild($user); + + } + return $this->id; + } public function create(User &$user) { if($this->id>0) return $this->update($user); + $query = array(); + $query['datec'] = date("Y-m-d H:i:s",$this->datec); + if(!isset($this->no_dt_maj))$query['tms'] = date('Y-m-d H:i:s'); + $this->set_save_query($query); + + $this->db->insert($this->table_element,$query); + + $this->id = $this->db->last_insert_id($this->table_element); + + $result = $this->call_trigger(strtoupper($this->element). '_CREATE', $user); + + $this->saveChild($user); + + + return $this->id; } + public function delete(){ + if($this->id>0){ + $this->call_trigger(strtoupper($this->element). '_DELETE', $user); + $this->db->delete($this->table_element,array('id'=>$this->id),array(0=>'id')); + + if($this->withChild) { + foreach($this->childtables as &$childTable) { + + $className = ucfirst($childTable); + foreach($this->{$className} as &$object) { + + $object->delete(); + + } + + } + } + + + + } + + + } public function get_date($field,$format='') { if(empty($this->{$field})) return ''; elseif($this->{$field}<=strtotime('1000-01-01 00:00:00')) return ''; diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index c63227e1601..395f6009169 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -288,5 +288,113 @@ abstract class DoliDB implements Database { return $this->lastqueryerror; } + + /** + * Generate and execute Update SQL commande + * + * @param string $table table to update + * @param array $values array of values to update + * @param int|string|array $key key of value to select row to update + * @return bool|result false or boolean + */ + function update($table,$values,$key){ + + foreach ($value as $k => $v) { + if(is_string($v)) $v=stripslashes($v); + + if (is_array($key)){ + $i=array_search($k , $key ); + if ( $i !== false) { + $where[] = $key[$i]."=" . $this->escape( $v ) ; + continue; + } + } else { + if ( $k == $key) { + $where[] = "$k=" .$this->escape( $v ) ; + continue; + } + } + + if(is_null($v)) $val = 'NULL'; + else if(is_int($v) || is_double($v)) $val=$v; + else $val = $this->escape( $v ); + + $tmp[] = "$k=$val"; + } + $sql = sprintf( "UPDATE $table SET %s WHERE %s" , implode( ",", $tmp ) , implode(" AND ",$where) ); + + $res = $this->query( $sql ); + + if($res===false) { + //error + return false; + } + + return true; + } + + /** + * Generate and execute Insert SQL commande + * + * @param string $table table to update + * @param array $values array of values to update + * @return bool|result false or boolean + */ + function insert($table,$values){ + + foreach ($values as $k => $v) { + + $fields[] = $k; + if(is_null($v)){ + $values[] = 'NULL'; + }else{ + $v=stripslashes($v); + $values[] =$this->escape( $v ); + } + } + $sql = sprintf( 'INSERT INTO '.$table.' ( %s ) values( %s ) ', implode( ",", $fields ) , implode( ",", $values ) ); + + $res = $this->query($sql); + + if($res===false) { + + return false; + } + + return true; + } + + /** + * Generate and execute Delete SQL commande + * + * @param string $table table for the delete + * @param array $values array of values to delete + * @param int|string|array $key key of value to select row to update + * @return bool|result false or boolean + */ + function delete($table,$values,$key){ + foreach ($values as $k => $v) { + if(is_string($v)) $v=stripslashes($v); + + if (is_array($key)){ + $i=array_search($k , $key ); + if ( $i !== false) { + $where[] = $key[$i]."=" . $this->escape( $v ) ; + continue; + } + } else { + if ( $k == $key) { + $where[] = "$k=" .$this->escape( $v ) ; + continue; + } + } + + } + + $sql = sprintf( 'DELETE FROM '.$table.' WHERE '.implode(" AND ",$where)); + + return $this->query( $sql ); + } + }