Skip to content

Commit 7c2ad59

Browse files
committed
Merge branch 'develop' into rtf
2 parents 2d88ea2 + e81d92e commit 7c2ad59

File tree

10 files changed

+425
-10
lines changed

10 files changed

+425
-10
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// Begin code
9+
$section = $phpWord->addSection();
10+
11+
// Add Field elements
12+
// See Element/Field.php for all options
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:');
17+
$section->addField('PAGE', array('format'=>'ArabicDash'));
18+
19+
$section->addText('Number of pages field:');
20+
$section->addField('NUMPAGES', array('format'=>'Arabic', 'numformat'=>'0,00'), array('PreserveFormat'));
21+
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+
27+
// Save file
28+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
29+
if (!CLI) {
30+
include_once 'Sample_Footer.php';
31+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function addElement($elementName)
5656

5757
// Get arguments
5858
$args = func_get_args();
59-
$withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun'));
59+
$withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun', 'Field'));
6060
if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) {
6161
$args[3] = null; // Remove paragraph style for texts in textrun
6262
}
@@ -143,6 +143,19 @@ public function addTextRun($paragraphStyle = null)
143143
return $this->addElement('TextRun', $paragraphStyle);
144144
}
145145

146+
/**
147+
* Add field element
148+
*
149+
* @param string $type
150+
* @param array $properties
151+
* @param array $options
152+
*/
153+
public function addField($type = null, $properties = array(), $options = array())
154+
{
155+
return $this->addElement('Field', $type, $properties, $options);
156+
157+
}
158+
146159
/**
147160
* Add link element
148161
*
@@ -317,6 +330,7 @@ private function checkValidity($method)
317330
'TextBreak' => $allContainers,
318331
'Image' => $allContainers,
319332
'Object' => $allContainers,
333+
'Field' => $allContainers,
320334
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
321335
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
322336
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

src/PhpWord/Element/Field.php

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
* Field element
22+
*
23+
* @since 0.11.0
24+
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_SimpleField.html
25+
*/
26+
class Field extends AbstractElement
27+
{
28+
/**
29+
* Field properties and options. Depending on type, a field can have different properties
30+
* and options
31+
*
32+
* @var array
33+
*/
34+
protected $fieldsArray = array(
35+
'PAGE'=>array(
36+
'properties'=>array(
37+
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
38+
),
39+
'options'=>array('PreserveFormat')
40+
),
41+
'NUMPAGES'=>array(
42+
'properties'=>array(
43+
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
44+
'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%')
45+
),
46+
'options'=>array('PreserveFormat')
47+
),
48+
'DATE'=>array(
49+
'properties'=> array(
50+
'dateformat' =>array('d-M-yyyy', 'dddd d MMMM yyyy', 'd MMMM yyyy', 'd-M-yy', 'yyyy-MM-dd',
51+
'd-MMM-yy', 'd/M/yyyy', 'd MMM. yy', 'd/M/yy', 'MMM-yy', 'd-M-yyy H:mm', 'd-M-yyyy H:mm:ss',
52+
'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss')
53+
),
54+
'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat')
55+
)
56+
);
57+
58+
/**
59+
* Field type
60+
*
61+
* @var string
62+
*/
63+
protected $type;
64+
65+
/**
66+
* Field properties
67+
*
68+
* @var array
69+
*/
70+
protected $properties = array();
71+
72+
/**
73+
* Field options
74+
*
75+
* @var array
76+
*/
77+
protected $options = array();
78+
79+
/**
80+
* Create a new Field Element
81+
*
82+
* @param string $type
83+
* @param array $properties
84+
* @param array $options
85+
*/
86+
public function __construct($type = null, $properties = array(), $options = array())
87+
{
88+
$this->setType($type);
89+
$this->setProperties($properties);
90+
$this->setOptions($options);
91+
}
92+
93+
/**
94+
* Set Field type
95+
*
96+
* @param string $type
97+
* @return string
98+
*/
99+
public function setType($type = null)
100+
{
101+
if (isset($type)) {
102+
if (array_key_exists($type, $this->fieldsArray)) {
103+
$this->type = $type;
104+
} else {
105+
throw new \InvalidArgumentException("Invalid type");
106+
}
107+
}
108+
return $this->type;
109+
}
110+
111+
/**
112+
* Get Field type
113+
*
114+
* @return string
115+
*/
116+
public function getType()
117+
{
118+
return $this->type;
119+
}
120+
121+
/**
122+
* Set Field properties
123+
*
124+
* @param array $properties
125+
* @return self
126+
*/
127+
public function setProperties($properties = array())
128+
{
129+
if (is_array($properties)) {
130+
foreach (array_keys($properties) as $propkey) {
131+
if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) {
132+
throw new \InvalidArgumentException("Invalid property");
133+
}
134+
}
135+
$this->properties = array_merge($this->properties, $properties);
136+
}
137+
return $this->properties;
138+
}
139+
140+
/**
141+
* Get Field properties
142+
*
143+
* @return array
144+
*/
145+
public function getProperties()
146+
{
147+
return $this->properties;
148+
}
149+
150+
/**
151+
* Set Field options
152+
*
153+
* @param array $options
154+
* @return self
155+
*/
156+
public function setOptions($options = array())
157+
{
158+
if (is_array($options)) {
159+
foreach (array_keys($options) as $optionkey) {
160+
if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) {
161+
throw new \InvalidArgumentException("Invalid option");
162+
}
163+
}
164+
$this->options = array_merge($this->options, $options);
165+
}
166+
return $this->options;
167+
}
168+
169+
/**
170+
* Get Field properties
171+
*
172+
* @return array
173+
*/
174+
public function getOptions()
175+
{
176+
return $this->options;
177+
}
178+
}

0 commit comments

Comments
 (0)