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