Skip to content

Ref field support #2207

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

Closed
wants to merge 5 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
9 changes: 9 additions & 0 deletions docs/usage/elements/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Currently the following fields are supported:
- XE
- INDEX
- FILENAME
- REF

``` php
<?php
Expand Down Expand Up @@ -37,4 +38,12 @@ $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');

//Adding reference to a bookmark
$fieldText->addField('REF', [
'name' => 'bookmark'
], [
'InsertParagraphNumberRelativeContext',
'CreateHyperLink',
]);
```
4 changes: 4 additions & 0 deletions src/PhpWord/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class Field extends AbstractElement
],
'options' => ['Path', 'PreserveFormat'],
],
'REF' => array(
'properties' => array('name' => ''),
'options' => array('f', 'h', 'n', 'p', 'r', 't', 'w'),
),
];

/**
Expand Down
102 changes: 102 additions & 0 deletions src/PhpWord/Writer/Word2007/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,106 @@

return $propertiesAndOptions;
}

/**
* Writes a REF field
*
* @param \PhpOffice\PhpWord\Element\Field $element
*/
protected function writeRef(\PhpOffice\PhpWord\Element\Field $element)

Check failure on line 235 in src/PhpWord/Writer/Word2007/Element/Field.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWord\Writer\Word2007\Element\Field::writeRef() has no return type specified.
{
$xmlWriter = $this->getXmlWriter();
$this->startElementP();

$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:fldChar');
$xmlWriter->writeAttribute('w:fldCharType', 'begin');
$xmlWriter->endElement(); // w:fldChar
$xmlWriter->endElement(); // w:r

$instruction = ' ' . $element->getType() . ' ';

foreach ($element->getProperties() as $property) {
$instruction .= $property . ' ';
}
foreach ($element->getOptions() as $optionKey => $optionValue) {
$instruction .= $this->convertRefOption($optionKey, $optionValue) . ' ';
}

$xmlWriter->startElement('w:r');
$this->writeFontStyle();
$xmlWriter->startElement('w:instrText');
$xmlWriter->writeAttribute('xml:space', 'preserve');
$xmlWriter->text($instruction);
$xmlWriter->endElement(); // w:instrText
$xmlWriter->endElement(); // w:r

if ($element->getText() != null) {
if ($element->getText() instanceof \PhpOffice\PhpWord\Element\TextRun) {
$containerWriter = new Container($xmlWriter, $element->getText(), true);
$containerWriter->write();

$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:instrText');
$xmlWriter->text('"' . $this->buildPropertiesAndOptions($element));
$xmlWriter->endElement(); // w:instrText
$xmlWriter->endElement(); // w:r

$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:instrText');
$xmlWriter->writeAttribute('xml:space', 'preserve');
$xmlWriter->text(' ');
$xmlWriter->endElement(); // w:instrText
$xmlWriter->endElement(); // w:r
}
}

$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:fldChar');
$xmlWriter->writeAttribute('w:fldCharType', 'separate');
$xmlWriter->endElement(); // w:fldChar
$xmlWriter->endElement(); // w:r

$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:rPr');
$xmlWriter->startElement('w:noProof');
$xmlWriter->endElement(); // w:noProof
$xmlWriter->endElement(); // w:rPr
$xmlWriter->writeElement('w:t', $element->getText() != null && is_string($element->getText()) ? $element->getText() : '1');
$xmlWriter->endElement(); // w:r

$xmlWriter->startElement('w:r');
$xmlWriter->startElement('w:fldChar');
$xmlWriter->writeAttribute('w:fldCharType', 'end');
$xmlWriter->endElement(); // w:fldChar
$xmlWriter->endElement(); // w:r

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

private function convertRefOption($optionKey, $optionValue)

Check failure on line 306 in src/PhpWord/Writer/Word2007/Element/Field.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWord\Writer\Word2007\Element\Field::convertRefOption() has no return type specified.

Check failure on line 306 in src/PhpWord/Writer/Word2007/Element/Field.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWord\Writer\Word2007\Element\Field::convertRefOption() has parameter $optionKey with no type specified.

Check failure on line 306 in src/PhpWord/Writer/Word2007/Element/Field.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWord\Writer\Word2007\Element\Field::convertRefOption() has parameter $optionValue with no type specified.
{
if ($optionKey === 'NumberSeperatorSequence') {
return '\\d ' . $optionValue;
}

switch ($optionValue) {
case 'IncrementAndInsertText':
return '\\f';
case 'CreateHyperLink':
return '\\h';
case 'NoTrailingPeriod':
return '\\n';
case 'IncludeAboveOrBelow':
return '\\p';
case 'InsertParagraphNumberRelativeContext':
return '\\r';
case 'SuppressNonDelimiterNonNumericalText':
return '\\t';
case 'InsertParagraphNumberFullContext':
return '\\w';
default:
return '';
}
}
}
62 changes: 62 additions & 0 deletions tests/PhpWord/Writer/Word2007/Element/FieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace PhpOffice\PhpWord\Writer\Word2007\Element;

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

/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Field
*/
class FieldTest extends TestCase
{
/**
* Executed before each method of the class
*/
public function tearDown()

Check failure on line 17 in tests/PhpWord/Writer/Word2007/Element/FieldTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWord\Writer\Word2007\Element\FieldTest::tearDown() has no return type specified.

Check failure on line 17 in tests/PhpWord/Writer/Word2007/Element/FieldTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Return type mixed of method PhpOffice\PhpWord\Writer\Word2007\Element\FieldTest::tearDown() is not covariant with return type void of method PHPUnit\Framework\TestCase::tearDown().
{
TestHelperDOCX::clear();

Check failure on line 19 in tests/PhpWord/Writer/Word2007/Element/FieldTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method clear() on an unknown class PhpOffice\PhpWord\TestHelperDOCX.
}

/**
* Test Field write
*/
public function testWriteWithRefType()

Check failure on line 25 in tests/PhpWord/Writer/Word2007/Element/FieldTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Method PhpOffice\PhpWord\Writer\Word2007\Element\FieldTest::testWriteWithRefType() has no return type specified.
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addField(
'REF',
array(
'name' => 'my-bookmark',
),
array(
'InsertParagraphNumberRelativeContext',
'CreateHyperLink',
)
);

$section->addListItem('line one item');
$section->addListItem('line two item');
$section->addBookmark('my-bookmark');
$section->addListItem('line three item');

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

Check failure on line 45 in tests/PhpWord/Writer/Word2007/Element/FieldTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method getDocument() on an unknown class PhpOffice\PhpWord\TestHelperDOCX.

$refFieldPath = '/w:document/w:body/w:p[1]/w:r[2]/w:instrText';

$this->assertTrue($doc->elementExists($refFieldPath));

$bookMarkElement = $doc->getElement($refFieldPath);

$this->assertNotNull($bookMarkElement);

$this->assertEquals(' REF my-bookmark \r \h ', $bookMarkElement->textContent);

$bookmarkPath = '/w:document/w:body/w:bookmarkStart';

$this->assertTrue($doc->elementExists($bookmarkPath));
$this->assertEquals('my-bookmark', $doc->getElementAttribute("$bookmarkPath", 'w:name'));
}
}
Loading