Skip to content

Added field 'FILENAME' #2510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.x/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- PDF Writer : Added callback for modifying the HTML
- Added Support for Language, both for document overall and individual text elements
- Template : Set a checkbox by [@nxtpge](https://github.com/nxtpge) in [#2509](https://github.com/PHPOffice/PHPWord/pull/2509)
- ODText / RTF / Word2007 Writer : Add field FILENAME by [@milkyway-git](https://github.com/milkyway-git) in [#2510](https://github.com/PHPOffice/PHPWord/pull/2510)

### Bug fixes

Expand Down
3 changes: 2 additions & 1 deletion docs/usage/elements/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Currently the following fields are supported:
- DATE
- XE
- INDEX
- FILENAME

``` php
<?php
Expand Down Expand Up @@ -36,4 +37,4 @@ $section->addField('XE', array(), array(), $fieldText);

//this actually adds the index
$section->addField('INDEX', array(), array('\\e " " \\h "A" \\c "3"'), 'right click to update index');
```
```
3 changes: 3 additions & 0 deletions samples/Sample_27_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

$section->addText('Number of pages field:');
$section->addField('NUMPAGES', ['numformat' => '0,00', 'format' => 'Arabic'], ['PreserveFormat']);

$section->addText('Filename field:');
$section->addField('FILENAME', ['format' => 'Upper'], ['Path', 'PreserveFormat']);
$section->addTextBreak();

$textrun = $section->addTextRun();
Expand Down
6 changes: 6 additions & 0 deletions src/PhpWord/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class Field extends AbstractElement
'properties' => ['StyleIdentifier' => ''],
'options' => ['PreserveFormat'],
],
'FILENAME' => [
'properties' => [
'format' => ['Upper', 'Lower', 'FirstCap', 'Caps'],
],
'options' => ['Path', 'PreserveFormat'],
],
];

/**
Expand Down
15 changes: 14 additions & 1 deletion src/PhpWord/Writer/ODText/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
// Not fully implemented
// - supports only PAGE and NUMPAGES
// - supports only PAGE, NUMPAGES, DATE and FILENAME
// - supports only default formats and options
// - supports style only if specified by name
// - spaces before and after field may be dropped
Expand Down Expand Up @@ -44,6 +44,7 @@ public function write(): void
case 'date':
case 'page':
case 'numpages':
case 'filename':
$this->writeDefault($element, $type);

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

break;
case 'filename':
$xmlWriter->startElement('text:file-name');
$xmlWriter->writeAttribute('text:fixed', 'false');
$options = $element->getOptions();
if ($options != null && in_array('Path', $options)) {
$xmlWriter->writeAttribute('text:display', 'full');
} else {
$xmlWriter->writeAttribute('text:display', 'name');
}
$xmlWriter->endElement();

break;
}
$xmlWriter->endElement(); // text:span
Expand Down
19 changes: 16 additions & 3 deletions src/PhpWord/Writer/RTF/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

namespace PhpOffice\PhpWord\Writer\RTF\Element;

use PhpOffice\PhpWord\Element\Field as ElementField;

/**
* Field element writer.
*
* Note: for now, only date, page and numpages fields are implemented for RTF.
* Note: for now, only date, page, numpages and filename fields are implemented for RTF.
*/
class Field extends Text
{
Expand All @@ -30,7 +32,7 @@ class Field extends Text
public function write()
{
$element = $this->element;
if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
if (!$element instanceof ElementField) {
return;
}

Expand Down Expand Up @@ -66,7 +68,18 @@ protected function writeNumpages()
return 'NUMPAGES';
}

protected function writeDate(\PhpOffice\PhpWord\Element\Field $element)
protected function writeFilename(ElementField $element): string
{
$content = 'FILENAME';
$options = $element->getOptions();
if ($options != null && in_array('Path', $options)) {
$content .= ' \\\\p';
}

return $content;
}

protected function writeDate(ElementField $element)
{
$content = '';
$content .= 'DATE';
Expand Down
22 changes: 13 additions & 9 deletions src/PhpWord/Writer/Word2007/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $ele
foreach ($properties as $propkey => $propval) {
switch ($propkey) {
case 'format':
$propertiesAndOptions .= '\* ' . $propval . ' ';
$propertiesAndOptions .= '\\* ' . $propval . ' ';

break;
case 'numformat':
$propertiesAndOptions .= '\# ' . $propval . ' ';
$propertiesAndOptions .= '\\# ' . $propval . ' ';

break;
case 'dateformat':
$propertiesAndOptions .= '\@ "' . $propval . '" ';
$propertiesAndOptions .= '\\@ "' . $propval . '" ';

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

break;
case 'LunarCalendar':
$propertiesAndOptions .= '\h ';
$propertiesAndOptions .= '\\h ';

break;
case 'SakaEraCalendar':
$propertiesAndOptions .= '\s ';
$propertiesAndOptions .= '\\s ';

break;
case 'LastUsedFormat':
$propertiesAndOptions .= '\l ';
$propertiesAndOptions .= '\\l ';

break;
case 'Bold':
$propertiesAndOptions .= '\b ';
$propertiesAndOptions .= '\\b ';

break;
case 'Italic':
$propertiesAndOptions .= '\i ';
$propertiesAndOptions .= '\\i ';

break;
case 'Path':
$propertiesAndOptions .= '\\p ';

break;
default:
Expand Down
64 changes: 64 additions & 0 deletions tests/PhpWordTests/Writer/ODText/Element/FieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWordTests\Writer\ODText\Element;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWordTests\TestHelperDOCX;
use PHPUnit\Framework\TestCase;

/**
* Test class for PhpOffice\PhpWord\Writer\ODText\Element subnamespace.
*/
class FieldTest extends TestCase
{
/**
* Executed before each method of the class.
*/
protected function tearDown(): void
{
TestHelperDOCX::clear();
}

public function testFieldFilename(): void
{
$phpWord = new PhpWord();

$section = $phpWord->addSection();
$section->addField('FILENAME');

$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');

self::assertTrue($doc->elementExists('/office:document-content/office:body/office:text/text:section/text:span/text:file-name'));
self::assertEquals('false', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:fixed'));
self::assertEquals('name', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:display'));
}

public function testFieldFilenameOptionPath(): void
{
$phpWord = new PhpWord();

$section = $phpWord->addSection();
$section->addField('FILENAME', [], ['Path']);

$doc = TestHelperDOCX::getDocument($phpWord, 'ODText');

self::assertTrue($doc->elementExists('/office:document-content/office:body/office:text/text:section/text:span/text:file-name'));
self::assertEquals('false', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:fixed'));
self::assertEquals('full', $doc->getElementAttribute('/office:document-content/office:body/office:text/text:section/text:span/text:file-name', 'text:display'));
}
}
18 changes: 18 additions & 0 deletions tests/PhpWordTests/Writer/RTF/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public function testUnmatchedElements(): void
}
}

public function testFilenameField(): void
{
$parentWriter = new RTF();
$element = new \PhpOffice\PhpWord\Element\Field('FILENAME');
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);

self::assertEquals("{\\field{\\*\\fldinst FILENAME}{\\fldrslt}}\\par\n", $this->removeCr($field));
}

public function testFilenameFieldOptionsPath(): void
{
$parentWriter = new RTF();
$element = new \PhpOffice\PhpWord\Element\Field('FILENAME', [], ['Path']);
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);

self::assertEquals("{\\field{\\*\\fldinst FILENAME \\\\p}{\\fldrslt}}\\par\n", $this->removeCr($field));
}

public function testPageField(): void
{
$parentWriter = new RTF();
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,44 @@ public function testStyledFieldElement(): void
self::assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
}

public function testFieldElementFilename(): void
{
$phpWord = new PhpWord();
$stnam = 'h1';
$phpWord->addFontStyle($stnam, ['name' => 'Courier New', 'size' => 8]);
$section = $phpWord->addSection();

$fld = $section->addField('FILENAME');
$fld->setFontStyle($stnam);
$doc = TestHelperDOCX::getDocument($phpWord);

$element = '/w:document/w:body/w:p/w:r[2]/w:instrText';
self::assertTrue($doc->elementExists($element));
self::assertEquals(' FILENAME ', $doc->getElement($element)->textContent);
$sty = '/w:document/w:body/w:p/w:r[2]/w:rPr';
self::assertTrue($doc->elementExists($sty));
self::assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
}

public function testFieldElementFilenameOptionsPath(): void
{
$phpWord = new PhpWord();
$stnam = 'h1';
$phpWord->addFontStyle($stnam, ['name' => 'Courier New', 'size' => 8]);
$section = $phpWord->addSection();

$fld = $section->addField('FILENAME', [], ['Path']);
$fld->setFontStyle($stnam);
$doc = TestHelperDOCX::getDocument($phpWord);

$element = '/w:document/w:body/w:p/w:r[2]/w:instrText';
self::assertTrue($doc->elementExists($element));
self::assertEquals(' FILENAME \p ', $doc->getElement($element)->textContent);
$sty = '/w:document/w:body/w:p/w:r[2]/w:rPr';
self::assertTrue($doc->elementExists($sty));
self::assertEquals($stnam, $doc->getElementAttribute($sty . '/w:rStyle', 'w:val'));
}

public function testFieldElementWithComplexText(): void
{
$phpWord = new PhpWord();
Expand Down