Tinebase_Export(_Doc) - allow subclasses to register twig function
authorPaul Mehrer <p.mehrer@metaways.de>
Fri, 11 Aug 2017 10:51:06 +0000 (12:51 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Fri, 11 Aug 2017 11:44:18 +0000 (13:44 +0200)
register a doc specific twig function addNewLine

Change-Id: I3b63f082b91cc5f945a970551bb5fb10891c7da3
Reviewed-on: http://gerrit.tine20.com/customers/5453
Tested-by: Jenkins CI (http://ci.tine20.com/) <tine20-jenkins@metaways.de>
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tine20/Tinebase/Export/Abstract.php
tine20/Tinebase/Export/Doc.php
tine20/Tinebase/Export/Richtext/TemplateProcessor.php

index 36f7d31..e752cb4 100644 (file)
@@ -564,6 +564,16 @@ abstract class Tinebase_Export_Abstract implements Tinebase_Record_IteratableInt
             return json_encode($string);
         });
 
+        $this->_addTwigFunctions();
+
+        $this->_twigTemplate = $this->_twigEnvironment->load($this->_templateFileName);
+    }
+
+    /**
+     * adds twig function to the twig environment to be used in the templates
+     */
+    protected function _addTwigFunctions()
+    {
         $locale = $this->_locale;
         $translate = $this->_translate;
         $this->_twigEnvironment->addFunction(new Twig_SimpleFunction('translate',
@@ -577,8 +587,6 @@ abstract class Tinebase_Export_Abstract implements Tinebase_Record_IteratableInt
             // TODO implement this!
             return $model;
         }));
-
-        $this->_twigTemplate = $this->_twigEnvironment->load($this->_templateFileName);
     }
 
     /**
index 7601c3c..def5427 100644 (file)
@@ -422,6 +422,7 @@ class Tinebase_Export_Doc extends Tinebase_Export_Abstract implements Tinebase_R
         $this->_docTemplate = $_processor;
 
         if (!isset($this->_subTwigTemplates[$subTempName])) {
+            /** @noinspection PhpUndefinedMethodInspection */
             $this->_twigEnvironment->getLoader()->addLoader(
                 new Tinebase_Twig_CallBackLoader($this->_templateFileName . $subTempName, $this->_getLastModifiedTimeStamp(),
                     array($this, '_getTwigSource')));
@@ -817,4 +818,17 @@ class Tinebase_Export_Doc extends Tinebase_Export_Abstract implements Tinebase_R
 
         return $this->_templateVariables;
     }
+
+    /**
+     * adds twig function to the twig environment to be used in the templates
+     */
+    protected function _addTwigFunctions()
+    {
+        parent::_addTwigFunctions();
+
+        $this->_twigEnvironment->addFunction(new Twig_SimpleFunction('addNewLine',
+            function ($str) {
+                return (is_scalar($str) && strlen($str) > 0) ? $str . "\x0B" : $str;
+            }));
+    }
 }
index 6fdd3c7..b0d26ef 100644 (file)
@@ -174,6 +174,15 @@ class Tinebase_Export_Richtext_TemplateProcessor extends \PhpOffice\PhpWord\Temp
             $this->zipClass->addFromString('word/_rels/footer' . $index . '.xml.rels', $data);
         }
 
+        // replace newline placeholder vertical tab (ascii 11)
+        foreach ($this->tempDocumentHeaders as &$xml) {
+            $xml = join('</w:t><w:br /><w:t>', mb_split("\x0B", $xml));
+        }
+        $this->tempDocumentMainPart = join('</w:t><w:br /><w:t>', mb_split("\x0B", $this->tempDocumentMainPart));
+        foreach ($this->tempDocumentFooters as &$xml) {
+            $xml = join('</w:t><w:br /><w:t>', mb_split("\x0B", $xml));
+        }
+
         return parent::save();
     }