Skip to content

Commit 913ac5a

Browse files
nxtpgeProgi1984
authored andcommitted
Template : Set a checkbox
1 parent 11a7aaa commit 913ac5a

File tree

8 files changed

+222
-0
lines changed

8 files changed

+222
-0
lines changed

docs/changes/1.x/1.2.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- PDF Writer : Added support for PageBreak
3030
- PDF Writer : Added callback for modifying the HTML
3131
- Added Support for Language, both for document overall and individual text elements
32+
- Template : Set a checkbox by [@nxtpge](https://github.com/nxtpge) in [#2509](https://github.com/PHPOffice/PHPWord/pull/2509)
3233

3334
### Bug fixes
3435

docs/usage/template.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ You can also set multiple values by passing all of them in an array.
3838
$templateProcessor->setValues(array('firstname' => 'John', 'lastname' => 'Doe'));
3939
```
4040

41+
## setCheckbox
42+
43+
Given a template containing a checkbox control with the title or tag named:
44+
45+
``` clean
46+
${checkbox}
47+
```
48+
The following will check the checkbox:
49+
50+
``` php
51+
<?php
52+
53+
$templateProcessor->setCheckbox('checkbox', true);
54+
```
55+
56+
!!! note annotate "To add a checkbox and set its title or tag in a template"
57+
58+
1. Go to **Developer** tab > **Controls** group
59+
2. Select the **Check Box Content Control**
60+
3. Right-click on your checkbox
61+
4. Click on **Properties**
62+
5. Set the title or the tag
63+
64+
These steps may change regarding the version of Microsoft Word used.
65+
4166
## setMacroOpeningChars
4267

4368
You can define a custom opening macro. The following will set ``{#`` as the opening search pattern.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ nav:
5555
- Comment: 'usage/elements/comment.md'
5656
- Field: 'usage/elements/field.md'
5757
- Footnote & Endnote: 'usage/elements/note.md'
58+
- Formula: 'usage/elements/formula.md'
5859
- Image: 'usage/elements/image.md'
5960
- Line: 'usage/elements/line.md'
6061
- Link: 'usage/elements/link.md'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
include_once 'Sample_Header.php';
4+
5+
use PhpOffice\PhpWord\TemplateProcessor;
6+
7+
// Template processor instance creation
8+
echo date('H:i:s'), ' Creating new TemplateProcessor instance...', EOL;
9+
$filename = 'Sample_42_TemplateSetCheckbox.docx';
10+
$templateProcessor = new TemplateProcessor(__DIR__ . "/resources/{$filename}");
11+
12+
$templateProcessor->setCheckbox('checkbox', true);
13+
$templateProcessor->setCheckbox('checkbox2', false);
14+
15+
echo date('H:i:s'), ' Saving the result document...', EOL;
16+
$templateProcessor->saveAs(__DIR__ . "/results/{$filename}");
17+
18+
echo getEndingNotes(['Word2007' => 'docx'], "results/{$filename}");
19+
if (!CLI) {
20+
include_once 'Sample_Footer.php';
21+
}
File renamed without changes.
Binary file not shown.

src/PhpWord/TemplateProcessor.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,27 @@ public function setValues(array $values): void
372372
}
373373
}
374374

375+
public function setCheckbox(string $search, bool $checked): void
376+
{
377+
$search = static::ensureMacroCompleted($search);
378+
$blockType = 'w:sdt';
379+
380+
$where = $this->findContainingXmlBlockForMacro($search, $blockType);
381+
if (!is_array($where)) {
382+
return;
383+
}
384+
385+
$block = $this->getSlice($where['start'], $where['end']);
386+
387+
$val = $checked ? '1' : '0';
388+
$block = preg_replace('/(<w14:checked w14:val=)".*?"(\/>)/', '$1"' . $val . '"$2', $block);
389+
390+
$text = $checked ? '' : '';
391+
$block = preg_replace('/(<w:t>).*?(<\/w:t>)/', '$1' . $text . '$2', $block);
392+
393+
$this->replaceXmlBlock($search, $block, $blockType);
394+
}
395+
375396
/**
376397
* @param string $search
377398
*/

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,159 @@ public function testSetValuesWithCustomMacro(): void
610610
self::assertStringContainsString('Hello John Doe', $templateProcessor->getMainPart());
611611
}
612612

613+
/**
614+
* @covers ::setCheckbox
615+
*/
616+
public function testSetCheckbox(): void
617+
{
618+
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>
619+
<w:p>
620+
<w:sdt>
621+
<w:sdtPr>
622+
<w:alias w:val="${checkbox}"/>
623+
<w14:checkbox>
624+
<w14:checked w14:val="0"/>
625+
</w14:checkbox>
626+
</w:sdtPr>
627+
<w:sdtContent>
628+
<w:r>
629+
<w:t>☐</w:t>
630+
</w:r>
631+
</w:sdtContent>
632+
</w:sdt>
633+
</w:p>
634+
<w:p>
635+
<w:sdt>
636+
<w:sdtPr>
637+
<w:alias w:val="${checkbox2}"/>
638+
<w14:checkbox>
639+
<w14:checked w14:val="1"/>
640+
</w14:checkbox>
641+
</w:sdtPr>
642+
<w:sdtContent>
643+
<w:r>
644+
<w:t>☒</w:t>
645+
</w:r>
646+
</w:sdtContent>
647+
</w:sdt>
648+
</w:p>';
649+
650+
$result = '<?xml version="1.0" encoding="UTF-8"?>
651+
<w:p>
652+
<w:sdt>
653+
<w:sdtPr>
654+
<w:alias w:val="${checkbox}"/>
655+
<w14:checkbox>
656+
<w14:checked w14:val="1"/>
657+
</w14:checkbox>
658+
</w:sdtPr>
659+
<w:sdtContent>
660+
<w:r>
661+
<w:t>☒</w:t>
662+
</w:r>
663+
</w:sdtContent>
664+
</w:sdt>
665+
</w:p>
666+
<w:p>
667+
<w:sdt>
668+
<w:sdtPr>
669+
<w:alias w:val="${checkbox2}"/>
670+
<w14:checkbox>
671+
<w14:checked w14:val="0"/>
672+
</w14:checkbox>
673+
</w:sdtPr>
674+
<w:sdtContent>
675+
<w:r>
676+
<w:t>☐</w:t>
677+
</w:r>
678+
</w:sdtContent>
679+
</w:sdt>
680+
</w:p>';
681+
682+
$templateProcessor = new TestableTemplateProcesor($mainPart);
683+
$templateProcessor->setCheckbox('checkbox', true);
684+
$templateProcessor->setCheckbox('checkbox2', false);
685+
686+
self::assertEquals(preg_replace('/>\s+</', '><', $result), preg_replace('/>\s+</', '><', $templateProcessor->getMainPart()));
687+
}
688+
689+
/**
690+
* @covers ::setCheckbox
691+
*/
692+
public function testSetCheckboxWithCustomMacro(): void
693+
{
694+
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>
695+
<w:p>
696+
<w:sdt>
697+
<w:sdtPr>
698+
<w:alias w:val="{#checkbox#}"/>
699+
<w14:checkbox>
700+
<w14:checked w14:val="0"/>
701+
</w14:checkbox>
702+
</w:sdtPr>
703+
<w:sdtContent>
704+
<w:r>
705+
<w:t>☐</w:t>
706+
</w:r>
707+
</w:sdtContent>
708+
</w:sdt>
709+
</w:p>
710+
<w:p>
711+
<w:sdt>
712+
<w:sdtPr>
713+
<w:alias w:val="{#checkbox2#}"/>
714+
<w14:checkbox>
715+
<w14:checked w14:val="1"/>
716+
</w14:checkbox>
717+
</w:sdtPr>
718+
<w:sdtContent>
719+
<w:r>
720+
<w:t>☒</w:t>
721+
</w:r>
722+
</w:sdtContent>
723+
</w:sdt>
724+
</w:p>';
725+
726+
$result = '<?xml version="1.0" encoding="UTF-8"?>
727+
<w:p>
728+
<w:sdt>
729+
<w:sdtPr>
730+
<w:alias w:val="{#checkbox#}"/>
731+
<w14:checkbox>
732+
<w14:checked w14:val="1"/>
733+
</w14:checkbox>
734+
</w:sdtPr>
735+
<w:sdtContent>
736+
<w:r>
737+
<w:t>☒</w:t>
738+
</w:r>
739+
</w:sdtContent>
740+
</w:sdt>
741+
</w:p>
742+
<w:p>
743+
<w:sdt>
744+
<w:sdtPr>
745+
<w:alias w:val="{#checkbox2#}"/>
746+
<w14:checkbox>
747+
<w14:checked w14:val="0"/>
748+
</w14:checkbox>
749+
</w:sdtPr>
750+
<w:sdtContent>
751+
<w:r>
752+
<w:t>☐</w:t>
753+
</w:r>
754+
</w:sdtContent>
755+
</w:sdt>
756+
</w:p>';
757+
758+
$templateProcessor = new TestableTemplateProcesor($mainPart);
759+
$templateProcessor->setMacroChars('{#', '#}');
760+
$templateProcessor->setCheckbox('checkbox', true);
761+
$templateProcessor->setCheckbox('checkbox2', false);
762+
763+
self::assertEquals(preg_replace('/>\s+</', '><', $result), preg_replace('/>\s+</', '><', $templateProcessor->getMainPart()));
764+
}
765+
613766
/**
614767
* @covers ::setImageValue
615768
*/

0 commit comments

Comments
 (0)