Word2007 Writer: Enable the missing custom document properties writer

This commit is contained in:
Ivan Lanin 2014-05-21 19:20:13 +07:00
parent 5188b36f22
commit 3569271277
10 changed files with 116 additions and 29 deletions

View File

@ -26,6 +26,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- PDF Writer: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin
- ODT Writer: Enable title element and custom document properties - @ivanlanin
- ODT Reader: Ability to read standard and custom document properties - @ivanlanin
- Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin
### Bugfixes

View File

@ -58,11 +58,6 @@ class XMLWriter
*/
public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
{
// Define date format
if (!defined('DATE_W3C')) {
define('DATE_W3C', 'Y-m-d\TH:i:sP');
}
// Create internal XMLWriter
$this->xmlWriter = new \XMLWriter();

View File

@ -28,6 +28,11 @@ use PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart as Word2007AbstractPart;
*/
abstract class AbstractPart extends Word2007AbstractPart
{
/**
* @var string Date format
*/
protected $dateFormat = 'Y-m-d\TH:i:s.000';
/**
* Write common root attributes
*/

View File

@ -53,12 +53,12 @@ class Meta extends AbstractPart
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
$xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy());
$xmlWriter->writeElement('dc:date', gmdate('Y-m-d\TH:i:s.000', $docProps->getModified()));
$xmlWriter->writeElement('dc:date', gmdate($this->dateFormat, $docProps->getModified()));
// Extended properties
$xmlWriter->writeElement('meta:generator', 'PHPWord');
$xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator());
$xmlWriter->writeElement('meta:creation-date', gmdate('Y-m-d\TH:i:s.000', $docProps->getCreated()));
$xmlWriter->writeElement('meta:creation-date', gmdate($this->dateFormat, $docProps->getCreated()));
$xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
// Category, company, and manager are put in meta namespace

View File

@ -57,6 +57,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
'Rels' => '_rels/.rels',
'DocPropsApp' => 'docProps/app.xml',
'DocPropsCore' => 'docProps/core.xml',
'DocPropsCustom' => 'docProps/custom.xml',
'RelsDocument' => 'word/_rels/document.xml.rels',
'Document' => 'word/document.xml',
'Styles' => 'word/styles.xml',

View File

@ -33,6 +33,11 @@ abstract class AbstractPart
*/
protected $parentWriter;
/**
* @var string Date format
*/
protected $dateFormat = 'Y-m-d\TH:i:sP';
/**
* Write part
*

View File

@ -40,6 +40,7 @@ class ContentTypes extends AbstractPart
$overrides = array(
'/docProps/core.xml' => $openXMLPrefix . 'package.core-properties+xml',
'/docProps/app.xml' => $openXMLPrefix . 'officedocument.extended-properties+xml',
'/docProps/custom.xml' => $openXMLPrefix . 'officedocument.custom-properties+xml',
'/word/document.xml' => $wordMLPrefix . 'document.main+xml',
'/word/styles.xml' => $wordMLPrefix . 'styles+xml',
'/word/numbering.xml' => $wordMLPrefix . 'numbering+xml',

View File

@ -54,13 +54,13 @@ class DocPropsCore extends AbstractPart
// dcterms:created
$xmlWriter->startElement('dcterms:created');
$xmlWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
$xmlWriter->writeRaw(date(DATE_W3C, $phpWord->getDocumentProperties()->getCreated()));
$xmlWriter->writeRaw(date($this->dateFormat, $phpWord->getDocumentProperties()->getCreated()));
$xmlWriter->endElement();
// dcterms:modified
$xmlWriter->startElement('dcterms:modified');
$xmlWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
$xmlWriter->writeRaw(date(DATE_W3C, $phpWord->getDocumentProperties()->getModified()));
$xmlWriter->writeRaw(date($this->dateFormat, $phpWord->getDocumentProperties()->getModified()));
$xmlWriter->endElement();
$xmlWriter->endElement(); // cp:coreProperties

View File

@ -0,0 +1,78 @@
<?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\Writer\Word2007\Part;
/**
* Word2007 custom document properties part writer: docProps/custom.xml
*
* @since 0.11.0
*/
class DocPropsCustom extends AbstractPart
{
/**
* Write part
*
* @return string
*/
public function write()
{
$phpWord = $this->getParentWriter()->getPhpWord();
$xmlWriter = $this->getXmlWriter();
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
$xmlWriter->startElement('Properties');
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
$xmlWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
$docProps = $phpWord->getDocumentProperties();
$properties = $docProps->getCustomProperties();
foreach ($properties as $key => $property) {
$propertyValue = $docProps->getCustomPropertyValue($property);
$propertyType = $docProps->getCustomPropertyType($property);
$xmlWriter->startElement('property');
$xmlWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
$xmlWriter->writeAttribute('pid', $key + 2);
$xmlWriter->writeAttribute('name', $property);
switch ($propertyType) {
case 'i':
$xmlWriter->writeElement('vt:i4', $propertyValue);
break;
case 'f':
$xmlWriter->writeElement('vt:r8', $propertyValue);
break;
case 'b':
$xmlWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
break;
case 'd':
$xmlWriter->startElement('vt:filetime');
$xmlWriter->writeRaw(date($this->dateFormat, $propertyValue));
$xmlWriter->endElement();
break;
default:
$xmlWriter->writeElement('vt:lpwstr', $propertyValue);
break;
}
$xmlWriter->endElement(); // property
}
$xmlWriter->endElement(); // Properties
return $xmlWriter->getData();
}
}

View File

@ -37,6 +37,7 @@ class Rels extends AbstractPart
$xmlRels = array(
'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties',
'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties',
'docProps/custom.xml' => 'officeDocument/2006/relationships/custom-properties',
'word/document.xml' => 'officeDocument/2006/relationships/officeDocument',
);
$xmlWriter = $this->getXmlWriter();