Merge branch 'develop' into rtf

This commit is contained in:
Ivan Lanin 2014-05-29 16:54:13 +07:00
commit 7c2ad59530
10 changed files with 425 additions and 10 deletions

View File

@ -4,16 +4,15 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
## 0.11.0 - Not yet released ## 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 ### Features
- Image: Ability to define relative and absolute positioning - @basjan GH-217 - 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 - 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 - Element: New `TextBox` element - @basjan @ivanlanin GH-228 GH-229 GH-231
- TextBox: Ability to add table inside textbox - @basjan GH-231
- HTML: Ability to add elements to PHPWord object via html - @basjan 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 - 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 - 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 - 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 - Image: Enable "image float left" - @ivanlanin GH-244
- RTF Writer: Ability to write document properties - @ivanlanin - RTF Writer: Ability to write document properties - @ivanlanin
- RTF Writer: Ability to write image - @ivanlanin - RTF Writer: Ability to write image - @ivanlanin
- Element: New `Field` element - @basjan GH-251
### Bugfixes ### Bugfixes

View File

@ -17,7 +17,7 @@ column shows the containers while the rows lists the elements.
+-------+-----------------+-----------+----------+----------+---------+------------+------------+ +-------+-----------------+-----------+----------+----------+---------+------------+------------+
| 4 | Title | 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 | | 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\*\* | - | | 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: Legend:
@ -473,3 +477,13 @@ Checkbox elements can be added to sections or table cells by using
- ``$text`` Text following the check box - ``$text`` Text following the check box
- ``$fontStyle`` See "Font style" section. - ``$fontStyle`` See "Font style" section.
- ``$paragraphStyle`` See "Paragraph style" section. - ``$paragraphStyle`` See "Paragraph style" section.
Textboxes
---------
To be completed
Fields
------
To be completed

View File

@ -33,6 +33,8 @@ Don't forget to change `code::` directive to `code-block::` in the resulting rst
- [Table of contents](#table-of-contents) - [Table of contents](#table-of-contents)
- [Footnotes & endnotes](#footnotes-endnotes) - [Footnotes & endnotes](#footnotes-endnotes)
- [Checkboxes](#checkboxes) - [Checkboxes](#checkboxes)
- [Textboxes](#textboxes)
- [Fields](#fields)
- [Templates](#templates) - [Templates](#templates)
- [Writers & readers](#writers-readers) - [Writers & readers](#writers-readers)
- [OOXML](#ooxml) - [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 | - | - | | 2 | Text Run | v | v | v | v | - | - |
| 3 | Link | v | v | v | v | v | v | | 3 | Link | v | v | v | v | v | v |
| 4 | Title | 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 | | 6 | Text Break | v | v | v | v | v | v |
| 7 | Page Break | v | - | - | - | - | - | | 7 | Page Break | v | - | - | - | - | - |
| 8 | List | v | v | v | 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 | - | - | - | - | - | | 13 | TOC | v | - | - | - | - | - |
| 14 | Footnote | v | - | - | v** | v** | - | | 14 | Footnote | v | - | - | v** | v** | - |
| 15 | Endnote | 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: Legend:
@ -832,6 +836,14 @@ $section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle])
- `$fontStyle` See "Font style" section. - `$fontStyle` See "Font style" section.
- `$paragraphStyle` See "Paragraph style" section. - `$paragraphStyle` See "Paragraph style" section.
## Textboxes
To be completed.
## Fields
To be completed.
# Templates # 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. 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.

View File

@ -0,0 +1,31 @@
<?php
include_once 'Sample_Header.php';
// New Word document
echo date('H:i:s'), " Create new PhpWord object", EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Begin code
$section = $phpWord->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';
}

View File

@ -56,7 +56,7 @@ abstract class AbstractContainer extends AbstractElement
// Get arguments // Get arguments
$args = func_get_args(); $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')) { if ($withoutP && ($elementName == 'Text' || $elementName == 'PreserveText')) {
$args[3] = null; // Remove paragraph style for texts in textrun $args[3] = null; // Remove paragraph style for texts in textrun
} }
@ -143,6 +143,19 @@ abstract class AbstractContainer extends AbstractElement
return $this->addElement('TextRun', $paragraphStyle); 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 * Add link element
* *
@ -317,6 +330,7 @@ abstract class AbstractContainer extends AbstractElement
'TextBreak' => $allContainers, 'TextBreak' => $allContainers,
'Image' => $allContainers, 'Image' => $allContainers,
'Object' => $allContainers, 'Object' => $allContainers,
'Field' => $allContainers,
'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'TextRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'ListItem' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),
'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'), 'ListItemRun' => array('Section', 'Header', 'Footer', 'Cell', 'TextBox'),

View File

@ -0,0 +1,178 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Element;
/**
* Field element
*
* @since 0.11.0
* @link http://www.schemacentral.com/sc/ooxml/t-w_CT_SimpleField.html
*/
class Field extends AbstractElement
{
/**
* 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('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;
}
}

View File

@ -0,0 +1,87 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
/**
* Field element writer
*
* @since 0.11.0
*/
class Field extends Text
{
/**
* Write field element
*/
public function write()
{
$xmlWriter = $this->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();
}
}

View File

@ -0,0 +1,75 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Element;
use PhpOffice\PhpWord\Element\Field;
use PhpOffice\PhpWord\PhpWord;
/**
* Test class for PhpOffice\PhpWord\Element\Field
*
* @runTestsInSeparateProcesses
*/
class FieldTest extends \PHPUnit_Framework_TestCase
{
/**
* New instance
*/
public function testConstructNull()
{
$oField = new Field();
$this->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'));
}
}

View File

@ -30,7 +30,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
{ {
$elements = array( $elements = array(
'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun', '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) { foreach ($elements as $element) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element; $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $element;

View File

@ -83,6 +83,10 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0')); 'innerMargin' => 10, 'borderSize' => 1, 'borderColor' => '#FF0'));
$section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center')); $section->addTextBox(array('wrappingStyle' => 'tight', 'positioning' => 'absolute', 'align' => 'center'));
$section->addListItemRun()->addText('List item run 1'); $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); $doc = TestHelperDOCX::getDocument($phpWord);