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

# · PHP · 379 lines · 205 code · 51 blank · 123 comment · 25 complexity · 128b3d0d4b4a02c3a0c1d3762bfa0bcf MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 PHPExcel
  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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.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. /** PHPExcel_Shared_XMLWriter */
  36. require_once 'PHPExcel/Shared/XMLWriter.php';
  37. /**
  38. * PHPExcel_Writer_Excel2007_Rels
  39. *
  40. * @category PHPExcel
  41. * @package PHPExcel_Writer_Excel2007
  42. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  43. */
  44. class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
  45. {
  46. /**
  47. * Write relationships to XML format
  48. *
  49. * @param PHPExcel $pPHPExcel
  50. * @return string XML Output
  51. * @throws Exception
  52. */
  53. public function writeRelationships(PHPExcel $pPHPExcel = null)
  54. {
  55. // Create XML writer
  56. $objWriter = null;
  57. if ($this->getParentWriter()->getUseDiskCaching()) {
  58. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  59. } else {
  60. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  61. }
  62. // XML header
  63. $objWriter->startDocument('1.0','UTF-8','yes');
  64. // Relationships
  65. $objWriter->startElement('Relationships');
  66. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  67. // Relationship docProps/app.xml
  68. $this->_writeRelationship(
  69. $objWriter,
  70. 3,
  71. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
  72. 'docProps/app.xml'
  73. );
  74. // Relationship docProps/core.xml
  75. $this->_writeRelationship(
  76. $objWriter,
  77. 2,
  78. 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
  79. 'docProps/core.xml'
  80. );
  81. // Relationship xl/workbook.xml
  82. $this->_writeRelationship(
  83. $objWriter,
  84. 1,
  85. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
  86. 'xl/workbook.xml'
  87. );
  88. $objWriter->endElement();
  89. // Return
  90. return $objWriter->getData();
  91. }
  92. /**
  93. * Write workbook relationships to XML format
  94. *
  95. * @param PHPExcel $pPHPExcel
  96. * @return string XML Output
  97. * @throws Exception
  98. */
  99. public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
  100. {
  101. // Create XML writer
  102. $objWriter = null;
  103. if ($this->getParentWriter()->getUseDiskCaching()) {
  104. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  105. } else {
  106. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  107. }
  108. // XML header
  109. $objWriter->startDocument('1.0','UTF-8','yes');
  110. // Relationships
  111. $objWriter->startElement('Relationships');
  112. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  113. // Relationship styles.xml
  114. $this->_writeRelationship(
  115. $objWriter,
  116. 1,
  117. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
  118. 'styles.xml'
  119. );
  120. // Relationship theme/theme1.xml
  121. $this->_writeRelationship(
  122. $objWriter,
  123. 2,
  124. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
  125. 'theme/theme1.xml'
  126. );
  127. // Relationship sharedStrings.xml
  128. $this->_writeRelationship(
  129. $objWriter,
  130. 3,
  131. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
  132. 'sharedStrings.xml'
  133. );
  134. // Relationships with sheets
  135. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  136. $this->_writeRelationship(
  137. $objWriter,
  138. ($i + 1 + 3),
  139. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
  140. 'worksheets/sheet' . ($i + 1) . '.xml'
  141. );
  142. }
  143. $objWriter->endElement();
  144. // Return
  145. return $objWriter->getData();
  146. }
  147. /**
  148. * Write worksheet relationships to XML format
  149. *
  150. * Numbering is as follows:
  151. * rId1 - Drawings
  152. * rId_hyperlink_x - Hyperlinks
  153. *
  154. * @param PHPExcel_Worksheet $pWorksheet
  155. * @param int $pWorksheetId
  156. * @return string XML Output
  157. * @throws Exception
  158. */
  159. public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
  160. {
  161. // Create XML writer
  162. $objWriter = null;
  163. if ($this->getParentWriter()->getUseDiskCaching()) {
  164. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  165. } else {
  166. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  167. }
  168. // XML header
  169. $objWriter->startDocument('1.0','UTF-8','yes');
  170. // Relationships
  171. $objWriter->startElement('Relationships');
  172. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  173. // Write drawing relationships?
  174. if ($pWorksheet->getDrawingCollection()->count() > 0) {
  175. $this->_writeRelationship(
  176. $objWriter,
  177. 1,
  178. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
  179. '../drawings/drawing' . $pWorksheetId . '.xml'
  180. );
  181. }
  182. // Write hyperlink relationships?
  183. $i = 1;
  184. foreach ($pWorksheet->getCellCollection() as $cell) {
  185. if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
  186. $this->_writeRelationship(
  187. $objWriter,
  188. '_hyperlink_' . $i,
  189. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
  190. $cell->getHyperlink()->getUrl(),
  191. 'External'
  192. );
  193. $i++;
  194. }
  195. }
  196. // Write comments relationship?
  197. $i = 1;
  198. if (count($pWorksheet->getComments()) > 0) {
  199. $this->_writeRelationship(
  200. $objWriter,
  201. '_comments_vml' . $i,
  202. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  203. '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
  204. );
  205. $this->_writeRelationship(
  206. $objWriter,
  207. '_comments' . $i,
  208. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
  209. '../comments' . $pWorksheetId . '.xml'
  210. );
  211. }
  212. // Write header/footer relationship?
  213. $i = 1;
  214. if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
  215. $this->_writeRelationship(
  216. $objWriter,
  217. '_headerfooter_vml' . $i,
  218. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
  219. '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
  220. );
  221. }
  222. $objWriter->endElement();
  223. // Return
  224. return $objWriter->getData();
  225. }
  226. /**
  227. * Write drawing relationships to XML format
  228. *
  229. * @param PHPExcel_Worksheet $pWorksheet
  230. * @return string XML Output
  231. * @throws Exception
  232. */
  233. public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  234. {
  235. // Create XML writer
  236. $objWriter = null;
  237. if ($this->getParentWriter()->getUseDiskCaching()) {
  238. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  239. } else {
  240. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  241. }
  242. // XML header
  243. $objWriter->startDocument('1.0','UTF-8','yes');
  244. // Relationships
  245. $objWriter->startElement('Relationships');
  246. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  247. // Loop trough images and write relationships
  248. $i = 1;
  249. $iterator = $pWorksheet->getDrawingCollection()->getIterator();
  250. while ($iterator->valid()) {
  251. if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing) {
  252. // Write relationship for image drawing
  253. $this->_writeRelationship(
  254. $objWriter,
  255. $i,
  256. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  257. '../media/' . $iterator->current()->getFilename()
  258. );
  259. }
  260. $iterator->next();
  261. $i++;
  262. }
  263. $objWriter->endElement();
  264. // Return
  265. return $objWriter->getData();
  266. }
  267. /**
  268. * Write header/footer drawing relationships to XML format
  269. *
  270. * @param PHPExcel_Worksheet $pWorksheet
  271. * @return string XML Output
  272. * @throws Exception
  273. */
  274. public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
  275. {
  276. // Create XML writer
  277. $objWriter = null;
  278. if ($this->getParentWriter()->getUseDiskCaching()) {
  279. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK);
  280. } else {
  281. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  282. }
  283. // XML header
  284. $objWriter->startDocument('1.0','UTF-8','yes');
  285. // Relationships
  286. $objWriter->startElement('Relationships');
  287. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
  288. // Loop trough images and write relationships
  289. foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
  290. // Write relationship for image drawing
  291. $this->_writeRelationship(
  292. $objWriter,
  293. $key,
  294. 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
  295. '../media/' . $value->getFilename()
  296. );
  297. }
  298. $objWriter->endElement();
  299. // Return
  300. return $objWriter->getData();
  301. }
  302. /**
  303. * Write Override content type
  304. *
  305. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  306. * @param int $pId Relationship ID. rId will be prepended!
  307. * @param string $pType Relationship type
  308. * @param string $pTarget Relationship target
  309. * @param string $pTargetMode Relationship target mode
  310. * @throws Exception
  311. */
  312. private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
  313. {
  314. if ($pType != '' && $pTarget != '') {
  315. // Write relationship
  316. $objWriter->startElement('Relationship');
  317. $objWriter->writeAttribute('Id', 'rId' . $pId);
  318. $objWriter->writeAttribute('Type', $pType);
  319. $objWriter->writeAttribute('Target', $pTarget);
  320. if ($pTargetMode != '') {
  321. $objWriter->writeAttribute('TargetMode', $pTargetMode);
  322. }
  323. $objWriter->endElement();
  324. } else {
  325. throw new Exception("Invalid parameters passed.");
  326. }
  327. }
  328. }