/branches/v1.3.0/Classes/PHPExcel/Writer/Excel2007/Rels.php

# · PHP · 278 lines · 140 code · 37 blank · 101 comment · 17 complexity · 0a4ab0b3721450bb3143fcdd6feb4c98 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel, Maarten Balliauw
  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 PHPExcel
  22. * @package PHPExcel_Writer_Excel2007
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel */
  28. require_once 'PHPExcel.php';
  29. /** PHPExcel_Worksheet */
  30. require_once 'PHPExcel/Worksheet.php';
  31. /** PHPExcel_Writer_Excel2007 */
  32. require_once 'PHPExcel/Writer/Excel2007.php';
  33. /** PHPExcel_Writer_Excel2007_WriterPart */
  34. require_once 'PHPExcel/Writer/Excel2007/WriterPart.php';
  35. /**
  36. * PHPExcel_Writer_Excel2007_Rels
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Writer_Excel2007
  40. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
  43. {
  44. /**
  45. * Write relationships to XML format
  46. *
  47. * @param PHPExcel $pPHPExcel
  48. * @return string XML Output
  49. * @throws Exception
  50. */
  51. public function writeRelationships($pPHPExcel = null)
  52. {
  53. if ($pPHPExcel instanceof PHPExcel) {
  54. // Create XML writer
  55. $objWriter = new xmlWriter();
  56. $objWriter->openMemory();
  57. // XML header
  58. $objWriter->startDocument('1.0','UTF-8','yes');
  59. // Relationships
  60. $objWriter->startElement('Relationships');
  61. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  62. // Relationship docProps/app.xml
  63. $this->_writeRelationship(
  64. $objWriter,
  65. 3,
  66. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  67. 'docProps/app.xml'
  68. );
  69. // Relationship docProps/core.xml
  70. $this->_writeRelationship(
  71. $objWriter,
  72. 2,
  73. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  74. 'docProps/core.xml'
  75. );
  76. // Relationship xl/workbook.xml
  77. $this->_writeRelationship(
  78. $objWriter,
  79. 1,
  80. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  81. 'xl/workbook.xml'
  82. );
  83. $objWriter->endElement();
  84. // Return
  85. return $objWriter->outputMemory(true);
  86. } else {
  87. throw new Exception("Invalid PHPExcel object passed.");
  88. }
  89. }
  90. /**
  91. * Write workbook relationships to XML format
  92. *
  93. * @param PHPExcel $pPHPExcel
  94. * @return string XML Output
  95. * @throws Exception
  96. */
  97. public function writeWorkbookRelationships($pPHPExcel = null)
  98. {
  99. if ($pPHPExcel instanceof PHPExcel) {
  100. // Create XML writer
  101. $objWriter = new xmlWriter();
  102. $objWriter->openMemory();
  103. // XML header
  104. $objWriter->startDocument('1.0','UTF-8','yes');
  105. // Relationships
  106. $objWriter->startElement('Relationships');
  107. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  108. // Relationship styles.xml
  109. $this->_writeRelationship(
  110. $objWriter,
  111. 1,
  112. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  113. 'styles.xml'
  114. );
  115. // Relationship theme/theme1.xml
  116. $this->_writeRelationship(
  117. $objWriter,
  118. 2,
  119. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  120. 'theme/theme1.xml'
  121. );
  122. // Relationship sharedStrings.xml
  123. $this->_writeRelationship(
  124. $objWriter,
  125. 3,
  126. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  127. 'sharedStrings.xml'
  128. );
  129. // Relationships with sheets
  130. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  131. $this->_writeRelationship(
  132. $objWriter,
  133. ($i + 1 + 3),
  134. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  135. 'worksheets/sheet' . ($i + 1) . '.xml'
  136. );
  137. }
  138. $objWriter->endElement();
  139. // Return
  140. return $objWriter->outputMemory(true);
  141. } else {
  142. throw new Exception("Invalid PHPExcel object passed.");
  143. }
  144. }
  145. /**
  146. * Write worksheet relationships to XML format
  147. *
  148. * @param PHPExcel_Worksheet $pWorksheet
  149. * @param int $pWorksheetId
  150. * @return string XML Output
  151. * @throws Exception
  152. */
  153. public function writeWorksheetRelationships($pWorksheet = null, $pWorksheetId = 1)
  154. {
  155. if ($pWorksheet instanceof PHPExcel_Worksheet) {
  156. // Create XML writer
  157. $objWriter = new xmlWriter();
  158. $objWriter->openMemory();
  159. // XML header
  160. $objWriter->startDocument('1.0','UTF-8','yes');
  161. // Relationships
  162. $objWriter->startElement('Relationships');
  163. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  164. // Write drawing relationships?
  165. if ($pWorksheet->getDrawingCollection()->count() > 0) {
  166. $this->_writeRelationship(
  167. $objWriter,
  168. 1,
  169. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  170. '../drawings/drawing' . $pWorksheetId . '.xml'
  171. );
  172. }
  173. $objWriter->endElement();
  174. // Return
  175. return $objWriter->outputMemory(true);
  176. } else {
  177. throw new Exception("Invalid PHPExcel_Worksheet object passed.");
  178. }
  179. }
  180. /**
  181. * Write drawing relationships to XML format
  182. *
  183. * @param PHPExcel_Worksheet $pWorksheet
  184. * @return string XML Output
  185. * @throws Exception
  186. */
  187. public function writeDrawingRelationships($pWorksheet = null)
  188. {
  189. if ($pWorksheet instanceof PHPExcel_Worksheet) {
  190. // Create XML writer
  191. $objWriter = new xmlWriter();
  192. $objWriter->openMemory();
  193. // XML header
  194. $objWriter->startDocument('1.0','UTF-8','yes');
  195. // Relationships
  196. $objWriter->startElement('Relationships');
  197. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  198. // Loop trough images and write relationships
  199. $i = 1;
  200. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
  201. while ($iterator->valid()) {
  202. // Write relationship
  203. $this->_writeRelationship(
  204. $objWriter,
  205. $i,
  206. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  207. '../media/' . $iterator->current()->getFilename()
  208. );
  209. $iterator->next();
  210. $i++;
  211. }
  212. $objWriter->endElement();
  213. // Return
  214. return $objWriter->outputMemory(true);
  215. } else {
  216. throw new Exception("Invalid PHPExcel_Worksheet object passed.");
  217. }
  218. }
  219. /**
  220. * Write Override content type
  221. *
  222. * @param xmlWriter $objWriter XML Writer
  223. * @param int $pId Relationship ID. rId will be prepended!
  224. * @param string $pType Relationship type
  225. * @param string $pTarget Relationship target
  226. * @throws Exception
  227. */
  228. private function _writeRelationship($objWriter = null, $pId = 1, $pType = '', $pTarget = '')
  229. {
  230. if ($objWriter instanceof xmlWriter && $pType != '' && $pTarget != '') {
  231. // Write relationship
  232. $objWriter->startElement('Relationship');
  233. $objWriter->writeAttribute('Id', 'rId' . $pId);
  234. $objWriter->writeAttribute('Type', $pType);
  235. $objWriter->writeAttribute('Target', $pTarget);
  236. $objWriter->endElement();
  237. } else {
  238. throw new Exception("Invalid parameters passed.");
  239. }
  240. }
  241. }