Rels.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_Rels extends PHPWord_Writer_Word2007_WriterPart {
  28. public function writeRelationships(PHPWord $pPHPWord = null) {
  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. $relationId = 1;
  42. // Relationship word/document.xml
  43. $this->_writeRelationship(
  44. $objWriter,
  45. $relationId,
  46. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  47. 'word/document.xml'
  48. );
  49. // Relationship docProps/core.xml
  50. $this->_writeRelationship(
  51. $objWriter,
  52. ++$relationId,
  53. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  54. 'docProps/core.xml'
  55. );
  56. // Relationship docProps/app.xml
  57. $this->_writeRelationship(
  58. $objWriter,
  59. ++$relationId,
  60. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  61. 'docProps/app.xml'
  62. );
  63. $objWriter->endElement();
  64. // Return
  65. return $objWriter->getData();
  66. }
  67. /**
  68. * Write Override content type
  69. *
  70. * @param PHPWord_Shared_XMLWriter $objWriter XML Writer
  71. * @param int $pId Relationship ID. rId will be prepended!
  72. * @param string $pType Relationship type
  73. * @param string $pTarget Relationship target
  74. * @param string $pTargetMode Relationship target mode
  75. * @throws Exception
  76. */
  77. private function _writeRelationship(PHPWord_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') {
  78. if($pType != '' && $pTarget != '') {
  79. if(strpos($pId, 'rId') === false) {
  80. $pId = 'rId' . $pId;
  81. }
  82. // Write relationship
  83. $objWriter->startElement('Relationship');
  84. $objWriter->writeAttribute('Id', $pId);
  85. $objWriter->writeAttribute('Type', $pType);
  86. $objWriter->writeAttribute('Target', $pTarget);
  87. if($pTargetMode != '') {
  88. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  89. }
  90. $objWriter->endElement();
  91. } else {
  92. throw new Exception("Invalid parameters passed.");
  93. }
  94. }
  95. }