ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Serialized.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/HashTable.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
51 
53 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
54 
55 
64 {
70  private $_spreadSheet;
71 
77  public function __construct(PHPExcel $pPHPExcel = null)
78  {
79  // Assign PHPExcel
80  $this->setPHPExcel($pPHPExcel);
81  }
82 
89  public function save($pFilename = null)
90  {
91  if (!is_null($this->_spreadSheet)) {
92  // Garbage collect
93  $this->_spreadSheet->garbageCollect();
94 
95  // Garbage collect...
96  foreach ($this->_spreadSheet->getAllSheets() as $sheet) {
97  $sheet->garbageCollect();
98  }
99 
100  // Create new ZIP file and open it for writing
101  $objZip = new ZipArchive();
102 
103  // Try opening the ZIP file
104  if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
105  if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
106  throw new Exception("Could not open " . $pFilename . " for writing.");
107  }
108  }
109 
110  // Add media
111  $sheetCount = $this->_spreadSheet->getSheetCount();
112  for ($i = 0; $i < $sheetCount; ++$i) {
113  for ($j = 0; $j < $this->_spreadSheet->getSheet($i)->getDrawingCollection()->count(); ++$j) {
114  if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j) instanceof PHPExcel_Worksheet_BaseDrawing) {
115  $imgTemp = $this->_spreadSheet->getSheet($i)->getDrawingCollection()->offsetGet($j);
116  $objZip->addFromString('media/' . $imgTemp->getFilename(), file_get_contents($imgTemp->getPath()));
117  }
118  }
119  }
120 
121  // Add phpexcel.xml to the document, which represents a PHP serialized PHPExcel object
122  $objZip->addFromString('phpexcel.xml', $this->_writeSerialized($this->_spreadSheet, $pFilename));
123 
124  // Close file
125  if ($objZip->close() === false) {
126  throw new Exception("Could not close zip file $pFilename.");
127  }
128  } else {
129  throw new Exception("PHPExcel object unassigned.");
130  }
131  }
132 
139  public function getPHPExcel() {
140  if (!is_null($this->_spreadSheet)) {
141  return $this->_spreadSheet;
142  } else {
143  throw new Exception("No PHPExcel assigned.");
144  }
145  }
146 
154  public function setPHPExcel(PHPExcel $pPHPExcel = null) {
155  $this->_spreadSheet = $pPHPExcel;
156  return $this;
157  }
158 
167  private function _writeSerialized(PHPExcel $pPHPExcel = null, $pFilename = '')
168  {
169  // Clone $pPHPExcel
170  $pPHPExcel = clone $pPHPExcel;
171 
172  // Update media links
173  $sheetCount = $pPHPExcel->getSheetCount();
174  for ($i = 0; $i < $sheetCount; ++$i) {
175  for ($j = 0; $j < $pPHPExcel->getSheet($i)->getDrawingCollection()->count(); ++$j) {
176  if ($pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j) instanceof PHPExcel_Worksheet_BaseDrawing) {
177  $imgTemp =& $pPHPExcel->getSheet($i)->getDrawingCollection()->offsetGet($j);
178  $imgTemp->setPath('zip://' . $pFilename . '#media/' . $imgTemp->getFilename(), false);
179  }
180  }
181  }
182 
183  // Create XML writer
184  $objWriter = new xmlWriter();
185  $objWriter->openMemory();
186  $objWriter->setIndent(true);
187 
188  // XML header
189  $objWriter->startDocument('1.0','UTF-8','yes');
190 
191  // PHPExcel
192  $objWriter->startElement('PHPExcel');
193  $objWriter->writeAttribute('version', '1.7.0');
194 
195  // Comment
196  $objWriter->writeComment('This file has been generated using PHPExcel v1.7.0 (http://www.codeplex.com/PHPExcel). It contains a base64 encoded serialized version of the PHPExcel internal object.');
197 
198  // Data
199  $objWriter->startElement('data');
200  $objWriter->writeCData( base64_encode(serialize($pPHPExcel)) );
201  $objWriter->endElement();
202 
203  $objWriter->endElement();
204 
205  // Return
206  return $objWriter->outputMemory(true);
207  }
208 }