Skip to content

Commit 3569271

Browse files
committed
Word2007 Writer: Enable the missing custom document properties writer
1 parent 5188b36 commit 3569271

File tree

10 files changed

+116
-29
lines changed

10 files changed

+116
-29
lines changed

CHANGELOG.md

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

3031
### Bugfixes
3132

src/PhpWord/Shared/XMLWriter.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ class XMLWriter
5858
*/
5959
public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
6060
{
61-
// Define date format
62-
if (!defined('DATE_W3C')) {
63-
define('DATE_W3C', 'Y-m-d\TH:i:sP');
64-
}
65-
6661
// Create internal XMLWriter
6762
$this->xmlWriter = new \XMLWriter();
6863

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
*/
2929
abstract class AbstractPart extends Word2007AbstractPart
3030
{
31+
/**
32+
* @var string Date format
33+
*/
34+
protected $dateFormat = 'Y-m-d\TH:i:s.000';
35+
3136
/**
3237
* Write common root attributes
3338
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function write()
5353
$xmlWriter->writeElement('dc:subject', $docProps->getSubject());
5454
$xmlWriter->writeElement('dc:description', $docProps->getDescription());
5555
$xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy());
56-
$xmlWriter->writeElement('dc:date', gmdate('Y-m-d\TH:i:s.000', $docProps->getModified()));
56+
$xmlWriter->writeElement('dc:date', gmdate($this->dateFormat, $docProps->getModified()));
5757

5858
// Extended properties
5959
$xmlWriter->writeElement('meta:generator', 'PHPWord');
6060
$xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator());
61-
$xmlWriter->writeElement('meta:creation-date', gmdate('Y-m-d\TH:i:s.000', $docProps->getCreated()));
61+
$xmlWriter->writeElement('meta:creation-date', gmdate($this->dateFormat, $docProps->getCreated()));
6262
$xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
6363

6464
// Category, company, and manager are put in meta namespace

src/PhpWord/Writer/Word2007.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,24 @@ public function __construct(PhpWord $phpWord = null)
5353

5454
// Create parts
5555
$this->parts = array(
56-
'ContentTypes' => '[Content_Types].xml',
57-
'Rels' => '_rels/.rels',
58-
'DocPropsApp' => 'docProps/app.xml',
59-
'DocPropsCore' => 'docProps/core.xml',
60-
'RelsDocument' => 'word/_rels/document.xml.rels',
61-
'Document' => 'word/document.xml',
62-
'Styles' => 'word/styles.xml',
63-
'Numbering' => 'word/numbering.xml',
64-
'Settings' => 'word/settings.xml',
65-
'WebSettings' => 'word/webSettings.xml',
66-
'FontTable' => 'word/fontTable.xml',
67-
'Theme' => 'word/theme/theme1.xml',
68-
'RelsPart' => '',
69-
'Header' => '',
70-
'Footer' => '',
71-
'Footnotes' => '',
72-
'Endnotes' => '',
56+
'ContentTypes' => '[Content_Types].xml',
57+
'Rels' => '_rels/.rels',
58+
'DocPropsApp' => 'docProps/app.xml',
59+
'DocPropsCore' => 'docProps/core.xml',
60+
'DocPropsCustom' => 'docProps/custom.xml',
61+
'RelsDocument' => 'word/_rels/document.xml.rels',
62+
'Document' => 'word/document.xml',
63+
'Styles' => 'word/styles.xml',
64+
'Numbering' => 'word/numbering.xml',
65+
'Settings' => 'word/settings.xml',
66+
'WebSettings' => 'word/webSettings.xml',
67+
'FontTable' => 'word/fontTable.xml',
68+
'Theme' => 'word/theme/theme1.xml',
69+
'RelsPart' => '',
70+
'Header' => '',
71+
'Footer' => '',
72+
'Footnotes' => '',
73+
'Endnotes' => '',
7374
);
7475
foreach (array_keys($this->parts) as $partName) {
7576
$partClass = get_class($this) . '\\Part\\' . $partName;

src/PhpWord/Writer/Word2007/Part/AbstractPart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ abstract class AbstractPart
3333
*/
3434
protected $parentWriter;
3535

36+
/**
37+
* @var string Date format
38+
*/
39+
protected $dateFormat = 'Y-m-d\TH:i:sP';
40+
3641
/**
3742
* Write part
3843
*

src/PhpWord/Writer/Word2007/Part/ContentTypes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function write()
4040
$overrides = array(
4141
'/docProps/core.xml' => $openXMLPrefix . 'package.core-properties+xml',
4242
'/docProps/app.xml' => $openXMLPrefix . 'officedocument.extended-properties+xml',
43+
'/docProps/custom.xml' => $openXMLPrefix . 'officedocument.custom-properties+xml',
4344
'/word/document.xml' => $wordMLPrefix . 'document.main+xml',
4445
'/word/styles.xml' => $wordMLPrefix . 'styles+xml',
4546
'/word/numbering.xml' => $wordMLPrefix . 'numbering+xml',

src/PhpWord/Writer/Word2007/Part/DocPropsCore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ public function write()
5454
// dcterms:created
5555
$xmlWriter->startElement('dcterms:created');
5656
$xmlWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
57-
$xmlWriter->writeRaw(date(DATE_W3C, $phpWord->getDocumentProperties()->getCreated()));
57+
$xmlWriter->writeRaw(date($this->dateFormat, $phpWord->getDocumentProperties()->getCreated()));
5858
$xmlWriter->endElement();
5959

6060
// dcterms:modified
6161
$xmlWriter->startElement('dcterms:modified');
6262
$xmlWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF');
63-
$xmlWriter->writeRaw(date(DATE_W3C, $phpWord->getDocumentProperties()->getModified()));
63+
$xmlWriter->writeRaw(date($this->dateFormat, $phpWord->getDocumentProperties()->getModified()));
6464
$xmlWriter->endElement();
6565

6666
$xmlWriter->endElement(); // cp:coreProperties
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Writer\Word2007\Part;
19+
20+
/**
21+
* Word2007 custom document properties part writer: docProps/custom.xml
22+
*
23+
* @since 0.11.0
24+
*/
25+
class DocPropsCustom extends AbstractPart
26+
{
27+
/**
28+
* Write part
29+
*
30+
* @return string
31+
*/
32+
public function write()
33+
{
34+
$phpWord = $this->getParentWriter()->getPhpWord();
35+
$xmlWriter = $this->getXmlWriter();
36+
37+
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
38+
$xmlWriter->startElement('Properties');
39+
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
40+
$xmlWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
41+
42+
$docProps = $phpWord->getDocumentProperties();
43+
$properties = $docProps->getCustomProperties();
44+
foreach ($properties as $key => $property) {
45+
$propertyValue = $docProps->getCustomPropertyValue($property);
46+
$propertyType = $docProps->getCustomPropertyType($property);
47+
48+
$xmlWriter->startElement('property');
49+
$xmlWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
50+
$xmlWriter->writeAttribute('pid', $key + 2);
51+
$xmlWriter->writeAttribute('name', $property);
52+
switch ($propertyType) {
53+
case 'i':
54+
$xmlWriter->writeElement('vt:i4', $propertyValue);
55+
break;
56+
case 'f':
57+
$xmlWriter->writeElement('vt:r8', $propertyValue);
58+
break;
59+
case 'b':
60+
$xmlWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
61+
break;
62+
case 'd':
63+
$xmlWriter->startElement('vt:filetime');
64+
$xmlWriter->writeRaw(date($this->dateFormat, $propertyValue));
65+
$xmlWriter->endElement();
66+
break;
67+
default:
68+
$xmlWriter->writeElement('vt:lpwstr', $propertyValue);
69+
break;
70+
}
71+
$xmlWriter->endElement(); // property
72+
}
73+
74+
$xmlWriter->endElement(); // Properties
75+
76+
return $xmlWriter->getData();
77+
}
78+
}

src/PhpWord/Writer/Word2007/Part/Rels.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ class Rels extends AbstractPart
3535
public function write()
3636
{
3737
$xmlRels = array(
38-
'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties',
39-
'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties',
40-
'word/document.xml' => 'officeDocument/2006/relationships/officeDocument',
38+
'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties',
39+
'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties',
40+
'docProps/custom.xml' => 'officeDocument/2006/relationships/custom-properties',
41+
'word/document.xml' => 'officeDocument/2006/relationships/officeDocument',
4142
);
4243
$xmlWriter = $this->getXmlWriter();
4344
$this->writeRels($xmlWriter, $xmlRels);

0 commit comments

Comments
 (0)