From 7926fd8083e4217639ad463418ab08d59d66fbfb Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 5 Sep 2015 00:43:13 +0200 Subject: [PATCH] NEW ODT generators can set meta properties of ODT file --- .../societe/doc/doc_generic_odt.modules.php | 14 +++- htdocs/includes/odtphp/Segment.php | 1 - htdocs/includes/odtphp/SegmentIterator.php | 1 - htdocs/includes/odtphp/odf.php | 67 ++++++++++++++++--- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/htdocs/core/modules/societe/doc/doc_generic_odt.modules.php b/htdocs/core/modules/societe/doc/doc_generic_odt.modules.php index 90ced6322b7..8a274f91ac7 100644 --- a/htdocs/core/modules/societe/doc/doc_generic_odt.modules.php +++ b/htdocs/core/modules/societe/doc/doc_generic_odt.modules.php @@ -408,8 +408,18 @@ class doc_generic_odt extends ModeleThirdPartyDoc } } else { - try { - $odfHandler->saveToDisk($file); + try { + $odfHandler->creator = $user->getFullName($outputlangs); + $odfHandler->title = $object->builddoc_filename; + $odfHandler->subject = $object->builddoc_filename; + + if (! empty($conf->global->ODT_ADD_DOLIBARR_ID)) + { + $odfHandler->userdefined['dol_id'] = $object->id; + $odfHandler->userdefined['dol_element'] = $object->element; + } + + $odfHandler->saveToDisk($file); }catch (Exception $e){ $this->error=$e->getMessage(); return -1; diff --git a/htdocs/includes/odtphp/Segment.php b/htdocs/includes/odtphp/Segment.php index 9bcfa0f65d1..c3e23ee9aec 100644 --- a/htdocs/includes/odtphp/Segment.php +++ b/htdocs/includes/odtphp/Segment.php @@ -6,7 +6,6 @@ class SegmentException extends Exception * Class for handling templating segments with odt files * You need PHP 5.2 at least * You need Zip Extension or PclZip library - * Encoding : ISO-8859-1 * * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com) * @copyright GPL License 2012 - Stephen Larroque - lrq3000@gmail.com diff --git a/htdocs/includes/odtphp/SegmentIterator.php b/htdocs/includes/odtphp/SegmentIterator.php index 3b0ccecf3ff..5f397c5f838 100644 --- a/htdocs/includes/odtphp/SegmentIterator.php +++ b/htdocs/includes/odtphp/SegmentIterator.php @@ -3,7 +3,6 @@ * Segments iterator * You need PHP 5.2 at least * You need Zip Extension or PclZip library - * Encoding : ISO-8859-1 * * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com) * @license http://www.gnu.org/copyleft/gpl.html GPL License diff --git a/htdocs/includes/odtphp/odf.php b/htdocs/includes/odtphp/odf.php index 26d1c057219..351a5969b14 100644 --- a/htdocs/includes/odtphp/odf.php +++ b/htdocs/includes/odtphp/odf.php @@ -6,14 +6,13 @@ class OdfException extends Exception * Templating class for odt file * You need PHP 5.2 at least * You need Zip Extension or PclZip library - * Encoding : ISO-8859-1 * * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com) - * @copyright GPL License 2010 - Laurent Destailleur - eldy@users.sourceforge.net - * @copyright GPL License 2010 - Vikas Mahajan - http://vikasmahajan.wordpress.com + * @copyright GPL License 2010-2015 - Laurent Destailleur - eldy@users.sourceforge.net + * @copyright GPL License 2010 - Vikas Mahajan - http://vikasmahajan.wordpress.com * @copyright GPL License 2012 - Stephen Larroque - lrq3000@gmail.com * @license http://www.gnu.org/copyleft/gpl.html GPL License - * @version 1.4.6 (last update 2013-04-07) + * @version 1.5.0 */ class Odf { @@ -25,6 +24,7 @@ class Odf ); protected $file; protected $contentXml; // To store content of content.xml file + protected $metaXml; // To store content of meta.xml file protected $stylesXml; // To store content of styles.xml file protected $manifestXml; // To store content of META-INF/manifest.xml file protected $tmpfile; @@ -32,6 +32,12 @@ class Odf protected $images = array(); protected $vars = array(); protected $segments = array(); + + public $creator; + public $title; + public $subject; + public $userdefined=array(); + const PIXEL_TO_CM = 0.026458333; /** * Class constructor @@ -86,6 +92,9 @@ class Odf if (($this->manifestXml = $this->file->getFromName('META-INF/manifest.xml')) === false) { throw new OdfException("Something is wrong with META-INF/manifest.xml in source file '$filename'"); } + if (($this->metaXml = $this->file->getFromName('meta.xml')) === false) { + throw new OdfException("Nothing to parse - Check that the meta.xml file is correctly formed in source file '$filename'"); + } if (($this->stylesXml = $this->file->getFromName('styles.xml')) === false) { throw new OdfException("Nothing to parse - Check that the styles.xml file is correctly formed in source file '$filename'"); } @@ -187,6 +196,7 @@ class Odf /** * Evaluating php codes inside the ODT and output the buffer (print, echo) inplace of the code * + * @return int 0 */ public function phpEval() { @@ -268,13 +278,13 @@ IMG; * Merge template variables * Called automatically for a save * - * @param string $type 'content' or 'styles' + * @param string $type 'content', 'styles' or 'meta' * @return void */ private function _parse($type='content') { // Conditionals substitution - // Note: must be done before content substitution, else the variable will be replaced by its value and the conditional won't work anymore + // Note: must be done before static substitution, else the variable will be replaced by its value and the conditional won't work anymore foreach($this->vars as $key => $value) { // If value is true (not 0 nor false nor null nor empty string) @@ -300,11 +310,11 @@ IMG; } } - // Content (variable) substitution + // Static substitution if ($type == 'content') $this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml); - // Styles substitution if ($type == 'styles') $this->stylesXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->stylesXml); - + if ($type == 'meta') $this->metaXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->metaXml); + } /** @@ -408,13 +418,21 @@ IMG; $res=$this->file->open($this->tmpfile); // tmpfile is odt template $this->_parse('content'); $this->_parse('styles'); + $this->_parse('meta'); + $this->setMetaData(); + //print $this->metaXml;exit; + if (! $this->file->addFromString('content.xml', $this->contentXml)) { - throw new OdfException('Error during file export addFromString'); + throw new OdfException('Error during file export addFromString content'); + } + if (! $this->file->addFromString('meta.xml', $this->metaXml)) { + throw new OdfException('Error during file export addFromString meta'); } if (! $this->file->addFromString('styles.xml', $this->stylesXml)) { - throw new OdfException('Error during file export addFromString'); + throw new OdfException('Error during file export addFromString styles'); } + foreach ($this->images as $imageKey => $imageValue) { // Add the image inside the ODT document $this->file->addFile($imageKey, 'Pictures/' . $imageValue); @@ -427,10 +445,37 @@ IMG; $this->file->close(); } + /** + * Update Meta information + * 2013-03-16T14:06:25 + * + * @return void + */ + public function setMetaData() + { + if (empty($this->creator)) $this->creator=''; + + $this->metaXml = preg_replace('/.*<\/dc:date>/', ''.gmdate("Y-m-d\TH:i:s").'', $this->metaXml); + $this->metaXml = preg_replace('/.*<\/dc:creator>/', ''.htmlspecialchars($this->creator).'', $this->metaXml); + $this->metaXml = preg_replace('/.*<\/dc:title>/', ''.htmlspecialchars($this->title).'', $this->metaXml); + $this->metaXml = preg_replace('/.*<\/dc:subject>/', ''.htmlspecialchars($this->subject).'', $this->metaXml); + + if (count($this->userdefined)) + { + foreach($this->userdefined as $key => $val) + { + $this->metaXml = preg_replace('', '', $this->metaXml); + $this->metaXml = preg_replace('/.*<\/meta:user-defined>/', '', $this->metaXml); + $this->metaXml = str_replace('', ''.htmlspecialchars($val).'', $this->metaXml); + } + } + } + /** * Update Manifest file according to added image files * * @param string $file Image file to add into manifest content + * @return void */ public function addImageToManifest($file) {