/protected/library/PHPExcel/Writer/Serialized.php

https://github.com/chrishubber/storytlr · PHP · 193 lines · 77 code · 27 blank · 89 comment · 14 complexity · 118784fc057a35de9f01031d9f8db5f7 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
  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 1.6.3, 2008-08-25
  26. */
  27. /** PHPExcel */
  28. require_once 'PHPExcel.php';
  29. /** PHPExcel_HashTable */
  30. require_once 'PHPExcel/HashTable.php';
  31. /** PHPExcel_IComparable */
  32. require_once 'PHPExcel/IComparable.php';
  33. /** PHPExcel_Worksheet */
  34. require_once 'PHPExcel/Worksheet.php';
  35. /** PHPExcel_Cell */
  36. require_once 'PHPExcel/Cell.php';
  37. /** PHPExcel_IWriter */
  38. require_once 'PHPExcel/Writer/IWriter.php';
  39. /**
  40. * PHPExcel_Writer_Serialized
  41. *
  42. * @category PHPExcel
  43. * @package PHPExcel_Writer
  44. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  45. */
  46. class PHPExcel_Writer_Serialized implements PHPExcel_Writer_IWriter
  47. {
  48. /**
  49. * Private PHPExcel
  50. *
  51. * @var PHPExcel
  52. */
  53. private $_spreadSheet;
  54. /**
  55. * Create a new PHPExcel_Writer_Serialized
  56. *
  57. * @param PHPExcel $pPHPExcel
  58. */
  59. public function __construct(PHPExcel $pPHPExcel = null)
  60. {
  61. // Assign PHPExcel
  62. $this->setPHPExcel($pPHPExcel);
  63. }
  64. /**
  65. * Save PHPExcel to file
  66. *
  67. * @param string $pFileName
  68. * @throws Exception
  69. */
  70. public function save($pFilename = null)
  71. {
  72. if (!is_null($this->_spreadSheet)) {
  73. // Garbage collect...
  74. foreach ($this->_spreadSheet->getAllSheets() as $sheet) {
  75. $sheet->garbageCollect();
  76. }
  77. // Create new ZIP file and open it for writing
  78. $objZip = new ZipArchive();
  79. // Try opening the ZIP file
  80. if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
  81. if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
  82. throw new Exception("Could not open " . $pFilename . " for writing.");
  83. }
  84. }
  85. // Add media
  86. for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); $i++) {
  87. for ($j = 0; $j < $this->_spreadSheet->getSheet($i)->getDrawingCollection()->count(); $j++) {
  88. if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j) instanceof PHPExcel_Worksheet_BaseDrawing) {
  89. $imgTemp = $this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j);
  90. $objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath()));
  91. }
  92. }
  93. }
  94. // Add phpexcel.xml to the document, which represents a PHP serialized PHPExcel object
  95. $objZip->addFromString('phpexcel.xml', $this->_writeSerialized($this->_spreadSheet, $pFilename));
  96. // Close file
  97. if ($objZip->close() === false) {
  98. throw new Exception("Could not close zip file $pFilename.");
  99. }
  100. } else {
  101. throw new Exception("PHPExcel object unassigned.");
  102. }
  103. }
  104. /**
  105. * Get PHPExcel object
  106. *
  107. * @return PHPExcel
  108. * @throws Exception
  109. */
  110. public function getPHPExcel() {
  111. if (!is_null($this->_spreadSheet)) {
  112. return $this->_spreadSheet;
  113. } else {
  114. throw new Exception("No PHPExcel assigned.");
  115. }
  116. }
  117. /**
  118. * Get PHPExcel object
  119. *
  120. * @param PHPExcel $pPHPExcel PHPExcel object
  121. * @throws Exception
  122. */
  123. public function setPHPExcel(PHPExcel $pPHPExcel = null) {
  124. $this->_spreadSheet = $pPHPExcel;
  125. }
  126. /**
  127. * Serialize PHPExcel object to XML
  128. *
  129. * @param PHPExcel $pPHPExcel
  130. * @param string $pFilename
  131. * @return string XML Output
  132. * @throws Exception
  133. */
  134. private function _writeSerialized(PHPExcel $pPHPExcel = null, $pFilename = '')
  135. {
  136. // Clone $pPHPExcel
  137. $pPHPExcel = clone $pPHPExcel;
  138. // Update media links
  139. for ($i = 0; $i < $pPHPExcel->getSheetCount(); $i++) {
  140. for ($j = 0; $j < $pPHPExcel->getSheet($i)->getDrawingCollection()->count(); $j++) {
  141. if ($pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j) instanceof PHPExcel_Worksheet_BaseDrawing) {
  142. $imgTemp =& $pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j);
  143. $imgTemp->setPath('zip://' . $pFilename . '#media/' . $imgTemp->getFilename(), false);
  144. }
  145. }
  146. }
  147. // Create XML writer
  148. $objWriter = new xmlWriter();
  149. $objWriter->openMemory();
  150. $objWriter->setIndent(true);
  151. // XML header
  152. $objWriter->startDocument('1.0','UTF-8','yes');
  153. // PHPExcel
  154. $objWriter->startElement('PHPExcel');
  155. $objWriter->writeAttribute('version', '1.6.3');
  156. // Comment
  157. $objWriter->writeComment('This file has been generated using PHPExcel v1.6.3 (http://www.codeplex.com/PHPExcel). It contains a base64 encoded serialized version of the PHPExcel internal object.');
  158. // Data
  159. $objWriter->startElement('data');
  160. $objWriter->writeCData( base64_encode(serialize($pPHPExcel)) );
  161. $objWriter->endElement();
  162. $objWriter->endElement();
  163. // Return
  164. return $objWriter->outputMemory(true);
  165. }
  166. }