Skip to content

Commit d52d9b8

Browse files
committed
Add support for XE and INDEX fields
1 parent c710690 commit d52d9b8

File tree

4 files changed

+111
-9
lines changed

4 files changed

+111
-9
lines changed

samples/Sample_27_Field.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@
1414
$section->addField('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat'));
1515

1616
$section->addText('Page field:');
17-
$section->addField('PAGE', array('format' => 'ArabicDash'));
17+
$section->addField('PAGE', array('format' => 'Arabic'));
1818

1919
$section->addText('Number of pages field:');
20-
$section->addField('NUMPAGES', array('format' => 'Arabic', 'numformat' => '0,00'), array('PreserveFormat'));
20+
$section->addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic'), array('PreserveFormat'));
21+
22+
$textrun = $section->addTextRun();
23+
$textrun->addText('An index field is ');
24+
$textrun->addField('XE', array(), array('Bold'), 'FieldValue');
25+
$textrun->addText('here:');
26+
27+
$section->addText('The actual index:');
28+
$section->addField('INDEX', array(), array('PreserveFormat'));
2129

2230
$textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
2331
$textrun->addText('This is the date of lunar calendar ');

src/PhpWord/Element/Field.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class Field extends AbstractElement
4040
),
4141
'NUMPAGES'=>array(
4242
'properties'=>array(
43-
'format' => array('Arabic', 'ArabicDash', 'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN'),
43+
'format' => array('Arabic', 'ArabicDash', 'CardText', 'DollarText', 'Ordinal', 'OrdText',
44+
'alphabetic', 'ALPHABETIC', 'roman', 'ROMAN', 'Caps', 'FirstCap', 'Lower', 'Upper'),
4445
'numformat' => array('0', '0,00', '#.##0', '#.##0,00', '€ #.##0,00(€ #.##0,00)', '0%', '0,00%')
4546
),
4647
'options'=>array('PreserveFormat')
@@ -52,6 +53,14 @@ class Field extends AbstractElement
5253
'h:mm am/pm', 'h:mm:ss am/pm', 'HH:mm', 'HH:mm:ss')
5354
),
5455
'options'=>array('PreserveFormat', 'LunarCalendar', 'SakaEraCalendar', 'LastUsedFormat')
56+
),
57+
'XE'=>array(
58+
'properties' => array(),
59+
'options' => array('Bold', 'Italic')
60+
),
61+
'INDEX'=>array(
62+
'properties' => array(),
63+
'options'=>array('PreserveFormat')
5564
)
5665
);
5766

@@ -62,6 +71,13 @@ class Field extends AbstractElement
6271
*/
6372
protected $type;
6473

74+
/**
75+
* Field text
76+
*
77+
* @var string
78+
*/
79+
protected $text;
80+
6581
/**
6682
* Field properties
6783
*
@@ -83,11 +99,12 @@ class Field extends AbstractElement
8399
* @param array $properties
84100
* @param array $options
85101
*/
86-
public function __construct($type = null, $properties = array(), $options = array())
102+
public function __construct($type = null, $properties = array(), $options = array(), $text = null)
87103
{
88104
$this->setType($type);
89105
$this->setProperties($properties);
90106
$this->setOptions($options);
107+
$this->setText($text);
91108
}
92109

93110
/**
@@ -184,4 +201,35 @@ public function getOptions()
184201
{
185202
return $this->options;
186203
}
204+
205+
/**
206+
* Set Field text
207+
*
208+
* @param string $text
209+
*
210+
* @return string
211+
*
212+
* @throws \InvalidArgumentException
213+
*/
214+
public function setText($text)
215+
{
216+
if (isset($text)) {
217+
if (is_string($text)) {
218+
$this->text = $text;
219+
} else {
220+
throw new \InvalidArgumentException("Invalid text");
221+
}
222+
}
223+
return $this->text;
224+
}
225+
226+
/**
227+
* Get Field text
228+
*
229+
* @return string
230+
*/
231+
public function getText()
232+
{
233+
return $this->text;
234+
}
187235
}

src/PhpWord/Writer/Word2007/Element/Field.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,18 @@ public function write()
3838
}
3939

4040
$instruction = ' ' . $element->getType() . ' ';
41+
if ($element->getText() != null) {
42+
$instruction .= '"' . $element->getText() . '" ';
43+
}
4144
$properties = $element->getProperties();
4245
foreach ($properties as $propkey => $propval) {
4346
switch ($propkey) {
4447
case 'format':
45-
case 'numformat':
4648
$instruction .= '\* ' . $propval . ' ';
4749
break;
50+
case 'numformat':
51+
$instruction .= '\# ' . $propval . ' ';
52+
break;
4853
case 'dateformat':
4954
$instruction .= '\@ "' . $propval . '" ';
5055
break;
@@ -66,22 +71,49 @@ public function write()
6671
case 'LastUsedFormat':
6772
$instruction .= '\l ';
6873
break;
74+
case 'Bold':
75+
$instruction .= '\b ';
76+
break;
77+
case 'Italic':
78+
$instruction .= '\i ';
79+
break;
6980
}
7081
}
7182

7283
$this->startElementP();
7384

74-
$xmlWriter->startElement('w:fldSimple');
75-
$xmlWriter->writeAttribute('w:instr', $instruction);
85+
$xmlWriter->startElement('w:r');
86+
$xmlWriter->startElement('w:fldChar');
87+
$xmlWriter->writeAttribute('w:fldCharType', 'begin');
88+
$xmlWriter->endElement(); // w:fldChar
89+
$xmlWriter->endElement(); // w:r
90+
91+
$xmlWriter->startElement('w:r');
92+
$xmlWriter->startElement('w:instrText');
93+
$xmlWriter->writeAttribute('xml:space', 'preserve');
94+
$xmlWriter->text($instruction);
95+
$xmlWriter->endElement(); // w:instrText
96+
$xmlWriter->endElement(); // w:r
97+
98+
$xmlWriter->startElement('w:r');
99+
$xmlWriter->startElement('w:fldChar');
100+
$xmlWriter->writeAttribute('w:fldCharType', 'separate');
101+
$xmlWriter->endElement(); // w:fldChar
102+
$xmlWriter->endElement(); // w:r
103+
76104
$xmlWriter->startElement('w:r');
77105
$xmlWriter->startElement('w:rPr');
78106
$xmlWriter->startElement('w:noProof');
79107
$xmlWriter->endElement(); // w:noProof
80108
$xmlWriter->endElement(); // w:rPr
81-
82109
$xmlWriter->writeElement('w:t', '1');
83110
$xmlWriter->endElement(); // w:r
84-
$xmlWriter->endElement(); // w:fldSimple
111+
112+
$xmlWriter->startElement('w:r');
113+
$xmlWriter->startElement('w:fldChar');
114+
$xmlWriter->writeAttribute('w:fldCharType', 'end');
115+
$xmlWriter->endElement(); // w:fldChar
116+
$xmlWriter->endElement(); // w:r
85117

86118
$this->endElementP(); // w:p
87119
}

tests/PhpWord/Element/FieldTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ public function testConstructWithTypePropertiesOptions()
7070
$this->assertEquals(array('SakaEraCalendar', 'PreserveFormat'), $oField->getOptions());
7171
}
7272

73+
/**
74+
* New instance with type and properties and options and text
75+
*/
76+
public function testConstructWithTypePropertiesOptionsText()
77+
{
78+
$oField = new Field('XE', array(), array('Bold'), 'FieldValue');
79+
80+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
81+
$this->assertEquals('XE', $oField->getType());
82+
$this->assertEquals(array(), $oField->getProperties());
83+
$this->assertEquals(array('Bold'), $oField->getOptions());
84+
$this->assertEquals('FieldValue', $oField->getText());
85+
}
86+
7387
/**
7488
* Test setType exception
7589
*

0 commit comments

Comments
 (0)