Skip to content

Commit 6faddc6

Browse files
authored
Merge pull request #1205 from troosan/update_fields
Allow to force an update of all fields on opening a document
2 parents 3429c44 + 5a5ae48 commit 6faddc6

File tree

8 files changed

+48
-2
lines changed

8 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ This is the last version to support PHP 5.3
1818
- Support for Comments - @troosan #1067
1919
- Support for paragraph textAlignment - @troosan #1165
2020
- Add support for HTML underline tag <u> in addHtml - @zNightFalLz #1186
21-
- Allow to change cell width unit - guillaume-ro-fr #986
21+
- Allow to change cell width unit - @guillaume-ro-fr #986
2222
- Allow to change the line height rule @troosan
23+
- Allow to force an update of all fields on opening a document - @troosan #951
2324

2425
### Fixed
2526
- Loosen dependency to Zend

docs/elements.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Your TOC can only be generated if you have add at least one title (See "Titles")
297297

298298
Options for ``$tocStyle``:
299299

300-
- ``tabLeader``. Fill type between the title text and the page number. Use the defined constants in PHPWord\\Style\\TOC.
300+
- ``tabLeader``. Fill type between the title text and the page number. Use the defined constants in ``\PhpOffice\PhpWord\Style\TOC``.
301301
- ``tabPos``. The position of the tab where the page number appears in twips.
302302
- ``indent``. The indent factor of the titles in twips.
303303

docs/general.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,12 @@ points to twips.
271271
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
272272
// 2 cm right margin
273273
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
274+
275+
Automatically Recalculate Fields on Open
276+
----------------------------------------
277+
278+
To force an update of the fields present in the document, set updateFields to true
279+
280+
.. code-block:: php
281+
282+
$phpWord->getSettings()->setUpdateFields(true);

samples/Sample_17_TitleTOC.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// New Word document
55
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
$phpWord->getSettings()->setUpdateFields(true);
78

89
// New section
910
$section = $phpWord->addSection();

src/PhpWord/Metadata/Settings.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ class Settings
116116
*/
117117
private $themeFontLang;
118118

119+
/**
120+
* Automatically Recalculate Fields on Open
121+
*
122+
* @var bool
123+
*/
124+
private $updateFields = false;
125+
119126
/**
120127
* Radix Point for Field Code Evaluation
121128
*
@@ -345,6 +352,22 @@ public function setThemeFontLang($themeFontLang)
345352
$this->themeFontLang = $themeFontLang;
346353
}
347354

355+
/**
356+
* @return bool
357+
*/
358+
public function hasUpdateFields()
359+
{
360+
return $this->updateFields;
361+
}
362+
363+
/**
364+
* @param bool $updateFields
365+
*/
366+
public function setUpdateFields($updateFields)
367+
{
368+
$this->updateFields = $updateFields === null ? false : $updateFields;
369+
}
370+
348371
/**
349372
* Returns the Radix Point for Field Code Evaluation
350373
*

src/PhpWord/Writer/Word2007/Part/Settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ private function getSettings()
147147
$this->setOnOffValue('w:doNotTrackMoves', $documentSettings->hasDoNotTrackMoves());
148148
$this->setOnOffValue('w:doNotTrackFormatting', $documentSettings->hasDoNotTrackFormatting());
149149
$this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders());
150+
$this->setOnOffValue('w:updateFields', $documentSettings->hasUpdateFields());
150151

151152
$this->setThemeFontLang($documentSettings->getThemeFontLang());
152153
$this->setRevisionView($documentSettings->getRevisionView());

src/PhpWord/Writer/Word2007/Style/Font.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ private function writeStyle()
104104

105105
// Bold, italic
106106
$xmlWriter->writeElementIf($style->isBold(), 'w:b');
107+
$xmlWriter->writeElementIf($style->isBold(), 'w:bCs');
107108
$xmlWriter->writeElementIf($style->isItalic(), 'w:i');
108109
$xmlWriter->writeElementIf($style->isItalic(), 'w:iCs');
109110

tests/PhpWord/Metadata/SettingsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,14 @@ public function testZoomEnum()
153153
$oSettings->setZoom(Zoom::FULL_PAGE);
154154
$this->assertEquals('fullPage', $oSettings->getZoom());
155155
}
156+
157+
/**
158+
* Test Update Fields on update
159+
*/
160+
public function testUpdateFields()
161+
{
162+
$oSettings = new Settings();
163+
$oSettings->setUpdateFields(true);
164+
$this->assertTrue($oSettings->hasUpdateFields());
165+
}
156166
}

0 commit comments

Comments
 (0)