From ac7f0f8a4e4de247af3d1257cdf87e13ca4ce978 Mon Sep 17 00:00:00 2001 From: Christian Foellmann Date: Thu, 26 Aug 2021 15:04:59 +0200 Subject: [PATCH] add hook 'functionGetEntity' to manipulate entities for queries on the fly --- htdocs/core/class/hookmanager.class.php | 1 + htdocs/core/lib/functions.lib.php | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/hookmanager.class.php b/htdocs/core/class/hookmanager.class.php index 345de3f5be5..458104b8790 100644 --- a/htdocs/core/class/hookmanager.class.php +++ b/htdocs/core/class/hookmanager.class.php @@ -188,6 +188,7 @@ class HookManager 'formConfirm', 'getAccessForbiddenMessage', 'getDirList', + 'functionGetEntity', 'getFormMail', 'getFormatedCustomerRef', 'getFormatedSupplierRef', diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index ee353269e29..93485c5f12a 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -104,7 +104,7 @@ function getDoliDBInstance($type, $host, $user, $pass, $name, $port) */ function getEntity($element, $shared = 1, $currentobject = null) { - global $conf, $mc; + global $conf, $mc, $hookmanager, $object; // fix different element names (France to English) switch ($element) { @@ -117,7 +117,7 @@ function getEntity($element, $shared = 1, $currentobject = null) } if (is_object($mc)) { - return $mc->getEntity($element, $shared, $currentobject); + $out = $mc->getEntity($element, $shared, $currentobject); } else { $out = ''; $addzero = array('user', 'usergroup', 'c_email_templates', 'email_template', 'default_values'); @@ -125,8 +125,26 @@ function getEntity($element, $shared = 1, $currentobject = null) $out .= '0,'; } $out .= ((int) $conf->entity); - return $out; } + + // Manipulate entities to query on the fly + $parameters = array( + 'object' => $object, + 'currentobject' => $currentobject, + 'element' => $element, + 'shared' => $shared, + ); + $reshook = $hookmanager->executeHooks('functionGetEntity', $parameters, $out, $element); // Note that $action and $object may have been modified by some hooks + + if (is_numeric($reshook)) { + if ($reshook == 0 && !empty($hookmanager->resprints)) { + $out .= ','.$hookmanager->resprints; // add + } elseif ($reshook == 1) { + $out = $hookmanager->resprints; // replace + } + } + + return $out; } /**