Calendar Export - fix docx export
authorPaul Mehrer <p.mehrer@metaways.de>
Thu, 1 Jun 2017 12:05:43 +0000 (14:05 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Thu, 1 Jun 2017 13:55:23 +0000 (15:55 +0200)
Change-Id: Id47aaf6ccf68f97eda8b03e02046229f5c095116
Reviewed-on: http://gerrit.tine20.com/customers/4799
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tests/tine20/Calendar/Export/DocTest.php
tine20/Calendar/Export/Doc.php
tine20/Calendar/Export/GenericTrait.php
tine20/Calendar/Export/templates/SimpleDocSheet.docx
tine20/Tinebase/Export/Abstract.php
tine20/Tinebase/Export/Doc.php

index 1323e51..f9e9c5d 100644 (file)
@@ -4,11 +4,10 @@
  *
  * @package     Calendar
  * @license     http://www.gnu.org/licenses/agpl.html
- * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2016-2017 Metaways Infosystems GmbH (http://www.metaways.de)
  * @author      Cornelius WeiƟ <c.weiss@metaways.de>
  */
 
-use \PhpOffice\PhpWord;
 
 /**
  * Calendar Doc generation class tests
@@ -24,23 +23,6 @@ class Calendar_Export_DocTest extends Calendar_TestCase
 {
     public function testExportSimpleDocSheet()
     {
-        $this->markTestSkipped('FIXME');
-
-        // skip tests for php7
-        // ERROR: PHP Fatal error:  Cannot use PhpOffice\PhpWord\Shared\String as String because 'String' is a special
-        //  class name in /usr/local/share/tine20.git/tine20/vendor/phpoffice/phpword/src/PhpWord/TemplateProcessor.php
-        //  on line 23
-        //if (PHP_VERSION_ID >= 70000) {
-//            $this->markTestSkipped('FIXME 0011730: fix doc export for php7');
-        //}
-
-        // make sure definition is imported
-        $definitionFile = __DIR__ . '/../../../../tine20/Calendar/Export/definitions/cal_default_doc_sheet.xml';
-        $calendarApp = Tinebase_Application::getInstance()->getApplicationByName('Calendar');
-        Tinebase_ImportExportDefinition::getInstance()->updateOrCreateFromFilename($definitionFile, $calendarApp, 'cal_default_doc_sheet');
-
-//        Tinebase_TransactionManager::getInstance()->commitTransaction($this->_transactionId);
-
         // @TODO have some demodata to export here
         $filter = new Calendar_Model_EventFilter(array(
 //            array('field' => 'period', 'operator' => 'within', 'value' => array(
@@ -48,13 +30,12 @@ class Calendar_Export_DocTest extends Calendar_TestCase
 //                'until' => ''
 //            ))
         ));
-        $doc = new Calendar_Export_DocSheet($filter);
+        $doc = new Calendar_Export_Doc($filter);
         $doc->generate();
 
         $tempfile = tempnam(Tinebase_Core::getTempDir(), __METHOD__ . '_') . '.docx';
         $doc->save($tempfile);
 
         $this->assertGreaterThan(0, filesize($tempfile));
-//        `open $tempfile`;
     }
 }
\ No newline at end of file
index 8f3af13..00b0236 100644 (file)
@@ -44,60 +44,6 @@ class Calendar_Export_Doc extends Tinebase_Export_Doc
     }
 
     /**
-     * export single record
-     * @param Tinebase_Record_Interface $record
-     */
-    public function processRecord(Tinebase_Record_Interface $record)
-    {
-        // progressive rendering is not possible with this advanced layout
-        // so we split into days at this stage only
-        $dayString = Tinebase_Translation::dateToStringInTzAndLocaleFormat($record->dtstart, null, null, $this->_config->dateformat);
-        $this->_daysEventMatrix[$dayString][] = $record;
-    }
-
-    /**
-     * do the rendering
-     *
-     * @param array $result
-     */
-    protected function _onAfterExportRecords($result)
-    {
-        $templateProcessor = $this->getDocument();
-
-        $this->_loadTwig();
-        $this->_hasTemplate = true;
-        $this->_dumpRecords = false;
-
-        // first step: generate layout
-        $dayblock = $templateProcessor->cloneBlock('DAYBLOCK', 1, false);
-        $seperator = $templateProcessor->cloneBlock('SEPARATOR', 1, false);
-
-        if (preg_match('/<w:tbl.*\${([^}]+)}/is', $dayblock, $matches)) {
-            $this->_cloneRow = $matches[1];
-        } else {
-            throw new Tinebase_Exception_UnexpectedValue('DAYBLOCK needs to contain a replacement variable');
-        }
-
-        $dayCount = count($this->_daysEventMatrix);
-        $daysblock = $dayCount ? $dayblock : '';
-        for ($i=1; $i<$dayCount; $i++) {
-            $daysblock .= $seperator;
-            $daysblock .= $dayblock;
-        }
-
-        $templateProcessor->replaceBlock('DAYBLOCK', $daysblock);
-        $templateProcessor->deleteBlock('SEPARATOR');
-
-        // second step: render events
-        foreach ($this->_daysEventMatrix as $dayString => $dayEvents) {
-            $this->processDay($dayString, $dayEvents);
-
-        }
-
-        Tinebase_Export_Abstract::_onAfterExportRecords($result);
-    }
-
-    /**
      * @param array $context
      * @return array
      */
@@ -110,23 +56,4 @@ class Calendar_Export_Doc extends Tinebase_Export_Doc
             )
         ));
     }
-
-    public function processDay($dayString, $dayEvents)
-    {
-        $templateProcessor = $this->getDocument();
-
-        $templateProcessor->setValue('day', $dayString, 1);
-        $templateProcessor->cloneRow($this->_cloneRow, count($dayEvents));
-
-        $this->processDayEvents($dayEvents);
-    }
-
-    public function processDayEvents($dayEvents)
-    {
-        $this->_rowCount = 0;
-        foreach($dayEvents as $idx => $event) {
-            ++$this->_rowCount;
-            parent::_processRecord($event);
-        }
-    }
 }
\ No newline at end of file
index 36099a6..ec75557 100644 (file)
@@ -126,14 +126,23 @@ trait Calendar_Export_GenericTrait
 
         $this->_sortRecords($records);
 
-        $this->_resolveRecords($records);
-        foreach($records as $idx => $record) {
-            $this->processRecord($record, $idx);
-        }
+        if ($this instanceof Tinebase_Export_Abstract) {
+            $this->_records = $records;
+            $this->_groupByProperty = 'dtstart';
+            $this->_groupByProcessor = function(&$val) {
+                $val = $val->format('Y-m-d');
+            };
+            parent::_exportRecords();
+        } else {
+            $this->_resolveRecords($records);
+            foreach ($records as $idx => $record) {
+                $this->processRecord($record, $idx);
+            }
 
-        $result = array();
+            $result = array();
 
-        $this->_onAfterExportRecords($result);
+            $this->_onAfterExportRecords($result);
+        }
     }
 
     protected function _sortRecords(&$records)
index e855895..fcabd2c 100644 (file)
Binary files a/tine20/Calendar/Export/templates/SimpleDocSheet.docx and b/tine20/Calendar/Export/templates/SimpleDocSheet.docx differ
index afef4d6..89c0110 100644 (file)
@@ -193,6 +193,8 @@ abstract class Tinebase_Export_Abstract implements Tinebase_Record_IteratableInt
 
     protected $_groupByProperty = null;
 
+    protected $_groupByProcessor = null;
+
     protected $_currentRowType = null;
 
     protected $_getRelations = false;
@@ -585,13 +587,19 @@ abstract class Tinebase_Export_Abstract implements Tinebase_Record_IteratableInt
 
         $first = $this->_firstIteration;
         foreach ($_records as $record) {
-            if (null !== $this->_groupByProperty && (true === $first ||
-                    $this->_lastGroupValue !== $record->{$this->_groupByProperty})) {
-                $this->_lastGroupValue = $record->{$this->_groupByProperty};
-                if (false === $first) {
-                    $this->_endGroup();
+            if (null !== $this->_groupByProperty) {
+                $propertyValue = $record->{$this->_groupByProperty};
+                if (null !== $this->_groupByProcessor) {
+                    $fn = $this->_groupByProcessor;
+                    $fn($propertyValue);
+                }
+                if (true === $first || $this->_lastGroupValue !== $propertyValue) {
+                    $this->_lastGroupValue = $propertyValue;
+                    if (false === $first) {
+                        $this->_endGroup();
+                    }
+                    $this->_startGroup();
                 }
-                $this->_startGroup();
                 // TODO fix this?
                 //$this->_writeGroupHeading($record);
             }
index 6683d01..7231841 100644 (file)
@@ -152,6 +152,7 @@ class Tinebase_Export_Doc extends Tinebase_Export_Abstract implements Tinebase_R
             return;
         }
 
+        $this->_firstIteration = true;
         $this->_currentProcessor = $this->_dataSources[$_name];
         $this->_rowCount = 0;
     }