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 1/4] 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 From 5ef681ef912f8e07a51fa14467037d78809c3628 Mon Sep 17 00:00:00 2001 From: Gauthier PC portable 024 Date: Fri, 15 Oct 2021 10:36:47 +0200 Subject: [PATCH 2/4] FIX : stickler --- htdocs/core/class/commonobject.class.php | 36 +++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 2bf6d758435..8689f7c26d1 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9058,7 +9058,7 @@ abstract class CommonObject $columnName = $deleteFromObject[2]; $TMoreSQL = array(); $more_sql = $deleteFromObject[3]; - if(!empty($more_sql)) $TMoreSQL['customsql'] = $more_sql; + if (!empty($more_sql)) $TMoreSQL['customsql'] = $more_sql; if (dol_include_once($filePath)) { $childObject = new $className($this->db); if (method_exists($childObject, 'deleteByParentField')) { @@ -9144,10 +9144,12 @@ abstract class CommonObject * * @param int $parentId Parent Id * @param string $parentField Name of Foreign key parent column + * @param array $filter an array filter + * @param string $filtermode AND or OR * @return int <0 if KO, >0 if OK * @throws Exception */ - public function deleteByParentField($parentId = 0, $parentField = '', $filter=array(), $filtermode="AND") + public function deleteByParentField($parentId = 0, $parentField = '', $filter = array(), $filtermode = "AND") { global $user; @@ -9160,22 +9162,22 @@ 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) { + // Manage filters + $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).")"; - } + $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) { From bbe867a7a97fb9c86191471a19577a21cbdd3281 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 16 Oct 2021 19:06:50 +0200 Subject: [PATCH 3/4] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 8689f7c26d1..ad99ef4fff3 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9169,9 +9169,9 @@ abstract class CommonObject if ($key == 'customsql') { $sqlwhere[] = $value; } elseif (strpos($value, '%') === false) { - $sqlwhere[] = $key.' IN ('.$this->db->sanitize($this->db->escape($value)).')'; + $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")"; } else { - $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\''; + $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'"; } } } From 9469c0dc2b922e66a82b36a294e3739b6a4f6f36 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 16 Oct 2021 19:10:50 +0200 Subject: [PATCH 4/4] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index ad99ef4fff3..c9740951fc9 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -9058,7 +9058,9 @@ abstract class CommonObject $columnName = $deleteFromObject[2]; $TMoreSQL = array(); $more_sql = $deleteFromObject[3]; - if (!empty($more_sql)) $TMoreSQL['customsql'] = $more_sql; + if (!empty($more_sql)) { + $TMoreSQL['customsql'] = $more_sql; + } if (dol_include_once($filePath)) { $childObject = new $className($this->db); if (method_exists($childObject, 'deleteByParentField')) {