diff --git a/.travis.yml b/.travis.yml index 43c4b205..74b77909 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: php +dist: precise + php: - 5.3 - 5.4 diff --git a/CHANGELOG.md b/CHANGELOG.md index e1e0245f..52bd6aac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). v0.14.0 (?? ???? 2017) ---------------------- -This release fixes several bugs and adds some new features +This release fixes several bugs and adds some new features. +This is the last version to support PHP 5.3 ### Added - Possibility to control the footnote numbering - @troosan #1068 @@ -13,8 +14,11 @@ This release fixes several bugs and adds some new features - Introduced the `\PhpOffice\PhpWord\SimpleType\NumberFormat` simple type. - @troosan - Support for ContextualSpacing - @postHawk #1088 - Possiblity to hide spelling and/or grammatical errors - @troosan #542 +- Support for Comments - @troosan #1067 +- Add support for changing the document language - @troosan #1108 ### Fixed +- Loosen dependency to Zend - Images are not being printed when generating PDF - @hubertinio #1074 #431 - Fixed some PHP 7 warnings - @ likeuntomurphy #927 - Fixed Word 97 reader - @alsofronie @Benpxpx @mario-rivera #912 #920 #892 @@ -23,7 +27,8 @@ This release fixes several bugs and adds some new features - Fixed Word2007 reader where unnecessary paragraphs were being created - @donghaobo #1043 #620 - Fixed Word2007 reader where margins were not being read correctly - @slowprog #885 #1008 - Impossible to add element PreserveText in Section - @rvanlaak #452 -- Missing options for numbering format - @troosan #1041 +- Added missing options for numbering format - @troosan #1041 +- Fixed impossibility to set a different footer for first page - @ctrlaltca #1116 v0.13.0 (31 July 2016) ------------------- diff --git a/README.md b/README.md index 6b9f7dfa..f712c6c6 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,7 @@ PHPWord requires the following: - PHP 5.3.3+ - [XML Parser extension](http://www.php.net/manual/en/xml.installation.php) - [Zend\Escaper component](http://framework.zend.com/manual/current/en/modules/zend.escaper.introduction.html) -- Zend\Stdlib component -- [Zend\Validator component](http://framework.zend.com/manual/current/en/modules/zend.validator.html) +- [Zend\Stdlib component](http://framework.zend.com/manual/current/en/modules/zend.stdlib.hydrator.html) - [Zip extension](http://php.net/manual/en/book.zip.php) (optional, used to write OOXML and ODF) - [GD extension](http://php.net/manual/en/book.image.php) (optional, used to add images) - [XMLWriter extension](http://php.net/manual/en/book.xmlwriter.php) (optional, used to write OOXML and ODF) diff --git a/docs/elements.rst b/docs/elements.rst index c89903f1..a35eb654 100644 --- a/docs/elements.rst +++ b/docs/elements.rst @@ -158,8 +158,8 @@ Parameters: - ``$text``. Text that appears in the document. - ``$depth``. Depth of list item. - ``$fontStyle``. See :ref:`font-style`. -- ``$listStyle``. List style of the current element TYPE\_NUMBER, - TYPE\_ALPHANUM, TYPE\_BULLET\_FILLED, etc. See list of constants in PHPWord\_Style\_ListItem. +- ``$listStyle``. List style of the current element TYPE\_NUMBER, + TYPE\_ALPHANUM, TYPE\_BULLET\_FILLED, etc. See list of constants in PHPWord\\Style\\ListItem. - ``$paragraphStyle``. See :ref:`paragraph-style`. Advanced usage: @@ -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 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. diff --git a/docs/general.rst b/docs/general.rst index 8e347089..87fecb77 100644 --- a/docs/general.rst +++ b/docs/general.rst @@ -80,8 +80,8 @@ folder `__. /* Note: we skip RTF, because it's not XML-based and requires a different example. */ /* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */ -Settings --------- +PHPWord Settings +---------------- The ``PhpOffice\PhpWord\Settings`` class provides some options that will affect the behavior of PHPWord. Below are the options. @@ -130,6 +130,35 @@ To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord c \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); +Default font +~~~~~~~~~~~~ + +By default, every text appears in Arial 10 point. You can alter the +default font by using the following two functions: + +.. code-block:: php + + $phpWord->setDefaultFontName('Times New Roman'); + $phpWord->setDefaultFontSize(12); + +Document settings +----------------- +Settings for the generated document can be set using ``$phpWord->getSettings()`` + +Magnification Setting +~~~~~~~~~~~~~~~~~~~~~ +The default zoom value is 100 percent. This can be changed either to another percentage + +.. code-block:: php + + $phpWord->getSettings()->setZoom(75); + +Or to predefined values ``fullPage``, ``bestFit``, ``textFit`` + +.. code-block:: php + + $phpWord->getSettings()->setZoom(Zoom::BEST_FIT); + Spelling and grammatical checks ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -141,16 +170,36 @@ For big documents this can slow down the opening of the document. You can hide t $phpWord->getSettings()->setHideGrammaticalErrors(true); $phpWord->getSettings()->setHideSpellingErrors(true); -Default font -~~~~~~~~~~~~ - -By default, every text appears in Arial 10 point. You can alter the -default font by using the following two functions: +You can also specify the status of the spell and grammar checks, marking spelling or grammar as dirty will force a re-check when opening the document. .. code-block:: php - $phpWord->setDefaultFontName('Times New Roman'); - $phpWord->setDefaultFontSize(12); + $proofState = new ProofState(); + $proofState->setGrammar(ProofState::CLEAN); + $proofState->setSpelling(ProofState::DIRTY); + + $phpWord->getSettings()->setProofState(proofState); + +Track Revisions +~~~~~~~~~~~~~~~ +Track changes can be activated using ``setTrackRevisions``, you can furture specify + +- Not to use move syntax, instead moved items will be seen as deleted in one place and added in another +- Not track formatting revisions + +.. code-block:: php + + $phpWord->getSettings()->setTrackRevisions(true); + $phpWord->getSettings()->setDoNotTrackMoves(true); + $phpWord->getSettings()->setDoNotTrackFormatting(true); + +Decimal Symbol +~~~~~~~~~~~~~~ +The default symbol to represent a decimal figure is the ``.`` in english. In french you might want to change it to ``,`` for instance. + +.. code-block:: php + + $phpWord->getSettings()->setDecimalSymbol(','); Document information -------------------- @@ -194,16 +243,3 @@ points to twips. $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); - -Language --------- - -You can hide spelling errors: - -.. code-block:: php - \PhpOffice\PhpWord\Settings::setSpellingErrorsHidden(true); - -And hide grammatical errors: - -.. code-block:: php - \PhpOffice\PhpWord\Settings::setGrammaticalErrorsHidden(true); diff --git a/src/PhpWord/ComplexType/ProofState.php b/src/PhpWord/ComplexType/ProofState.php new file mode 100644 index 00000000..e5ac9c20 --- /dev/null +++ b/src/PhpWord/ComplexType/ProofState.php @@ -0,0 +1,104 @@ +spelling = $spelling; + } else { + throw new \InvalidArgumentException("Invalid value, dirty or clean possible"); + } + return $this; + } + + /** + * Get the Spell Checking State + * + * @return string + */ + public function getSpelling() + { + return $this->spelling; + } + + /** + * Set the Grammatical Checking State (dirty or clean) + * + * @param string $grammar + * @throws \InvalidArgumentException + * @return self + */ + public function setGrammar($grammar) + { + if ($grammar == self::CLEAN || $grammar == self::DIRTY) { + $this->grammar = $grammar; + } else { + throw new \InvalidArgumentException("Invalid value, dirty or clean possible"); + } + return $this; + } + + /** + * Get the Grammatical Checking State + * + * @return string + */ + public function getGrammar() + { + return $this->grammar; + } +} diff --git a/src/PhpWord/ComplexType/TrackChangesView.php b/src/PhpWord/ComplexType/TrackChangesView.php new file mode 100644 index 00000000..ea26bc3c --- /dev/null +++ b/src/PhpWord/ComplexType/TrackChangesView.php @@ -0,0 +1,166 @@ +markup; + } + + /** + * Set Display Visual Indicator Of Markup Area + * + * @param boolean $markup + * Set to true to show markup + */ + public function setMarkup($markup) + { + $this->markup = $markup === null ? true : $markup; + } + + /** + * Get Display Comments + * + * @return boolean True if comments are shown + */ + public function hasComments() + { + return $this->comments; + } + + /** + * Set Display Comments + * + * @param boolean $comments + * Set to true to show comments + */ + public function setComments($comments) + { + $this->comments = $comments === null ? true : $comments; + } + + /** + * Get Display Content Revisions + * + * @return boolean True if content revisions are shown + */ + public function hasInsDel() + { + return $this->insDel; + } + + /** + * Set Display Content Revisions + * + * @param boolean $insDel + * Set to true to show content revisions + */ + public function setInsDel($insDel) + { + $this->insDel = $insDel === null ? true : $insDel; + } + + /** + * Get Display Formatting Revisions + * + * @return boolean True if formatting revisions are shown + */ + public function hasFormatting() + { + return $this->formatting; + } + + /** + * Set Display Formatting Revisions + * + * @param boolean $insDel + * Set to true to show formatting revisions + */ + public function setFormatting($formatting) + { + $this->formatting = $formatting === null ? true : $formatting; + } + + /** + * Get Display Ink Annotations + * + * @return boolean True if ink annotations are shown + */ + public function hasInkAnnotations() + { + return $this->inkAnnotations; + } + + /** + * Set Display Ink Annotations + * + * @param boolean $inkAnnotations + * Set to true to show ink annotations + */ + public function setInkAnnotations($inkAnnotations) + { + $this->inkAnnotations = $inkAnnotations === null ? true : $inkAnnotations; + } +} diff --git a/src/PhpWord/Element/Section.php b/src/PhpWord/Element/Section.php index 6e199bb9..3758af99 100644 --- a/src/PhpWord/Element/Section.php +++ b/src/PhpWord/Element/Section.php @@ -181,6 +181,11 @@ class Section extends AbstractContainer return true; } } + foreach ($this->footers as $footer) { + if ($footer->getType() == Header::FIRST) { + return true; + } + } return false; } diff --git a/src/PhpWord/Metadata/Settings.php b/src/PhpWord/Metadata/Settings.php index d1d1f0ce..9b2c2285 100644 --- a/src/PhpWord/Metadata/Settings.php +++ b/src/PhpWord/Metadata/Settings.php @@ -14,9 +14,12 @@ * @copyright 2010-2016 PHPWord contributors * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 */ - namespace PhpOffice\PhpWord\Metadata; +use PhpOffice\PhpWord\ComplexType\ProofState; +use PhpOffice\PhpWord\SimpleType\Zoom; +use PhpOffice\PhpWord\ComplexType\TrackChangesView; + /** * Setting class * @@ -26,24 +29,67 @@ namespace PhpOffice\PhpWord\Metadata; class Settings { + /** + * Magnification Setting + * + * @link http://www.datypic.com/sc/ooxml/e-w_zoom-1.html + * @var mixed either integer, in which case it treated as a percent, or one of PhpOffice\PhpWord\SimpleType\Zoom + */ + private $zoom = 100; + /** * Hide spelling errors - * + * * @var boolean */ private $hideSpellingErrors = false; /** * Hide grammatical errors - * + * * @var boolean */ private $hideGrammaticalErrors = false; + /** + * Visibility of Annotation Types + * + * @var TrackChangesView + */ + private $revisionView; + + /** + * Track Revisions to Document + * + * @var boolean + */ + private $trackRevisions = false; + + /** + * Do Not Use Move Syntax When Tracking Revisions + * + * @var boolean + */ + private $doNotTrackMoves = false; + + /** + * Do Not Track Formatting Revisions When Tracking Revisions + * + * @var boolean + */ + private $doNotTrackFormatting = false; + + /** + * Spelling and Grammatical Checking State + * + * @var \PhpOffice\PhpWord\Metadata\ProofState + */ + private $proofState; + /** * Document Editing Restrictions - * - * @var PhpOffice\PhpWord\Metadata\Protection + * + * @var \PhpOffice\PhpWord\Metadata\Protection */ private $documentProtection; @@ -54,6 +100,13 @@ class Settings */ private $evenAndOddHeaders = false; + /** + * Radix Point for Field Code Evaluation + * + * @var string + */ + private $decimalSymbol = '.'; + /** * @return Protection */ @@ -73,6 +126,25 @@ class Settings $this->documentProtection = $documentProtection; } + /** + * @return ProofState + */ + public function getProofState() + { + if ($this->proofState == null) { + $this->proofState = new ProofState(); + } + return $this->proofState; + } + + /** + * @param ProofState $proofState + */ + public function setProofState($proofState) + { + $this->proofState = $proofState; + } + /** * Are spelling errors hidden * @@ -128,4 +200,114 @@ class Settings { $this->evenAndOddHeaders = $evenAndOddHeaders === null ? true : $evenAndOddHeaders; } + + /** + * Get the Visibility of Annotation Types + * + * @return \PhpOffice\PhpWord\ComplexType\TrackChangesView + */ + public function getRevisionView() + { + return $this->revisionView; + } + + /** + * Set the Visibility of Annotation Types + * + * @param TrackChangesView $trackChangesView + */ + public function setRevisionView(TrackChangesView $trackChangesView = null) + { + $this->revisionView = $trackChangesView; + } + + /** + * @return boolean + */ + public function hasTrackRevisions() + { + return $this->trackRevisions; + } + + /** + * @param boolean $trackRevisions + */ + public function setTrackRevisions($trackRevisions) + { + $this->trackRevisions = $trackRevisions === null ? true : $trackRevisions; + } + + /** + * @return boolean + */ + public function hasDoNotTrackMoves() + { + return $this->doNotTrackMoves; + } + + /** + * @param boolean $doNotTrackMoves + */ + public function setDoNotTrackMoves($doNotTrackMoves) + { + $this->doNotTrackMoves = $doNotTrackMoves === null ? true : $doNotTrackMoves; + } + + /** + * @return boolean + */ + public function hasDoNotTrackFormatting() + { + return $this->doNotTrackFormatting; + } + + /** + * @param boolean $doNotTrackFormatting + */ + public function setDoNotTrackFormatting($doNotTrackFormatting) + { + $this->doNotTrackFormatting = $doNotTrackFormatting === null ? true : $doNotTrackFormatting; + } + + /** + * @return mixed + */ + public function getZoom() + { + return $this->zoom; + } + + /** + * @param mixed $zoom + */ + public function setZoom($zoom) + { + if (is_numeric($zoom)) { + // zoom is a percentage + $this->zoom = $zoom; + } else { + Zoom::validate($zoom); + $this->zoom = $zoom; + } + } + + /** + * Returns the Radix Point for Field Code Evaluation + * + * @return string + */ + public function getDecimalSymbol() + { + return $this->decimalSymbol; + } + + /** + * sets the Radix Point for Field Code Evaluation + * + * @param string $decimalSymbol + */ + public function setDecimalSymbol($decimalSymbol) + { + $this->decimalSymbol = $decimalSymbol; + } } diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index df0d6dcf..d2ffc1f3 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Reader\Word2007; use PhpOffice\Common\XMLReader; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\ComplexType\TrackChangesView; /** * Settings reader @@ -28,7 +29,7 @@ use PhpOffice\PhpWord\PhpWord; class Settings extends AbstractPart { - private static $booleanProperties = array('hideSpellingErrors', 'hideGrammaticalErrors', 'evenAndOddHeaders'); + private static $booleanProperties = array('hideSpellingErrors', 'hideGrammaticalErrors', 'trackRevisions', 'doNotTrackMoves', 'doNotTrackFormatting', 'evenAndOddHeaders'); /** * Read settings.xml. @@ -58,7 +59,7 @@ class Settings extends AbstractPart } } else if (method_exists($this, $method)) { $this->$method($xmlReader, $phpWord, $node); - } else if (method_exists($this, $method)) { + } else if (method_exists($docSettings, $method)) { $docSettings->$method($value); } } @@ -79,4 +80,61 @@ class Settings extends AbstractPart $edit = $xmlReader->getAttribute('w:edit', $node); $documentProtection->setEditing($edit); } + + /** + * Sets the proof state + * + * @param XMLReader $xmlReader + * @param PhpWord $phpWord + * @param \DOMNode $node + */ + protected function setProofState(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $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 + * @param \DOMNode $node + */ + protected function setZoom(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $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 + * @param \DOMNode $node + */ + protected function setRevisionView(XMLReader $xmlReader, PhpWord $phpWord, \DOMNode $node) + { + $revisionView = new TrackChangesView(); + $revisionView->setMarkup($xmlReader->getAttribute('w:markup', $node)); + $revisionView->setComments($xmlReader->getAttribute('w:comments', $node)); + $revisionView->setInsDel($xmlReader->getAttribute('w:insDel', $node)); + $revisionView->setFormatting($xmlReader->getAttribute('w:formatting', $node)); + $revisionView->setInkAnnotations($xmlReader->getAttribute('w:inkAnnotations', $node)); + $phpWord->getSettings()->setRevisionView($revisionView); + } } diff --git a/src/PhpWord/Shared/AbstractEnum.php b/src/PhpWord/Shared/AbstractEnum.php index 4584c2f9..f116e511 100644 --- a/src/PhpWord/Shared/AbstractEnum.php +++ b/src/PhpWord/Shared/AbstractEnum.php @@ -19,16 +19,39 @@ abstract class AbstractEnum return self::$constCacheArray[$calledClass]; } + /** + * Returns all values for this enum + * + * @return array + */ public static function values() { return array_values(self::getConstants()); } - public static function validate($value) + /** + * Returns true the value is valid for this enum + * + * @param strign $value + * @return boolean true if value is valid + */ + public static function isValid($value) { $values = array_values(self::getConstants()); - if (!in_array($value, $values, true)) { + return in_array($value, $values, true); + } + + /** + * Validates that the value passed is a valid value + * + * @param string $value + * @throws \InvalidArgumentException if the value passed is not valid for this enum + */ + public static function validate($value) + { + if (!self::isValid($value)) { $calledClass = get_called_class(); + $values = array_values(self::getConstants()); throw new \InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values)); } } diff --git a/src/PhpWord/SimpleType/Jc.php b/src/PhpWord/SimpleType/Jc.php index e90674a4..5c399a16 100644 --- a/src/PhpWord/SimpleType/Jc.php +++ b/src/PhpWord/SimpleType/Jc.php @@ -17,7 +17,7 @@ namespace PhpOffice\PhpWord\SimpleType; -use Zend\Validator\InArray; +use PhpOffice\PhpWord\Shared\AbstractEnum; /** * Horizontal Alignment Type. @@ -28,10 +28,11 @@ use Zend\Validator\InArray; * @since 0.13.0 * * @see \PhpOffice\PhpWord\SimpleType\JcTable For table alignment modes available since ISO/IEC-29500:2008. + * @link http://www.datypic.com/sc/ooxml/t-w_ST_Jc.html * * @codeCoverageIgnore */ -final class Jc +final class Jc extends AbstractEnum { const START = 'start'; const CENTER = 'center'; @@ -65,34 +66,4 @@ final class Jc * @deprecated 0.13.0 For documents based on ISO/IEC 29500:2008 and later use `BOTH` instead. */ const JUSTIFY = 'justify'; - - /** - * @since 0.13.0 - * - * @return \Zend\Validator\InArray - */ - final public static function getValidator() - { - // todo: consider caching validator instances. - return new InArray( - array ( - 'haystack' => array( - self::START, - self::CENTER, - self::END, - self::BOTH, - self::MEDIUM_KASHIDA, - self::DISTRIBUTE, - self::NUM_TAB, - self::HIGH_KASHIDA, - self::LOW_KASHIDA, - self::THAI_DISTRIBUTE, - self::LEFT, - self::RIGHT, - self::JUSTIFY, - ), - 'strict' => InArray::COMPARE_STRICT, - ) - ); - } } diff --git a/src/PhpWord/SimpleType/JcTable.php b/src/PhpWord/SimpleType/JcTable.php index d9648477..865b25a8 100644 --- a/src/PhpWord/SimpleType/JcTable.php +++ b/src/PhpWord/SimpleType/JcTable.php @@ -17,7 +17,7 @@ namespace PhpOffice\PhpWord\SimpleType; -use Zend\Validator\InArray; +use PhpOffice\PhpWord\Shared\AbstractEnum; /** * Table Alignment Type. @@ -28,25 +28,9 @@ use Zend\Validator\InArray; * * @codeCoverageIgnore */ -final class JcTable +final class JcTable extends AbstractEnum { const START = 'start'; const CENTER = 'center'; const END = 'end'; - - /** - * @since 0.13.0 - * - * @return \Zend\Validator\InArray - */ - final public static function getValidator() - { - // todo: consider caching validator instances. - return new InArray( - array ( - 'haystack' => array(self::START, self::CENTER, self::END), - 'strict' => InArray::COMPARE_STRICT, - ) - ); - } } diff --git a/src/PhpWord/SimpleType/Zoom.php b/src/PhpWord/SimpleType/Zoom.php new file mode 100644 index 00000000..3b78fdf9 --- /dev/null +++ b/src/PhpWord/SimpleType/Zoom.php @@ -0,0 +1,42 @@ +isValid($value)) { + if (Jc::isValid($value)) { $this->alignment = $value; } diff --git a/src/PhpWord/Style/NumberingLevel.php b/src/PhpWord/Style/NumberingLevel.php index 827d4354..38b9fbee 100644 --- a/src/PhpWord/Style/NumberingLevel.php +++ b/src/PhpWord/Style/NumberingLevel.php @@ -300,7 +300,7 @@ class NumberingLevel extends AbstractStyle */ public function setAlignment($value) { - if (Jc::getValidator()->isValid($value)) { + if (Jc::isValid($value)) { $this->alignment = $value; } diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index a9b53b2b..bad9ace9 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -240,7 +240,7 @@ class Paragraph extends Border */ public function setAlignment($value) { - if (Jc::getValidator()->isValid($value)) { + if (Jc::isValid($value)) { $this->alignment = $value; } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index 91809528..a542af7b 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -510,7 +510,7 @@ class Table extends Border */ public function setAlignment($value) { - if (JcTable::getValidator()->isValid($value) || Jc::getValidator()->isValid($value)) { + if (JcTable::isValid($value) || Jc::isValid($value)) { $this->alignment = $value; } diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 602205e9..529d47af 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -18,6 +18,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\Settings as DocumentSettings; +use PhpOffice\PhpWord\ComplexType\ProofState; +use PhpOffice\PhpWord\ComplexType\TrackChangesView; /** * Word2007 settings part writer: word/settings.xml @@ -99,14 +101,17 @@ class Settings extends AbstractPart */ private function getSettings() { + + /** @var \PhpOffice\PhpWord\Metadata\Settings $documentSettings */ + $documentSettings = $this->getParentWriter()->getPhpWord()->getSettings(); + // Default settings $this->settings = array( - 'w:zoom' => array('@attributes' => array('w:percent' => '100')), 'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')), 'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')), 'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')), 'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')), - 'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')), + 'w:decimalSymbol' => array('@attributes' => array('w:val' => $documentSettings->getDecimalSymbol())), 'w:listSeparator' => array('@attributes' => array('w:val' => ';')), 'w:compat' => array(), 'm:mathPr' => array( @@ -140,15 +145,17 @@ class Settings extends AbstractPart ), ); - /** @var \PhpOffice\PhpWord\Metadata\Settings $documentSettings */ - $documentSettings = $this->getParentWriter()->getPhpWord()->getSettings(); - $this->setOnOffValue('w:hideSpellingErrors', $documentSettings->hasHideSpellingErrors()); $this->setOnOffValue('w:hideGrammaticalErrors', $documentSettings->hasHideGrammaticalErrors()); + $this->setOnOffValue('w:trackRevisions', $documentSettings->hasTrackRevisions()); + $this->setOnOffValue('w:doNotTrackMoves', $documentSettings->hasDoNotTrackMoves()); + $this->setOnOffValue('w:doNotTrackFormatting', $documentSettings->hasDoNotTrackFormatting()); $this->setOnOffValue('w:evenAndOddHeaders', $documentSettings->hasEvenAndOddHeaders()); - // Other settings + $this->setRevisionView($documentSettings->getRevisionView()); $this->setDocumentProtection($documentSettings->getDocumentProtection()); + $this->setProofState($documentSettings->getProofState()); + $this->setZoom($documentSettings->getZoom()); $this->getCompatibility(); } @@ -161,7 +168,11 @@ class Settings extends AbstractPart private function setOnOffValue($settingName, $booleanValue) { if ($booleanValue !== null && is_bool($booleanValue)) { - $this->settings[$settingName] = array('@attributes' => array('w:val' => $booleanValue ? 'true': 'false')); + if ($booleanValue) { + $this->settings[$settingName] = array('@attributes' => array()); + } else { + $this->settings[$settingName] = array('@attributes' => array('w:val' => 'false')); + } } } @@ -183,6 +194,55 @@ class Settings extends AbstractPart } } + /** + * Set the Proof state + * + * @param ProofState $proofState + */ + private function setProofState(ProofState $proofState = null) + { + if ($proofState != null && $proofState->getGrammar() !== null && $proofState->getSpelling() !== null) { + $this->settings['w:proofState'] = array( + '@attributes' => array( + 'w:spelling' => $proofState->getSpelling(), + 'w:grammar' => $proofState->getGrammar() + ) + ); + } + } + + /** + * Set the Proof state + * + * @param ProofState $proofState + */ + private function setRevisionView(TrackChangesView $trackChangesView = null) + { + if ($trackChangesView != null) { + + $revisionView['w:markup'] = $trackChangesView->hasMarkup() ? 'true': 'false'; + $revisionView['w:comments'] = $trackChangesView->hasComments() ? 'true': 'false'; + $revisionView['w:insDel'] = $trackChangesView->hasInsDel() ? 'true': 'false'; + $revisionView['w:formatting'] = $trackChangesView->hasFormatting() ? 'true': 'false'; + $revisionView['w:inkAnnotations'] = $trackChangesView->hasInkAnnotations() ? 'true': 'false'; + + $this->settings['w:revisionView'] = array('@attributes' => $revisionView); + } + } + + /** + * Set the magnification + * + * @param mixed $zoom + */ + private function setZoom($zoom = null) + { + if ($zoom !== null) { + $attr = is_int($zoom) ? 'w:percent' : 'w:val'; + $this->settings['w:zoom'] = array('@attributes' => array($attr => $zoom)); + } + } + /** * Get compatibility setting. * diff --git a/tests/PhpWord/Metadata/SettingsTest.php b/tests/PhpWord/Metadata/SettingsTest.php index 35c15edd..fff51652 100644 --- a/tests/PhpWord/Metadata/SettingsTest.php +++ b/tests/PhpWord/Metadata/SettingsTest.php @@ -17,6 +17,9 @@ namespace PhpOffice\PhpWord\Metadata; +use PhpOffice\PhpWord\ComplexType\ProofState; +use PhpOffice\PhpWord\SimpleType\Zoom; + /** * Test class for PhpOffice\PhpWord\Metadata\Settings * @@ -66,4 +69,70 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $oSettings->getDocumentProtection()->setEditing('trackedChanges'); $this->assertEquals('trackedChanges', $oSettings->getDocumentProtection()->getEditing()); } + + /** + * TrackRevistions + */ + public function testTrackRevisions() + { + $oSettings = new Settings(); + $oSettings->setTrackRevisions(true); + $this->assertEquals(true, $oSettings->hasTrackRevisions()); + } + + /** + * DoNotTrackFormatting + */ + public function testDoNotTrackFormatting() + { + $oSettings = new Settings(); + $oSettings->setDoNotTrackFormatting(true); + $this->assertEquals(true, $oSettings->hasDoNotTrackFormatting()); + } + + /** + * DoNotTrackMoves + */ + public function testDoNotTrackMoves() + { + $oSettings = new Settings(); + $oSettings->setDoNotTrackMoves(true); + $this->assertEquals(true, $oSettings->hasDoNotTrackMoves()); + } + + /** + * ProofState + */ + public function testProofState() + { + $proofState = new ProofState(); + $proofState->setGrammar(ProofState::CLEAN); + $proofState->setSpelling(ProofState::DIRTY); + + $oSettings = new Settings(); + $oSettings->setProofState($proofState); + $this->assertNotNull($oSettings->getProofState()); + $this->assertEquals(ProofState::CLEAN, $oSettings->getProofState()->getGrammar()); + $this->assertEquals(ProofState::DIRTY, $oSettings->getProofState()->getSpelling()); + } + + /** + * Zoom as percentage + */ + public function testZoomPercentage() + { + $oSettings = new Settings(); + $oSettings->setZoom(75); + $this->assertEquals(75, $oSettings->getZoom()); + } + + /** + * Zoom as string + */ + public function testZoomEnum() + { + $oSettings = new Settings(); + $oSettings->setZoom(Zoom::FULL_PAGE); + $this->assertEquals('fullPage', $oSettings->getZoom()); + } } diff --git a/tests/PhpWord/Reader/MsDocTest.php b/tests/PhpWord/Reader/MsDocTest.php new file mode 100644 index 00000000..b4173d17 --- /dev/null +++ b/tests/PhpWord/Reader/MsDocTest.php @@ -0,0 +1,59 @@ +assertTrue($object->canRead($filename)); + } + + /** + * Can read exception + */ + public function testCanReadFailed() + { + $object = new MsDoc(); + $filename = __DIR__ . '/../_files/documents/foo.doc'; + $this->assertFalse($object->canRead($filename)); + } + + /** + * Load + */ + public function testLoad() + { + $filename = __DIR__ . '/../_files/documents/reader.doc'; + $phpWord = IOFactory::load($filename, 'MsDoc'); + $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); + } +} diff --git a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php index fe6ea61c..828e1283 100644 --- a/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWord/Writer/Word2007/Part/SettingsTest.php @@ -19,6 +19,8 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\TestHelperDOCX; use PhpOffice\PhpWord\Settings; +use PhpOffice\PhpWord\SimpleType\Zoom; +use PhpOffice\PhpWord\ComplexType\TrackChangesView; /** * Test class for PhpOffice\PhpWord\Writer\Word2007\Part\Settings @@ -102,7 +104,7 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertTrue($doc->elementExists($path, $file)); $element = $doc->getElement($path, $file); - $this->assertEquals('true', $element->getAttribute('w:val')); + $this->assertNotEquals('false', $element->getAttribute('w:val')); } /** @@ -121,6 +123,125 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $this->assertTrue($doc->elementExists($path, $file)); $element = $doc->getElement($path, $file); - $this->assertEquals('true', $element->getAttribute('w:val')); + $this->assertNotEquals('false', $element->getAttribute('w:val')); + } + + /** + * Test zoom percentage + */ + public function testZoomPercentage() + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setZoom(75); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:zoom'; + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertEquals('75', $element->getAttribute('w:percent')); + } + + /** + * Test zoom value + */ + public function testZoomValue() + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setZoom(Zoom::FULL_PAGE); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:zoom'; + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertEquals('fullPage', $element->getAttribute('w:val')); + } + + /** + * Test Revision View + */ + public function testRevisionView() + { + $trackChangesView = new TrackChangesView(); + $trackChangesView->setFormatting(false); + $trackChangesView->setComments(true); + + $phpWord = new PhpWord(); + $phpWord->getSettings()->setRevisionView($trackChangesView); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:revisionView'; + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertEquals('false', $element->getAttribute('w:formatting')); + $this->assertEquals('true', $element->getAttribute('w:comments')); + } + + /** + * Test track Revisions + */ + public function testTrackRevisions() + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setTrackRevisions(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:trackRevisions'; + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertNotEquals('false', $element->getAttribute('w:val')); + } + + /** + * Test doNotTrackMoves + */ + public function testDoNotTrackMoves() + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setDoNotTrackMoves(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:doNotTrackMoves'; + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertNotEquals('false', $element->getAttribute('w:val')); + } + + /** + * Test DoNotTrackFormatting + */ + public function testDoNotTrackFormatting() + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setDoNotTrackFormatting(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:doNotTrackFormatting'; + $this->assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + $this->assertNotEquals('false', $element->getAttribute('w:val')); } } diff --git a/tests/PhpWord/_files/documents/reader.doc b/tests/PhpWord/_files/documents/reader.doc new file mode 100644 index 00000000..a5ce295d Binary files /dev/null and b/tests/PhpWord/_files/documents/reader.doc differ diff --git a/tests/PhpWord/_files/documents/reader.docx b/tests/PhpWord/_files/documents/reader.docx index d09091b1..ef6b6615 100644 Binary files a/tests/PhpWord/_files/documents/reader.docx and b/tests/PhpWord/_files/documents/reader.docx differ