diff --git a/docs/templates-processing.rst b/docs/templates-processing.rst index 1f7f5f2b80..745bb2a7d6 100644 --- a/docs/templates-processing.rst +++ b/docs/templates-processing.rst @@ -35,6 +35,32 @@ You can also set multiple values by passing all of them in an array. $templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe')); +setCheckbox +""""""""""" +Given a template containing a checkbox control with the title or tag named: + +.. code-block:: clean + + ${checkbox} + +The following will check the checkbox: + +.. code-block:: php + + $templateProcessor->setCheckbox('checkbox', true); + +.. note:: + + To add a checkbox and set its title or tag in a template: + + 1. Go to **Developer** tab > **Controls** group, + 2. Select the **Check Box Content Control**, + 3. Right-click on your checkbox, + 4. Click on **Properties**, + 5. Set the title or the tag. + + These steps may change regarding the version of Microsoft Word used. + setMacroOpeningChars """""""" You can define a custom opening macro. The following will set ``{#`` as the opening search pattern. diff --git a/samples/Sample_42_TemplateSetCheckbox.php b/samples/Sample_42_TemplateSetCheckbox.php new file mode 100644 index 0000000000..a252201cab --- /dev/null +++ b/samples/Sample_42_TemplateSetCheckbox.php @@ -0,0 +1,19 @@ +setCheckbox('checkbox', true); +$templateProcessor->setCheckbox('checkbox2', false); + +echo date('H:i:s'), ' Saving the result document...', EOL; +$templateProcessor->saveAs("results/{$filename}"); + +echo getEndingNotes(['Word2007' => 'docx'], "results/{$filename}"); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/samples/resources/Sample_42_TemplateSetCheckbox.docx b/samples/resources/Sample_42_TemplateSetCheckbox.docx new file mode 100644 index 0000000000..9abc486b69 Binary files /dev/null and b/samples/resources/Sample_42_TemplateSetCheckbox.docx differ diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 6ef22fc538..4d3d21d2e3 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -353,6 +353,27 @@ public function setValues(array $values): void } } + public function setCheckbox(string $search, bool $checked): void + { + $search = static::ensureMacroCompleted($search); + $blockType = 'w:sdt'; + + $where = $this->findContainingXmlBlockForMacro($search, $blockType); + if (!is_array($where)) { + return; + } + + $block = $this->getSlice($where['start'], $where['end']); + + $val = $checked ? '1' : '0'; + $block = preg_replace('/()/', '$1"' . $val . '"$2', $block); + + $text = $checked ? '☒' : '☐'; + $block = preg_replace('/().*?(<\/w:t>)/', '$1' . $text . '$2', $block); + + $this->replaceXmlBlock($search, $block, $blockType); + } + /** * @param string $search */ diff --git a/tests/PhpWordTests/TemplateProcessorTest.php b/tests/PhpWordTests/TemplateProcessorTest.php index d8ff9ace9e..8ab1918be8 100644 --- a/tests/PhpWordTests/TemplateProcessorTest.php +++ b/tests/PhpWordTests/TemplateProcessorTest.php @@ -609,6 +609,159 @@ public function testSetValuesWithCustomMacro(): void self::assertStringContainsString('Hello John Doe', $templateProcessor->getMainPart()); } + /** + * @covers ::setCheckbox + */ + public function testSetCheckbox(): void + { + $mainPart = ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + '; + + $result = ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + '; + + $templateProcessor = new TestableTemplateProcesor($mainPart); + $templateProcessor->setCheckbox('checkbox', true); + $templateProcessor->setCheckbox('checkbox2', false); + + self::assertEquals(preg_replace('/>\s+<', $result), preg_replace('/>\s+<', $templateProcessor->getMainPart())); + } + + /** + * @covers ::setCheckbox + */ + public function testSetCheckboxWithCustomMacro(): void + { + $mainPart = ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + '; + + $result = ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + '; + + $templateProcessor = new TestableTemplateProcesor($mainPart); + $templateProcessor->setMacroChars('{#', '#}'); + $templateProcessor->setCheckbox('checkbox', true); + $templateProcessor->setCheckbox('checkbox2', false); + + self::assertEquals(preg_replace('/>\s+<', $result), preg_replace('/>\s+<', $templateProcessor->getMainPart())); + } + /** * @covers ::setImageValue */