No Description

Rels.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace PhpOffice\PhpWord\Writer\Word2007\Part;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. use PhpOffice\PhpWord\Shared\XMLWriter;
  20. /**
  21. * Word2007 main relationship writer: _rels/.rels
  22. *
  23. * @since 0.10.0
  24. */
  25. class Rels extends AbstractPart
  26. {
  27. /**
  28. * Write part
  29. *
  30. * @return string
  31. */
  32. public function write()
  33. {
  34. $xmlRels = array(
  35. 'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties',
  36. 'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties',
  37. 'docProps/custom.xml' => 'officeDocument/2006/relationships/custom-properties',
  38. 'word/document.xml' => 'officeDocument/2006/relationships/officeDocument',
  39. );
  40. $xmlWriter = $this->getXmlWriter();
  41. $this->writeRels($xmlWriter, $xmlRels);
  42. return $xmlWriter->getData();
  43. }
  44. /**
  45. * Write relationships.
  46. *
  47. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  48. * @param array $xmlRels
  49. * @param array $mediaRels
  50. * @param int $relId
  51. * @return void
  52. */
  53. protected function writeRels(XMLWriter $xmlWriter, $xmlRels = array(), $mediaRels = array(), $relId = 1)
  54. {
  55. $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
  56. $xmlWriter->startElement('Relationships');
  57. $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  58. // XML files relationships
  59. foreach ($xmlRels as $target => $type) {
  60. $this->writeRel($xmlWriter, $relId++, $type, $target);
  61. }
  62. // Media relationships
  63. foreach ($mediaRels as $mediaRel) {
  64. $this->writeMediaRel($xmlWriter, $relId++, $mediaRel);
  65. }
  66. $xmlWriter->endElement(); // Relationships
  67. }
  68. /**
  69. * Write media relationships.
  70. *
  71. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  72. * @param int $relId
  73. * @param array $mediaRel
  74. * @return void
  75. */
  76. private function writeMediaRel(XMLWriter $xmlWriter, $relId, $mediaRel)
  77. {
  78. $typePrefix = 'officeDocument/2006/relationships/';
  79. $typeMapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink');
  80. $targetMapping = array('image' => 'media/', 'object' => 'embeddings/');
  81. $mediaType = $mediaRel['type'];
  82. $type = isset($typeMapping[$mediaType]) ? $typeMapping[$mediaType] : $mediaType;
  83. $targetPrefix = isset($targetMapping[$mediaType]) ? $targetMapping[$mediaType] : '';
  84. $target = $mediaRel['target'];
  85. $targetMode = ($type == 'hyperlink') ? 'External' : '';
  86. $this->writeRel($xmlWriter, $relId, $typePrefix . $type, $targetPrefix . $target, $targetMode);
  87. }
  88. /**
  89. * Write individual rels entry.
  90. *
  91. * Format:
  92. * <Relationship Id="rId..." Type="http://..." Target="....xml" TargetMode="..." />
  93. *
  94. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  95. * @param int $relId Relationship ID
  96. * @param string $type Relationship type
  97. * @param string $target Relationship target
  98. * @param string $targetMode Relationship target mode
  99. * @return void
  100. * @throws \PhpOffice\PhpWord\Exception\Exception
  101. */
  102. private function writeRel(XMLWriter $xmlWriter, $relId, $type, $target, $targetMode = '')
  103. {
  104. if ($type != '' && $target != '') {
  105. if (strpos($relId, 'rId') === false) {
  106. $relId = 'rId' . $relId;
  107. }
  108. $xmlWriter->startElement('Relationship');
  109. $xmlWriter->writeAttribute('Id', $relId);
  110. $xmlWriter->writeAttribute('Type', 'http://schemas.openxmlformats.org/' . $type);
  111. $xmlWriter->writeAttribute('Target', $target);
  112. if ($targetMode != '') {
  113. $xmlWriter->writeAttribute('TargetMode', $targetMode);
  114. }
  115. $xmlWriter->endElement();
  116. } else {
  117. throw new Exception("Invalid parameters passed.");
  118. }
  119. }
  120. }