From 5aac7f478244f39a8cddb39b94ac9b2a8b6d6ce8 Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Tue, 20 May 2014 22:28:34 +0200 Subject: [PATCH 1/4] From upstream --- src/PhpWord/Element/AbstractContainer.php | 14 ++ src/PhpWord/Element/Field.php | 180 ++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 src/PhpWord/Element/Field.php diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 86b604f4..5cff4bbd 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -128,6 +128,20 @@ abstract class AbstractContainer extends AbstractElement return $this->addGenericElement('TextRun', $paragraphStyle); } + /** + * Add field element + * @param + */ + public function addField($type = null, $properties = array(), $options = array()){ + //$this->checkValidity('Field'); + $elementDocPart = $this->checkElementDocPart(); + $element = new Field($type, $properties, $options); + $element->setDocPart($this->getDocPart(), $this->getDocPartId()); + $this->addElement($element); + + return $element; + } + /** * Add link element * diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php new file mode 100644 index 00000000..111d774e --- /dev/null +++ b/src/PhpWord/Element/Field.php @@ -0,0 +1,180 @@ + + + + + + 5 + + + + */ + + +namespace PhpOffice\PhpWord\Element; + +use PhpOffice\PhpWord\Shared\String; + +/** + * Field element + */ +class Field extends AbstractElement +{ + /** @const */ + + //self::$fieldsArray; + protected $fieldsArray = array( + 'PAGE'=>array( + 'properties'=>array( + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + ), + 'options'=>array() + ), + 'NUMPAGES'=>array( + 'properties'=>array( + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') + ), + 'options'=>array() + ) + ); + + /** + * Field type + * + * @var string + */ + protected $type; + + /** + * Field properties + * + * @var array + */ + protected $properties = array(); + + /** + * Field options + * + * @var array + */ + protected $options = array(); + + /** + * Create a new Field Element + * + * @param string $type + * @param mixed $properties + * @param mixed $options + */ + public function __construct($type = null, $properties = array(), $options = array()) + { + $this->setType($type); + $this->setProperties($properties); + $this->setOptions($options); + } + + /** + * Set Field type + * + * @param string + * @return string + */ + public function setType($type = null) + { + if (isset($type)) { + if (array_key_exists($type, $this->fieldsArray)) { + $this->type = $type; + } else { + throw new \InvalidArgumentException("Invalid type"); + } + } + return $this->type; + } + + /** + * Get Field type + * + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * Set Field properties + * + * @param array + * @return self + */ + public function setProperties($properties = array()) + { + if (is_array($properties)) { +//CREATE FUNCTION, WHICH MATCHES SUBARRAY + + if (array_key_exists($properties, $this->fieldsArray[$this->type])) { + $this->properties=array_merge($this->properties, $properties); + } else { + throw new \InvalidArgumentException("Invalid property"); + } + } + return self; + } + + /** + * Get Field properties + * + * @return array + */ + public function getProperties() + { + return $this->properties; + } + + /** + * Set Field options + * + * @param array + * @return self + */ + public function setOptions($options = array()) + { + if (is_array($options)) { + if (array_key_exists($options, self::$fieldsArray[$this->type])) { + $this->options=array_merge($this->options, $options); + } else { + throw new \InvalidArgumentException("Invalid option"); + } + } + return self; + } + + /** + * Get Field properties + * + * @return array + */ + public function getOptions() + { + return $this->options; + } + +} From 079d08e94a431341c4e8ebb5e092dca4493032dd Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Wed, 28 May 2014 17:59:44 +0200 Subject: [PATCH 2/4] Added Field Element --- samples/Sample_27_Field.php | 21 +++++ src/PhpWord/Element/AbstractContainer.php | 12 +-- src/PhpWord/Element/Field.php | 75 ++++++++-------- src/PhpWord/Writer/Word2007/Element/Field.php | 88 +++++++++++++++++++ tests/PhpWord/Tests/Element/FieldTest.php | 75 ++++++++++++++++ 5 files changed, 225 insertions(+), 46 deletions(-) create mode 100644 samples/Sample_27_Field.php create mode 100644 src/PhpWord/Writer/Word2007/Element/Field.php create mode 100644 tests/PhpWord/Tests/Element/FieldTest.php diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php new file mode 100644 index 00000000..6a556cca --- /dev/null +++ b/samples/Sample_27_Field.php @@ -0,0 +1,21 @@ +addSection(); + +// Add Field elements +// See Element/Field.php for all options +$section->addField('DATE', array('dateformat'=>'d-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar')); +$section->addField('PAGE', array('format'=>'ArabicDash')); +$section->addField('NUMPAGES', array('format'=>'Arabic', 'numformat'=>'0,00'), array('PreserveFormat')); + +// Save file +echo write($phpWord, basename(__FILE__, '.php'), $writers); +if (!CLI) { + include_once 'Sample_Footer.php'; +} diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 56425f4f..85aa5d5a 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -56,7 +56,7 @@ abstract class AbstractContainer extends AbstractElement // Get arguments $args = func_get_args(); - $withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')); + $withoutP = in_array($this->container, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun', 'Field')); if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) { $args[3] = null; // Remove paragraph style for texts in textrun } @@ -147,14 +147,10 @@ abstract class AbstractContainer extends AbstractElement * Add field element * @param */ - public function addField($type = null, $properties = array(), $options = array()){ - //$this->checkValidity('Field'); - $elementDocPart = $this->checkElementDocPart(); - $element = new Field($type, $properties, $options); - $element->setDocPart($this->getDocPart(), $this->getDocPartId()); - $this->addElement($element); + public function addField($type = null, $properties = array(), $options = array()) + { + return $this->addElement('Field', $type, $properties, $options); - return $element; } /** diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 111d774e..5d6101e2 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -15,19 +15,6 @@ * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ -/* - * - - - - - 5 - - - - */ - - namespace PhpOffice\PhpWord\Element; use PhpOffice\PhpWord\Shared\String; @@ -37,22 +24,35 @@ use PhpOffice\PhpWord\Shared\String; */ class Field extends AbstractElement { - /** @const */ - //self::$fieldsArray; + + /** + * Field properties and options. Depending on type, a field can have different properties + * and options + * + * @var array + */ protected $fieldsArray = array( - 'PAGE'=>array( - 'properties'=>array( - 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), - ), - 'options'=>array() + 'PAGE'=>array( + 'properties'=>array( + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + ), + 'options'=>array('PreserveFormat') ), 'NUMPAGES'=>array( - 'properties'=>array( - 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), - 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') - ), - 'options'=>array() + 'properties'=>array( + 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') + ), + 'options'=>array('PreserveFormat') + ), + 'DATE'=>array( + 'properties'=> array( + 'dateformat' =>array('d-M-yyyy', 'dddd d MMMM yyyy', 'd MMMM yyyy', 'd-M-yy', 'yyyy-MM-dd', + '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', + 'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss') + ), + 'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') ) ); @@ -128,15 +128,14 @@ class Field extends AbstractElement public function setProperties($properties = array()) { if (is_array($properties)) { -//CREATE FUNCTION, WHICH MATCHES SUBARRAY - - if (array_key_exists($properties, $this->fieldsArray[$this->type])) { - $this->properties=array_merge($this->properties, $properties); - } else { - throw new \InvalidArgumentException("Invalid property"); + foreach ($properties as $propkey => $propval) { + if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) { + throw new \InvalidArgumentException("Invalid property"); + } } + $this->properties=array_merge($this->properties, $properties); } - return self; + return $this->properties; } /** @@ -158,13 +157,14 @@ class Field extends AbstractElement public function setOptions($options = array()) { if (is_array($options)) { - if (array_key_exists($options, self::$fieldsArray[$this->type])) { - $this->options=array_merge($this->options, $options); - } else { - throw new \InvalidArgumentException("Invalid option"); + foreach ($options as $optionkey => $optionval) { + if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) { + throw new \InvalidArgumentException("Invalid option"); + } } + $this->options=array_merge($this->options, $options); } - return self; + return $this->options; } /** @@ -176,5 +176,4 @@ class Field extends AbstractElement { return $this->options; } - } diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php new file mode 100644 index 00000000..24656537 --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -0,0 +1,88 @@ +getXmlWriter(); + $element = $this->getElement(); + if (!$element instanceof \PhpOffice\PhpWord\Element\Field) { + return; + } + + $instruction=' '.$element->getType().' '; + $properties=$element->getProperties(); + foreach ($properties as $propkey => $propval) { + switch ($propkey) { + case 'format': + $instruction.='\* '.$propval.' '; + break; + case 'numformat': + $instruction.='\* '.$propval.' '; + break; + case 'dateformat': + $instruction.='\@ "'.$propval.'" '; + break; + } + } + + $options=$element->getOptions(); + foreach ($options as $option) { + switch ($option) { + case 'PreserveFormat': + $instruction.='\* MERGEFORMAT '; + break; + case 'LunarCalendar': + $instruction.='\h '; + break; + case 'SakaEraCalendar': + $instruction.='\s '; + break; + case 'LastUsedFormat': + $instruction.='\l '; + break; + } + } + $this->writeOpeningWP(); + $xmlWriter->startElement('w:fldSimple'); + $xmlWriter->writeAttribute('w:instr', $instruction); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:rPr'); + $xmlWriter->startElement('w:noProof'); + $xmlWriter->endElement(); // w:noProof + $xmlWriter->endElement(); // w:rPr + + $xmlWriter->startElement('w:t'); + $xmlWriter->writeRaw('1'); + $xmlWriter->endElement(); // w:t + $xmlWriter->endElement(); // w:r + $xmlWriter->endElement(); // w:fldSimple + + $this->writeClosingWP(); + } +} diff --git a/tests/PhpWord/Tests/Element/FieldTest.php b/tests/PhpWord/Tests/Element/FieldTest.php new file mode 100644 index 00000000..68fd8a84 --- /dev/null +++ b/tests/PhpWord/Tests/Element/FieldTest.php @@ -0,0 +1,75 @@ +assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + } + + /** + * New instance with type + */ + public function testConstructWithType() + { + $oField = new Field('DATE'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals($oField->getType(), 'DATE'); + } + + /** + * New instance with type and properties + */ + public function testConstructWithTypeProperties() + { + $oField = new Field('DATE', array('dateformat'=>'d-M-yyyy')); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals($oField->getType(), 'DATE'); + $this->assertEquals($oField->getProperties(), array('dateformat'=>'d-M-yyyy')); + } + + /** + * New instance with type and properties and options + */ + public function testConstructWithTypePropertiesOptions() + { + $oField = new Field('DATE', array('dateformat'=>'d-M-yyyy'), array('SakaEraCalendar', 'PreserveFormat')); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals($oField->getType(), 'DATE'); + $this->assertEquals($oField->getProperties(), array('dateformat'=>'d-M-yyyy')); + $this->assertEquals($oField->getOptions(), array('SakaEraCalendar', 'PreserveFormat')); + } +} From 8f74f26fd483d9ef8f56250388df37bf708d0072 Mon Sep 17 00:00:00 2001 From: Bas-Jan 't Jong Date: Wed, 28 May 2014 20:35:24 +0200 Subject: [PATCH 3/4] Travis build error fix --- src/PhpWord/Element/Field.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 5d6101e2..0bde0387 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -128,7 +128,7 @@ class Field extends AbstractElement public function setProperties($properties = array()) { if (is_array($properties)) { - foreach ($properties as $propkey => $propval) { + foreach (array_keys($properties) as $propkey) { if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) { throw new \InvalidArgumentException("Invalid property"); } @@ -157,7 +157,7 @@ class Field extends AbstractElement public function setOptions($options = array()) { if (is_array($options)) { - foreach ($options as $optionkey => $optionval) { + foreach (array_keys($options) as $optionkey) { if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) { throw new \InvalidArgumentException("Invalid option"); } From e81d92e26529ced07481f898661526c6ca7f4a9d Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 29 May 2014 16:21:15 +0700 Subject: [PATCH 4/4] Update changelog, docs, and unit tests for new `Field` element #251 --- CHANGELOG.md | 8 +-- docs/elements.rst | 18 +++++- docs/src/documentation.md | 16 +++++- samples/Sample_27_Field.php | 12 +++- src/PhpWord/Element/AbstractContainer.php | 10 +++- src/PhpWord/Element/Field.php | 55 +++++++++---------- src/PhpWord/Writer/Word2007/Element/Field.php | 29 +++++----- .../Tests/Writer/Word2007/ElementTest.php | 2 +- .../Writer/Word2007/Part/DocumentTest.php | 4 ++ 9 files changed, 98 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41f0c3ee..4584c4a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,16 +4,15 @@ This is the changelog between releases of PHPWord. Releases are listed in revers ## 0.11.0 - Not yet released -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. +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. ### Features - Image: Ability to define relative and absolute positioning - @basjan GH-217 - Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219 -- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 GH-229 -- TextBox: Ability to add table inside textbox - @basjan GH-231 +- Element: New `TextBox` element - @basjan @ivanlanin GH-228 GH-229 GH-231 - HTML: Ability to add elements to PHPWord object via html - @basjan GH-231 -- ListItemRun: New element that can add a list item with inline formatting like a textrun - @basjan GH-235 +- Element: New `ListItemRun` element that can add a list item with inline formatting like a textrun - @basjan GH-235 - Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149 - RTF Writer: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 - 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 - Image: Enable "image float left" - @ivanlanin GH-244 - RTF Writer: Ability to write document properties - @ivanlanin - RTF Writer: Ability to write image - @ivanlanin +- Element: New `Field` element - @basjan GH-251 ### Bugfixes diff --git a/docs/elements.rst b/docs/elements.rst index c86d1074..7e0c33f6 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -17,7 +17,7 @@ column shows the containers while the rows lists the elements. +-------+-----------------+-----------+----------+----------+---------+------------+------------+ | 4 | Title | v | ? | ? | ? | ? | ? | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ -| 5 | Preserve Text | ? | v | v | v\* | ? | ? | +| 5 | Preserve Text | ? | v | v | v\* | - | - | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ | 6 | Text Break | v | v | v | v | v | v | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ @@ -39,7 +39,11 @@ column shows the containers while the rows lists the elements. +-------+-----------------+-----------+----------+----------+---------+------------+------------+ | 15 | Endnote | v | - | - | v\*\* | v\*\* | - | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ -| 16 | CheckBox | v | v | v | v | ? | ? | +| 16 | CheckBox | v | v | v | v | - | - | ++-------+-----------------+-----------+----------+----------+---------+------------+------------+ +| 17 | TextBox | v | v | v | v | - | - | ++-------+-----------------+-----------+----------+----------+---------+------------+------------+ +| 18 | Field | v | v | v | v | v | v | +-------+-----------------+-----------+----------+----------+---------+------------+------------+ Legend: @@ -473,3 +477,13 @@ Checkbox elements can be added to sections or table cells by using - ``$text`` Text following the check box - ``$fontStyle`` See "Font style" section. - ``$paragraphStyle`` See "Paragraph style" section. + +Textboxes +--------- + +To be completed + +Fields +------ + +To be completed diff --git a/docs/src/documentation.md b/docs/src/documentation.md index 6d34ff9f..889842f9 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -33,6 +33,8 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst - [Table of contents](#table-of-contents) - [Footnotes & endnotes](#footnotes-endnotes) - [Checkboxes](#checkboxes) + - [Textboxes](#textboxes) + - [Fields](#fields) - [Templates](#templates) - [Writers & readers](#writers-readers) - [OOXML](#ooxml) @@ -447,7 +449,7 @@ Below are the matrix of element availability in each container. The column shows | 2 | Text Run | v | v | v | v | - | - | | 3 | Link | v | v | v | v | v | v | | 4 | Title | v | ? | ? | ? | ? | ? | -| 5 | Preserve Text | ? | v | v | v* | ? | ? | +| 5 | Preserve Text | ? | v | v | v* | - | - | | 6 | Text Break | v | v | v | v | v | v | | 7 | Page Break | v | - | - | - | - | - | | 8 | List | v | v | v | v | - | - | @@ -458,7 +460,9 @@ Below are the matrix of element availability in each container. The column shows | 13 | TOC | v | - | - | - | - | - | | 14 | Footnote | v | - | - | v** | v** | - | | 15 | Endnote | v | - | - | v** | v** | - | -| 16 | CheckBox | v | v | v | v | ? | ? | +| 16 | CheckBox | v | v | v | v | - | - | +| 17 | TextBox | v | v | v | v | - | - | +| 18 | Field | v | v | v | v | v | v | Legend: @@ -832,6 +836,14 @@ $section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle]) - `$fontStyle` See "Font style" section. - `$paragraphStyle` See "Paragraph style" section. +## Textboxes + +To be completed. + +## Fields + +To be completed. + # Templates 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. diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 6a556cca..fd750372 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -10,10 +10,20 @@ $section = $phpWord->addSection(); // Add Field elements // See Element/Field.php for all options -$section->addField('DATE', array('dateformat'=>'d-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar')); +$section->addText('Date field:'); +$section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat')); + +$section->addText('Page field:'); $section->addField('PAGE', array('format'=>'ArabicDash')); + +$section->addText('Number of pages field:'); $section->addField('NUMPAGES', array('format'=>'Arabic', 'numformat'=>'0,00'), array('PreserveFormat')); +$textrun = $section->addTextRun(array('align' => 'center')); +$textrun->addText('This is the date of lunar calendar '); +$textrun->addField('DATE', array('dateformat'=>'d-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar')); +$textrun->addText(' written in a textrun.'); + // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 85aa5d5a..71c5ae2a 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -145,14 +145,17 @@ abstract class AbstractContainer extends AbstractElement /** * Add field element - * @param + * + * @param string $type + * @param array $properties + * @param array $options */ public function addField($type = null, $properties = array(), $options = array()) { return $this->addElement('Field', $type, $properties, $options); - + } - + /** * Add link element * @@ -327,6 +330,7 @@ abstract class AbstractContainer extends AbstractElement 'TextBreak' => $allContainers, 'Image' => $allContainers, 'Object' => $allContainers, + 'Field' => $allContainers, 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 0bde0387..7503dc9b 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -1,35 +1,34 @@ array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') ) ); - + /** * Field type * @@ -81,8 +80,8 @@ class Field extends AbstractElement * Create a new Field Element * * @param string $type - * @param mixed $properties - * @param mixed $options + * @param array $properties + * @param array $options */ public function __construct($type = null, $properties = array(), $options = array()) { @@ -94,7 +93,7 @@ class Field extends AbstractElement /** * Set Field type * - * @param string + * @param string $type * @return string */ public function setType($type = null) @@ -122,7 +121,7 @@ class Field extends AbstractElement /** * Set Field properties * - * @param array + * @param array $properties * @return self */ public function setProperties($properties = array()) @@ -130,10 +129,10 @@ class Field extends AbstractElement if (is_array($properties)) { foreach (array_keys($properties) as $propkey) { if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) { - throw new \InvalidArgumentException("Invalid property"); + throw new \InvalidArgumentException("Invalid property"); } } - $this->properties=array_merge($this->properties, $properties); + $this->properties = array_merge($this->properties, $properties); } return $this->properties; } @@ -151,7 +150,7 @@ class Field extends AbstractElement /** * Set Field options * - * @param array + * @param array $options * @return self */ public function setOptions($options = array()) @@ -162,11 +161,11 @@ class Field extends AbstractElement throw new \InvalidArgumentException("Invalid option"); } } - $this->options=array_merge($this->options, $options); + $this->options = array_merge($this->options, $options); } return $this->options; } - + /** * Get Field properties * diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index 24656537..68c4c40c 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -20,7 +20,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; /** * Field element writer * - * @since 0.10.0 + * @since 0.11.0 */ class Field extends Text { @@ -30,44 +30,43 @@ class Field extends Text public function write() { $xmlWriter = $this->getXmlWriter(); - $element = $this->getElement(); + $element = $this->getElement(); if (!$element instanceof \PhpOffice\PhpWord\Element\Field) { return; } - $instruction=' '.$element->getType().' '; - $properties=$element->getProperties(); + $instruction = ' ' . $element->getType() . ' '; + $properties = $element->getProperties(); foreach ($properties as $propkey => $propval) { switch ($propkey) { case 'format': - $instruction.='\* '.$propval.' '; - break; case 'numformat': - $instruction.='\* '.$propval.' '; + $instruction .= '\* ' . $propval . ' '; break; case 'dateformat': - $instruction.='\@ "'.$propval.'" '; + $instruction .= '\@ "' . $propval . '" '; break; } } - - $options=$element->getOptions(); + + $options = $element->getOptions(); foreach ($options as $option) { switch ($option) { case 'PreserveFormat': - $instruction.='\* MERGEFORMAT '; + $instruction .= '\* MERGEFORMAT '; break; case 'LunarCalendar': - $instruction.='\h '; + $instruction .= '\h '; break; case 'SakaEraCalendar': - $instruction.='\s '; + $instruction .= '\s '; break; case 'LastUsedFormat': - $instruction.='\l '; + $instruction .= '\l '; break; } } + $this->writeOpeningWP(); $xmlWriter->startElement('w:fldSimple'); $xmlWriter->writeAttribute('w:instr', $instruction); @@ -76,7 +75,7 @@ class Field extends Text $xmlWriter->startElement('w:noProof'); $xmlWriter->endElement(); // w:noProof $xmlWriter->endElement(); // w:rPr - + $xmlWriter->startElement('w:t'); $xmlWriter->writeRaw('1'); $xmlWriter->endElement(); // w:t diff --git a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php index 4d1d7ce2..ee8b88ae 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/ElementTest.php @@ -30,7 +30,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase { $elements = array( 'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', - 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC' + 'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC', 'Field' ); foreach ($elements as $element) { $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element; diff --git a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php index 8ed1246f..09f7d6a4 100644 --- a/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php +++ b/tests/PhpWord/Tests/Writer/Word2007/Part/DocumentTest.php @@ -83,6 +83,10 @@ class DocumentTest extends \PHPUnit_Framework_TestCase 'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0')); $section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center')); $section->addListItemRun()->addText('List item run 1'); + $section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar')); + $section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'SakaEraCalendar')); + $section->addField('DATE', array('dateformat'=>'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat', 'LastUsedFormat')); + $section->addField('PAGE', array('format'=>'ArabicDash')); $doc = TestHelperDOCX::getDocument($phpWord);