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 new file mode 100644 index 00000000..fd750372 --- /dev/null +++ b/samples/Sample_27_Field.php @@ -0,0 +1,31 @@ +addSection(); + +// Add Field elements +// See Element/Field.php for all options +$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) { + include_once 'Sample_Footer.php'; +} diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index dc87cf18..71c5ae2a 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 } @@ -143,6 +143,19 @@ abstract class AbstractContainer extends AbstractElement return $this->addElement('TextRun', $paragraphStyle); } + /** + * Add field element + * + * @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 * @@ -317,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 new file mode 100644 index 00000000..7503dc9b --- /dev/null +++ b/src/PhpWord/Element/Field.php @@ -0,0 +1,178 @@ +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('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') + ) + ); + + /** + * 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 array $properties + * @param array $options + */ + public function __construct($type = null, $properties = array(), $options = array()) + { + $this->setType($type); + $this->setProperties($properties); + $this->setOptions($options); + } + + /** + * Set Field type + * + * @param string $type + * @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 $properties + * @return self + */ + public function setProperties($properties = array()) + { + 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"); + } + } + $this->properties = array_merge($this->properties, $properties); + } + return $this->properties; + } + + /** + * Get Field properties + * + * @return array + */ + public function getProperties() + { + return $this->properties; + } + + /** + * Set Field options + * + * @param array $options + * @return self + */ + public function setOptions($options = array()) + { + if (is_array($options)) { + foreach (array_keys($options) as $optionkey) { + if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) { + throw new \InvalidArgumentException("Invalid option"); + } + } + $this->options = array_merge($this->options, $options); + } + return $this->options; + } + + /** + * Get Field properties + * + * @return array + */ + public function getOptions() + { + 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..68c4c40c --- /dev/null +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -0,0 +1,87 @@ +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': + 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')); + } +} 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);