From 8c7c4dcb7d951dbc8b9820f67dc398a4a4369abc Mon Sep 17 00:00:00 2001 From: Benjamin Klix Date: Mon, 8 Nov 2021 17:26:30 +0100 Subject: [PATCH] FEATURE: Add handling of array fields --- .../Controller/DatabaseStorageController.php | 75 ++++++++++++------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/Classes/Controller/DatabaseStorageController.php b/Classes/Controller/DatabaseStorageController.php index 6d1b83d..595b209 100644 --- a/Classes/Controller/DatabaseStorageController.php +++ b/Classes/Controller/DatabaseStorageController.php @@ -1,4 +1,5 @@ getProperties(); foreach ($properties as &$value) { - if ($value instanceof PersistentResource) { - $value = $this->resourceManager->getPublicPersistentResourceUri($value) ?: '-'; - } elseif (is_string($value)) { - } elseif (is_object($value) && method_exists($value, '__toString')) { - $value = (string)$value; - } elseif (isset($value['dateFormat'], $value['date'])) { - $timezone = null; - if(isset($value['timezone'])){ - $timezone = new \DateTimeZone($value['timezone']); + if (is_array($value)) { + // Todo fix this for deep arrays + foreach ($value as &$innerValue) { + $innerValue = $this->getStringValue($innerValue); } - $dateTime = \DateTime::createFromFormat($value['dateFormat'], $value['date'], $timezone); - $value = $dateTime->format($this->settings['datetimeFormat']); + $value = sprintf('', implode('
  • ', $value)); } else { - $value = '-'; + $value = $this->getStringValue($value); } } @@ -258,22 +254,7 @@ public function exportAction(string $identifier, string $writerType = 'Xlsx', bo $values = []; foreach ($entry->getProperties() as $value) { - if ($value instanceof PersistentResource) { - $values[] = $this->resourceManager->getPublicPersistentResourceUri($value) ?: '-'; - } elseif (is_string($value)) { - $values[] = $value; - } elseif (is_object($value) && method_exists($value, '__toString')) { - $values[] = (string)$value; - } elseif (isset($value['dateFormat'], $value['date'])) { - $timezone = null; - if(isset($value['timezone'])){ - $timezone = new \DateTimeZone($value['timezone']); - } - $dateTime = \DateTime::createFromFormat($value['dateFormat'], $value['date'], $timezone); - $values[] = $dateTime->format($this->settings['datetimeFormat']); - } else { - $values[] = '-'; - } + $values[] = $this->getStringValue($value); } if ($exportDateTime) { @@ -326,4 +307,42 @@ public function exportAction(string $identifier, string $writerType = 'Xlsx', bo $writer->save('php://output'); exit; } + + /** + * Internal function to replace value with a string for export / listing. + * + * @param mixed $value The database column value. + * @param int $indent The level of indentation (for array values). + * + * @return string + */ + protected function getStringValue($value, int $indent = 0): string + { + if ($value instanceof PersistentResource) { + return $this->resourceManager->getPublicPersistentResourceUri($value) ?: '-'; + } elseif (is_string($value)) { + return $value; + } elseif (is_object($value) && method_exists($value, '__toString')) { + return (string)$value; + } elseif (isset($value['dateFormat'], $value['date'])) { + $timezone = null; + if (isset($value['timezone'])) { + $timezone = new \DateTimeZone($value['timezone']); + } + $dateTime = \DateTime::createFromFormat($value['dateFormat'], $value['date'], $timezone); + return $dateTime->format($this->settings['datetimeFormat']); + } elseif (is_array($value)) { + foreach ($value as &$innerValue) { + $innerValue = $this->getStringValue($innerValue, $indent + 1); + } + $prefix = str_repeat(' ', $indent * 2) . '- '; + return sprintf( + '%s%s', + $prefix, + implode("\r\n" . $prefix, $value) + ); + } + + return '-'; + } }