diff --git a/htdocs/core/class/coreobject.class.php b/htdocs/core/class/coreobject.class.php index 7d784073412..dc52fdd9bf5 100644 --- a/htdocs/core/class/coreobject.class.php +++ b/htdocs/core/class/coreobject.class.php @@ -25,6 +25,8 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; class CoreObject extends CommonObject { + public $db; + protected $__fields=array(); /** * Constructor @@ -33,8 +35,6 @@ class CoreObject extends CommonObject { */ function __construct(DoliDB &$db) { - $this->db = &$db; - $this->date_0 = '1001-01-01 00:00:00'; //TODO there is a solution for this ? } @@ -179,27 +179,55 @@ class CoreObject extends CommonObject { return implode(',', $keys); } + private function set_vars_by_db(&$obj){ + + foreach ($this->__fields as $field=>$info) { + if($this->is_date($info)){ + if($obj->{$field} === '0000-00-00 00:00:00' || $obj->{$field} === '1000-01-01 00:00:00')$this->{$field} = 0; + else $this->{$field} = strtotime($obj->{$field}); + } + elseif($this->is_array($info)){ + $this->{$field} = @unserialize($obj->{$field}); + //HACK POUR LES DONNES NON UTF8 + if($this->{$field}===FALSE)@unserialize(utf8_decode($obj->{$field})); + } + elseif($this->is_int($info)){ + $this->{$field} = (int)$obj->{$field}; + } + elseif($this->is_float($info)){ + $this->{$field} = (double)$obj->{$field}; + } + elseif($this->is_null($info)){ + $val = $obj->{$field}; + // zero is not null + $this->{$field} = (is_null($val) || (empty($val) && $val!==0 && $val!=='0')?null:$val); + } + else{ + $this->{$field} = $obj->{$field}; + } + + } + } + public function fetch($id, $loadChild = true) { if(empty($id)) return false; $sql = 'SELECT '.$this->get_field_list().',datec,tms - FROM '.$this->table_element.' + FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.$id; - + var_dump($sql); $res = $this->db->query( $sql ); - var_dump($sql); - if($obj = $db->fetch_object($res)) { + + if($obj = $this->db->fetch_object($res)) { $this->rowid=$id; - $this->set_vars_by_db($db); + $this->set_vars_by_db($obj); $this->datec=$this->db->idate($obj->datec); $this->tms=$this->db->idate($obj->tms); - if($loadChild) $this->loadChild($db); - - $this->run_trigger($db, 'load'); + if($loadChild) $this->fetchChild(); return $this->id; } @@ -208,6 +236,38 @@ class CoreObject extends CommonObject { } } + + public function fetchChild() { + if($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) { + + foreach($this->childtables as &$childTable) { + + $className = ucfirst($childTable); + + $this->{$className}=array(); + + $sql = " SELECT rowid FROM ".MAIN_DB_PREFIX.$childTable." WHERE ".$this->fk_element."=".$this->rowid; + $res = $this->db->query($sql); + if($res) { + + while($obj = $db->fetch_object($res)) { + + $o=new $className($this->db); + $o->fetch($obj->rowid); + + $this->{$className}[] = $o; + + } + + } + + } + + } + + } + + public function update() { diff --git a/htdocs/core/class/listview.class.php b/htdocs/core/class/listview.class.php index 0cedad18855..8df337b699c 100644 --- a/htdocs/core/class/listview.class.php +++ b/htdocs/core/class/listview.class.php @@ -820,7 +820,6 @@ class Listview { } private function parse_sql( &$THeader, &$TField,&$TParam, $sql) { - global $db; $this->sql = $this->limitSQL($sql, $TParam); @@ -828,17 +827,26 @@ class Listview { $this->THideFlip = array_flip($TParam['hide']); - $res = $db->query($this->sql); - $first=true; - while($currentLine = $db->fetch_object($res)) { - if($first) { - $this->init_entete($THeader, $TParam, $currentLine); - $first = false; + $res = $this->db->query($this->sql); + if($res!==false) { + + dol_syslog(get_class($this)."::parse_sql id=".$this->id." sql=".$this->sql, LOG_DEBUG); + + $first=true; + while($currentLine = $this->db->fetch_object($res)) { + if($first) { + $this->init_entete($THeader, $TParam, $currentLine); + $first = false; + } + + $this->set_line($TField, $TParam, $currentLine); + } - $this->set_line($TField, $TParam, $currentLine); - } - + else { + dol_syslog(get_class($this)."::parse_sql id=".$this->id." sql=".$this->sql, LOG_ERR); + } + } } diff --git a/htdocs/inventory/class/inventory.class.php b/htdocs/inventory/class/inventory.class.php index f158ab260cf..d092a876ad2 100644 --- a/htdocs/inventory/class/inventory.class.php +++ b/htdocs/inventory/class/inventory.class.php @@ -69,6 +69,8 @@ class Inventory extends CoreObject ,'title'=>array('type'=>'string') ); + public $db; + function __construct(DoliDB &$db) { @@ -292,15 +294,22 @@ class Inventory extends CoreObject return 1; } + public function getNomUrl($picto = 1) { + global $langs; + + $title = !empty($this->title) ? $this->title : $langs->trans('inventoryTitle').' '.$this->id; + + return ''.($picto ? img_picto('','object_list.png','',0).' ' : '').$title.''; + + } + static function getLink($id) { global $langs,$db; $i = new Inventory($db); $i->fetch($id, false); - $title = !empty($i->title) ? $i->title : $langs->trans('inventoryTitle').' '.$i->id; - - return ''.img_picto('','object_list.png','',0).' '.$title.''; + return $i->getNomUrl(); } @@ -309,7 +318,7 @@ class Inventory extends CoreObject if($type=='All') { - $sql="SELECT i.rowid, e.label, i.date_inventory, i.fk_warehouse, i.date_cre, i.date_maj, i.status + $sql="SELECT i.rowid, e.label, i.date_inventory, i.fk_warehouse, i.datec, i.tms, i.status FROM ".MAIN_DB_PREFIX."inventory i LEFT JOIN ".MAIN_DB_PREFIX."entrepot e ON (e.rowid = i.fk_warehouse) WHERE i.entity=".(int) $conf->entity;