Skip to content

Commit b919d58

Browse files
committed
add reader for settings
1 parent e9cc289 commit b919d58

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

src/PhpWord/Metadata/Settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function hasHideSpellingErrors()
9090
*/
9191
public function setHideSpellingErrors($hideSpellingErrors)
9292
{
93-
$this->hideSpellingErrors = $hideSpellingErrors;
93+
$this->hideSpellingErrors = $hideSpellingErrors === null ? true : $hideSpellingErrors;
9494
}
9595

9696
/**
@@ -110,7 +110,7 @@ public function hasHideGrammaticalErrors()
110110
*/
111111
public function setHideGrammaticalErrors($hideGrammaticalErrors)
112112
{
113-
$this->hideGrammaticalErrors = $hideGrammaticalErrors;
113+
$this->hideGrammaticalErrors = $hideGrammaticalErrors === null ? true : $hideGrammaticalErrors;
114114
}
115115

116116
/**
@@ -126,6 +126,6 @@ public function hasEvenAndOddHeaders()
126126
*/
127127
public function setEvenAndOddHeaders($evenAndOddHeaders)
128128
{
129-
$this->evenAndOddHeaders = $evenAndOddHeaders;
129+
$this->evenAndOddHeaders = $evenAndOddHeaders === null ? true : $evenAndOddHeaders;
130130
}
131131
}

src/PhpWord/Reader/Word2007.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function load($docFile)
5555
array('stepPart' => 'document', 'stepItems' => array(
5656
'endnotes' => 'Endnotes',
5757
'footnotes' => 'Footnotes',
58+
'settings' => 'Settings',
5859
)),
5960
);
6061

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\Reader\Word2007;
19+
20+
use PhpOffice\Common\XMLReader;
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\Metadata\Protection;
23+
24+
/**
25+
* Settings reader
26+
*
27+
* @since 0.14.0
28+
*/
29+
class Settings extends AbstractPart
30+
{
31+
32+
private static $booleanProperties = array('hideSpellingErrors', 'hideGrammaticalErrors', 'evenAndOddHeaders');
33+
private static $decimalProperties = array('zoom');
34+
35+
/**
36+
* Read settings.xml.
37+
*
38+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
39+
* @return void
40+
*/
41+
public function read(PhpWord $phpWord)
42+
{
43+
$xmlReader = new XMLReader();
44+
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
45+
46+
$docSettings = $phpWord->getSettings();
47+
48+
$nodes = $xmlReader->getElements('*');
49+
if ($nodes->length > 0) {
50+
foreach ($nodes as $node) {
51+
$name = str_replace('w:', '', $node->nodeName);
52+
$value = $xmlReader->getAttribute('w:val', $node);
53+
$method = 'set' . $name;
54+
55+
if (in_array($name, $this::$booleanProperties)) {
56+
if ($value == 'false') {
57+
$docSettings->$method(false);
58+
} else {
59+
$docSettings->$method(true);
60+
}
61+
} else if (method_exists($this, $method)) {
62+
$this->$method($xmlReader, $phpWord, $node);
63+
} else if (method_exists($this, $method)) {
64+
$docSettings->$method($value);
65+
}
66+
}
67+
}
68+
}
69+
70+
private function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) {
71+
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
72+
73+
$edit = $xmlReader->getAttribute('w:edit', $node);
74+
$documentProtection->setEditing($edit);
75+
}
76+
}

0 commit comments

Comments
 (0)