Settings.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Reader\Word2007;
  18. use PhpOffice\PhpWord\ComplexType\TrackChangesView;
  19. use PhpOffice\PhpWord\PhpWord;
  20. use PhpOffice\PhpWord\Shared\XMLReader;
  21. use PhpOffice\PhpWord\Style\Language;
  22. /**
  23. * Settings reader
  24. *
  25. * @since 0.14.0
  26. */
  27. class Settings extends AbstractPart
  28. {
  29. private static $booleanProperties = array(
  30. 'mirrorMargins',
  31. 'hideSpellingErrors',
  32. 'hideGrammaticalErrors',
  33. 'trackRevisions',
  34. 'doNotTrackMoves',
  35. 'doNotTrackFormatting',
  36. 'evenAndOddHeaders',
  37. 'updateFields',
  38. 'autoHyphenation',
  39. 'doNotHyphenateCaps',
  40. );
  41. /**
  42. * Read settings.xml.
  43. *
  44. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  45. */
  46. public function read(PhpWord $phpWord)
  47. {
  48. $xmlReader = new XMLReader();
  49. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  50. $docSettings = $phpWord->getSettings();
  51. $nodes = $xmlReader->getElements('*');
  52. if ($nodes->length > 0) {
  53. foreach ($nodes as $node) {
  54. $name = str_replace('w:', '', $node->nodeName);
  55. $value = $xmlReader->getAttribute('w:val', $node);
  56. $method = 'set' . $name;
  57. if (in_array($name, $this::$booleanProperties)) {
  58. if ($value == 'false') {
  59. $docSettings->$method(false);
  60. } else {
  61. $docSettings->$method(true);
  62. }
  63. } elseif (method_exists($this, $method)) {
  64. $this->$method($xmlReader, $phpWord, $node);
  65. } elseif (method_exists($docSettings, $method)) {
  66. $docSettings->$method($value);
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Sets the document Language
  73. *
  74. * @param XMLReader $xmlReader
  75. * @param PhpWord $phpWord
  76. * @param \DOMElement $node
  77. */
  78. protected function setThemeFontLang(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  79. {
  80. $val = $xmlReader->getAttribute('w:val', $node);
  81. $eastAsia = $xmlReader->getAttribute('w:eastAsia', $node);
  82. $bidi = $xmlReader->getAttribute('w:bidi', $node);
  83. $themeFontLang = new Language();
  84. $themeFontLang->setLatin($val);
  85. $themeFontLang->setEastAsia($eastAsia);
  86. $themeFontLang->setBidirectional($bidi);
  87. $phpWord->getSettings()->setThemeFontLang($themeFontLang);
  88. }
  89. /**
  90. * Sets the document protection
  91. *
  92. * @param XMLReader $xmlReader
  93. * @param PhpWord $phpWord
  94. * @param \DOMElement $node
  95. */
  96. protected function setDocumentProtection(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  97. {
  98. $documentProtection = $phpWord->getSettings()->getDocumentProtection();
  99. $edit = $xmlReader->getAttribute('w:edit', $node);
  100. if ($edit !== null) {
  101. $documentProtection->setEditing($edit);
  102. }
  103. }
  104. /**
  105. * Sets the proof state
  106. *
  107. * @param XMLReader $xmlReader
  108. * @param PhpWord $phpWord
  109. * @param \DOMElement $node
  110. */
  111. protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  112. {
  113. $proofState = $phpWord->getSettings()->getProofState();
  114. $spelling = $xmlReader->getAttribute('w:spelling', $node);
  115. $grammar = $xmlReader->getAttribute('w:grammar', $node);
  116. if ($spelling !== null) {
  117. $proofState->setSpelling($spelling);
  118. }
  119. if ($grammar !== null) {
  120. $proofState->setGrammar($grammar);
  121. }
  122. }
  123. /**
  124. * Sets the proof state
  125. *
  126. * @param XMLReader $xmlReader
  127. * @param PhpWord $phpWord
  128. * @param \DOMElement $node
  129. */
  130. protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  131. {
  132. $percent = $xmlReader->getAttribute('w:percent', $node);
  133. $val = $xmlReader->getAttribute('w:val', $node);
  134. if ($percent !== null || $val !== null) {
  135. $phpWord->getSettings()->setZoom($percent === null ? $val : $percent);
  136. }
  137. }
  138. /**
  139. * Set the Revision view
  140. *
  141. * @param XMLReader $xmlReader
  142. * @param PhpWord $phpWord
  143. * @param \DOMElement $node
  144. */
  145. protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  146. {
  147. $revisionView = new TrackChangesView();
  148. $revisionView->setMarkup(filter_var($xmlReader->getAttribute('w:markup', $node), FILTER_VALIDATE_BOOLEAN));
  149. $revisionView->setComments($xmlReader->getAttribute('w:comments', $node));
  150. $revisionView->setInsDel(filter_var($xmlReader->getAttribute('w:insDel', $node), FILTER_VALIDATE_BOOLEAN));
  151. $revisionView->setFormatting(filter_var($xmlReader->getAttribute('w:formatting', $node), FILTER_VALIDATE_BOOLEAN));
  152. $revisionView->setInkAnnotations(filter_var($xmlReader->getAttribute('w:inkAnnotations', $node), FILTER_VALIDATE_BOOLEAN));
  153. $phpWord->getSettings()->setRevisionView($revisionView);
  154. }
  155. /**
  156. * @param XMLReader $xmlReader
  157. * @param PhpWord $phpWord
  158. * @param \DOMElement $node
  159. */
  160. protected function setConsecutiveHyphenLimit(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  161. {
  162. $value = $xmlReader->getAttribute('w:val', $node);
  163. if ($value !== null) {
  164. $phpWord->getSettings()->setConsecutiveHyphenLimit($value);
  165. }
  166. }
  167. /**
  168. * @param XMLReader $xmlReader
  169. * @param PhpWord $phpWord
  170. * @param \DOMElement $node
  171. */
  172. protected function setHyphenationZone(XMLReader $xmlReader, PhpWord $phpWord, \DOMElement $node)
  173. {
  174. $value = $xmlReader->getAttribute('w:val', $node);
  175. if ($value !== null) {
  176. $phpWord->getSettings()->setHyphenationZone($value);
  177. }
  178. }
  179. }