ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Excel2007.php
Go to the documentation of this file.
1 <?php
37 {
43  private $_preCalculateFormulas = true;
44 
50  private $_office2003compatibility = false;
51 
57  private $_writerParts = array();
58 
64  private $_spreadSheet;
65 
71  private $_stringTable = array();
72 
79 
85  private $_fillHashTable;
86 
92  private $_fontHashTable;
93 
100 
107 
114 
120  private $_useDiskCaching = false;
121 
127  private $_diskCachingDirectory = './';
128 
134  public function __construct(PHPExcel $pPHPExcel = null)
135  {
136  // Assign PHPExcel
137  $this->setPHPExcel($pPHPExcel);
138 
139  $writerPartsArray = array( 'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
140  'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
141  'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
142  'rels' => 'PHPExcel_Writer_Excel2007_Rels',
143  'theme' => 'PHPExcel_Writer_Excel2007_Theme',
144  'style' => 'PHPExcel_Writer_Excel2007_Style',
145  'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
146  'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
147  'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
148  'comments' => 'PHPExcel_Writer_Excel2007_Comments'
149  );
150 
151  // Initialise writer parts
152  // and Assign their parent IWriters
153  foreach ($writerPartsArray as $writer => $class) {
154  $this->_writerParts[$writer] = new $class($this);
155  }
156 
157  $hashTablesArray = array( '_stylesConditionalHashTable', '_fillHashTable', '_fontHashTable',
158  '_bordersHashTable', '_numFmtHashTable', '_drawingHashTable'
159  );
160 
161  // Set HashTable variables
162  foreach ($hashTablesArray as $tableName) {
163  $this->$tableName = new PHPExcel_HashTable();
164  }
165  }
166 
173  public function getWriterPart($pPartName = '') {
174  if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
175  return $this->_writerParts[strtolower($pPartName)];
176  } else {
177  return null;
178  }
179  }
180 
187  public function save($pFilename = null)
188  {
189  if ($this->_spreadSheet !== NULL) {
190  // garbage collect
191  $this->_spreadSheet->garbageCollect();
192 
193  // If $pFilename is php://output or php://stdout, make it a temporary file...
194  $originalFilename = $pFilename;
195  if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
196  $pFilename = @tempnam('./', 'phpxltmp');
197  if ($pFilename == '') {
198  $pFilename = $originalFilename;
199  }
200  }
201 
202  $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
203  PHPExcel_Calculation::getInstance()->writeDebugLog = false;
206 
207  // Create string lookup table
208  $this->_stringTable = array();
209  for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
210  $this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
211  }
212 
213  // Create styles dictionaries
214  $this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
215  $this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
216  $this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
217  $this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
218  $this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
219 
220  // Create drawing dictionary
221  $this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
222 
223  // Create new ZIP file and open it for writing
224  $zipClass = PHPExcel_Settings::getZipClass();
225  $objZip = new $zipClass();
226 
227  if (file_exists($pFilename)) {
228  unlink($pFilename);
229  }
230  // Try opening the ZIP file
231  if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
232  if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
233  throw new Exception("Could not open " . $pFilename . " for writing.");
234  }
235  }
236 
237  // Add [Content_Types].xml to ZIP file
238  $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet));
239 
240  // Add relationships to ZIP file
241  $objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
242  $objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
243 
244  // Add document properties to ZIP file
245  $objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
246  $objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
247  $customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
248  if ($customPropertiesPart !== NULL) {
249  $objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
250  }
251 
252  // Add theme to ZIP file
253  $objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
254 
255  // Add string table to ZIP file
256  $objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));
257 
258  // Add styles to ZIP file
259  $objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
260 
261  // Add workbook to ZIP file
262  $objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet));
263 
264  // Add worksheets
265  for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
266  $objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable));
267  }
268 
269  // Add worksheet relationships (drawings, ...)
270  for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
271 
272  // Add relationships
273  $objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1)));
274 
275  // Add drawing relationship parts
276  if ($this->_spreadSheet->getSheet($i)->getDrawingCollection()->count() > 0) {
277  // Drawing relationships
278  $objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i)));
279 
280  // Drawings
281  $objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i)));
282  }
283 
284  // Add comment relationship parts
285  if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
286  // VML Comments
287  $objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
288 
289  // Comments
290  $objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
291  }
292 
293  // Add header/footer relationship parts
294  if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
295  // VML Drawings
296  $objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
297 
298  // VML Drawing relationships
299  $objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
300 
301  // Media
302  foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
303  $objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
304  }
305  }
306  }
307 
308  // Add media
309  for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
310  if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_Drawing) {
311  $imageContents = null;
312  $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
313 
314  if (strpos($imagePath, 'zip://') !== false) {
315  $imagePath = substr($imagePath, 6);
316  $imagePathSplitted = explode('#', $imagePath);
317 
318  $imageZip = new ZipArchive();
319  $imageZip->open($imagePathSplitted[0]);
320  $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
321  $imageZip->close();
322  unset($imageZip);
323  } else {
324  $imageContents = file_get_contents($imagePath);
325  }
326 
327  $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
328  } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPExcel_Worksheet_MemoryDrawing) {
329  ob_start();
330  call_user_func(
331  $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
332  $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
333  );
334  $imageContents = ob_get_contents();
335  ob_end_clean();
336 
337  $objZip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
338  }
339  }
340 
342  PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
343 
344  // Close file
345  if ($objZip->close() === false) {
346  throw new Exception("Could not close zip file $pFilename.");
347  }
348 
349  // If a temporary file was used, copy it to the correct file stream
350  if ($originalFilename != $pFilename) {
351  if (copy($pFilename, $originalFilename) === false) {
352  throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
353  }
354  @unlink($pFilename);
355  }
356  } else {
357  throw new Exception("PHPExcel object unassigned.");
358  }
359  }
360 
367  public function getPHPExcel() {
368  if ($this->_spreadSheet !== null) {
369  return $this->_spreadSheet;
370  } else {
371  throw new Exception("No PHPExcel assigned.");
372  }
373  }
374 
382  public function setPHPExcel(PHPExcel $pPHPExcel = null) {
383  $this->_spreadSheet = $pPHPExcel;
384  return $this;
385  }
386 
392  public function getStringTable() {
393  return $this->_stringTable;
394  }
395 
401  public function getStylesConditionalHashTable() {
403  }
404 
410  public function getFillHashTable() {
411  return $this->_fillHashTable;
412  }
413 
419  public function getFontHashTable() {
420  return $this->_fontHashTable;
421  }
422 
428  public function getBordersHashTable() {
430  }
431 
437  public function getNumFmtHashTable() {
439  }
440 
446  public function getDrawingHashTable() {
448  }
449 
455  public function getPreCalculateFormulas() {
457  }
458 
464  public function setPreCalculateFormulas($pValue = true) {
465  $this->_preCalculateFormulas = $pValue;
466  }
467 
473  public function getOffice2003Compatibility() {
475  }
476 
483  public function setOffice2003Compatibility($pValue = false) {
484  $this->_office2003compatibility = $pValue;
485  return $this;
486  }
487 
493  public function getUseDiskCaching() {
494  return $this->_useDiskCaching;
495  }
496 
505  public function setUseDiskCaching($pValue = false, $pDirectory = null) {
506  $this->_useDiskCaching = $pValue;
507 
508  if ($pDirectory !== NULL) {
509  if (is_dir($pDirectory)) {
510  $this->_diskCachingDirectory = $pDirectory;
511  } else {
512  throw new Exception("Directory does not exist: $pDirectory");
513  }
514  }
515  return $this;
516  }
517 
523  public function getDiskCachingDirectory() {
525  }
526 }