Skip to content

Commit e9cc289

Browse files
committed
refactor Settings to not mix PHPWord settings and document settings
1 parent 21303ed commit e9cc289

File tree

11 files changed

+252
-117
lines changed

11 files changed

+252
-117
lines changed

docs/containers.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ that are available for the footer. See "Footer" section for detail.
9898
Additionally, only inside of the header reference you can add watermarks
9999
or background pictures. See "Watermarks" section.
100100

101+
You can pass an optional parameter to specify where the header/footer should be applied, it can be
102+
103+
- ``Footer::AUTO`` default, all pages except if overridden by first or even
104+
- ``Footer::FIRST`` each first page of the section
105+
- ``Footer::EVEN`` each even page of the section. Will only be applied if the evenAndOddHeaders is set to true in phpWord->settings
106+
101107
Footers
102108
-------
103109

docs/general.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ For big documents this can slow down the opening of the document. You can hide t
138138

139139
.. code-block:: php
140140
141-
\PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true);
142-
\PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true);
141+
$phpWord->getSettings()->setHideGrammaticalErrors(true);
142+
$phpWord->getSettings()->setHideSpellingErrors(true);
143143
144144
Default font
145145
~~~~~~~~~~~~

src/PhpWord/Element/Footer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Footer extends AbstractContainer
2626
* Header/footer types constants
2727
*
2828
* @var string
29-
* @link http://www.schemacentral.com/sc/ooxml/a-wtype-4.html Header or Footer Type
29+
* @link http://www.datypic.com/sc/ooxml/t-w_ST_HdrFtr.html Header or Footer Type
3030
*/
3131
const AUTO = 'default'; // default and odd pages
3232
const FIRST = 'first';

src/PhpWord/Metadata/Settings.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2016 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Metadata;
19+
20+
/**
21+
* Setting class
22+
*
23+
* @since 0.14.0
24+
* @link http://www.datypic.com/sc/ooxml/t-w_CT_Settings.html
25+
*/
26+
class Settings
27+
{
28+
29+
/**
30+
* Hide spelling errors
31+
*
32+
* @var boolean
33+
*/
34+
private $hideSpellingErrors = false;
35+
36+
/**
37+
* Hide grammatical errors
38+
*
39+
* @var boolean
40+
*/
41+
private $hideGrammaticalErrors = false;
42+
43+
/**
44+
* Document Editing Restrictions
45+
*
46+
* @var PhpOffice\PhpWord\Metadata\Protection
47+
*/
48+
private $documentProtection;
49+
50+
/**
51+
* Enables different header for odd and even pages.
52+
*
53+
* @var bool
54+
*/
55+
private $evenAndOddHeaders = false;
56+
57+
/**
58+
* @return Protection
59+
*/
60+
public function getDocumentProtection()
61+
{
62+
if ($this->documentProtection == null) {
63+
$this->documentProtection = new Protection();
64+
}
65+
return $this->documentProtection;
66+
}
67+
68+
/**
69+
* @param Protection $documentProtection
70+
*/
71+
public function setDocumentProtection($documentProtection)
72+
{
73+
$this->documentProtection = $documentProtection;
74+
}
75+
76+
/**
77+
* Are spelling errors hidden
78+
*
79+
* @return boolean
80+
*/
81+
public function hasHideSpellingErrors()
82+
{
83+
return $this->hideSpellingErrors;
84+
}
85+
86+
/**
87+
* Hide spelling errors
88+
*
89+
* @param boolean $hideSpellingErrors
90+
*/
91+
public function setHideSpellingErrors($hideSpellingErrors)
92+
{
93+
$this->hideSpellingErrors = $hideSpellingErrors;
94+
}
95+
96+
/**
97+
* Are grammatical errors hidden
98+
*
99+
* @return boolean
100+
*/
101+
public function hasHideGrammaticalErrors()
102+
{
103+
return $this->hideGrammaticalErrors;
104+
}
105+
106+
/**
107+
* Hide grammatical errors
108+
*
109+
* @param boolean $hideGrammaticalErrors
110+
*/
111+
public function setHideGrammaticalErrors($hideGrammaticalErrors)
112+
{
113+
$this->hideGrammaticalErrors = $hideGrammaticalErrors;
114+
}
115+
116+
/**
117+
* @return boolean
118+
*/
119+
public function hasEvenAndOddHeaders()
120+
{
121+
return $this->evenAndOddHeaders;
122+
}
123+
124+
/**
125+
* @param boolean $evenAndOddHeaders
126+
*/
127+
public function setEvenAndOddHeaders($evenAndOddHeaders)
128+
{
129+
$this->evenAndOddHeaders = $evenAndOddHeaders;
130+
}
131+
}

src/PhpWord/PhpWord.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function __construct()
9191
}
9292

9393
// Metadata
94-
$metadata = array('DocInfo', 'Protection', 'Compatibility');
94+
$metadata = array('DocInfo', 'Settings', 'Compatibility');
9595
foreach ($metadata as $meta) {
9696
$class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta;
9797
$this->metadata[$meta] = new $class();
@@ -170,10 +170,12 @@ public function getDocInfo()
170170
*
171171
* @return \PhpOffice\PhpWord\Metadata\Protection
172172
* @since 0.12.0
173+
* @deprecated Get the Document protection from PhpWord->getSettings()->getDocumentProtection();
174+
* @codeCoverageIgnore
173175
*/
174176
public function getProtection()
175177
{
176-
return $this->metadata['Protection'];
178+
return $this->getSettings()->getDocumentProtection();
177179
}
178180

179181
/**
@@ -187,6 +189,17 @@ public function getCompatibility()
187189
return $this->metadata['Compatibility'];
188190
}
189191

192+
/**
193+
* Get compatibility
194+
*
195+
* @return \PhpOffice\PhpWord\Metadata\Settings
196+
* @since 0.14.0
197+
*/
198+
public function getSettings()
199+
{
200+
return $this->metadata['Settings'];
201+
}
202+
190203
/**
191204
* Get all sections
192205
*

src/PhpWord/Settings.php

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,6 @@ class Settings
119119
*/
120120
private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
121121

122-
/**
123-
* Hide spelling errors
124-
* @var boolean
125-
*/
126-
private static $spellingErrorsHidden = false;
127-
128-
/**
129-
* Hide grammatical errors
130-
* @var boolean
131-
*/
132-
private static $grammaticalErrorsHidden = false;
133-
134122
/**
135123
* The user defined temporary directory.
136124
*
@@ -146,13 +134,6 @@ class Settings
146134
*/
147135
private static $outputEscapingEnabled = false;
148136

149-
/**
150-
* Enables different header for odd and even pages.
151-
*
152-
* @var bool
153-
*/
154-
private static $evenAndOddHeaders = false;
155-
156137
/**
157138
* Return the compatibility option used by the XMLWriter
158139
*
@@ -359,22 +340,6 @@ public static function setOutputEscapingEnabled($outputEscapingEnabled)
359340
self::$outputEscapingEnabled = $outputEscapingEnabled;
360341
}
361342

362-
/**
363-
* @return boolean
364-
*/
365-
public static function isEvenAndOddHeaders()
366-
{
367-
return self::$evenAndOddHeaders;
368-
}
369-
370-
/**
371-
* @param boolean $evenAndOddHeaders
372-
*/
373-
public static function setEvenAndOddHeaders($evenAndOddHeaders)
374-
{
375-
self::$evenAndOddHeaders = $evenAndOddHeaders;
376-
}
377-
378343
/**
379344
* Get default font name
380345
*
@@ -428,46 +393,6 @@ public static function setDefaultFontSize($value)
428393
return false;
429394
}
430395

431-
/**
432-
* Are spelling errors hidden
433-
*
434-
* @return boolean
435-
*/
436-
public static function isSpellingErrorsHidden()
437-
{
438-
return self::$spellingErrorsHidden;
439-
}
440-
441-
/**
442-
* Hide spelling errors
443-
*
444-
* @param boolean $spellingErrorsHidden
445-
*/
446-
public static function setSpellingErrorsHidden($spellingErrorsHidden)
447-
{
448-
self::$spellingErrorsHidden = $spellingErrorsHidden;
449-
}
450-
451-
/**
452-
* Are grammatical errors hidden
453-
*
454-
* @return boolean
455-
*/
456-
public static function isGrammaticalErrorsHidden()
457-
{
458-
return self::$grammaticalErrorsHidden;
459-
}
460-
461-
/**
462-
* Hide grammatical errors
463-
*
464-
* @param boolean $grammaticalErrorsHidden
465-
*/
466-
public static function setGrammaticalErrorsHidden($grammaticalErrorsHidden)
467-
{
468-
self::$grammaticalErrorsHidden = $grammaticalErrorsHidden;
469-
}
470-
471396
/**
472397
* Load setting from phpword.yml or phpword.yml.dist
473398
*

src/PhpWord/SimpleType/NumberFormat.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
* @since 0.14.0
2626
*
2727
* @see http://www.datypic.com/sc/ooxml/t-w_ST_NumberFormat.html.
28-
*
29-
* @codeCoverageIgnore
3028
*/
3129
final class NumberFormat extends AbstractEnum
3230
{

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ private function getSettings()
105105
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
106106
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
107107
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
108-
'w:evenAndOddHeaders' => array('@attributes' => array('w:val' => DocumentSettings::isEvenAndOddHeaders() ? 'true': 'false')),
109108
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
110-
'w:hideSpellingErrors' => array('@attributes' => array('w:val' => DocumentSettings::isSpellingErrorsHidden() ? 'true' : 'false')),
111-
'w:hideGrammaticalErrors' => array('@attributes' => array('w:val' => DocumentSettings::isGrammaticalErrorsHidden() ? 'true' : 'false')),
112109
'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
113110
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
114111
'w:compat' => array(),
@@ -143,24 +140,44 @@ private function getSettings()
143140
),
144141
);
145142

143+
/** @var \PhpOffice\PhpWord\Metadata\Settings $documentSettings */
144+
$documentSettings = $this->getParentWriter()->getPhpWord()->getSettings();
145+
146+
$this->setOnOffValue('w:hideSpellingErrors', $documentSettings->hasHideSpellingErrors());
147+
$this->setOnOffValue('w:hideGrammaticalErrors', $documentSettings->hasHideGrammaticalErrors());
148+
$this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders());
149+
146150
// Other settings
147-
$this->getProtection();
151+
$this->setDocumentProtection($documentSettings->getDocumentProtection());
148152
$this->getCompatibility();
149153
}
150154

155+
/**
156+
* Adds a boolean attribute to the settings array
157+
*
158+
* @param string $settingName
159+
* @param boolean $booleanValue
160+
*/
161+
private function setOnOffValue($settingName, $booleanValue)
162+
{
163+
if ($booleanValue !== null && is_bool($booleanValue)) {
164+
$this->settings[$settingName] = array('@attributes' => array('w:val' => $booleanValue ? 'true': 'false'));
165+
}
166+
}
167+
151168
/**
152169
* Get protection settings.
153170
*
171+
* @param \PhpOffice\PhpWord\Metadata\Settings $documentProtection
154172
* @return void
155173
*/
156-
private function getProtection()
174+
private function setDocumentProtection($documentProtection)
157175
{
158-
$protection = $this->getParentWriter()->getPhpWord()->getProtection();
159-
if ($protection->getEditing() !== null) {
176+
if ($documentProtection != null && $documentProtection->getEditing() !== null) {
160177
$this->settings['w:documentProtection'] = array(
161178
'@attributes' => array(
162179
'w:enforcement' => 1,
163-
'w:edit' => $protection->getEditing(),
180+
'w:edit' => $documentProtection->getEditing(),
164181
)
165182
);
166183
}

0 commit comments

Comments
 (0)