From 883ed9c6230fd1b1cf511923a63d9507107cd3af Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Fri, 3 Nov 2023 09:53:57 +0100 Subject: [PATCH 1/3] NEW lazy load to substitute project variables --- htdocs/core/lib/functions.lib.php | 82 ++++++++++++++++++++++++--- htdocs/projet/class/project.class.php | 32 +++++++++++ 2 files changed, 105 insertions(+), 9 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 2b67b68061550..703e34df8af36 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8245,15 +8245,34 @@ function getCommonSubstitutionArray($outputlangs, $onlykey = 0, $exclude = null, $substitutionarray['__ATTENDEE_LASTNAME__'] = isset($object->lastname) ? $object->lastname : ''; } + $project = null; if (is_object($object->project)) { - $substitutionarray['__PROJECT_ID__'] = (is_object($object->project) ? $object->project->id : ''); - $substitutionarray['__PROJECT_REF__'] = (is_object($object->project) ? $object->project->ref : ''); - $substitutionarray['__PROJECT_NAME__'] = (is_object($object->project) ? $object->project->title : ''); - } - if (is_object($object->projet)) { // Deprecated, for backward compatibility - $substitutionarray['__PROJECT_ID__'] = (is_object($object->projet) ? $object->projet->id : ''); - $substitutionarray['__PROJECT_REF__'] = (is_object($object->projet) ? $object->projet->ref : ''); - $substitutionarray['__PROJECT_NAME__'] = (is_object($object->projet) ? $object->projet->title : ''); + $project = $object->project; + } elseif (is_object($object->projet)) { // Deprecated, for backward compatibility + $project = $object->projet; + } + if ($project) { + $substitutionarray['__PROJECT_ID__'] = $project->id; + $substitutionarray['__PROJECT_REF__'] = $project->ref; + $substitutionarray['__PROJECT_NAME__'] = $project->title; + } else { + // can substitute variables for project : uses lazy load in "make_substitutions" method + $project_id = 0; + if ($object->fk_project > 0) { + $project_id = $object->fk_project; + } elseif ($object->fk_projet > 0) { + $project_id = $object->fk_project; + } + if ($project_id > 0) { + // path:class:method:id + $substitutionarray['__PROJECT_ID__@lazyload'] = '/projet/class/project.class.php:Project:fetchAndSetSubstitution:' . $project_id; + $substitutionarray['__PROJECT_REF__@lazyload'] = '/projet/class/project.class.php:Project:fetchAndSetSubstitution:' . $project_id; + $substitutionarray['__PROJECT_NAME__@lazyload'] = '/projet/class/project.class.php:Project:fetchAndSetSubstitution:' . $project_id; + } else { + $substitutionarray['__PROJECT_ID__'] = ''; + $substitutionarray['__PROJECT_REF__'] = ''; + $substitutionarray['__PROJECT_NAME__'] = ''; + } } if (is_object($object) && $object->element == 'project') { $substitutionarray['__PROJECT_NAME__'] = $object->title; @@ -8573,7 +8592,7 @@ function getCommonSubstitutionArray($outputlangs, $onlykey = 0, $exclude = null, */ function make_substitutions($text, $substitutionarray, $outputlangs = null, $converttextinhtmlifnecessary = 0) { - global $conf, $langs; + global $conf, $db, $langs; if (!is_array($substitutionarray)) { return 'ErrorBadParameterSubstitutionArrayWhenCalling_make_substitutions'; @@ -8691,6 +8710,51 @@ function make_substitutions($text, $substitutionarray, $outputlangs = null, $con $valuetouseforsubstitution = $tmpobj->$method($id, '__XXX__'); And make the replacement of "__XXX__@lazyload" with $valuetouseforsubstitution */ + $memory_object_list = array(); + foreach ($substitutionarray as $key => $value) { + $lazy_load_arr = array(); + if (preg_match('/(__[A-Z\_]+__)@lazyload$/', $key, $lazy_load_arr)) { + if (isset($lazy_load_arr[1]) && !empty($lazy_load_arr[1])) { + $key_to_substitute = $lazy_load_arr[1]; + if (preg_match('/' . preg_quote($key_to_substitute, '/') . '/', $text)) { + $param_arr = explode(':', $value); + // path:class:method:id + if (count($param_arr) == 4) { + $path = $param_arr[0]; + $class = $param_arr[1]; + $method = $param_arr[2]; + $id = (int) $param_arr[3]; + + // load class file and init object in memory + if (dol_is_file(DOL_DOCUMENT_ROOT . $path)) { + require_once DOL_DOCUMENT_ROOT . $path; + if (class_exists($class)) { + $memory_object_list[$class] = array( + 'list' => array(), + ); + } + } + + // fetch object and set substitution + if (isset($memory_object_list[$class]) && isset($memory_object_list[$class]['list'])) { + if (method_exists($class, $method)) { + if (!isset($memory_object_list[$class]['list'][$id])) { + $tmpobj = new $class($db); + $valuetouseforsubstitution = $tmpobj->$method($id, $key_to_substitute); + $memory_object_list[$class]['list'][$id] = $tmpobj; + } else { + $tmpobj = $memory_object_list[$class]['list'][$id]; + $valuetouseforsubstitution = $tmpobj->$method($id, $key_to_substitute, true); + } + + $text = str_replace("$key_to_substitute", "$valuetouseforsubstitution", $text); // We must keep the " to work when value is 123.5 for example + } + } + } + } + } + } + } return $text; } diff --git a/htdocs/projet/class/project.class.php b/htdocs/projet/class/project.class.php index 470c940f10f3c..38abd37a7edb8 100644 --- a/htdocs/projet/class/project.class.php +++ b/htdocs/projet/class/project.class.php @@ -776,6 +776,38 @@ public function fetch($id, $ref = '', $ref_ext = '', $email_msgid = '') } } + /** + * Fetch object and substitute key + * + * @param int $id Project id + * @param string $key Key to substitute + * @param bool $fetched [=false] Not already fetched + * @return string Substitute key + */ + public function fetchAndSetSubstitution($id, $key, $fetched = false) + { + $substitution = ''; + + if ($fetched === false) { + $res = $this->fetch($id); + if ($res > 0) { + $fetched = true; + } + } + + if ($fetched === true) { + if ($key == '__PROJECT_ID__') { + $substitution = ($this->id > 0 ? $this->id : ''); + } elseif ($key == '__PROJECT_REF__') { + $substitution = $this->ref; + } elseif ($key == '__PROJECT_NAME__') { + $substitution = $this->title; + } + } + + return $substitution; + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return list of elements for type, linked to a project From e4bdc97b1f0c2b40537e08a4c32e23faf378e6a3 Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Fri, 3 Nov 2023 10:34:07 +0100 Subject: [PATCH 2/3] FIX load class file --- htdocs/core/lib/functions.lib.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 703e34df8af36..edf3e2decd146 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8725,13 +8725,15 @@ function make_substitutions($text, $substitutionarray, $outputlangs = null, $con $method = $param_arr[2]; $id = (int) $param_arr[3]; - // load class file and init object in memory - if (dol_is_file(DOL_DOCUMENT_ROOT . $path)) { - require_once DOL_DOCUMENT_ROOT . $path; - if (class_exists($class)) { - $memory_object_list[$class] = array( - 'list' => array(), - ); + // load class file and init object list in memory + if (!isset($memory_object_list[$class])) { + if (dol_is_file(DOL_DOCUMENT_ROOT . $path)) { + require_once DOL_DOCUMENT_ROOT . $path; + if (class_exists($class)) { + $memory_object_list[$class] = array( + 'list' => array(), + ); + } } } From 12a6378a1da43714332b6fc2330910116195492a Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Tue, 7 Nov 2023 14:03:30 +0100 Subject: [PATCH 3/3] Keep key to substitute if no link with project --- htdocs/core/lib/functions.lib.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index edf3e2decd146..3763c63a8df6e 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -8268,10 +8268,6 @@ function getCommonSubstitutionArray($outputlangs, $onlykey = 0, $exclude = null, $substitutionarray['__PROJECT_ID__@lazyload'] = '/projet/class/project.class.php:Project:fetchAndSetSubstitution:' . $project_id; $substitutionarray['__PROJECT_REF__@lazyload'] = '/projet/class/project.class.php:Project:fetchAndSetSubstitution:' . $project_id; $substitutionarray['__PROJECT_NAME__@lazyload'] = '/projet/class/project.class.php:Project:fetchAndSetSubstitution:' . $project_id; - } else { - $substitutionarray['__PROJECT_ID__'] = ''; - $substitutionarray['__PROJECT_REF__'] = ''; - $substitutionarray['__PROJECT_NAME__'] = ''; } } if (is_object($object) && $object->element == 'project') {