ZIP->addData('mimetype', 'application/vnd.oasis.opendocument.text', 'mimetype');
$params->ZIP->addData('meta.xml', $meta);
$params->ZIP->addData('settings.xml', $settings->getContent());
$autostyles = $params->styleset->export('office:automatic-styles');
$commonstyles = $params->styleset->export('office:styles');
$masterstyles = $params->styleset->export('office:master-styles');
$value = '<' . '?xml version="1.0" encoding="UTF-8"?' . ">\n";
$value .= 'content;
$value .= '';
$value .= '';
$value .= '';
$params->ZIP->addData('content.xml', $value);
// Edit 'styles.xml'
$value = io_readFile(DOKU_PLUGIN.'odt/styles.xml');
// Insert new master page styles
$page = '';
foreach ($pagestyles as $name => $layout_name) {
$page .= '';
}
if ( !empty($page) ) {
$value = str_replace('', $page.'', $value);
}
// Add common styles.
$original = XMLUtil::getElement('office:styles', $value);
$value = str_replace($original, $commonstyles, $value);
// Add automatic styles.
$value = str_replace('', $autostyles, $value);
$params->ZIP->addData('styles.xml', $value);
// build final manifest
$params->ZIP->addData('META-INF/manifest.xml', $params->manifest->getContent());
}
/**
* Build the document from the template.
* (code taken from old function 'document_end_scratch')
*
* @param ODTInternalParams $params
* @param string $meta
* @param string $userfields
* @return mixed
*/
protected static function buildFromODTTemplate(ODTInternalParams $params, $meta=null, $userfields=null, $pagestyles=null, $template=NULL, $tempDir=NULL){
// for the temp dir
global $ID;
if (!isset($template) || !isset($tempDir)) {
return;
}
// Temp dir
if (is_dir($tempDir)) { io_rmdir($tempDir,true); }
io_mkdir_p($tempDir);
// Extract template
try {
$ZIPextract = new \splitbrain\PHPArchive\Zip();
$ZIPextract->open($template);
$ZIPextract->extract($tempDir);
$ZIPextract->open($template);
$templateContents = $ZIPextract->contents();
} catch (\splitbrain\PHPArchive\ArchiveIOException $e) {
throw new Exception(' Error extracting the zip archive:'.$template.' to '.$tempDir);
}
// Replace metadata
io_saveFile($tempDir.'/meta.xml', $meta);
// Evtl. copy page format of first page to different style
$first_master = $params->styleset->getStyleAtIndex ('office:master-styles', 0);
if (isset($first_master) &&
$first_master->getProperty('style-page-layout-name') != $params->document->getStyleName('first page')) {
// The master page of the template references a different page layout style
// then used by us for the first page. Copy the page format settings.
$source = $params->document->getStyle($params->document->getStyleName('first page'));
$dest = $params->document->getStyle($first_master->getProperty('style-page-layout-name'));
if (isset($source) && isset($dest)) {
$dest->setProperty('width', $source->getProperty('width'));
$dest->setProperty('height', $source->getProperty('height'));
$dest->setProperty('margin-top', $source->getProperty('margin-top'));
$dest->setProperty('margin-right', $source->getProperty('margin-right'));
$dest->setProperty('margin-bottom', $source->getProperty('margin-bottom'));
$dest->setProperty('margin-left', $source->getProperty('margin-left'));
}
}
$autostyles = $params->styleset->export('office:automatic-styles');
$commonstyles = $params->styleset->export('office:styles');
$masterstyles = $params->styleset->export('office:master-styles');
// Prepare content
$missingfonts = $params->styleset->getMissingFonts($tempDir.'/styles.xml');
// Insert content
$old_content = io_readFile($tempDir.'/content.xml');
if (strpos($old_content, 'DOKUWIKI-ODT-INSERT') !== FALSE) { // Replace the mark
self::replaceInFile('/]*>DOKUWIKI-ODT-INSERT<\/text:p>/',
$params->content, $tempDir.'/content.xml', true);
} else { // Append to the template
self::replaceInFile('', $params->content.'', $tempDir.'/content.xml');
}
// Cut off unwanted content
if (strpos($old_content, 'DOKUWIKI-ODT-CUT-START') !== FALSE
&& strpos($old_content, 'DOKUWIKI-ODT-CUT-STOP') !== FALSE) {
self::replaceInFile('/DOKUWIKI-ODT-CUT-START.*DOKUWIKI-ODT-CUT-STOP/',
'', $tempDir.'/content.xml', true);
}
// Insert userfields
if (strpos($old_content, "text:user-field-decls") === FALSE) { // no existing userfields
self::replaceInFile('/]*)>/U', ''.$userfields, $tempDir.'/content.xml', TRUE);
} else {
self::replaceInFile('', substr($userfields,23), $tempDir.'/content.xml');
}
// Insert styles & fonts
$value = io_readFile($tempDir.'/content.xml');
$original = XMLUtil::getElement('office:automatic-styles', $value);
self::replaceInFile($original, $autostyles, $tempDir.'/content.xml');
$value = io_readFile($tempDir.'/styles.xml');
$original = XMLUtil::getElement('office:automatic-styles', $value);
self::replaceInFile($original, $autostyles, $tempDir.'/styles.xml');
$value = io_readFile($tempDir.'/styles.xml');
$original = XMLUtil::getElement('office:styles', $value);
self::replaceInFile($original, $commonstyles, $tempDir.'/styles.xml');
self::replaceInFile('', $missingfonts.'', $tempDir.'/styles.xml');
// Insert page styles
$page = '';
foreach ($pagestyles as $name => $layout_name) {
$page .= '';
}
if ( !empty($page) ) {
self::replaceInFile('', $page.'', $tempDir.'/styles.xml');
}
// Add manifest data
self::replaceInFile('', $params->manifest->getExtraContent() . '', $tempDir . '/META-INF/manifest.xml');
// Build the Zip
foreach ($templateContents as $fileInfo) {
if (!$fileInfo->getIsdir()) {
$params->ZIP->addFile($tempDir.'/'.$fileInfo->getPath(), $fileInfo);
}
}
io_rmdir($tempDir,true);
}
/**
* Build the document from the template.
* (code taken from old function 'document_end_scratch')
*
* @param ODTInternalParams $params
* @param string $meta
* @param string $userfields
* @return mixed
*/
public static function buildZIPFile(ODTInternalParams $params, $meta=null, $userfields=null, $pagestyles=null, $template=NULL, $tempDir=NULL){
if ( !isset($template) ) {
self::buildFromScratch($params, $meta, $userfields, $pagestyles);
} else {
self::buildFromODTTemplate($params, $meta, $userfields, $pagestyles, $template, $tempDir);
}
$params->ZIP->close();
}
/**
* @param string $from
* @param string $to
* @param string $file
* @param bool $regexp
*/
protected static function replaceInFile($from, $to, $file, $regexp=FALSE) {
$value = io_readFile($file);
if ($regexp) {
$value = preg_replace($from, $to, $value);
} else {
$value = str_replace($from, $to, $value);
}
$file_f = fopen($file, 'w');
fwrite($file_f, $value);
fclose($file_f);
}
}