DocumentRels.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. class PHPWord_Writer_Word2007_DocumentRels extends PHPWord_Writer_Word2007_WriterPart {
  28. public function writeDocumentRels($_relsCollection) {
  29. // Create XML writer
  30. $objWriter = null;
  31. if ($this->getParentWriter()->getUseDiskCaching()) {
  32. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  33. } else {
  34. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
  35. }
  36. // XML header
  37. $objWriter->startDocument('1.0','UTF-8','yes');
  38. // Relationships
  39. $objWriter->startElement('Relationships');
  40. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  41. // Relationship word/document.xml
  42. $this->_writeRelationship(
  43. $objWriter,
  44. 1,
  45. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  46. 'styles.xml'
  47. );
  48. // Relationship word/numbering.xml
  49. $this->_writeRelationship(
  50. $objWriter,
  51. 2,
  52. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering',
  53. 'numbering.xml'
  54. );
  55. // Relationship word/settings.xml
  56. $this->_writeRelationship(
  57. $objWriter,
  58. 3,
  59. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings',
  60. 'settings.xml'
  61. );
  62. // Relationship word/settings.xml
  63. $this->_writeRelationship(
  64. $objWriter,
  65. 4,
  66. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  67. 'theme/theme1.xml'
  68. );
  69. // Relationship word/settings.xml
  70. $this->_writeRelationship(
  71. $objWriter,
  72. 5,
  73. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings',
  74. 'webSettings.xml'
  75. );
  76. // Relationship word/settings.xml
  77. $this->_writeRelationship(
  78. $objWriter,
  79. 6,
  80. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
  81. 'fontTable.xml'
  82. );
  83. // Relationships to Images / Embeddings / Headers / Footers
  84. foreach($_relsCollection as $relation) {
  85. $relationType = $relation['type'];
  86. $relationName = $relation['target'];
  87. $relationId = $relation['rID'];
  88. $targetMode = ($relationType == 'hyperlink') ? 'External' : '';
  89. $this->_writeRelationship(
  90. $objWriter,
  91. $relationId,
  92. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'.$relationType,
  93. $relationName,
  94. $targetMode
  95. );
  96. }
  97. $objWriter->endElement();
  98. // Return
  99. return $objWriter->getData();
  100. }
  101. public function writeHeaderFooterRels($_relsCollection) {
  102. // Create XML writer
  103. $objWriter = null;
  104. if ($this->getParentWriter()->getUseDiskCaching()) {
  105. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  106. } else {
  107. $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY);
  108. }
  109. // XML header
  110. $objWriter->startDocument('1.0','UTF-8','yes');
  111. // Relationships
  112. $objWriter->startElement('Relationships');
  113. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  114. // Relationships to Images / Embeddings / Headers / Footers
  115. foreach($_relsCollection as $relation) {
  116. $relationType = $relation['type'];
  117. $relationName = $relation['target'];
  118. $relationId = $relation['rID'];
  119. $this->_writeRelationship(
  120. $objWriter,
  121. $relationId,
  122. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'.$relationType,
  123. $relationName
  124. );
  125. }
  126. $objWriter->endElement();
  127. // Return
  128. return $objWriter->getData();
  129. }
  130. private function _writeRelationship(PHPWord_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') {
  131. if($pType != '' && $pTarget != '') {
  132. if(strpos($pId, 'rId') === false) {
  133. $pId = 'rId' . $pId;
  134. }
  135. // Write relationship
  136. $objWriter->startElement('Relationship');
  137. $objWriter->writeAttribute('Id', $pId);
  138. $objWriter->writeAttribute('Type', $pType);
  139. $objWriter->writeAttribute('Target', $pTarget);
  140. if($pTargetMode != '') {
  141. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  142. }
  143. $objWriter->endElement();
  144. } else {
  145. throw new Exception("Invalid parameters passed.");
  146. }
  147. }
  148. }
  149. ?>