Skip to content

Commit e81d92e

Browse files
committed
Update changelog, docs, and unit tests for new Field element #251
1 parent 8f74f26 commit e81d92e

File tree

9 files changed

+98
-56
lines changed

9 files changed

+98
-56
lines changed

CHANGELOG.md

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

55
## 0.11.0 - Not yet released
66

7-
This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new relative and absolute positioning for image; new `TextBox` and `ListItemRun` element; refactorings of writer classes into parts, elements, and styles; and ability to add elements to PHPWord object via HTML.
7+
This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three new elements were added: TextBox, ListItemRun, and Field. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted.
88

99
### Features
1010

1111
- Image: Ability to define relative and absolute positioning - @basjan GH-217
1212
- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219
13-
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 GH-229
14-
- TextBox: Ability to add table inside textbox - @basjan GH-231
13+
- Element: New `TextBox` element - @basjan @ivanlanin GH-228 GH-229 GH-231
1514
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
16-
- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235
15+
- Element: New `ListItemRun` element that can add a list item with inline formatting like a textrun - @basjan GH-235
1716
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
1817
- RTF Writer: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
1918
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
@@ -30,6 +29,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
3029
- Image: Enable "image float left" - @ivanlanin GH-244
3130
- RTF Writer: Ability to write document properties - @ivanlanin
3231
- RTF Writer: Ability to write image - @ivanlanin
32+
- Element: New `Field` element - @basjan GH-251
3333

3434
### Bugfixes
3535

docs/elements.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ column shows the containers while the rows lists the elements.
1717
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
1818
| 4 | Title | v | ? | ? | ? | ? | ? |
1919
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
20-
| 5 | Preserve Text | ? | v | v | v\* | ? | ? |
20+
| 5 | Preserve Text | ? | v | v | v\* | - | - |
2121
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
2222
| 6 | Text Break | v | v | v | v | v | v |
2323
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
@@ -39,7 +39,11 @@ column shows the containers while the rows lists the elements.
3939
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
4040
| 15 | Endnote | v | - | - | v\*\* | v\*\* | - |
4141
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
42-
| 16 | CheckBox | v | v | v | v | ? | ? |
42+
| 16 | CheckBox | v | v | v | v | - | - |
43+
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
44+
| 17 | TextBox | v | v | v | v | - | - |
45+
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
46+
| 18 | Field | v | v | v | v | v | v |
4347
+-------+-----------------+-----------+----------+----------+---------+------------+------------+
4448

4549
Legend:
@@ -473,3 +477,13 @@ Checkbox elements can be added to sections or table cells by using
473477
- ``$text`` Text following the check box
474478
- ``$fontStyle`` See "Font style" section.
475479
- ``$paragraphStyle`` See "Paragraph style" section.
480+
481+
Textboxes
482+
---------
483+
484+
To be completed
485+
486+
Fields
487+
------
488+
489+
To be completed

docs/src/documentation.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst
3333
- [Table of contents](#table-of-contents)
3434
- [Footnotes & endnotes](#footnotes-endnotes)
3535
- [Checkboxes](#checkboxes)
36+
- [Textboxes](#textboxes)
37+
- [Fields](#fields)
3638
- [Templates](#templates)
3739
- [Writers & readers](#writers-readers)
3840
- [OOXML](#ooxml)
@@ -447,7 +449,7 @@ Below are the matrix of element availability in each container. The column shows
447449
| 2 | Text Run | v | v | v | v | - | - |
448450
| 3 | Link | v | v | v | v | v | v |
449451
| 4 | Title | v | ? | ? | ? | ? | ? |
450-
| 5 | Preserve Text | ? | v | v | v* | ? | ? |
452+
| 5 | Preserve Text | ? | v | v | v* | - | - |
451453
| 6 | Text Break | v | v | v | v | v | v |
452454
| 7 | Page Break | v | - | - | - | - | - |
453455
| 8 | List | v | v | v | v | - | - |
@@ -458,7 +460,9 @@ Below are the matrix of element availability in each container. The column shows
458460
| 13 | TOC | v | - | - | - | - | - |
459461
| 14 | Footnote | v | - | - | v** | v** | - |
460462
| 15 | Endnote | v | - | - | v** | v** | - |
461-
| 16 | CheckBox | v | v | v | v | ? | ? |
463+
| 16 | CheckBox | v | v | v | v | - | - |
464+
| 17 | TextBox | v | v | v | v | - | - |
465+
| 18 | Field | v | v | v | v | v | v |
462466

463467
Legend:
464468

@@ -832,6 +836,14 @@ $section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle])
832836
- `$fontStyle` See "Font style" section.
833837
- `$paragraphStyle` See "Paragraph style" section.
834838

839+
## Textboxes
840+
841+
To be completed.
842+
843+
## Fields
844+
845+
To be completed.
846+
835847
# Templates
836848

837849
You can create a docx template with included search-patterns that can be replaced by any value you wish. Only single-line values can be replaced. To load a template file, use the `loadTemplate` method. After loading the docx template, you can use the `setValue` method to change the value of a search pattern. The search-pattern model is: `${search-pattern}`. It is not possible to add new PHPWord elements to a loaded template file.

samples/Sample_27_Field.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@
1010

1111
// Add Field elements
1212
// See Element/Field.php for all options
13-
$section->addField('DATE', array('dateformat'=>'d-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar'));
13+
$section->addText('Date field:');
14+
$section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat'));
15+
16+
$section->addText('Page field:');
1417
$section->addField('PAGE', array('format'=>'ArabicDash'));
18+
19+
$section->addText('Number of pages field:');
1520
$section->addField('NUMPAGES', array('format'=>'Arabic', 'numformat'=>'0,00'), array('PreserveFormat'));
1621

22+
$textrun = $section->addTextRun(array('align' => 'center'));
23+
$textrun->addText('This is the date of lunar calendar ');
24+
$textrun->addField('DATE', array('dateformat'=>'d-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar'));
25+
$textrun->addText(' written in a textrun.');
26+
1727
// Save file
1828
echo write($phpWord, basename(__FILE__, '.php'), $writers);
1929
if (!CLI) {

src/PhpWord/Element/AbstractContainer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,17 @@ public function addTextRun($paragraphStyle = null)
145145

146146
/**
147147
* Add field element
148-
* @param
148+
*
149+
* @param string $type
150+
* @param array $properties
151+
* @param array $options
149152
*/
150153
public function addField($type = null, $properties = array(), $options = array())
151154
{
152155
return $this->addElement('Field', $type, $properties, $options);
153-
156+
154157
}
155-
158+
156159
/**
157160
* Add link element
158161
*
@@ -327,6 +330,7 @@ private function checkValidity($method)
327330
'TextBreak' => $allContainers,
328331
'Image' => $allContainers,
329332
'Object' => $allContainers,
333+
'Field' => $allContainers,
330334
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
331335
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
332336
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

src/PhpWord/Element/Field.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
<?php
22
/**
33
* 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-
*/
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+
*/
1717

1818
namespace PhpOffice\PhpWord\Element;
1919

20-
use PhpOffice\PhpWord\Shared\String;
21-
2220
/**
2321
* Field element
22+
*
23+
* @since 0.11.0
24+
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_SimpleField.html
2425
*/
2526
class Field extends AbstractElement
2627
{
27-
28-
2928
/**
3029
* Field properties and options. Depending on type, a field can have different properties
3130
* and options
32-
*
31+
*
3332
* @var array
3433
*/
3534
protected $fieldsArray = array(
@@ -55,7 +54,7 @@ class Field extends AbstractElement
5554
'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat')
5655
)
5756
);
58-
57+
5958
/**
6059
* Field type
6160
*
@@ -81,8 +80,8 @@ class Field extends AbstractElement
8180
* Create a new Field Element
8281
*
8382
* @param string $type
84-
* @param mixed $properties
85-
* @param mixed $options
83+
* @param array $properties
84+
* @param array $options
8685
*/
8786
public function __construct($type = null, $properties = array(), $options = array())
8887
{
@@ -94,7 +93,7 @@ public function __construct($type = null, $properties = array(), $options = arra
9493
/**
9594
* Set Field type
9695
*
97-
* @param string
96+
* @param string $type
9897
* @return string
9998
*/
10099
public function setType($type = null)
@@ -122,18 +121,18 @@ public function getType()
122121
/**
123122
* Set Field properties
124123
*
125-
* @param array
124+
* @param array $properties
126125
* @return self
127126
*/
128127
public function setProperties($properties = array())
129128
{
130129
if (is_array($properties)) {
131130
foreach (array_keys($properties) as $propkey) {
132131
if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) {
133-
throw new \InvalidArgumentException("Invalid property");
132+
throw new \InvalidArgumentException("Invalid property");
134133
}
135134
}
136-
$this->properties=array_merge($this->properties, $properties);
135+
$this->properties = array_merge($this->properties, $properties);
137136
}
138137
return $this->properties;
139138
}
@@ -151,7 +150,7 @@ public function getProperties()
151150
/**
152151
* Set Field options
153152
*
154-
* @param array
153+
* @param array $options
155154
* @return self
156155
*/
157156
public function setOptions($options = array())
@@ -162,11 +161,11 @@ public function setOptions($options = array())
162161
throw new \InvalidArgumentException("Invalid option");
163162
}
164163
}
165-
$this->options=array_merge($this->options, $options);
164+
$this->options = array_merge($this->options, $options);
166165
}
167166
return $this->options;
168167
}
169-
168+
170169
/**
171170
* Get Field properties
172171
*

src/PhpWord/Writer/Word2007/Element/Field.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Field element writer
2222
*
23-
* @since 0.10.0
23+
* @since 0.11.0
2424
*/
2525
class Field extends Text
2626
{
@@ -30,44 +30,43 @@ class Field extends Text
3030
public function write()
3131
{
3232
$xmlWriter = $this->getXmlWriter();
33-
$element = $this->getElement();
33+
$element = $this->getElement();
3434
if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
3535
return;
3636
}
3737

38-
$instruction=' '.$element->getType().' ';
39-
$properties=$element->getProperties();
38+
$instruction = ' ' . $element->getType() . ' ';
39+
$properties = $element->getProperties();
4040
foreach ($properties as $propkey => $propval) {
4141
switch ($propkey) {
4242
case 'format':
43-
$instruction.='\* '.$propval.' ';
44-
break;
4543
case 'numformat':
46-
$instruction.='\* '.$propval.' ';
44+
$instruction .= '\* ' . $propval . ' ';
4745
break;
4846
case 'dateformat':
49-
$instruction.='\@ "'.$propval.'" ';
47+
$instruction .= '\@ "' . $propval . '" ';
5048
break;
5149
}
5250
}
53-
54-
$options=$element->getOptions();
51+
52+
$options = $element->getOptions();
5553
foreach ($options as $option) {
5654
switch ($option) {
5755
case 'PreserveFormat':
58-
$instruction.='\* MERGEFORMAT ';
56+
$instruction .= '\* MERGEFORMAT ';
5957
break;
6058
case 'LunarCalendar':
61-
$instruction.='\h ';
59+
$instruction .= '\h ';
6260
break;
6361
case 'SakaEraCalendar':
64-
$instruction.='\s ';
62+
$instruction .= '\s ';
6563
break;
6664
case 'LastUsedFormat':
67-
$instruction.='\l ';
65+
$instruction .= '\l ';
6866
break;
6967
}
7068
}
69+
7170
$this->writeOpeningWP();
7271
$xmlWriter->startElement('w:fldSimple');
7372
$xmlWriter->writeAttribute('w:instr', $instruction);
@@ -76,7 +75,7 @@ public function write()
7675
$xmlWriter->startElement('w:noProof');
7776
$xmlWriter->endElement(); // w:noProof
7877
$xmlWriter->endElement(); // w:rPr
79-
78+
8079
$xmlWriter->startElement('w:t');
8180
$xmlWriter->writeRaw('1');
8281
$xmlWriter->endElement(); // w:t

tests/PhpWord/Tests/Writer/Word2007/ElementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testUnmatchedElements()
3030
{
3131
$elements = array(
3232
'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun',
33-
'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC'
33+
'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC', 'Field'
3434
);
3535
foreach ($elements as $element) {
3636
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element;

tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public function testElements()
8383
'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0'));
8484
$section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center'));
8585
$section->addListItemRun()->addText('List item run 1');
86+
$section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar'));
87+
$section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'SakaEraCalendar'));
88+
$section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'LastUsedFormat'));
89+
$section->addField('PAGE', array('format'=>'ArabicDash'));
8690

8791
$doc = TestHelperDOCX::getDocument($phpWord);
8892

0 commit comments

Comments
 (0)