Merge all Word2007 relationships writer

This commit is contained in:
Ivan Lanin 2014-04-03 21:03:10 +07:00
parent 1751ab09ee
commit 0fc1a06267
7 changed files with 123 additions and 208 deletions

View File

@ -10,7 +10,7 @@ $document = $phpWord->loadTemplate('resources/Sample_07_TemplateCloneRow.docx');
// Variables on different parts of document // Variables on different parts of document
$document->setValue('weekday', date('l')); // On section/content $document->setValue('weekday', date('l')); // On section/content
$document->setValue('time', date('H:i')); // On footer $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 // Simple table
$document->cloneRow('rowValue', 10); $document->cloneRow('rowValue', 10);

View File

@ -15,14 +15,12 @@ use PhpOffice\PhpWord\Footnote;
use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Writer\Word2007\ContentTypes; use PhpOffice\PhpWord\Writer\Word2007\ContentTypes;
use PhpOffice\PhpWord\Writer\Word2007\Rels;
use PhpOffice\PhpWord\Writer\Word2007\DocProps; use PhpOffice\PhpWord\Writer\Word2007\DocProps;
use PhpOffice\PhpWord\Writer\Word2007\Document; use PhpOffice\PhpWord\Writer\Word2007\Document;
use PhpOffice\PhpWord\Writer\Word2007\DocumentRels;
use PhpOffice\PhpWord\Writer\Word2007\Footer; use PhpOffice\PhpWord\Writer\Word2007\Footer;
use PhpOffice\PhpWord\Writer\Word2007\Footnotes; use PhpOffice\PhpWord\Writer\Word2007\Footnotes;
use PhpOffice\PhpWord\Writer\Word2007\FootnotesRels;
use PhpOffice\PhpWord\Writer\Word2007\Header; use PhpOffice\PhpWord\Writer\Word2007\Header;
use PhpOffice\PhpWord\Writer\Word2007\Rels;
use PhpOffice\PhpWord\Writer\Word2007\Styles; use PhpOffice\PhpWord\Writer\Word2007\Styles;
/** /**
@ -58,13 +56,11 @@ class Word2007 extends Writer implements IWriter
$this->writerParts['contenttypes'] = new ContentTypes(); $this->writerParts['contenttypes'] = new ContentTypes();
$this->writerParts['rels'] = new Rels(); $this->writerParts['rels'] = new Rels();
$this->writerParts['docprops'] = new DocProps(); $this->writerParts['docprops'] = new DocProps();
$this->writerParts['documentrels'] = new DocumentRels();
$this->writerParts['document'] = new Document(); $this->writerParts['document'] = new Document();
$this->writerParts['styles'] = new Styles(); $this->writerParts['styles'] = new Styles();
$this->writerParts['header'] = new Header(); $this->writerParts['header'] = new Header();
$this->writerParts['footer'] = new Footer(); $this->writerParts['footer'] = new Footer();
$this->writerParts['footnotes'] = new Footnotes(); $this->writerParts['footnotes'] = new Footnotes();
$this->writerParts['footnotesrels'] = new FootnotesRels();
foreach ($this->writerParts as $writer) { foreach ($this->writerParts as $writer) {
$writer->setParentWriter($this); $writer->setParentWriter($this);
} }
@ -118,7 +114,7 @@ class Word2007 extends Writer implements IWriter
if (count($hdrMedia) > 0) { if (count($hdrMedia) > 0) {
$objZip->addFromString( $objZip->addFromString(
'word/_rels/' . $hdrFile . '.xml.rels', 'word/_rels/' . $hdrFile . '.xml.rels',
$this->getWriterPart('documentrels')->writeHeaderFooterRels($hdrMedia) $this->getWriterPart('rels')->writeMediaRels($hdrMedia)
); );
foreach ($hdrMedia as $element) { foreach ($hdrMedia as $element) {
if ($element['type'] != 'hyperlink') { if ($element['type'] != 'hyperlink') {
@ -134,7 +130,7 @@ class Word2007 extends Writer implements IWriter
if (count($ftrMedia) > 0) { if (count($ftrMedia) > 0) {
$objZip->addFromString( $objZip->addFromString(
'word/_rels/' . $ftrFile . '.xml.rels', 'word/_rels/' . $ftrFile . '.xml.rels',
$this->getWriterPart('documentrels')->writeHeaderFooterRels($ftrMedia) $this->getWriterPart('rels')->writeMediaRels($ftrMedia)
); );
foreach ($ftrMedia as $element) { foreach ($ftrMedia as $element) {
if ($element['type'] != 'hyperlink') { if ($element['type'] != 'hyperlink') {
@ -198,7 +194,7 @@ class Word2007 extends Writer implements IWriter
if (!empty($footnoteMedia)) { if (!empty($footnoteMedia)) {
$objZip->addFromString( $objZip->addFromString(
'word/_rels/footnotes.xml.rels', '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 $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/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord));
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($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/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)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
// Write static files // Write static files

View File

@ -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 * Write inline paragraph style
* *

View File

@ -1,74 +0,0 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007;
/**
* Word2007 document rels part writer
*/
class DocumentRels extends Base
{
/**
* Write word/_rels/document.xml.rels
*
* @param array $relsCollection
*/
public function writeDocumentRels($relsCollection)
{
// Create XML writer
$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');
// 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();
}
}

View File

@ -1,33 +0,0 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace PhpOffice\PhpWord\Writer\Word2007;
/**
* Word2007 footnotes rel part writer
*/
class FootnotesRels extends Base
{
/**
* Write word/_rels/footnotes.xml.rels
*
* @param mixed $relsCollection
*/
public function writeFootnotesRels($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();
}
}

View File

@ -9,58 +9,134 @@
namespace PhpOffice\PhpWord\Writer\Word2007; 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 * Write _rels/.rels
* *
* @param PhpWord $phpWord * @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(); $xmlWriter = $this->getXmlWriter();
$this->writeRels($xmlWriter, $rels);
// 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();
return $xmlWriter->getData(); 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:
* <Relationship Id="rId..." Type="http://..." Target="....xml" TargetMode="..." />
*
* @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.");
}
}
} }

View File

@ -38,13 +38,11 @@ class Word2007Test extends \PHPUnit_Framework_TestCase
'ContentTypes', 'ContentTypes',
'Rels', 'Rels',
'DocProps', 'DocProps',
'DocumentRels',
'Document', 'Document',
'Styles', 'Styles',
'Header', 'Header',
'Footer', 'Footer',
'Footnotes', 'Footnotes',
'FootnotesRels',
); );
foreach ($writerParts as $part) { foreach ($writerParts as $part) {
$this->assertInstanceOf( $this->assertInstanceOf(