Settings: OOXML compatibility

This commit is contained in:
Ivan Lanin 2014-06-21 03:07:11 +07:00
parent 0ea2193906
commit 5be8414ef8
8 changed files with 167 additions and 67 deletions

View File

@ -18,7 +18,8 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
- Chart: Basic 2D chart (pie, doughnut, bar, line, area, scatter, radar) - @ivanlanin GH-278
- Chart: 3D charts and ability to set width and height - @ivanlanin
- FormField: Ability to add textinput, checkbox, and dropdown form elements - @ivanlanin GH-266
- Security: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin
- Setting: Ability to define document protection (readOnly, comments, trackedChanges, forms) - @ivanlanin
- Setting: Ability to remove [Compatibility Mode] text in the MS Word title bar - @ivanlanin
### Bugfixes

View File

@ -87,3 +87,12 @@ Apply 'HeadingN' paragraph style to TextRun or Link. Sample code:
// Link
$section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2');
Remove [Compatibility Mode] text in the MS Word title bar
---------------------------------------------------------
Use the ``Metadata\Compatibility\setOoxmlVersion(n)`` method with ``n`` is the version of Office (14 = Office 2010, 15 = Office 2013).
.. code-block:: php
$phpWord->getCompatibility()->setOoxmlVersion(15);

View File

@ -1053,6 +1053,14 @@ $textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link');
$section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2');
```
## Remove [Compatibility Mode] text in the MS Word title bar
Use the `Metadata\Compatibility\setOoxmlVersion(n)` method with `n` is the version of Office (14 = Office 2010, 15 = Office 2013).
```php
$phpWord->getCompatibility()->setOoxmlVersion(15);
```
# Frequently asked questions
## Is this the same with PHPWord that I found in CodePlex?

View File

@ -0,0 +1,62 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Metadata;
/**
* Compatibility setting class
*
* @since 0.12.0
* @link http://www.datypic.com/sc/ooxml/t-w_CT_Compat.html
*/
class Compatibility
{
/**
* OOXML version
*
* 12 = 2007
* 14 = 2010
* 15 = 2013
*
* @var int
* @link http://msdn.microsoft.com/en-us/library/dd909048%28v=office.12%29.aspx
*/
private $ooxmlVersion = 12;
/**
* Get OOXML version
*
* @return int
*/
public function getOoxmlVersion()
{
return $this->ooxmlVersion;
}
/**
* Set OOXML version
*
* @param int $value
* @return self
*/
public function setOoxmlVersion($value)
{
$this->ooxmlVersion = $value;
return $this;
}
}

View File

@ -81,17 +81,19 @@ class PhpWord
*/
public function __construct()
{
// Collection
$collections = array('Titles', 'Footnotes', 'Endnotes', 'Charts');
foreach ($collections as $collection) {
$class = 'PhpOffice\\PhpWord\\Collection\\' . $collection;
$this->collections[$collection] = new $class();
}
$metadata = 'PhpOffice\\PhpWord\\Metadata\\Protection';
$this->metadata['Protection'] = new $metadata();
$metadata = 'PhpOffice\\PhpWord\\Metadata\\DocInfo';
$this->metadata['DocInfo'] = new $metadata();
// Metadata
$metadata = array('DocInfo', 'Protection', 'Compatibility');
foreach ($metadata as $meta) {
$class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta;
$this->metadata[$meta] = new $class();
}
}
/**
@ -147,9 +149,6 @@ class PhpWord
if (in_array($function, $addStyle)) {
return forward_static_call_array(array('PhpOffice\\PhpWord\\Style', $function), $args);
}
// All other methods
return null;
}
/**
@ -173,6 +172,17 @@ class PhpWord
return $this->metadata['Protection'];
}
/**
* Get compatibility
*
* @return \PhpOffice\PhpWord\Metadata\Compatibility
* @since 0.12.0
*/
public function getCompatibility()
{
return $this->metadata['Compatibility'];
}
/**
* Get all sections
*
@ -331,7 +341,7 @@ class PhpWord
/**
* Set document properties object
*
* @param \PhpOffice\PhpWord\Metadata\DocInfo
* @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties
* @return self
* @deprecated 0.12.0
* @codeCoverageIgnore

View File

@ -24,6 +24,13 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part;
*/
class Settings extends AbstractPart
{
/**
* Settings value
*
* @var array
*/
private $settings = array();
/**
* Write part
*
@ -31,7 +38,7 @@ class Settings extends AbstractPart
*/
public function write()
{
$settings = $this->getSettings();
$this->getSettings();
$xmlWriter = $this->getXmlWriter();
@ -45,7 +52,7 @@ class Settings extends AbstractPart
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
foreach ($settings as $settingKey => $settingValue) {
foreach ($this->settings as $settingKey => $settingValue) {
$this->writeSetting($xmlWriter, $settingKey, $settingValue);
}
@ -84,63 +91,19 @@ class Settings extends AbstractPart
/**
* Get settings
*
* @return array
*/
private function getSettings()
{
// Default settings
$settings = $this->getDefaultSettings();
// Protection
$protection = $this->getParentWriter()->getPhpWord()->getProtection();
if ($protection->getEditing() !== null) {
$settings['w:documentProtection'] = array(
'@attributes' => array(
'w:enforcement' => 1,
'w:edit' => $protection->getEditing(),
)
);
}
return $settings;
}
/**
* Get default settings
*
* @return array
*/
private function getDefaultSettings()
{
return array(
$this->settings = array(
'w:zoom' => array('@attributes' => array('w:percent' => '100')),
'w:view' => array('@attributes' => array('w:val' => 'print')),
'w:embedSystemFonts' => '',
'w:defaultTabStop' => array('@attributes' => array('w:val' => '708')),
'w:hyphenationZone' => array('@attributes' => array('w:val' => '425')),
'w:doNotHyphenateCaps' => '',
'w:characterSpacingControl' => array('@attributes' => array('w:val' => 'doNotCompress')),
'w:doNotValidateAgainstSchema' => '',
'w:doNotDemarcateInvalidXml' => '',
'w:compat' => array(
'w:useNormalStyleForList' => '',
'w:doNotUseIndentAsNumberingTabStop' => '',
'w:useAltKinsokuLineBreakRules' => '',
'w:allowSpaceOfSameStyleInTable' => '',
'w:doNotSuppressIndentation' => '',
'w:doNotAutofitConstrainedTables' => '',
'w:autofitToFirstFixedWidthCell' => '',
'w:underlineTabInNumList' => '',
'w:displayHangulFixedWidth' => '',
// Commented for GH-274
// 'w:splitPgBreakAndParaMark' => '',
'w:doNotVertAlignCellWithSp' => '',
'w:doNotBreakConstrainedForcedTable' => '',
'w:doNotVertAlignInTxbx' => '',
'w:useAnsiKerningPairs' => '',
'w:cachedColBalance' => '',
),
'w:themeFontLang' => array('@attributes' => array('w:val' => 'en-US')),
'w:decimalSymbol' => array('@attributes' => array('w:val' => '.')),
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
'w:compat' => '',
'm:mathPr' => array(
'm:mathFont' => array('@attributes' => array('m:val' => 'Cambria Math')),
'm:brkBin' => array('@attributes' => array('m:val' => 'before')),
@ -154,8 +117,6 @@ class Settings extends AbstractPart
'm:intLim' => array('@attributes' => array('m:val' => 'subSup')),
'm:naryLim' => array('@attributes' => array('m:val' => 'undOvr')),
),
'w:uiCompat97To2003' => '',
'w:themeFontLang' => array('@attributes' => array('w:val' => 'de-DE')),
'w:clrSchemeMapping' => array(
'@attributes' => array(
'w:bg1' => 'light1',
@ -172,10 +133,41 @@ class Settings extends AbstractPart
'w:followedHyperlink' => 'followedHyperlink',
),
),
'w:doNotIncludeSubdocsInStats' => '',
'w:doNotAutoCompressPictures' => '',
'w:decimalSymbol' => array('@attributes' => array('w:val' => ',')),
'w:listSeparator' => array('@attributes' => array('w:val' => ';')),
);
// Other settings
$this->getProtection();
$this->getCompatibility();
}
/**
* Get protection settings
*/
private function getProtection()
{
$protection = $this->getParentWriter()->getPhpWord()->getProtection();
if ($protection->getEditing() !== null) {
$this->settings['w:documentProtection'] = array(
'@attributes' => array(
'w:enforcement' => 1,
'w:edit' => $protection->getEditing(),
)
);
}
}
/**
* Get compatibility setting
*/
private function getCompatibility()
{
$compatibility = $this->getParentWriter()->getPhpWord()->getCompatibility();
if ($compatibility->getOoxmlVersion() !== null) {
$this->settings['w:compat']['w:compatSetting'] = array('@attributes' => array(
'w:name' => 'compatibilityMode',
'w:uri' => 'http://schemas.microsoft.com/office/word',
'w:val' => $compatibility->getOoxmlVersion(),
));
}
}
}

View File

@ -75,6 +75,8 @@ class StyleTest extends \PHPUnit_Framework_TestCase
}
/**
* Test default paragraph style
*
* @covers ::setDefaultParagraphStyle
* @test
*/

View File

@ -49,4 +49,20 @@ class SettingsTest extends \PHPUnit_Framework_TestCase
$path = '/w:settings/w:documentProtection';
$this->assertTrue($doc->elementExists($path, $file));
}
/**
* Test compatibility
*/
public function testCompatibility()
{
$phpWord = new PhpWord();
$phpWord->getCompatibility()->setOoxmlVersion(15);
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/settings.xml';
$path = '/w:settings/w:compat/w:compatSetting';
$this->assertTrue($doc->elementExists($path, $file));
}
}