Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW lazy load to substitute project variables #26451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 71 additions & 9 deletions htdocs/core/lib/functions.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8245,15 +8245,30 @@ 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;
}
}
if (is_object($object) && $object->element == 'project') {
$substitutionarray['__PROJECT_NAME__'] = $object->title;
Expand Down Expand Up @@ -8573,7 +8588,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';
Expand Down Expand Up @@ -8691,6 +8706,53 @@ 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 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(),
);
}
}
}

// 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;
}
Expand Down
32 changes: 32 additions & 0 deletions htdocs/projet/class/project.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down