Skip to content
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

Add field 'FILENAME' #2498

Closed
wants to merge 9 commits into from
Closed
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
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
12 changes: 11 additions & 1 deletion src/PhpWord/Writer/RTF/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* 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 Down Expand Up @@ -66,6 +66,16 @@ protected function writeNumpages()
return 'NUMPAGES';
}

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

protected function writeDate(\PhpOffice\PhpWord\Element\Field $element)
{
$content = '';
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