Skip to content

Commit ac23e90

Browse files
committed
ODT Reader: Ability to read standard and custom document properties
1 parent 930d8de commit ac23e90

File tree

6 files changed

+96
-6
lines changed

6 files changed

+96
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
2525
- ODT Writer: Enable section and column - @ivanlanin
2626
- PDF Writer: Add TCPDF and mPDF as optional PDF renderer library - @ivanlanin
2727
- ODT Writer: Enable title element and custom document properties - @ivanlanin
28+
- ODT Reader: Ability to read standard and custom document properties - @ivanlanin
2829

2930
### Bugfixes
3031

17 Bytes
Binary file not shown.

src/PhpWord/Reader/ODText.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function load($docFile)
4040

4141
$readerParts = array(
4242
'content.xml' => 'Content',
43+
'meta.xml' => 'Meta',
4344
);
4445

4546
foreach ($readerParts as $xmlFile => $partName) {

src/PhpWord/Reader/ODText/Content.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
/**
2424
* Content reader
25+
*
26+
* @since 0.10.0
2527
*/
2628
class Content extends AbstractPart
2729
{

src/PhpWord/Reader/ODText/Meta.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Reader\ODText;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\Shared\XMLReader;
22+
23+
/**
24+
* Meta reader
25+
*
26+
* @since 0.11.0
27+
*/
28+
class Meta extends AbstractPart
29+
{
30+
/**
31+
* Read meta.xml
32+
*
33+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
34+
* @todo Process property type
35+
*/
36+
public function read(PhpWord &$phpWord)
37+
{
38+
$xmlReader = new XMLReader();
39+
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
40+
$docProps = $phpWord->getDocumentProperties();
41+
42+
$metaNode = $xmlReader->getElement('office:meta');
43+
44+
// Standard properties
45+
$properties = array(
46+
'title' => 'dc:title',
47+
'subject' => 'dc:subject',
48+
'description' => 'dc:description',
49+
'keywords' => 'meta:keyword',
50+
'creator' => 'meta:initial-creator',
51+
'lastModifiedBy' => 'dc:creator',
52+
// 'created' => 'meta:creation-date',
53+
// 'modified' => 'dc:date',
54+
);
55+
foreach ($properties as $property => $path) {
56+
$method = "set{$property}";
57+
$propertyNode = $xmlReader->getElement($path, $metaNode);
58+
if ($propertyNode !== null && method_exists($docProps, $method)) {
59+
$docProps->$method($propertyNode->nodeValue);
60+
}
61+
}
62+
63+
// Custom properties
64+
$propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode);
65+
foreach ($propertyNodes as $propertyNode) {
66+
$property = $xmlReader->getAttribute('meta:name', $propertyNode);
67+
68+
// Set category, company, and manager property
69+
if (in_array($property, array('Category', 'Company', 'Manager'))) {
70+
$method = "set{$property}";
71+
$docProps->$method($propertyNode->nodeValue);
72+
73+
// Set other custom properties
74+
} else {
75+
$docProps->setCustomProperty($property, $propertyNode->nodeValue);
76+
}
77+
}
78+
}
79+
}

src/PhpWord/Writer/ODText/Part/Meta.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* ODText meta part writer: meta.xml
24+
*
25+
* @since 0.11.0
2426
*/
2527
class Meta extends AbstractPart
2628
{
@@ -47,19 +49,19 @@ public function write()
4749
$xmlWriter->startElement('office:meta');
4850

4951
// Core properties
52+
$xmlWriter->writeElement('dc:title', $docProps->getTitle());
53+
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
54+
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
5055
$xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy());
5156
$xmlWriter->writeElement('dc:date', gmdate('Y-m-d\TH:i:s.000', $docProps->getModified()));
52-
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
53-
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
54-
$xmlWriter->writeElement('dc:title', $docProps->getTitle());
5557

5658
// Extended properties
5759
$xmlWriter->writeElement('meta:generator', 'PHPWord');
58-
$xmlWriter->writeElement('meta:creation-date', gmdate('Y-m-d\TH:i:s.000', $docProps->getCreated()));
5960
$xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator());
61+
$xmlWriter->writeElement('meta:creation-date', gmdate('Y-m-d\TH:i:s.000', $docProps->getCreated()));
6062
$xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
6163

62-
// Category, company, and manager should be put in meta namespace
64+
// Category, company, and manager are put in meta namespace
6365
$properties = array('Category', 'Company', 'Manager');
6466
foreach ($properties as $property) {
6567
$method = "get{$property}";
@@ -87,12 +89,17 @@ public function write()
8789
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
8890
* @param string $property
8991
* @param string $value
90-
* @param string $type
92+
* @param string $type string (default/null)
93+
*
94+
* @todo Handle other `$type`: double|date|dateTime|duration|boolean
9195
*/
9296
private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value, $type = null)
9397
{
9498
$xmlWriter->startElement('meta:user-defined');
9599
$xmlWriter->writeAttribute('meta:name', $property);
100+
if ($type !== null) {
101+
$xmlWriter->writeAttribute('meta:value-type', $type);
102+
}
96103
$xmlWriter->writeRaw($value);
97104
$xmlWriter->endElement(); // meta:user-defined
98105
}

0 commit comments

Comments
 (0)