2012-05-06 18:25:40 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* 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.
|
2012-05-06 18:25:40 +02:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2012-05-06 18:25:40 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-04-30 08:54:38 +07:00
|
|
|
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-04-03 21:03:10 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
|
|
|
|
use PhpOffice\PhpWord\Shared\XMLWriter;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
2014-04-03 21:03:10 +07:00
|
|
|
* Word2007 relationship writer
|
|
|
|
|
*
|
2014-04-11 19:04:53 +07:00
|
|
|
* @since 0.10.0
|
2014-03-24 00:26:10 +07:00
|
|
|
*/
|
2014-04-30 08:54:38 +07:00
|
|
|
class Rels extends AbstractPart
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-04-03 21:03:10 +07:00
|
|
|
/**
|
|
|
|
|
* Base relationship URL
|
|
|
|
|
*/
|
|
|
|
|
const RELS_BASE = 'http://schemas.openxmlformats.org/';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Write word/_rels/(header|footer|footnotes)*.xml.rels
|
|
|
|
|
*
|
|
|
|
|
* @param array $mediaRels
|
|
|
|
|
*/
|
|
|
|
|
public function writeMediaRels($mediaRels)
|
|
|
|
|
{
|
|
|
|
|
$xmlWriter = $this->getXmlWriter();
|
|
|
|
|
$this->writeRels($xmlWriter, null, $mediaRels);
|
2012-05-06 18:25:40 +02:00
|
|
|
|
2014-04-03 21:03:10 +07:00
|
|
|
return $xmlWriter->getData();
|
|
|
|
|
}
|
2012-05-06 18:25:40 +02:00
|
|
|
|
2014-04-03 21:03:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write relationships
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
2014-04-05 19:02:49 +07:00
|
|
|
* @param null|array $xmlRels
|
2014-04-03 21:03:10 +07:00
|
|
|
* @param null|array $mediaRels
|
2014-04-22 17:25:10 +07:00
|
|
|
* @param integer $relId
|
2014-04-03 21:03:10 +07:00
|
|
|
*/
|
2014-05-06 01:45:13 +07:00
|
|
|
protected function writeRels(XMLWriter $xmlWriter, $xmlRels = null, $mediaRels = null, $relId = 1)
|
2014-04-03 21:03:10 +07:00
|
|
|
{
|
|
|
|
|
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
|
|
|
|
|
$xmlWriter->startElement('Relationships');
|
|
|
|
|
$xmlWriter->writeAttribute('xmlns', self::RELS_BASE . 'package/2006/relationships');
|
2014-04-05 19:02:49 +07:00
|
|
|
|
|
|
|
|
// XML files relationships
|
|
|
|
|
if (is_array($xmlRels)) {
|
|
|
|
|
foreach ($xmlRels as $target => $type) {
|
2014-04-22 17:25:10 +07:00
|
|
|
$this->writeRel($xmlWriter, $relId++, $type, $target);
|
2014-04-03 21:03:10 +07:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-05 19:02:49 +07:00
|
|
|
|
|
|
|
|
// Media relationships
|
2014-04-04 00:29:57 +07:00
|
|
|
if (!is_null($mediaRels) && is_array($mediaRels)) {
|
|
|
|
|
$mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink');
|
2014-04-18 23:12:51 +07:00
|
|
|
$targetPaths = array('image' => 'media/', 'object' => 'embeddings/');
|
2014-04-03 21:03:10 +07:00
|
|
|
foreach ($mediaRels as $mediaRel) {
|
2014-04-18 23:12:51 +07:00
|
|
|
$mediaType = $mediaRel['type'];
|
|
|
|
|
$type = array_key_exists($mediaType, $mapping) ? $mapping[$mediaType] : $mediaType;
|
|
|
|
|
$target = array_key_exists($mediaType, $targetPaths) ? $targetPaths[$mediaType] : '';
|
|
|
|
|
$target .= $mediaRel['target'];
|
2014-04-03 21:03:10 +07:00
|
|
|
$targetMode = ($type == 'hyperlink') ? 'External' : '';
|
2014-04-22 17:25:10 +07:00
|
|
|
$this->writeRel($xmlWriter, $relId++, "officeDocument/2006/relationships/{$type}", $target, $targetMode);
|
2014-04-03 21:03:10 +07:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-16 22:13:00 +07:00
|
|
|
|
|
|
|
|
$xmlWriter->endElement(); // Relationships
|
2014-04-03 21:03:10 +07:00
|
|
|
}
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-04-03 21:03:10 +07:00
|
|
|
/**
|
|
|
|
|
* Write individual rels entry
|
|
|
|
|
*
|
|
|
|
|
* Format:
|
|
|
|
|
* <Relationship Id="rId..." Type="http://..." Target="....xml" TargetMode="..." />
|
|
|
|
|
*
|
2014-04-16 17:22:30 +04:00
|
|
|
* @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
|
2014-04-22 17:25:10 +07:00
|
|
|
* @param int $relId Relationship ID
|
2014-04-03 21:03:10 +07:00
|
|
|
* @param string $type Relationship type
|
|
|
|
|
* @param string $target Relationship target
|
|
|
|
|
* @param string $targetMode Relationship target mode
|
|
|
|
|
*/
|
2014-04-22 17:25:10 +07:00
|
|
|
private function writeRel(XMLWriter $xmlWriter, $relId, $type, $target, $targetMode = '')
|
2014-04-03 21:03:10 +07:00
|
|
|
{
|
|
|
|
|
if ($type != '' && $target != '') {
|
2014-04-22 17:25:10 +07:00
|
|
|
if (strpos($relId, 'rId') === false) {
|
|
|
|
|
$relId = 'rId' . $relId;
|
2014-04-03 21:03:10 +07:00
|
|
|
}
|
|
|
|
|
$xmlWriter->startElement('Relationship');
|
2014-04-22 17:25:10 +07:00
|
|
|
$xmlWriter->writeAttribute('Id', $relId);
|
2014-04-03 21:03:10 +07:00
|
|
|
$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.");
|
|
|
|
|
}
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
2012-05-06 18:25:40 +02:00
|
|
|
}
|