Skip to content

Commit 02197a7

Browse files
milkyway-gitProgi1984
authored andcommitted
Added field 'FILENAME'
1 parent 16a5e2b commit 02197a7

File tree

9 files changed

+79
-12
lines changed

9 files changed

+79
-12
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 [#](https://github.com/PHPOffice/PHPWord/pull/)
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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Field element writer.
2222
*
23-
* Note: for now, only date, page and numpages fields are implemented for RTF.
23+
* Note: for now, only date, page, numpages and filename fields are implemented for RTF.
2424
*/
2525
class Field extends Text
2626
{
@@ -66,6 +66,17 @@ protected function writeNumpages()
6666
return 'NUMPAGES';
6767
}
6868

69+
protected function writeFilename(\PhpOffice\PhpWord\Element\Field $element)
70+
{
71+
$content = 'FILENAME';
72+
$options = $element->getOptions();
73+
if ($options != null && in_array('Path', $options)) {
74+
$content .= ' \\\\p';
75+
}
76+
77+
return $content;
78+
}
79+
6980
protected function writeDate(\PhpOffice\PhpWord\Element\Field $element)
7081
{
7182
$content = '';

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:

tests/PhpWordTests/Writer/RTF/ElementTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ 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+
4857
public function testPageField(): void
4958
{
5059
$parentWriter = new RTF();

tests/PhpWordTests/Writer/Word2007/ElementTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,25 @@ 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+
309328
public function testFieldElementWithComplexText(): void
310329
{
311330
$phpWord = new PhpWord();

0 commit comments

Comments
 (0)