Added Field Element

This commit is contained in:
Bas-Jan 't Jong 2014-05-28 17:59:44 +02:00
parent 9e99080777
commit 079d08e94a
5 changed files with 225 additions and 46 deletions

View File

@ -0,0 +1,21 @@
<?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->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';
}

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
} }
@ -147,14 +147,10 @@ abstract class AbstractContainer extends AbstractElement
* Add field element * Add field element
* @param * @param
*/ */
public function addField($type = null, $properties = array(), $options = array()){ public function addField($type = null, $properties = array(), $options = array())
//$this->checkValidity('Field'); {
$elementDocPart = $this->checkElementDocPart(); return $this->addElement('Field', $type, $properties, $options);
$element = new Field($type, $properties, $options);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);
return $element;
} }
/** /**

View File

@ -15,19 +15,6 @@
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/ */
/*
* <w:fldSimple w:instr=" NUMWORDS \# " #.##0,00;(€ #.##0,00)" \* Arabic \* MERGEFORMAT ">
<w:r>
<w:rPr>
<w:noProof/>
</w:rPr>
<w:t>5</w:t>
</w:r>
</w:fldSimple>
*/
namespace PhpOffice\PhpWord\Element; namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\String;
@ -37,22 +24,35 @@ use PhpOffice\PhpWord\Shared\String;
*/ */
class Field extends AbstractElement 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( protected $fieldsArray = array(
'PAGE'=>array( 'PAGE'=>array(
'properties'=>array( 'properties'=>array(
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
), ),
'options'=>array() 'options'=>array('PreserveFormat')
), ),
'NUMPAGES'=>array( 'NUMPAGES'=>array(
'properties'=>array( 'properties'=>array(
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%')
), ),
'options'=>array() '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()) public function setProperties($properties = array())
{ {
if (is_array($properties)) { if (is_array($properties)) {
//CREATE FUNCTION, WHICH MATCHES SUBARRAY foreach ($properties as $propkey => $propval) {
if (!(array_key_exists($propkey, $this->fieldsArray[$this->type]['properties']))) {
if (array_key_exists($properties, $this->fieldsArray[$this->type])) { throw new \InvalidArgumentException("Invalid property");
$this->properties=array_merge($this->properties, $properties); }
} else {
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()) public function setOptions($options = array())
{ {
if (is_array($options)) { if (is_array($options)) {
if (array_key_exists($options, self::$fieldsArray[$this->type])) { foreach ($options as $optionkey => $optionval) {
$this->options=array_merge($this->options, $options); if (!(array_key_exists($optionkey, $this->fieldsArray[$this->type]['options']))) {
} else { throw new \InvalidArgumentException("Invalid option");
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; return $this->options;
} }
} }

View File

@ -0,0 +1,88 @@
<?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.10.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':
$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();
}
}

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'));
}
}