ODT Reader: Ability to read standard and custom document properties

This commit is contained in:
Ivan Lanin 2014-05-20 18:00:00 +07:00
parent 930d8de462
commit ac23e90ef4
6 changed files with 96 additions and 6 deletions

View File

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

View File

@ -40,6 +40,7 @@ class ODText extends AbstractReader implements ReaderInterface
$readerParts = array( $readerParts = array(
'content.xml' => 'Content', 'content.xml' => 'Content',
'meta.xml' => 'Meta',
); );
foreach ($readerParts as $xmlFile => $partName) { foreach ($readerParts as $xmlFile => $partName) {

View File

@ -22,6 +22,8 @@ use PhpOffice\PhpWord\Shared\XMLReader;
/** /**
* Content reader * Content reader
*
* @since 0.10.0
*/ */
class Content extends AbstractPart class Content extends AbstractPart
{ {

View File

@ -0,0 +1,79 @@
<?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\Reader\ODText;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Meta reader
*
* @since 0.11.0
*/
class Meta extends AbstractPart
{
/**
* Read meta.xml
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @todo Process property type
*/
public function read(PhpWord &$phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocumentProperties();
$metaNode = $xmlReader->getElement('office:meta');
// Standard properties
$properties = array(
'title' => 'dc:title',
'subject' => 'dc:subject',
'description' => 'dc:description',
'keywords' => 'meta:keyword',
'creator' => 'meta:initial-creator',
'lastModifiedBy' => 'dc:creator',
// 'created' => 'meta:creation-date',
// 'modified' => 'dc:date',
);
foreach ($properties as $property => $path) {
$method = "set{$property}";
$propertyNode = $xmlReader->getElement($path, $metaNode);
if ($propertyNode !== null && method_exists($docProps, $method)) {
$docProps->$method($propertyNode->nodeValue);
}
}
// Custom properties
$propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode);
foreach ($propertyNodes as $propertyNode) {
$property = $xmlReader->getAttribute('meta:name', $propertyNode);
// Set category, company, and manager property
if (in_array($property, array('Category', 'Company', 'Manager'))) {
$method = "set{$property}";
$docProps->$method($propertyNode->nodeValue);
// Set other custom properties
} else {
$docProps->setCustomProperty($property, $propertyNode->nodeValue);
}
}
}
}

View File

@ -21,6 +21,8 @@ use PhpOffice\PhpWord\Shared\XMLWriter;
/** /**
* ODText meta part writer: meta.xml * ODText meta part writer: meta.xml
*
* @since 0.11.0
*/ */
class Meta extends AbstractPart class Meta extends AbstractPart
{ {
@ -47,19 +49,19 @@ class Meta extends AbstractPart
$xmlWriter->startElement('office:meta'); $xmlWriter->startElement('office:meta');
// Core properties // Core properties
$xmlWriter->writeElement('dc:title', $docProps->getTitle());
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
$xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy()); $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('Y-m-d\TH:i:s.000', $docProps->getModified()));
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
$xmlWriter->writeElement('dc:title', $docProps->getTitle());
// Extended properties // Extended properties
$xmlWriter->writeElement('meta:generator', 'PHPWord'); $xmlWriter->writeElement('meta:generator', 'PHPWord');
$xmlWriter->writeElement('meta:creation-date', gmdate('Y-m-d\TH:i:s.000', $docProps->getCreated()));
$xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator()); $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:keyword', $docProps->getKeywords()); $xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
// Category, company, and manager should be put in meta namespace // Category, company, and manager are put in meta namespace
$properties = array('Category', 'Company', 'Manager'); $properties = array('Category', 'Company', 'Manager');
foreach ($properties as $property) { foreach ($properties as $property) {
$method = "get{$property}"; $method = "get{$property}";
@ -87,12 +89,17 @@ class Meta extends AbstractPart
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
* @param string $property * @param string $property
* @param string $value * @param string $value
* @param string $type * @param string $type string (default/null)
*
* @todo Handle other `$type`: double|date|dateTime|duration|boolean
*/ */
private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value, $type = null) private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value, $type = null)
{ {
$xmlWriter->startElement('meta:user-defined'); $xmlWriter->startElement('meta:user-defined');
$xmlWriter->writeAttribute('meta:name', $property); $xmlWriter->writeAttribute('meta:name', $property);
if ($type !== null) {
$xmlWriter->writeAttribute('meta:value-type', $type);
}
$xmlWriter->writeRaw($value); $xmlWriter->writeRaw($value);
$xmlWriter->endElement(); // meta:user-defined $xmlWriter->endElement(); // meta:user-defined
} }