Skip to content

Commit 6a81691

Browse files
committed
PHPOffice#266: Ability to add textinput, checkbox, and dropdown form elements
1 parent a13e5b2 commit 6a81691

File tree

14 files changed

+449
-25
lines changed

14 files changed

+449
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
44

55
## 0.12.0 - Not yet released
66

7-
This release added drawing shapes (arc, curve, line, polyline, rect, oval) and basic 2D chart (pie, doughnut, bar, line, area, scatter, radar) elements along with some new styles.
7+
This release added form fields (textinput, checkbox, and dropdown), drawing shapes (arc, curve, line, polyline, rect, oval), and basic 2D chart (pie, doughnut, bar, line, area, scatter, radar) elements along with some new styles.
88

99
### Features
1010

@@ -17,6 +17,7 @@ This release added drawing shapes (arc, curve, line, polyline, rect, oval) and b
1717
- General: New `Shared\Converter` static class - @ivanlanin
1818
- Chart: Basic 2D chart (pie, doughnut, bar, line, area, scatter, radar) - @ivanlanin GH-278
1919
- Chart: 3D charts and ability to set width and height - @ivanlanin
20+
- FormField: Ability to add textinput, checkbox, and dropdown form elements - @ivanlanin GH-266
2021

2122
### Bugfixes
2223

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ With PHPWord, you can create DOCX, ODT, or RTF documents dynamically using your
3232
- Insert footnotes and endnotes
3333
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
3434
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
35+
- Insert form fields (textinput, checkbox, and dropdown)
3536
- Create document from templates
3637
- Use XSL 1.0 style sheets to transform main document part of OOXML template
3738
- ... and many more features on progress

docs/elements.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ column shows the containers while the rows lists the elements.
5151
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
5252
| 21 | Chart | v | - | - | - | - | - |
5353
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
54+
| 22 | Form fields | v | v | v | v | v | v |
55+
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
5456

5557
Legend:
5658

@@ -406,3 +408,8 @@ Charts
406408
------
407409

408410
To be completed.
411+
412+
Form fields
413+
-----------
414+
415+
To be completed.

docs/intro.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Features
4747
- Insert footnotes and endnotes
4848
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
4949
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
50+
- Insert form fields (textinput, checkbox, and dropdown)
5051
- Create document from templates
5152
- Use XSL 1.0 style sheets to transform main document part of OOXML
5253
template

docs/src/documentation.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst
3838
- [Lines](#lines)
3939
- [Shapes](#shapes)
4040
- [Charts](#charts)
41+
- [FormFields](#form-fields)
4142
- [Styles](#styles)
4243
- [Section](#section)
4344
- [Font](#font)
@@ -79,6 +80,7 @@ PHPWord is an open source project licensed under the terms of [LGPL version 3](h
7980
- Insert footnotes and endnotes
8081
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
8182
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
83+
- Insert form fields (textinput, checkbox, and dropdown)
8284
- Create document from templates
8385
- Use XSL 1.0 style sheets to transform main document part of OOXML template
8486
- ... and many more features on progress
@@ -444,6 +446,7 @@ Below are the matrix of element availability in each container. The column shows
444446
| 19 | Line | v | v | v | v | v | v |
445447
| 20 | Shape | v | v | v | v | v | v |
446448
| 21 | Chart | v | - | - | - | - | - |
449+
| 22 | Form Fields | v | v | v | v | v | v |
447450

448451
Legend:
449452

@@ -745,6 +748,10 @@ To be completed.
745748

746749
To be completed.
747750

751+
## Form fields
752+
753+
To be completed.
754+
748755
# Styles
749756

750757
## Section

samples/Sample_33_FormField.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word document
5+
echo date('H:i:s'), " Create new PhpWord object", EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
$section = $phpWord->addSection();
9+
10+
$textrun = $section->addTextRun();
11+
$textrun->addText('Form fields can be added in a text run and can be in form of textinput ');
12+
$textrun->addFormField('textinput')->setName('MyTextBox');
13+
$textrun->addText(', checkbox ');
14+
$textrun->addFormField('checkbox')->setDefault(true);
15+
$textrun->addText(', or dropdown ');
16+
$textrun->addFormField('dropdown')->setEntries(array('Choice 1', 'Choice 2', 'Choice 3'));
17+
$textrun->addText('. You have to set document protection to "forms" to enable dropdown.');
18+
19+
$section->addText('They can also be added as a stand alone paragraph.');
20+
$section->addFormField('textinput')->setValue('Your name');
21+
22+
// Save file
23+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
24+
if (!CLI) {
25+
include_once 'Sample_Footer.php';
26+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* @method Field addField(string $type = null, array $properties = array(), array $options = array())
4242
* @method Line addLine(mixed $lineStyle = null)
4343
* @method Shape addObject(string $type, mixed $style = null)
44-
* @method Chart addChart()
44+
* @method Chart addChart(string $type, array $categories, array $values, array $style = null)
45+
* @method FormField addFormField(string $type, mixed $fStyle = null, mixed $pStyle = null)
4546
*
4647
* @since 0.10.0
4748
*/
@@ -78,7 +79,7 @@ public function __call($function, $args)
7879
$elements = array('Text', 'TextRun', 'Link', 'PreserveText', 'TextBreak',
7980
'ListItem', 'ListItemRun', 'Table', 'Image', 'Object', 'Footnote',
8081
'Endnote', 'CheckBox', 'TextBox', 'Field', 'Line', 'Shape',
81-
'Title', 'TOC', 'PageBreak', 'Chart');
82+
'Title', 'TOC', 'PageBreak', 'Chart', 'FormField');
8283
$functions = array();
8384
for ($i = 0; $i < count($elements); $i++) {
8485
$functions[$i] = 'add' . $elements[$i];
@@ -190,6 +191,7 @@ private function checkValidity($method)
190191
'Field' => $allContainers,
191192
'Line' => $allContainers,
192193
'Shape' => $allContainers,
194+
'FormField' => $allContainers,
193195
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
194196
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
195197
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

src/PhpWord/Element/Chart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use PhpOffice\PhpWord\Style\Chart as ChartStyle;
2121

22-
2322
/**
2423
* Chart element
2524
*
@@ -61,6 +60,7 @@ class Chart extends AbstractElement
6160
* @param string $type
6261
* @param array $categories
6362
* @param array $values
63+
* @param array $style
6464
*/
6565
public function __construct($type, $categories, $values, $style = null)
6666
{

src/PhpWord/Element/CheckBox.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* Check box element
24+
*
25+
* @since 0.10.0
2426
*/
2527
class CheckBox extends Text
2628
{

src/PhpWord/Element/FormField.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Element;
19+
20+
/**
21+
* Form field element
22+
*
23+
* @since 0.12.0
24+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_FFData.html
25+
*/
26+
class FormField extends Text
27+
{
28+
/**
29+
* Form field type: textinput|checkbox|dropdown
30+
*
31+
* @var string
32+
*/
33+
private $type = 'textinput';
34+
35+
/**
36+
* Form field name
37+
*
38+
* @var string
39+
*/
40+
private $name;
41+
42+
/**
43+
* Default value
44+
*
45+
* - TextInput: string
46+
* - CheckBox: bool
47+
* - DropDown: int Index of entries (zero based)
48+
*
49+
* @var string|bool|int
50+
*/
51+
private $default;
52+
53+
/**
54+
* Value
55+
*
56+
* @var string|bool|int
57+
*/
58+
private $value;
59+
60+
/**
61+
* Dropdown entries
62+
*
63+
* @var array
64+
*/
65+
private $entries = array();
66+
67+
/**
68+
* Create new instance
69+
*
70+
* @param string $type
71+
* @param mixed $fontStyle
72+
* @param mixed $paragraphStyle
73+
* @return self
74+
*/
75+
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
76+
{
77+
$this->setType($type);
78+
}
79+
80+
/**
81+
* Get type
82+
*
83+
* @return string
84+
*/
85+
public function getType()
86+
{
87+
return $this->type;
88+
}
89+
90+
/**
91+
* Set type
92+
*
93+
* @param string $value
94+
* @return self
95+
*/
96+
public function setType($value)
97+
{
98+
$enum = array('textinput', 'checkbox', 'dropdown');
99+
$this->type = $this->setEnumVal($value, $enum, $this->type);
100+
101+
return $this;
102+
}
103+
104+
/**
105+
* Get name
106+
*
107+
* @return string
108+
*/
109+
public function getName()
110+
{
111+
return $this->name;
112+
}
113+
114+
/**
115+
* Set name
116+
*
117+
* @param string|bool|int $value
118+
* @return self
119+
*/
120+
public function setName($value)
121+
{
122+
$this->name = $value;
123+
124+
return $this;
125+
}
126+
127+
/**
128+
* Get default
129+
*
130+
* @return string|bool|int
131+
*/
132+
public function getDefault()
133+
{
134+
return $this->default;
135+
}
136+
137+
/**
138+
* Set default
139+
*
140+
* @param string|bool|int $value
141+
* @return self
142+
*/
143+
public function setDefault($value)
144+
{
145+
$this->default = $value;
146+
147+
return $this;
148+
}
149+
150+
/**
151+
* Get value
152+
*
153+
* @return string|bool|int
154+
*/
155+
public function getValue()
156+
{
157+
return $this->value;
158+
}
159+
160+
/**
161+
* Set value
162+
*
163+
* @param string|bool|int $value
164+
* @return self
165+
*/
166+
public function setValue($value)
167+
{
168+
$this->value = $value;
169+
170+
return $this;
171+
}
172+
173+
/**
174+
* Get entries
175+
*
176+
* @return array
177+
*/
178+
public function getEntries()
179+
{
180+
return $this->entries;
181+
}
182+
183+
/**
184+
* Set entries
185+
*
186+
* @param array $value
187+
* @return self
188+
*/
189+
public function setEntries($value)
190+
{
191+
$this->entries = $value;
192+
193+
return $this;
194+
}
195+
}

0 commit comments

Comments
 (0)