Skip to content

Commit

Permalink
FEATURE: Add handling of array fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-K committed Nov 8, 2021
1 parent 805500c commit 8c7c4dc
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions Classes/Controller/DatabaseStorageController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file controls the backend of the database storage.
*
Expand All @@ -12,6 +13,7 @@
* @license https://github.com/die-wegmeister/Wegmeister.DatabaseStorage/blob/master/LICENSE GPL-3.0-or-later
* @link https://github.com/die-wegmeister/Wegmeister.DatabaseStorage
*/

namespace Wegmeister\DatabaseStorage\Controller;

use Neos\Flow\Annotations as Flow;
Expand Down Expand Up @@ -138,20 +140,14 @@ public function showAction(string $identifier)
$properties = $entry->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('<ul><li>%s</li></ul>', implode('</li><li>', $value));
} else {
$value = '-';
$value = $this->getStringValue($value);
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 '-';
}
}

0 comments on commit 8c7c4dc

Please sign in to comment.