Skip to content

Allow to force an update of all fields on opening a document #1205

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 3 commits into from
Nov 25, 2017
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ This is the last version to support PHP 5.3
- Support for Comments - @troosan #1067
- Support for paragraph textAlignment - @troosan #1165
- Add support for HTML underline tag <u> in addHtml - @zNightFalLz #1186
- Allow to change cell width unit - guillaume-ro-fr #986
- Allow to change cell width unit - @guillaume-ro-fr #986
- Allow to change the line height rule @troosan
- Allow to force an update of all fields on opening a document - @troosan #951

### Fixed
- Loosen dependency to Zend
Expand Down
2 changes: 1 addition & 1 deletion docs/elements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ Your TOC can only be generated if you have add at least one title (See "Titles")

Options for ``$tocStyle``:

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

Expand Down
9 changes: 9 additions & 0 deletions docs/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,12 @@ points to twips.
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
// 2 cm right margin
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));

Automatically Recalculate Fields on Open
----------------------------------------

To force an update of the fields present in the document, set updateFields to true

.. code-block:: php

$phpWord->getSettings()->setUpdateFields(true);
1 change: 1 addition & 0 deletions samples/Sample_17_TitleTOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// New Word document
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setUpdateFields(true);

// New section
$section = $phpWord->addSection();
Expand Down
23 changes: 23 additions & 0 deletions src/PhpWord/Metadata/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ class Settings
*/
private $themeFontLang;

/**
* Automatically Recalculate Fields on Open
*
* @var bool
*/
private $updateFields = false;

/**
* Radix Point for Field Code Evaluation
*
Expand Down Expand Up @@ -345,6 +352,22 @@ public function setThemeFontLang($themeFontLang)
$this->themeFontLang = $themeFontLang;
}

/**
* @return bool
*/
public function hasUpdateFields()
{
return $this->updateFields;
}

/**
* @param bool $updateFields
*/
public function setUpdateFields($updateFields)
{
$this->updateFields = $updateFields === null ? false : $updateFields;
}

/**
* Returns the Radix Point for Field Code Evaluation
*
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Writer/Word2007/Part/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private function getSettings()
$this->setOnOffValue('w:doNotTrackMoves', $documentSettings->hasDoNotTrackMoves());
$this->setOnOffValue('w:doNotTrackFormatting', $documentSettings->hasDoNotTrackFormatting());
$this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders());
$this->setOnOffValue('w:updateFields', $documentSettings->hasUpdateFields());

$this->setThemeFontLang($documentSettings->getThemeFontLang());
$this->setRevisionView($documentSettings->getRevisionView());
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Writer/Word2007/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private function writeStyle()

// Bold, italic
$xmlWriter->writeElementIf($style->isBold(), 'w:b');
$xmlWriter->writeElementIf($style->isBold(), 'w:bCs');
$xmlWriter->writeElementIf($style->isItalic(), 'w:i');
$xmlWriter->writeElementIf($style->isItalic(), 'w:iCs');

Expand Down
10 changes: 10 additions & 0 deletions tests/PhpWord/Metadata/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,14 @@ public function testZoomEnum()
$oSettings->setZoom(Zoom::FULL_PAGE);
$this->assertEquals('fullPage', $oSettings->getZoom());
}

/**
* Test Update Fields on update
*/
public function testUpdateFields()
{
$oSettings = new Settings();
$oSettings->setUpdateFields(true);
$this->assertTrue($oSettings->hasUpdateFields());
}
}