Skip to content

Commit

Permalink
FIX: Prevent errors for fields starting with equal sign (=)
Browse files Browse the repository at this point in the history
PhpSpreadsheet will try to calculate a formula on those fields which will probably result in not being able to download that whole list.
  • Loading branch information
Benjamin-K committed Dec 7, 2023
1 parent d4c7c6a commit ba2019f
Showing 1 changed file with 67 additions and 38 deletions.
105 changes: 67 additions & 38 deletions Classes/Controller/DatabaseStorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Wegmeister\DatabaseStorage\Domain\Model\DatabaseStorage;
use Wegmeister\DatabaseStorage\Domain\Repository\DatabaseStorageRepository;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\IOFactory;
Expand Down Expand Up @@ -215,8 +216,6 @@ public function exportAction(string $identifier, string $writerType = 'Xlsx', bo

$entries = $this->databaseStorageRepository->findByStorageidentifier($identifier)->toArray();

$dataArray = [];

$spreadsheet = new Spreadsheet();

$spreadsheet->getProperties()
Expand All @@ -225,57 +224,57 @@ public function exportAction(string $identifier, string $writerType = 'Xlsx', bo
->setSubject($this->settings['subject']);

$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setTitle($this->settings['title']);
$activeSheet = $spreadsheet->getActiveSheet();
$activeSheet->setTitle($this->settings['title']);

$titles = [];
$columns = 0;
foreach ($entries[0]->getProperties() as $title => $value) {
$titles[] = $title;
$columnLetter = $this->getColumnLetter($columns);
$activeSheet
->getCell($columnLetter . '1')
->setValueExplicit($title, DataType::TYPE_STRING);
$columns++;
}
if ($exportDateTime) {
// TODO: Translate title for datetime
$titles[] = 'DateTime';
$title = 'DateTime';
$activeSheet
->getCell($columnLetter . '1')
->setValueExplicit($title, DataType::TYPE_STRING);
$columns++;
}

$dataArray[] = $titles;

// Set styles for titles (bold and centered)
$lastColumnLetter = $this->getColumnLetter($columns - 1);
$columnStyle = $activeSheet->getStyle('A1:' . $lastColumnLetter . '1');
$columnStyle->getFont()->setBold(true);
$columnStyle->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER)
->setVertical(Alignment::VERTICAL_CENTER);

foreach ($entries as $entry) {
$values = [];
foreach ($entries as $i => $entry) {
$columnIndex = 0;

foreach ($entry->getProperties() as $value) {
$values[] = $this->getStringValue($value);
$columnLetter = $this->getColumnLetter($columnIndex);
$value = $this->getStringValue($value);

// Use setValueExplicit to prevent Excel from interpreting values as formulas.
$activeSheet
->getCell($columnLetter . ($i + 2))
->setValueExplicit($value, DataType::TYPE_STRING);
$columnIndex++;
}

if ($exportDateTime) {
$values[] = $entry->getDateTime()->format($this->settings['datetimeFormat']);
}

$dataArray[] = $values;
}

$spreadsheet->getActiveSheet()->fromArray($dataArray);

// Set headlines bold
$prefixIndex = 64;
$prefixKey = '';
for ($i = 0; $i < $columns; $i++) {
$index = $i % 26;
$columnStyle = $spreadsheet->getActiveSheet()->getStyle($prefixKey . chr(65 + $index) . '1');
$columnStyle->getFont()->setBold(true);
$columnStyle->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER)
->setVertical(Alignment::VERTICAL_CENTER);

if ($index + 1 > 25) {
$prefixIndex++;
$prefixKey = chr($prefixIndex);
$columnLetter = $this->getColumnLetter($columnIndex);
$value = $entry->getDateTime()->format($this->settings['datetimeFormat']);
$activeSheet
->getCell($columnLetter . ($i + 2))
->setValueExplicit($value, DataType::TYPE_STRING);
}
}


if (ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
Expand Down Expand Up @@ -310,20 +309,33 @@ public function exportAction(string $identifier, string $writerType = 'Xlsx', bo
*/
protected function getStringValue($value, int $indent = 0): string
{
// For resources return the public uri.
if ($value instanceof PersistentResource) {
return $this->resourceManager->getPublicPersistentResourceUri($value) ?: '-';
} elseif (is_string($value)) {
}

// Strings should be return as is.
if (is_string($value)) {
return $value;
} elseif (is_object($value) && method_exists($value, '__toString')) {
}

// For any object that has a `__toString` method, return the string representation.
if (is_object($value) && method_exists($value, '__toString')) {
return (string)$value;
} elseif (isset($value['dateFormat'], $value['date'])) {
}

// For DateTime objects, return the formatted date as defined in Settings.yaml.
if (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)) {
}

// For arrays, return the entries as a list with indentation.
if (is_array($value)) {
foreach ($value as &$innerValue) {
$innerValue = $this->getStringValue($innerValue, $indent + 1);
}
Expand All @@ -335,6 +347,23 @@ protected function getStringValue($value, int $indent = 0): string
);
}

// If all else fails, return a dash.
return '-';
}

/**
* Get column letter for a given index.
* @param int $index The index to get the prefix for.
* @return string
*/
protected function getColumnLetter(int $index): string
{
$prefixLetter = '';
if ($index > 25) {
$prefixLetter = chr(floor($index / 26) + 64);
}
$letter = $prefixLetter . chr(($index % 26) + 65);

return $letter;
}
}

0 comments on commit ba2019f

Please sign in to comment.