From 412746ec8fea883dc437e6f3135d33773bc02be9 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Fri, 15 Oct 2021 10:18:32 +0200 Subject: [PATCH] NEW : we need to be able to put more filters on deleteByParentField() function --- htdocs/core/class/commonobject.class.php | 24 ++++++++++++++++++++++-- htdocs/hrm/class/job.class.php | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 37083cdf5f8..2bf6d758435 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9056,10 +9056,13 @@ abstract class CommonObject $className = str_replace('@', '', $deleteFromObject[0]); $filePath = $deleteFromObject[1]; $columnName = $deleteFromObject[2]; + $TMoreSQL = array(); + $more_sql = $deleteFromObject[3]; + if(!empty($more_sql)) $TMoreSQL['customsql'] = $more_sql; if (dol_include_once($filePath)) { $childObject = new $className($this->db); if (method_exists($childObject, 'deleteByParentField')) { - $result = $childObject->deleteByParentField($this->id, $columnName); + $result = $childObject->deleteByParentField($this->id, $columnName, $TMoreSQL); if ($result < 0) { $error++; $this->errors[] = $childObject->error; @@ -9144,7 +9147,7 @@ abstract class CommonObject * @return int <0 if KO, >0 if OK * @throws Exception */ - public function deleteByParentField($parentId = 0, $parentField = '') + public function deleteByParentField($parentId = 0, $parentField = '', $filter=array(), $filtermode="AND") { global $user; @@ -9157,6 +9160,23 @@ abstract class CommonObject $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$this->table_element; $sql .= " WHERE ".$parentField." = ".(int) $parentId; + // Manage filter + $sqlwhere = array(); + if (count($filter) > 0) { + foreach ($filter as $key => $value) { + if ($key == 'customsql') { + $sqlwhere[] = $value; + } elseif (strpos($value, '%') === false) { + $sqlwhere[] = $key.' IN ('.$this->db->sanitize($this->db->escape($value)).')'; + } else { + $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\''; + } + } + } + if (count($sqlwhere) > 0) { + $sql .= " AND (".implode(" ".$filtermode." ", $sqlwhere).")"; + } + $resql = $this->db->query($sql); if (!$resql) { $this->errors[] = $this->db->lasterror(); diff --git a/htdocs/hrm/class/job.class.php b/htdocs/hrm/class/job.class.php index 4ff5ccbe303..01cc9260c61 100644 --- a/htdocs/hrm/class/job.class.php +++ b/htdocs/hrm/class/job.class.php @@ -154,7 +154,7 @@ class Job extends CommonObject // * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will // * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object // */ - // protected $childtablesoncascade = array('hrm_jobdet'); + protected $childtablesoncascade = array("@SkillRank:hrm/class/skillrank.class.php:fk_object:objecttype='job'"); // /** // * @var JobLine[] Array of subtable lines