202 lines
6.4 KiB
PHP
Raw Normal View History

2017-07-06 22:41:04 +02:00
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
2018-03-08 23:52:25 +01:00
* @copyright 2010-2018 PHPWord contributors
2017-07-06 22:41:04 +02:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Language;
2017-07-06 22:41:04 +02:00
/**
* Settings reader
*
* @since 0.14.0
*/
class Settings extends AbstractPart
{
private static $booleanProperties = array(
'mirrorMargins',
'hideSpellingErrors',
'hideGrammaticalErrors',
'trackRevisions',
'doNotTrackMoves',
'doNotTrackFormatting',
'evenAndOddHeaders',
'updateFields',
'autoHyphenation',
'doNotHyphenateCaps',
);
2017-07-06 22:41:04 +02:00
/**
* Read settings.xml.
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord $phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docSettings = $phpWord->getSettings();
$nodes = $xmlReader->getElements('*');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$name = str_replace('w:', '', $node->nodeName);
$value = $xmlReader->getAttribute('w:val', $node);
$method = 'set' . $name;
if (in_array($name, $this::$booleanProperties)) {
if ($value == 'false') {
$docSettings->$method(false);
} else {
$docSettings->$method(true);
}
} elseif (method_exists($this, $method)) {
2017-07-06 22:41:04 +02:00
$this->$method($xmlReader, $phpWord, $node);
} elseif (method_exists($docSettings, $method)) {
2017-07-06 22:41:04 +02:00
$docSettings->$method($value);
}
}
}
}
/**
* Sets the document Language
*
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
2018-07-14 03:28:09 +02:00
* @param \DOMElement $node
*/
protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$val = $xmlReader->getAttribute('w:val', $node);
$eastAsia = $xmlReader->getAttribute('w:eastAsia', $node);
$bidi = $xmlReader->getAttribute('w:bidi', $node);
$themeFontLang = new Language();
$themeFontLang->setLatin($val);
$themeFontLang->setEastAsia($eastAsia);
$themeFontLang->setBidirectional($bidi);
$phpWord->getSettings()->setThemeFontLang($themeFontLang);
}
2017-07-06 23:03:07 +02:00
/**
* Sets the document protection
*
2017-07-06 23:03:07 +02:00
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
2018-07-14 03:28:09 +02:00
* @param \DOMElement $node
2017-07-06 23:03:07 +02:00
*/
protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
2017-07-07 08:02:31 +02:00
{
2017-07-06 22:41:04 +02:00
$documentProtection = $phpWord->getSettings()->getDocumentProtection();
$edit = $xmlReader->getAttribute('w:edit', $node);
if ($edit !== null) {
$documentProtection->setEditing($edit);
}
2017-07-06 22:41:04 +02:00
}
/**
* Sets the proof state
*
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
2018-07-14 03:28:09 +02:00
* @param \DOMElement $node
*/
protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$proofState = $phpWord->getSettings()->getProofState();
$spelling = $xmlReader->getAttribute('w:spelling', $node);
$grammar = $xmlReader->getAttribute('w:grammar', $node);
if ($spelling !== null) {
$proofState->setSpelling($spelling);
}
if ($grammar !== null) {
$proofState->setGrammar($grammar);
}
}
/**
* Sets the proof state
*
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
2018-07-14 03:28:09 +02:00
* @param \DOMElement $node
*/
protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$percent = $xmlReader->getAttribute('w:percent', $node);
$val = $xmlReader->getAttribute('w:val', $node);
if ($percent !== null || $val !== null) {
$phpWord->getSettings()->setZoom($percent === null ? $val : $percent);
}
}
/**
* Set the Revision view
*
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
2018-07-14 03:28:09 +02:00
* @param \DOMElement $node
*/
protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$revisionView = new TrackChangesView();
2017-11-26 17:54:47 +01:00
$revisionView->setMarkup(filter_var($xmlReader->getAttribute('w:markup', $node), FILTER_VALIDATE_BOOLEAN));
$revisionView->setComments($xmlReader->getAttribute('w:comments', $node));
$revisionView->setInsDel(filter_var($xmlReader->getAttribute('w:insDel', $node), FILTER_VALIDATE_BOOLEAN));
2017-11-26 17:54:47 +01:00
$revisionView->setFormatting(filter_var($xmlReader->getAttribute('w:formatting', $node), FILTER_VALIDATE_BOOLEAN));
$revisionView->setInkAnnotations(filter_var($xmlReader->getAttribute('w:inkAnnotations', $node), FILTER_VALIDATE_BOOLEAN));
$phpWord->getSettings()->setRevisionView($revisionView);
}
/**
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
* @param \DOMElement $node
*/
protected function setConsecutiveHyphenLimit(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$value = $xmlReader->getAttribute('w:val', $node);
if ($value !== null) {
$phpWord->getSettings()->setConsecutiveHyphenLimit($value);
}
}
/**
* @param XMLReader $xmlReader
* @param PhpWord $phpWord
* @param \DOMElement $node
*/
protected function setHyphenationZone(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
{
$value = $xmlReader->getAttribute('w:val', $node);
if ($value !== null) {
$phpWord->getSettings()->setHyphenationZone($value);
}
}
2017-07-06 22:41:04 +02:00
}