bb617e8bb188e12ae0cbed8d3f575b906e46b5c3.svn-base 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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_OpenDocument
  23. * @copyright Copyright (c) 2006 - 2014 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. /**
  28. * PHPExcel_Writer_OpenDocument
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_OpenDocument
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. * @author Alexander Pervakov <frost-nzcr4@jagmort.com>
  34. * @link http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os.html
  35. */
  36. class PHPExcel_Writer_OpenDocument extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  37. {
  38. /**
  39. * Private writer parts
  40. *
  41. * @var PHPExcel_Writer_OpenDocument_WriterPart[]
  42. */
  43. private $_writerParts = array();
  44. /**
  45. * Private PHPExcel
  46. *
  47. * @var PHPExcel
  48. */
  49. private $_spreadSheet;
  50. /**
  51. * Create a new PHPExcel_Writer_OpenDocument
  52. *
  53. * @param PHPExcel $pPHPExcel
  54. */
  55. public function __construct(PHPExcel $pPHPExcel = null)
  56. {
  57. $this->setPHPExcel($pPHPExcel);
  58. $writerPartsArray = array(
  59. 'content' => 'PHPExcel_Writer_OpenDocument_Content',
  60. 'meta' => 'PHPExcel_Writer_OpenDocument_Meta',
  61. 'meta_inf' => 'PHPExcel_Writer_OpenDocument_MetaInf',
  62. 'mimetype' => 'PHPExcel_Writer_OpenDocument_Mimetype',
  63. 'settings' => 'PHPExcel_Writer_OpenDocument_Settings',
  64. 'styles' => 'PHPExcel_Writer_OpenDocument_Styles',
  65. 'thumbnails' => 'PHPExcel_Writer_OpenDocument_Thumbnails'
  66. );
  67. foreach ($writerPartsArray as $writer => $class) {
  68. $this->_writerParts[$writer] = new $class($this);
  69. }
  70. }
  71. /**
  72. * Get writer part
  73. *
  74. * @param string $pPartName Writer part name
  75. * @return PHPExcel_Writer_Excel2007_WriterPart
  76. */
  77. public function getWriterPart($pPartName = '')
  78. {
  79. if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
  80. return $this->_writerParts[strtolower($pPartName)];
  81. } else {
  82. return null;
  83. }
  84. }
  85. /**
  86. * Save PHPExcel to file
  87. *
  88. * @param string $pFilename
  89. * @throws PHPExcel_Writer_Exception
  90. */
  91. public function save($pFilename = NULL)
  92. {
  93. if (!$this->_spreadSheet) {
  94. throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
  95. }
  96. // garbage collect
  97. $this->_spreadSheet->garbageCollect();
  98. // If $pFilename is php://output or php://stdout, make it a temporary file...
  99. $originalFilename = $pFilename;
  100. if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
  101. $pFilename = @tempnam(PHPExcel_Shared_File::sys_get_temp_dir(), 'phpxltmp');
  102. if ($pFilename == '') {
  103. $pFilename = $originalFilename;
  104. }
  105. }
  106. $objZip = $this->_createZip($pFilename);
  107. $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
  108. $objZip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
  109. $objZip->addFromString('content.xml', $this->getWriterPart('content')->write());
  110. $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
  111. $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
  112. $objZip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
  113. $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
  114. // Close file
  115. if ($objZip->close() === false) {
  116. throw new PHPExcel_Writer_Exception("Could not close zip file $pFilename.");
  117. }
  118. // If a temporary file was used, copy it to the correct file stream
  119. if ($originalFilename != $pFilename) {
  120. if (copy($pFilename, $originalFilename) === false) {
  121. throw new PHPExcel_Writer_Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
  122. }
  123. @unlink($pFilename);
  124. }
  125. }
  126. /**
  127. * Create zip object
  128. *
  129. * @param string $pFilename
  130. * @throws PHPExcel_Writer_Exception
  131. * @return ZipArchive
  132. */
  133. private function _createZip($pFilename)
  134. {
  135. // Create new ZIP file and open it for writing
  136. $zipClass = PHPExcel_Settings::getZipClass();
  137. $objZip = new $zipClass();
  138. // Retrieve OVERWRITE and CREATE constants from the instantiated zip class
  139. // This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
  140. $ro = new ReflectionObject($objZip);
  141. $zipOverWrite = $ro->getConstant('OVERWRITE');
  142. $zipCreate = $ro->getConstant('CREATE');
  143. if (file_exists($pFilename)) {
  144. unlink($pFilename);
  145. }
  146. // Try opening the ZIP file
  147. if ($objZip->open($pFilename, $zipOverWrite) !== true) {
  148. if ($objZip->open($pFilename, $zipCreate) !== true) {
  149. throw new PHPExcel_Writer_Exception("Could not open $pFilename for writing.");
  150. }
  151. }
  152. return $objZip;
  153. }
  154. /**
  155. * Get PHPExcel object
  156. *
  157. * @return PHPExcel
  158. * @throws PHPExcel_Writer_Exception
  159. */
  160. public function getPHPExcel()
  161. {
  162. if ($this->_spreadSheet !== null) {
  163. return $this->_spreadSheet;
  164. } else {
  165. throw new PHPExcel_Writer_Exception('No PHPExcel assigned.');
  166. }
  167. }
  168. /**
  169. * Set PHPExcel object
  170. *
  171. * @param PHPExcel $pPHPExcel PHPExcel object
  172. * @throws PHPExcel_Writer_Exception
  173. * @return PHPExcel_Writer_Excel2007
  174. */
  175. public function setPHPExcel(PHPExcel $pPHPExcel = null)
  176. {
  177. $this->_spreadSheet = $pPHPExcel;
  178. return $this;
  179. }
  180. }