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