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