diff --git a/samples/Sample_27_Field.php b/samples/Sample_27_Field.php index 57747895..7e2b968e 100644 --- a/samples/Sample_27_Field.php +++ b/samples/Sample_27_Field.php @@ -14,10 +14,18 @@ $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->addField('PAGE', array('format' => 'Arabic')); $section->addText('Number of pages field:'); -$section->addField('NUMPAGES', array('format' => 'Arabic', 'numformat' => '0,00'), array('PreserveFormat')); +$section->addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic'), array('PreserveFormat')); + +$textrun = $section->addTextRun(); +$textrun->addText('An index field is '); +$textrun->addField('XE', array(), array('Bold'), 'FieldValue'); +$textrun->addText('here:'); + +$section->addText('The actual index:'); +$section->addField('INDEX', array(), array('PreserveFormat')); $textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER)); $textrun->addText('This is the date of lunar calendar '); diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index 48dc1d2e..b5dec19a 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -40,7 +40,8 @@ class Field extends AbstractElement ), 'NUMPAGES'=>array( 'properties'=>array( - 'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'), + 'format' => array('Arabic', 'ArabicDash', 'CardText', 'DollarText', 'Ordinal', 'OrdText', + 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN', 'Caps', 'FirstCap', 'Lower', 'Upper'), 'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%') ), 'options'=>array('PreserveFormat') @@ -52,6 +53,14 @@ class Field extends AbstractElement 'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss') ), 'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat') + ), + 'XE'=>array( + 'properties' => array(), + 'options' => array('Bold', 'Italic') + ), + 'INDEX'=>array( + 'properties' => array(), + 'options'=>array('PreserveFormat') ) ); @@ -62,6 +71,13 @@ class Field extends AbstractElement */ protected $type; + /** + * Field text + * + * @var string + */ + protected $text; + /** * Field properties * @@ -83,11 +99,12 @@ class Field extends AbstractElement * @param array $properties * @param array $options */ - public function __construct($type = null, $properties = array(), $options = array()) + public function __construct($type = null, $properties = array(), $options = array(), $text = null) { $this->setType($type); $this->setProperties($properties); $this->setOptions($options); + $this->setText($text); } /** @@ -184,4 +201,35 @@ class Field extends AbstractElement { return $this->options; } + + /** + * Set Field text + * + * @param string $text + * + * @return string + * + * @throws \InvalidArgumentException + */ + public function setText($text) + { + if (isset($text)) { + if (is_string($text)) { + $this->text = $text; + } else { + throw new \InvalidArgumentException("Invalid text"); + } + } + return $this->text; + } + + /** + * Get Field text + * + * @return string + */ + public function getText() + { + return $this->text; + } } diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index ae4c66ba..ad8032c7 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -38,13 +38,18 @@ class Field extends Text } $instruction = ' ' . $element->getType() . ' '; + if ($element->getText() != null) { + $instruction .= '"' . $element->getText() . '" '; + } $properties = $element->getProperties(); foreach ($properties as $propkey => $propval) { switch ($propkey) { case 'format': - case 'numformat': $instruction .= '\* ' . $propval . ' '; break; + case 'numformat': + $instruction .= '\# ' . $propval . ' '; + break; case 'dateformat': $instruction .= '\@ "' . $propval . '" '; break; @@ -66,22 +71,49 @@ class Field extends Text case 'LastUsedFormat': $instruction .= '\l '; break; + case 'Bold': + $instruction .= '\b '; + break; + case 'Italic': + $instruction .= '\i '; + break; } } $this->startElementP(); - $xmlWriter->startElement('w:fldSimple'); - $xmlWriter->writeAttribute('w:instr', $instruction); + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'begin'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:instrText'); + $xmlWriter->writeAttribute('xml:space', 'preserve'); + $xmlWriter->text($instruction); + $xmlWriter->endElement(); // w:instrText + $xmlWriter->endElement(); // w:r + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'separate'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r + $xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:rPr'); $xmlWriter->startElement('w:noProof'); $xmlWriter->endElement(); // w:noProof $xmlWriter->endElement(); // w:rPr - $xmlWriter->writeElement('w:t', '1'); $xmlWriter->endElement(); // w:r - $xmlWriter->endElement(); // w:fldSimple + + $xmlWriter->startElement('w:r'); + $xmlWriter->startElement('w:fldChar'); + $xmlWriter->writeAttribute('w:fldCharType', 'end'); + $xmlWriter->endElement(); // w:fldChar + $xmlWriter->endElement(); // w:r $this->endElementP(); // w:p } diff --git a/tests/PhpWord/Element/FieldTest.php b/tests/PhpWord/Element/FieldTest.php index b9afad1f..a37722e5 100644 --- a/tests/PhpWord/Element/FieldTest.php +++ b/tests/PhpWord/Element/FieldTest.php @@ -70,6 +70,20 @@ class FieldTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('SakaEraCalendar', 'PreserveFormat'), $oField->getOptions()); } + /** + * New instance with type and properties and options and text + */ + public function testConstructWithTypePropertiesOptionsText() + { + $oField = new Field('XE', array(), array('Bold'), 'FieldValue'); + + $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); + $this->assertEquals('XE', $oField->getType()); + $this->assertEquals(array(), $oField->getProperties()); + $this->assertEquals(array('Bold'), $oField->getOptions()); + $this->assertEquals('FieldValue', $oField->getText()); + } + /** * Test setType exception *