Skip to content

Commit

Permalink
fix: return early if $data is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
eaCe committed Aug 6, 2023
1 parent 9d6beed commit 99a41f4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
\rex_extension::register('YFORM_DATA_LIST_LINKS', 'YFormExport\ExtensionPoints::YFORM_DATA_LIST_LINKS');

new Export();

/**
* register EP, attach messages.
*/
\rex_extension::register('YFORM_MANAGER_DATA_PAGE_HEADER', 'YFormExport\ExtensionPoints::YFORM_MANAGER_DATA_PAGE_HEADER');
}
1 change: 1 addition & 0 deletions lang/de_de.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yform_export_data_empty = Es wurde keine Daten zum Exportieren gefunden.
1 change: 1 addition & 0 deletions lang/en_gb.lang
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yform_export_data_empty = No data found to export.
17 changes: 15 additions & 2 deletions lib/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ private function setRelations(): void

if ($tableRelations) {
foreach ($tableRelations as $column => $field) {
$relations[$column] = rex_yform_value_be_manager_relation::getListValues($field->getElement('table'), $field->getElement('field'));
$relations[$column] = rex_yform_value_be_manager_relation::getListValues(
$field->getElement('table'),
$field->getElement('field'),
);
}

$i = 2;
Expand Down Expand Up @@ -190,6 +193,12 @@ private function exportTableSet(): void
/** set column types to format cells */
$this->setColumnTypes();

/** exit early if $data is empty */
if (empty($data)) {
rex::setProperty('yform_export_data_empty', true);
return;
}

/** set header labels */
$this->setHeader($data[0]);

Expand Down Expand Up @@ -281,7 +290,11 @@ private function exportTableSet(): void

$writer = new Xlsx($this->spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="' . urlencode(time() . '_' . $this->table->getTableName() . '.xlsx') . '"');
header(
'Content-Disposition: attachment; filename="' . urlencode(
time() . '_' . $this->table->getTableName() . '.xlsx',
) . '"',
);
$writer->save('php://output');
exit;
}
Expand Down
22 changes: 22 additions & 0 deletions lib/ExtensionPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace YFormExport;

use rex;
use rex_exception;
use rex_extension_point;
use rex_i18n;
use rex_url;
use rex_view;
use rex_yform_manager_table;

class ExtensionPoints
Expand All @@ -23,6 +25,7 @@ public static function YFORM_DATA_LIST_LINKS(rex_extension_point $ep): void // p
$linkParams = [
'func' => 'yform_table_export',
'table' => $table->getTableName(),
'table_name' => $table->getTableName(),
];

$item = [];
Expand All @@ -36,4 +39,23 @@ public static function YFORM_DATA_LIST_LINKS(rex_extension_point $ep): void // p
$linkSets['table_links'][] = $item;
$ep->setSubject($linkSets);
}

/**
* add messages.
* @throws rex_exception
*/
public static function YFORM_MANAGER_DATA_PAGE_HEADER(rex_extension_point $ep): void // phpcs:ignore
{
$tableHeader = $ep->getSubject();
$message = '';

/**
* show message if no data is available.
*/
if (rex::hasProperty('yform_export_data_empty')) {
$message = rex_view::error(rex_i18n::msg('yform_export_data_empty'));
}

$ep->setSubject($tableHeader . $message);
}
}
7 changes: 4 additions & 3 deletions package.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package: yform_export
version: '0.0.1'
version: '0.0.11'
author: eaCe
supportpage: https://github.com/eaCe/yform_export

requires:
packages:
yform: '^4.0.0'
redaxo: '^5.10.0'
redaxo: '^5.12.0'
php:
version: '>=7.4'
version: '>=8.0'

installer_ignore:
- node_modules
- .git
Expand Down

0 comments on commit 99a41f4

Please sign in to comment.