diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 4dd667e388e..cf9c09b70d6 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1514,10 +1514,10 @@ abstract class CommonObject if (! empty($sourceid) && ! empty($sourcetype) && empty($targetid) && empty($targettype)) $justsource=true; if (empty($sourceid) && empty($sourcetype) && ! empty($targetid) && ! empty($targettype)) $justtarget=true; - $sourceid = (! empty($sourceid) ? $sourceid : $this->id ); - $targetid = (! empty($targetid) ? $targetid : $this->id ); - $sourcetype = (! empty($sourcetype) ? $sourcetype : (! empty($this->origin) ? $this->origin : $this->element ) ); - $targettype = (! empty($targettype) ? $targettype : $this->element ); + $sourceid = (! empty($sourceid) ? $sourceid : $this->id); + $targetid = (! empty($targetid) ? $targetid : $this->id); + $sourcetype = (! empty($sourcetype) ? $sourcetype : (! empty($this->origin) ? $this->origin : $this->element)); + $targettype = (! empty($targettype) ? $targettype : $this->element); // Links beetween objects are stored in this table $sql = 'SELECT fk_source, sourcetype, fk_target, targettype'; @@ -1630,10 +1630,56 @@ abstract class CommonObject } } + /** + * Update object linked of a current object + * + * @param int $sourceid Object source id + * @param string $sourcetype Object source type + * @param int $targetid Object target id + * @param string $targettype Object target type + * @return int >0 if OK, <0 if KO + */ + function updateObjectLinked($sourceid='', $sourcetype='', $targetid='', $targettype='') + { + $updatesource=false; + $updatetarget=false; + + if (! empty($sourceid) && ! empty($sourcetype) && empty($targetid) && empty($targettype)) $updatesource=true; + else if (empty($sourceid) && empty($sourcetype) && ! empty($targetid) && ! empty($targettype)) $updatetarget=true; + + $sql = "UPDATE ".MAIN_DB_PREFIX."element_element SET "; + if ($updatesource) + { + $sql.= "fk_source = ".$sourceid; + $sql.= ", sourcetype = '".$sourcetype."'"; + $sql.= " WHERE fk_target = ".$this->id; + $sql.= " AND targettype = '".$this->element."'"; + } + else if ($updatetarget) + { + $sql.= "fk_target = ".$targetid; + $sql.= ", targettype = '".$targettype."'"; + $sql.= " WHERE fk_source = ".$this->id; + $sql.= " AND sourcetype = '".$this->element."'"; + } + + dol_syslog(get_class($this)."::updateObjectLinked sql=".$sql, LOG_DEBUG); + if ($this->db->query($sql)) + { + return 1; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog(get_class($this)."::updateObjectLinked error=".$this->error, LOG_ERR); + return -1; + } + } + /** - * Delete all links between an object $this + * Delete all links between an object $this * - * @return int >0 if OK, <0 if KO + * @return int >0 if OK, <0 if KO */ function deleteObjectLinked() {