From 3a43630f94a0593b6f2b46e225b222762a3ecee7 Mon Sep 17 00:00:00 2001 From: Dominic Tubach Date: Fri, 8 Nov 2024 12:02:42 +0100 Subject: [PATCH] Prevent infinite loop when tokens are used in headers or footers --- .../LocalUnoconv/PhpWordTemplateProcessor.php | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/CRM/Civioffice/DocumentRendererType/LocalUnoconv/PhpWordTemplateProcessor.php b/CRM/Civioffice/DocumentRendererType/LocalUnoconv/PhpWordTemplateProcessor.php index 4a4b973..bd67c08 100644 --- a/CRM/Civioffice/DocumentRendererType/LocalUnoconv/PhpWordTemplateProcessor.php +++ b/CRM/Civioffice/DocumentRendererType/LocalUnoconv/PhpWordTemplateProcessor.php @@ -104,20 +104,21 @@ public function replaceHtmlToken(string $macroVariable, string $renderedTokenMes // styles. PhpWord\Shared\Html::addHtml($section, $renderedTokenMessage); $elements = $section->getElements(); - // setValue() and setElementsValue() replace only the first occurrence of macro variables in the document, so loop - // until all have been replaced. - do { - if ([] === $elements) { - // Note: If the paragraph had the macro as its only content, it - // will not be removed (i.e. leave an empty paragraph). - $this->setValue($macroVariable, ''); - } - else { + if ([] === $elements) { + // Note: If the paragraph had the macro as its only content, it + // will not be removed (i.e. leave an empty paragraph). + $this->setValue($macroVariable, ''); + } + else { + // setElementsValue() replace only the first occurrence of macro variables in the document, so loop + // until all have been replaced. + // @todo Replace not only macros in the main part, but also in headers and footers. + do { // ... or as HTML: Render all elements and insert in the text // run or paragraph containing the macro. $this->setElementsValue($macroVariable, $elements, TRUE); - } - } while (in_array($macroVariable, $this->getVariables(), TRUE)); + } while (in_array($macroVariable, $this->getVariablesForPart($this->tempDocumentMainPart), TRUE)); + } } catch (Exception $exception) { throw new CRM_Core_Exception(