Ejemplo n.º 1
0
 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;
 }