public function getDocument($_filename = null) { $this->_addStyles(); $filename = $_filename !== null ? $_filename : tempnam($this->_tmpdir, 'OpenDocument'); $tempDir = $this->_tmpdir . DIRECTORY_SEPARATOR . 'od_' . md5(uniqid(rand(), true)); if (file_exists($tempDir)) { throw new Exception('Directory already exists.'); } mkdir($tempDir); if ($this->_templateFile !== null) { $templateZip = new ZipArchive(); if ($templateZip->open($this->_templateFile) === TRUE) { $templateZip->extractTo($tempDir); $templateZip->close(); } } if ($this->_templateFile === null) { mkdir($tempDir . DIRECTORY_SEPARATOR . 'META-INF'); file_put_contents($tempDir . DIRECTORY_SEPARATOR . 'mimetype', $this->_body->getContentType()); file_put_contents($tempDir . DIRECTORY_SEPARATOR . 'meta.xml', $this->_meta); file_put_contents($tempDir . DIRECTORY_SEPARATOR . 'settings.xml', $this->_settings); file_put_contents($tempDir . DIRECTORY_SEPARATOR . 'META-INF/manifest.xml', $this->_manifest); } // ObjectReplacements // $images = $this->_document->xpath('//draw:image'); // foreach($images as $image) { // $image_sxe = dom_import_simplexml($image); // $image_sxe->parentNode->removeChild($image_sxe); // } // // $images = $this->_document->xpath('//draw:image'); // Tinebase_Core::getLogger()->err(print_r($images, TRUE)); file_put_contents($tempDir . DIRECTORY_SEPARATOR . 'content.xml', $this->_document->saveXML()); file_put_contents($tempDir . DIRECTORY_SEPARATOR . 'styles.xml', $this->_styles); $zip = new ZipArchive(); $opened = $zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE); if ($opened !== true) { throw new Exception('could not open zip file'); } $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($tempDir)); foreach ($iterator as $fullFilename => $cur) { // the second parameter of the addFile function needs always the unix directory separator $localname = str_replace('\\', '/', substr($fullFilename, strlen($tempDir) + 1)); $zip->addFile($fullFilename, $localname); } $zip->close(); // delete files / remove dir removeDir($tempDir); return $filename; }
/** * generate export * * @return string filename */ public function generate() { $this->_createDocument(); // build export table (use current table if using template) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Creating export for ' . $this->_modelName . ' . ' . $this->_getDataTableName()); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($this->_config->toArray(), TRUE)); } $this->_spreadSheetObject = $this->_openDocumentObject->getBody(); // append / use existing table if ($this->_spreadSheetObject->tableExists($this->_getDataTableName()) === true) { $this->_activeTable = $this->_spreadSheetObject->getTable($this->_getDataTableName()); } else { $this->_activeTable = $this->_spreadSheetObject->appendTable($this->_getDataTableName()); } $this->_setColumnStyles(); // add header (disabled at the moment) if (isset($this->_config->header) && $this->_config->header) { $this->_addHead($this->_activeTable); } $this->_exportRecords(); // create file $result = $this->_openDocumentObject->getDocument(); return $result; }