From 0fc1a0626735558fd0234ae23bde0653b6a41079 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 3 Apr 2014 21:03:10 +0700 Subject: [PATCH] Merge all Word2007 relationships writer --- samples/Sample_07_TemplateCloneRow.php | 2 +- src/PhpWord/Writer/Word2007.php | 16 +- src/PhpWord/Writer/Word2007/Base.php | 48 ------ src/PhpWord/Writer/Word2007/DocumentRels.php | 74 --------- src/PhpWord/Writer/Word2007/FootnotesRels.php | 33 ---- src/PhpWord/Writer/Word2007/Rels.php | 156 +++++++++++++----- tests/PhpWord/Tests/Writer/Word2007Test.php | 2 - 7 files changed, 123 insertions(+), 208 deletions(-) delete mode 100755 src/PhpWord/Writer/Word2007/DocumentRels.php delete mode 100644 src/PhpWord/Writer/Word2007/FootnotesRels.php diff --git a/samples/Sample_07_TemplateCloneRow.php b/samples/Sample_07_TemplateCloneRow.php index 72464d84..789d22f0 100755 --- a/samples/Sample_07_TemplateCloneRow.php +++ b/samples/Sample_07_TemplateCloneRow.php @@ -10,7 +10,7 @@ $document = $phpWord->loadTemplate('resources/Sample_07_TemplateCloneRow.docx'); // Variables on different parts of document $document->setValue('weekday', date('l')); // On section/content $document->setValue('time', date('H:i')); // On footer -$document->setValue('serverName', $_SERVER['SERVER_NAME']); // On header +$document->setValue('serverName', realpath(__DIR__)); // On header // Simple table $document->cloneRow('rowValue', 10); diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index f86f30ac..7c87c2d4 100755 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -15,14 +15,12 @@ use PhpOffice\PhpWord\Footnote; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Writer\Word2007\ContentTypes; +use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\DocProps; use PhpOffice\PhpWord\Writer\Word2007\Document; -use PhpOffice\PhpWord\Writer\Word2007\DocumentRels; use PhpOffice\PhpWord\Writer\Word2007\Footer; use PhpOffice\PhpWord\Writer\Word2007\Footnotes; -use PhpOffice\PhpWord\Writer\Word2007\FootnotesRels; use PhpOffice\PhpWord\Writer\Word2007\Header; -use PhpOffice\PhpWord\Writer\Word2007\Rels; use PhpOffice\PhpWord\Writer\Word2007\Styles; /** @@ -58,13 +56,11 @@ class Word2007 extends Writer implements IWriter $this->writerParts['contenttypes'] = new ContentTypes(); $this->writerParts['rels'] = new Rels(); $this->writerParts['docprops'] = new DocProps(); - $this->writerParts['documentrels'] = new DocumentRels(); $this->writerParts['document'] = new Document(); $this->writerParts['styles'] = new Styles(); $this->writerParts['header'] = new Header(); $this->writerParts['footer'] = new Footer(); $this->writerParts['footnotes'] = new Footnotes(); - $this->writerParts['footnotesrels'] = new FootnotesRels(); foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } @@ -118,7 +114,7 @@ class Word2007 extends Writer implements IWriter if (count($hdrMedia) > 0) { $objZip->addFromString( 'word/_rels/' . $hdrFile . '.xml.rels', - $this->getWriterPart('documentrels')->writeHeaderFooterRels($hdrMedia) + $this->getWriterPart('rels')->writeMediaRels($hdrMedia) ); foreach ($hdrMedia as $element) { if ($element['type'] != 'hyperlink') { @@ -134,7 +130,7 @@ class Word2007 extends Writer implements IWriter if (count($ftrMedia) > 0) { $objZip->addFromString( 'word/_rels/' . $ftrFile . '.xml.rels', - $this->getWriterPart('documentrels')->writeHeaderFooterRels($ftrMedia) + $this->getWriterPart('rels')->writeMediaRels($ftrMedia) ); foreach ($ftrMedia as $element) { if ($element['type'] != 'hyperlink') { @@ -198,7 +194,7 @@ class Word2007 extends Writer implements IWriter if (!empty($footnoteMedia)) { $objZip->addFromString( 'word/_rels/footnotes.xml.rels', - $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnoteMedia) + $this->getWriterPart('rels')->writeMediaRels($footnoteMedia) ); } } @@ -214,11 +210,11 @@ class Word2007 extends Writer implements IWriter $footers ) ); - $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->phpWord)); + $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels()); $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord)); $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->phpWord)); + $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('rels')->writeDocRels($sectionElements)); $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord)); - $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); // Write static files diff --git a/src/PhpWord/Writer/Word2007/Base.php b/src/PhpWord/Writer/Word2007/Base.php index 50bd6425..f8873152 100644 --- a/src/PhpWord/Writer/Word2007/Base.php +++ b/src/PhpWord/Writer/Word2007/Base.php @@ -1070,54 +1070,6 @@ class Base extends WriterPart } } - /** - * Write media rels (image, embeddings, hyperlink) - * - * @param XMLWriter $xmlWriter - * @param array $mediaRels - */ - protected function writeMediaRels(XMLWriter $xmlWriter, $mediaRels) - { - $rTypePrefix = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'; - foreach ($mediaRels as $mediaRel) { - $rId = $mediaRel['rID']; - $rType = $mediaRel['type']; - $rName = $mediaRel['target']; // file name - $targetMode = ($rType == 'hyperlink') ? 'External' : ''; - $rType = $rTypePrefix . ($rType == 'embeddings' ? 'oleObject' : $rType); - $this->writeRel($xmlWriter, $rId, $rType, $rName, $targetMode); - } - - } - - /** - * Write individual rels entry - * - * @param XMLWriter $xmlWriter - * @param int $pId Relationship ID - * @param string $pType Relationship type - * @param string $pTarget Relationship target - * @param string $pTargetMode Relationship target mode - */ - protected function writeRel(XMLWriter $xmlWriter, $pId, $pType, $pTarget, $pTargetMode = '') - { - if ($pType != '' && $pTarget != '') { - if (strpos($pId, 'rId') === false) { - $pId = 'rId' . $pId; - } - $xmlWriter->startElement('Relationship'); - $xmlWriter->writeAttribute('Id', $pId); - $xmlWriter->writeAttribute('Type', $pType); - $xmlWriter->writeAttribute('Target', $pTarget); - if ($pTargetMode != '') { - $xmlWriter->writeAttribute('TargetMode', $pTargetMode); - } - $xmlWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); - } - } - /** * Write inline paragraph style * diff --git a/src/PhpWord/Writer/Word2007/DocumentRels.php b/src/PhpWord/Writer/Word2007/DocumentRels.php deleted file mode 100755 index f5ca69fc..00000000 --- a/src/PhpWord/Writer/Word2007/DocumentRels.php +++ /dev/null @@ -1,74 +0,0 @@ -getXmlWriter(); - - // XML header - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // Relationships - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - - // Write static files - $staticFiles = array( - 'styles' => 'styles.xml', - 'numbering' => 'numbering.xml', - 'settings' => 'settings.xml', - 'theme' => 'theme/theme1.xml', - 'webSettings' => 'webSettings.xml', - 'fontTable' => 'fontTable.xml', - ); - $i = 0; - foreach ($staticFiles as $type => $file) { - $i++; - $schema = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $type; - $this->writeRel($xmlWriter, $i, $schema, $file); - } - - // Write media relationship (image, oleObject, hyperlink) - $this->writeMediaRels($xmlWriter, $relsCollection); - - $xmlWriter->endElement(); // Relationships - - return $xmlWriter->getData(); - } - - /** - * Write header footer rels word/_rels/*.xml.rels - * - * @param array $relsCollection - */ - public function writeHeaderFooterRels($relsCollection) - { - $xmlWriter = $this->getXmlWriter(); - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - $this->writeMediaRels($xmlWriter, $relsCollection); - $xmlWriter->endElement(); - - return $xmlWriter->getData(); - } -} diff --git a/src/PhpWord/Writer/Word2007/FootnotesRels.php b/src/PhpWord/Writer/Word2007/FootnotesRels.php deleted file mode 100644 index e2cfd99f..00000000 --- a/src/PhpWord/Writer/Word2007/FootnotesRels.php +++ /dev/null @@ -1,33 +0,0 @@ -getXmlWriter(); - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - $this->writeMediaRels($xmlWriter, $relsCollection); - $xmlWriter->endElement(); - - return $xmlWriter->getData(); - } -} diff --git a/src/PhpWord/Writer/Word2007/Rels.php b/src/PhpWord/Writer/Word2007/Rels.php index d5476cf8..8f684c47 100755 --- a/src/PhpWord/Writer/Word2007/Rels.php +++ b/src/PhpWord/Writer/Word2007/Rels.php @@ -9,58 +9,134 @@ namespace PhpOffice\PhpWord\Writer\Word2007; -use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Shared\XMLWriter; /** - * Word2007 rels part writer + * Word2007 relationship writer + * + * @since 0.9.2 */ -class Rels extends Base +class Rels extends WriterPart { + /** + * Base relationship URL + */ + const RELS_BASE = 'http://schemas.openxmlformats.org/'; + /** * Write _rels/.rels * * @param PhpWord $phpWord */ - public function writeRelationships(PhpWord $phpWord = null) + public function writeMainRels() { - // Create XML writer + $rels = array( + 'word/document.xml' => 'officeDocument/2006/relationships/officeDocument', + 'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties', + 'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties', + ); $xmlWriter = $this->getXmlWriter(); - - // XML header - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - - // Relationships - $xmlWriter->startElement('Relationships'); - $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); - - $relationId = 1; - - // Relationship word/document.xml - $this->writeRel( - $xmlWriter, - $relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', - 'word/document.xml' - ); - - // Relationship docProps/core.xml - $this->writeRel( - $xmlWriter, - ++$relationId, - 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', - 'docProps/core.xml' - ); - - // Relationship docProps/app.xml - $this->writeRel( - $xmlWriter, - ++$relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', - 'docProps/app.xml' - ); - - $xmlWriter->endElement(); + $this->writeRels($xmlWriter, $rels); return $xmlWriter->getData(); } + + /** + * Write word/_rels/document.xml.rels + * + * @param array $mediaRels + */ + public function writeDocRels($mediaRels) + { + $rels = array( + 'styles.xml' => 'officeDocument/2006/relationships/styles', + 'numbering.xml' => 'officeDocument/2006/relationships/numbering', + 'settings.xml' => 'officeDocument/2006/relationships/settings', + 'theme/theme1.xml' => 'officeDocument/2006/relationships/theme', + 'webSettings.xml' => 'officeDocument/2006/relationships/webSettings', + 'fontTable.xml' => 'officeDocument/2006/relationships/fontTable', + ); + $xmlWriter = $this->getXmlWriter(); + $this->writeRels($xmlWriter, $rels, $mediaRels); + + return $xmlWriter->getData(); + } + + /** + * Write word/_rels/(header|footer|footnotes)*.xml.rels + * + * @param array $mediaRels + */ + public function writeMediaRels($mediaRels) + { + $xmlWriter = $this->getXmlWriter(); + $this->writeRels($xmlWriter, null, $mediaRels); + + return $xmlWriter->getData(); + } + + + /** + * Write relationships + * + * @param XMLWriter $xmlWriter + * @param null|array $rels + * @param null|array $mediaRels + * @param integer $id + */ + private function writeRels(XMLWriter $xmlWriter, $rels = null, $mediaRels = null, $id = 1) + { + $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); + $xmlWriter->startElement('Relationships'); + $xmlWriter->writeAttribute('xmlns', self::RELS_BASE . 'package/2006/relationships'); + if (is_array($rels)) { + foreach ($rels as $target => $type) { + $this->writeRel($xmlWriter, $id++, $type, $target); + } + } + if (is_array($mediaRels)) { + $typePrefix = 'officeDocument/2006/relationships/'; + foreach ($mediaRels as $mediaRel) { + $id = $mediaRel['rID']; + $type = $mediaRel['type']; + $target = $mediaRel['target']; // file name + $targetMode = ($type == 'hyperlink') ? 'External' : ''; + $type = $typePrefix . ($type == 'embeddings' ? 'oleObject' : $type); + $this->writeRel($xmlWriter, $id, $type, $target, $targetMode); + } + } + $xmlWriter->endElement(); + } + + /** + * Write individual rels entry + * + * Format: + * + * + * @param XMLWriter $xmlWriter + * @param int $id Relationship ID + * @param string $type Relationship type + * @param string $target Relationship target + * @param string $targetMode Relationship target mode + */ + private function writeRel(XMLWriter $xmlWriter, $id, $type, $target, $targetMode = '') + { + if ($type != '' && $target != '') { + if (strpos($id, 'rId') === false) { + $id = 'rId' . $id; + } + $xmlWriter->startElement('Relationship'); + $xmlWriter->writeAttribute('Id', $id); + $xmlWriter->writeAttribute('Type', self::RELS_BASE . $type); + $xmlWriter->writeAttribute('Target', $target); + if ($targetMode != '') { + $xmlWriter->writeAttribute('TargetMode', $targetMode); + } + $xmlWriter->endElement(); + } else { + throw new Exception("Invalid parameters passed."); + } + } } diff --git a/tests/PhpWord/Tests/Writer/Word2007Test.php b/tests/PhpWord/Tests/Writer/Word2007Test.php index 7b722a9c..fe80adb0 100644 --- a/tests/PhpWord/Tests/Writer/Word2007Test.php +++ b/tests/PhpWord/Tests/Writer/Word2007Test.php @@ -38,13 +38,11 @@ class Word2007Test extends \PHPUnit_Framework_TestCase 'ContentTypes', 'Rels', 'DocProps', - 'DocumentRels', 'Document', 'Styles', 'Header', 'Footer', 'Footnotes', - 'FootnotesRels', ); foreach ($writerParts as $part) { $this->assertInstanceOf(