Skip to content

Commit 45345a3

Browse files
authored
Merge pull request #2510 from PHPOffice/pr2498
Added field 'FILENAME'
2 parents 16a5e2b + a209906 commit 45345a3

File tree

10 files changed

+175
-14
lines changed

10 files changed

+175
-14
lines changed

docs/changes/1.x/1.2.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- PDF Writer : Added callback for modifying the HTML
3131
- Added Support for Language, both for document overall and individual text elements
3232
- Template : Set a checkbox by [@nxtpge](https://github.com/nxtpge) in [#2509](https://github.com/PHPOffice/PHPWord/pull/2509)
33+
- ODText / RTF / Word2007 Writer : Add field FILENAME by [@milkyway-git](https://github.com/milkyway-git) in [#2510](https://github.com/PHPOffice/PHPWord/pull/2510)
3334

3435
### Bug fixes
3536

docs/usage/elements/field.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Currently the following fields are supported:
77
- DATE
88
- XE
99
- INDEX
10+
- FILENAME
1011

1112
``` php
1213
<?php
@@ -36,4 +37,4 @@ $section->addField('XE', array(), array(), $fieldText);
3637

3738
//this actually adds the index
3839
$section->addField('INDEX', array(), array('\\e " " \\h "A" \\c "3"'), 'right click to update index');
39-
```
40+
```

samples/Sample_27_Field.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
$section->addText('Number of pages field:');
2828
$section->addField('NUMPAGES', ['numformat' => '0,00', 'format' => 'Arabic'], ['PreserveFormat']);
29+
30+
$section->addText('Filename field:');
31+
$section->addField('FILENAME', ['format' => 'Upper'], ['Path', 'PreserveFormat']);
2932
$section->addTextBreak();
3033

3134
$textrun = $section->addTextRun();

src/PhpWord/Element/Field.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class Field extends AbstractElement
8585
'properties' => ['StyleIdentifier' => ''],
8686
'options' => ['PreserveFormat'],
8787
],
88+
'FILENAME' => [
89+
'properties' => [
90+
'format' => ['Upper', 'Lower', 'FirstCap', 'Caps'],
91+
],
92+
'options' => ['Path', 'PreserveFormat'],
93+
],
8894
];
8995

9096
/**

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
1616
*/
1717
// Not fully implemented
18-
// - supports only PAGE and NUMPAGES
18+
// - supports only PAGE, NUMPAGES, DATE and FILENAME
1919
// - supports only default formats and options
2020
// - supports style only if specified by name
2121
// - spaces before and after field may be dropped
@@ -44,6 +44,7 @@ public function write(): void
4444
case 'date':
4545
case 'page':
4646
case 'numpages':
47+
case 'filename':
4748
$this->writeDefault($element, $type);
4849

4950
break;
@@ -78,6 +79,18 @@ private function writeDefault(\PhpOffice\PhpWord\Element\Field $element, $type):
7879
$xmlWriter->startElement('text:page-count');
7980
$xmlWriter->endElement();
8081

82+
break;
83+
case 'filename':
84+
$xmlWriter->startElement('text:file-name');
85+
$xmlWriter->writeAttribute('text:fixed', 'false');
86+
$options = $element->getOptions();
87+
if ($options != null && in_array('Path', $options)) {
88+
$xmlWriter->writeAttribute('text:display', 'full');
89+
} else {
90+
$xmlWriter->writeAttribute('text:display', 'name');
91+
}
92+
$xmlWriter->endElement();
93+
8194
break;
8295
}
8396
$xmlWriter->endElement(); // text:span

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\RTF\Element;
1919

20+
use PhpOffice\PhpWord\Element\Field as ElementField;
21+
2022
/**
2123
* Field element writer.
2224
*
23-
* Note: for now, only date, page and numpages fields are implemented for RTF.
25+
* Note: for now, only date, page, numpages and filename fields are implemented for RTF.
2426
*/
2527
class Field extends Text
2628
{
@@ -30,7 +32,7 @@ class Field extends Text
3032
public function write()
3133
{
3234
$element = $this->element;
33-
if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
35+
if (!$element instanceof ElementField) {
3436
return;
3537
}
3638

@@ -66,7 +68,18 @@ protected function writeNumpages()
6668
return 'NUMPAGES';
6769
}
6870

69-
protected function writeDate(\PhpOffice\PhpWord\Element\Field $element)
71+
protected function writeFilename(ElementField $element): string
72+
{
73+
$content = 'FILENAME';
74+
$options = $element->getOptions();
75+
if ($options != null && in_array('Path', $options)) {
76+
$content .= ' \\\\p';
77+
}
78+
79+
return $content;
80+
}
81+
82+
protected function writeDate(ElementField $element)
7083
{
7184
$content = '';
7285
$content .= 'DATE';

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $ele
166166
foreach ($properties as $propkey => $propval) {
167167
switch ($propkey) {
168168
case 'format':
169-
$propertiesAndOptions .= '\* ' . $propval . ' ';
169+
$propertiesAndOptions .= '\\* ' . $propval . ' ';
170170

171171
break;
172172
case 'numformat':
173-
$propertiesAndOptions .= '\# ' . $propval . ' ';
173+
$propertiesAndOptions .= '\\# ' . $propval . ' ';
174174

175175
break;
176176
case 'dateformat':
177-
$propertiesAndOptions .= '\@ "' . $propval . '" ';
177+
$propertiesAndOptions .= '\\@ "' . $propval . '" ';
178178

179179
break;
180180
case 'macroname':
@@ -192,27 +192,31 @@ private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $ele
192192
foreach ($options as $option) {
193193
switch ($option) {
194194
case 'PreserveFormat':
195-
$propertiesAndOptions .= '\* MERGEFORMAT ';
195+
$propertiesAndOptions .= '\\* MERGEFORMAT ';
196196

197197
break;
198198
case 'LunarCalendar':
199-
$propertiesAndOptions .= '\h ';
199+
$propertiesAndOptions .= '\\h ';
200200

201201
break;
202202
case 'SakaEraCalendar':
203-
$propertiesAndOptions .= '\s ';
203+
$propertiesAndOptions .= '\\s ';
204204

205205
break;
206206
case 'LastUsedFormat':
207-
$propertiesAndOptions .= '\l ';
207+
$propertiesAndOptions .= '\\l ';
208208

209209
break;
210210
case 'Bold':
211-
$propertiesAndOptions .= '\b ';
211+
$propertiesAndOptions .= '\\b ';
212212

213213
break;
214214
case 'Italic':
215-
$propertiesAndOptions .= '\i ';
215+
$propertiesAndOptions .= '\\i ';
216+
217+
break;
218+
case 'Path':
219+
$propertiesAndOptions .= '\\p ';
216220

217221
break;
218222
default:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWordTests\Writer\ODText\Element;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWordTests\TestHelperDOCX;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Test class for PhpOffice\PhpWord\Writer\ODText\Element subnamespace.
26+
*/
27+
class FieldTest extends TestCase
28+
{
29+
/**
30+
* Executed before each method of the class.
31+
*/
32+
protected function tearDown(): void
33+
{
34+
TestHelperDOCX::clear();
35+
}
36+
37+
public function testFieldFilename(): void
38+
{
39+
$phpWord = new PhpWord();
40+
41+
$section = $phpWord->addSection();
42+
$section->addField('FILENAME');
43+
44+
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
45+
46+
self::assertTrue($doc->elementExists('/office:document-content/office:body/office:text/text:section/text:span/text:file-name'));
47+
self::assertEquals('false', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:fixed'));
48+
self::assertEquals('name', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:display'));
49+
}
50+
51+
public function testFieldFilenameOptionPath(): void
52+
{
53+
$phpWord = new PhpWord();
54+
55+
$section = $phpWord->addSection();
56+
$section->addField('FILENAME', [], ['Path']);
57+
58+
$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
59+
60+
self::assertTrue($doc->elementExists('/office:document-content/office:body/office:text/text:section/text:span/text:file-name'));
61+
self::assertEquals('false', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:fixed'));
62+
self::assertEquals('full', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:display'));
63+
}
64+
}

tests/PhpWordTests/Writer/RTF/ElementTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public function testUnmatchedElements(): void
4545
}
4646
}
4747

48+
public function testFilenameField(): void
49+
{
50+
$parentWriter = new RTF();
51+
$element = new \PhpOffice\PhpWord\Element\Field('FILENAME');
52+
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
53+
54+
self::assertEquals("{\\field{\\*\\fldinst FILENAME}{\\fldrslt}}\\par\n", $this->removeCr($field));
55+
}
56+
57+
public function testFilenameFieldOptionsPath(): void
58+
{
59+
$parentWriter = new RTF();
60+
$element = new \PhpOffice\PhpWord\Element\Field('FILENAME', [], ['Path']);
61+
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
62+
63+
self::assertEquals("{\\field{\\*\\fldinst FILENAME \\\\p}{\\fldrslt}}\\par\n", $this->removeCr($field));
64+
}
65+
4866
public function testPageField(): void
4967
{
5068
$parentWriter = new RTF();

tests/PhpWordTests/Writer/Word2007/ElementTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,44 @@ public function testStyledFieldElement(): void
306306
self::assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
307307
}
308308

309+
public function testFieldElementFilename(): void
310+
{
311+
$phpWord = new PhpWord();
312+
$stnam = 'h1';
313+
$phpWord->addFontStyle($stnam, ['name' => 'Courier New', 'size' => 8]);
314+
$section = $phpWord->addSection();
315+
316+
$fld = $section->addField('FILENAME');
317+
$fld->setFontStyle($stnam);
318+
$doc = TestHelperDOCX::getDocument($phpWord);
319+
320+
$element = '/w:document/w:body/w:p/w:r[2]/w:instrText';
321+
self::assertTrue($doc->elementExists($element));
322+
self::assertEquals(' FILENAME ', $doc->getElement($element)->textContent);
323+
$sty = '/w:document/w:body/w:p/w:r[2]/w:rPr';
324+
self::assertTrue($doc->elementExists($sty));
325+
self::assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
326+
}
327+
328+
public function testFieldElementFilenameOptionsPath(): void
329+
{
330+
$phpWord = new PhpWord();
331+
$stnam = 'h1';
332+
$phpWord->addFontStyle($stnam, ['name' => 'Courier New', 'size' => 8]);
333+
$section = $phpWord->addSection();
334+
335+
$fld = $section->addField('FILENAME', [], ['Path']);
336+
$fld->setFontStyle($stnam);
337+
$doc = TestHelperDOCX::getDocument($phpWord);
338+
339+
$element = '/w:document/w:body/w:p/w:r[2]/w:instrText';
340+
self::assertTrue($doc->elementExists($element));
341+
self::assertEquals(' FILENAME \p ', $doc->getElement($element)->textContent);
342+
$sty = '/w:document/w:body/w:p/w:r[2]/w:rPr';
343+
self::assertTrue($doc->elementExists($sty));
344+
self::assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
345+
}
346+
309347
public function testFieldElementWithComplexText(): void
310348
{
311349
$phpWord = new PhpWord();

0 commit comments

Comments
 (0)