From 602b9c26ae7d4408f84cb7e6fc47a00c339db60a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 1 Aug 2025 18:37:20 +0200 Subject: [PATCH] Fix case of ismultientitymanaged that is key@table --- .../template/class/api_mymodule.class.php | 11 ++++++++--- .../template/class/myobject.class.php | 12 ++++++++---- .../modules/mymodule/mod_myobject_standard.php | 14 ++++++++------ htdocs/modulebuilder/template/myobject_list.php | 7 ++++++- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/htdocs/modulebuilder/template/class/api_mymodule.class.php b/htdocs/modulebuilder/template/class/api_mymodule.class.php index a264a2b668c..773d7753243 100644 --- a/htdocs/modulebuilder/template/class/api_mymodule.class.php +++ b/htdocs/modulebuilder/template/class/api_mymodule.class.php @@ -137,9 +137,14 @@ class MyModuleApi extends DolibarrApi $sql = "SELECT t.rowid"; $sql .= " FROM ".$this->db->prefix().$tmpobject->table_element." AS t"; $sql .= " LEFT JOIN ".$this->db->prefix().$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields - $sql .= " WHERE 1 = 1"; - if ($tmpobject->ismultientitymanaged) { - $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')'; + if (!empty($tmpobject->ismultientitymanaged) && (int) $tmpobject->ismultientitymanaged == 1) { + $sql .= " WHERE t.entity IN (".getEntity($tmpobject->element).")"; + } elseif (preg_match('/^\w+@\w+$/', (string) $tmpobject->ismultientitymanaged)) { + $tmparray = explode('@', (string) $tmpobject->ismultientitymanaged); + $sql .= " LEFT JOIN ".$this->db->prefix().$tmparray[1]." as pt ON t.".$tmparray[0]." = pt.rowid"; + $sql .= " WHERE pt.entity IN (".getEntity($tmpobject->element).")"; + } else { + $sql .= " WHERE 1 = 1"; } if ($restrictonsocid && $socid) { $sql .= " AND t.fk_soc = ".((int) $socid); diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index 14cc845db2b..e2b134e11f3 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -64,8 +64,8 @@ class MyObject extends CommonObject public $isextrafieldmanaged = 0; /** - * @var int<0,1>|string|null Does this object support multicompany module ? - * 0=No test on entity, 1=Test with field entity in local table, 'field@table'=Test entity into the field@table (example 'fk_soc@societe') + * @var int<0,1>|string Does this object support multicompany module ? + * 0=No test on entity, 1=Test with field entity in local table, 'field@table'=Test entity into the field@table (example 'fk_soc@societe') */ public $ismultientitymanaged = 0; @@ -460,11 +460,15 @@ class MyObject extends CommonObject $sql = "SELECT "; $sql .= $this->getFieldList('t'); $sql .= " FROM ".$this->db->prefix().$this->table_element." as t"; - if (isset($this->isextrafieldmanaged) && $this->isextrafieldmanaged == 1) { + if (!empty($this->isextrafieldmanaged) && $this->isextrafieldmanaged == 1) { $sql .= " LEFT JOIN ".$this->db->prefix().$this->table_element."_extrafields as te ON te.fk_object = t.rowid"; } - if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { + if (!empty($this->ismultientitymanaged) && (int) $this->ismultientitymanaged == 1) { $sql .= " WHERE t.entity IN (".getEntity($this->element).")"; + } elseif (preg_match('/^\w+@\w+$/', (string) $this->ismultientitymanaged)) { + $tmparray = explode('@', (string) $this->ismultientitymanaged); + $sql .= " LEFT JOIN ".$this->db->prefix().$tmparray[1]." as pt ON t.".$this->db->sanitize($tmparray[0])." = pt.rowid"; + $sql .= " WHERE pt.entity IN (".getEntity($this->element).")"; } else { $sql .= " WHERE 1 = 1"; } diff --git a/htdocs/modulebuilder/template/core/modules/mymodule/mod_myobject_standard.php b/htdocs/modulebuilder/template/core/modules/mymodule/mod_myobject_standard.php index 8a78ef0e429..c6c42666d19 100644 --- a/htdocs/modulebuilder/template/core/modules/mymodule/mod_myobject_standard.php +++ b/htdocs/modulebuilder/template/core/modules/mymodule/mod_myobject_standard.php @@ -92,13 +92,15 @@ class mod_myobject_standard extends ModeleNumRefMyObject $max = ''; $posindice = strlen($this->prefix) + 6; - $sql = "SELECT MAX(CAST(SUBSTRING(ref FROM ".$posindice.") AS SIGNED)) as max"; - $sql .= " FROM ".$db->prefix()."mymodule_myobject"; - $sql .= " WHERE ref LIKE '".$db->escape($this->prefix)."____-%'"; + $sql = "SELECT MAX(CAST(SUBSTRING(t.ref FROM ".$posindice.") AS SIGNED)) as max"; + $sql .= " FROM ".$db->prefix()."mymodule_myobject as t"; + $sql .= " WHERE t.ref LIKE '".$db->escape($this->prefix)."____-%'"; if ($object->ismultientitymanaged == 1) { - $sql .= " AND entity = ".$conf->entity; - } elseif (!is_numeric($object->ismultientitymanaged)) { // @phan-suppress-current-line PhanPluginEmptyStatementIf - // TODO + $sql .= " AND entity = ".((int) $conf->entity); + } elseif (preg_match('/^\w+@\w+$/', (string) $this->ismultientitymanaged)) { + $tmparray = explode('@', (string) $this->ismultientitymanaged); + $sql .= " LEFT JOIN ".$this->db->prefix().$tmparray[1]." as pt ON t.".$this->db->sanitize($tmparray[0])." = pt.rowid"; + $sql .= " WHERE pt.entity IN (".getEntity($this->element).")"; } $resql = $db->query($sql); diff --git a/htdocs/modulebuilder/template/myobject_list.php b/htdocs/modulebuilder/template/myobject_list.php index 6b5131c50b0..769fd0cfe37 100644 --- a/htdocs/modulebuilder/template/myobject_list.php +++ b/htdocs/modulebuilder/template/myobject_list.php @@ -325,8 +325,13 @@ if (isset($extrafields->attributes[$object->table_element]['label']) && is_array $parameters = array(); $reshook = $hookmanager->executeHooks('printFieldListFrom', $parameters, $object, $action); // Note that $action and $object may have been modified by hook $sql .= $hookmanager->resPrint; -if ($object->ismultientitymanaged == 1) { + +if (!empty($object->ismultientitymanaged) && (int) $object->ismultientitymanaged == 1) { $sql .= " WHERE t.entity IN (".getEntity($object->element, (GETPOSTINT('search_current_entity') ? 0 : 1)).")"; +} elseif (preg_match('/^\w+@\w+$/', (string) $object->ismultientitymanaged)) { + $tmparray = explode('@', (string) $object->ismultientitymanaged); + $sql .= " LEFT JOIN ".$object->db->prefix().$tmparray[1]." as pt ON t.".$db->sanitize($tmparray[0])." = pt.rowid"; + $sql .= " WHERE pt.entity IN (".getEntity($object->element, (GETPOSTINT('search_current_entity') ? 0 : 1)).")"; } else { $sql .= " WHERE 1 = 1"; }