Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
76 / 76 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
FormField | |
100.00% |
76 / 76 |
|
100.00% |
4 / 4 |
14 | |
100.00% |
1 / 1 |
write | |
100.00% |
49 / 49 |
|
100.00% |
1 / 1 |
4 | |||
writeTextInput | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
writeCheckBox | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
writeDropDown | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Writer\Word2007\Element; |
19 | |
20 | use PhpOffice\PhpWord\Element\FormField as FormFieldElement; |
21 | use PhpOffice\PhpWord\Shared\XMLWriter; |
22 | |
23 | /** |
24 | * FormField element writer. |
25 | * |
26 | * Note: DropDown is active when document protection is set to `forms` |
27 | * |
28 | * @since 0.12.0 |
29 | * @see http://www.datypic.com/sc/ooxml/t-w_CT_FFData.html |
30 | * |
31 | * @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
32 | */ |
33 | class FormField extends Text |
34 | { |
35 | /** @const int Length of filler when value is null */ |
36 | const FILLER_LENGTH = 30; |
37 | |
38 | /** |
39 | * Write element. |
40 | */ |
41 | public function write(): void |
42 | { |
43 | $xmlWriter = $this->getXmlWriter(); |
44 | $element = $this->getElement(); |
45 | if (!$element instanceof FormFieldElement) { |
46 | return; |
47 | } |
48 | |
49 | $type = $element->getType(); |
50 | $instructions = ['textinput' => 'FORMTEXT', 'checkbox' => 'FORMCHECKBOX', 'dropdown' => 'FORMDROPDOWN']; |
51 | $instruction = $instructions[$type]; |
52 | $writeFormField = "write{$type}"; |
53 | $name = $element->getName(); |
54 | if ($name === null) { |
55 | $name = $type . $element->getElementId(); |
56 | } |
57 | $value = $element->getValue(); |
58 | if ($value === null) { |
59 | $value = str_repeat(' ', self::FILLER_LENGTH); |
60 | } |
61 | |
62 | $this->startElementP(); |
63 | |
64 | $xmlWriter->startElement('w:r'); |
65 | $xmlWriter->startElement('w:fldChar'); |
66 | $xmlWriter->writeAttribute('w:fldCharType', 'begin'); |
67 | $xmlWriter->startElement('w:ffData'); |
68 | $xmlWriter->writeElementBlock('w:enabled', 'w:val', 1); |
69 | $xmlWriter->writeElementBlock('w:name', 'w:val', $name); |
70 | $xmlWriter->writeElementBlock('w:calcOnExit', 'w:val', 0); |
71 | $this->$writeFormField($xmlWriter, $element); |
72 | $xmlWriter->endElement(); // w:ffData |
73 | $xmlWriter->endElement(); // w:fldChar |
74 | $xmlWriter->endElement(); // w:r |
75 | |
76 | $xmlWriter->startElement('w:r'); |
77 | $this->writeFontStyle(); |
78 | $xmlWriter->startElement('w:instrText'); |
79 | $xmlWriter->writeAttribute('xml:space', 'preserve'); |
80 | $xmlWriter->text("{$instruction}"); |
81 | $xmlWriter->endElement(); // w:instrText |
82 | $xmlWriter->endElement(); // w:r |
83 | |
84 | $xmlWriter->startElement('w:r'); |
85 | $this->writeFontStyle(); |
86 | $xmlWriter->writeElementBlock('w:fldChar', 'w:fldCharType', 'separate'); |
87 | $xmlWriter->endElement(); // w:r |
88 | |
89 | $xmlWriter->startElement('w:r'); |
90 | $this->writeFontStyle(); |
91 | $xmlWriter->startElement('w:t'); |
92 | $xmlWriter->writeAttribute('xml:space', 'preserve'); |
93 | $this->writeText($value); |
94 | $xmlWriter->endElement(); // w:t |
95 | $xmlWriter->endElement(); // w:r |
96 | |
97 | $xmlWriter->startElement('w:r'); |
98 | $this->writeFontStyle(); |
99 | $xmlWriter->writeElementBlock('w:fldChar', 'w:fldCharType', 'end'); |
100 | $xmlWriter->endElement(); // w:r |
101 | |
102 | $this->endElementP(); // w:p |
103 | } |
104 | |
105 | /** |
106 | * Write textinput. |
107 | * |
108 | * @see http://www.datypic.com/sc/ooxml/t-w_CT_FFTextInput.html |
109 | */ |
110 | private function writeTextInput(XMLWriter $xmlWriter, FormFieldElement $element): void |
111 | { |
112 | $default = $element->getDefault(); |
113 | |
114 | $xmlWriter->startElement('w:textInput'); |
115 | $xmlWriter->writeElementBlock('w:default', 'w:val', $default); |
116 | $xmlWriter->endElement(); |
117 | } |
118 | |
119 | /** |
120 | * Write checkbox. |
121 | * |
122 | * @see http://www.datypic.com/sc/ooxml/t-w_CT_FFCheckBox.html |
123 | */ |
124 | private function writeCheckBox(XMLWriter $xmlWriter, FormFieldElement $element): void |
125 | { |
126 | $default = $element->getDefault() ? 1 : 0; |
127 | $value = $element->getValue(); |
128 | if ($value == null) { |
129 | $value = $default; |
130 | } |
131 | $value = $value ? 1 : 0; |
132 | |
133 | $xmlWriter->startElement('w:checkBox'); |
134 | $xmlWriter->writeElementBlock('w:sizeAuto', 'w:val', ''); |
135 | $xmlWriter->writeElementBlock('w:default', 'w:val', $default); |
136 | $xmlWriter->writeElementBlock('w:checked', 'w:val', $value); |
137 | $xmlWriter->endElement(); |
138 | } |
139 | |
140 | /** |
141 | * Write dropdown. |
142 | * |
143 | * @see http://www.datypic.com/sc/ooxml/t-w_CT_FFDDList.html |
144 | */ |
145 | private function writeDropDown(XMLWriter $xmlWriter, FormFieldElement $element): void |
146 | { |
147 | $default = $element->getDefault(); |
148 | $value = $element->getValue(); |
149 | if ($value == null) { |
150 | $value = $default; |
151 | } |
152 | $entries = $element->getEntries(); |
153 | |
154 | $xmlWriter->startElement('w:ddList'); |
155 | $xmlWriter->writeElementBlock('w:result', 'w:val', $value); |
156 | $xmlWriter->writeElementBlock('w:default', 'w:val', $default); |
157 | foreach ($entries as $entry) { |
158 | if ($entry == null || $entry == '') { |
159 | $entry = str_repeat(' ', self::FILLER_LENGTH); |
160 | } |
161 | $xmlWriter->writeElementBlock('w:listEntry', 'w:val', $entry); |
162 | } |
163 | $xmlWriter->endElement(); |
164 | } |
165 | } |