From 71dd235408c6577ed73848c1bbe1b8163cf6ed56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Klatt?= Date: Wed, 30 Jun 2021 15:34:49 +0200 Subject: [PATCH] TE-9182 Added possibility to download GUI tables as CSV file (#8346) TE-9182 Export Table Views --- composer.json | 2 +- .../Controller/DownloadController.php | 28 ++++++++++++ .../Communication/Table/CustomerTable.php | 44 +++++++++++++++++++ .../Customer/Presentation/Index/index.twig | 1 + 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/Spryker/Zed/Customer/Communication/Controller/DownloadController.php diff --git a/composer.json b/composer.json index b4c16e69..ee7de072 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "php": ">=7.3", "spryker/country": "^3.0.0", "spryker/customer-extension": "^1.3.0", - "spryker/gui": "^3.17.0", + "spryker/gui": "^3.39.0", "spryker/kernel": "^3.52.0", "spryker/locale": "^3.0.0", "spryker/mail": "^4.6.0", diff --git a/src/Spryker/Zed/Customer/Communication/Controller/DownloadController.php b/src/Spryker/Zed/Customer/Communication/Controller/DownloadController.php new file mode 100644 index 00000000..0ad78f7b --- /dev/null +++ b/src/Spryker/Zed/Customer/Communication/Controller/DownloadController.php @@ -0,0 +1,28 @@ +getFactory()->createCustomerTable()->streamDownload(); + } +} diff --git a/src/Spryker/Zed/Customer/Communication/Table/CustomerTable.php b/src/Spryker/Zed/Customer/Communication/Table/CustomerTable.php index 5a15b86a..c4c29da9 100644 --- a/src/Spryker/Zed/Customer/Communication/Table/CustomerTable.php +++ b/src/Spryker/Zed/Customer/Communication/Table/CustomerTable.php @@ -9,12 +9,15 @@ use Orm\Zed\Customer\Persistence\Map\SpyCustomerTableMap; use Orm\Zed\Customer\Persistence\SpyCustomer; +use Propel\Runtime\ActiveQuery\ModelCriteria; +use Propel\Runtime\ActiveRecord\ActiveRecordInterface; use Propel\Runtime\Collection\ObjectCollection; use Spryker\Zed\Customer\Communication\Table\PluginExecutor\CustomerTableExpanderPluginExecutorInterface; use Spryker\Zed\Customer\Dependency\Service\CustomerToUtilDateTimeServiceInterface; use Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface; use Spryker\Zed\Gui\Communication\Table\AbstractTable; use Spryker\Zed\Gui\Communication\Table\TableConfiguration; +use Spryker\Zed\PropelOrm\Business\Runtime\ActiveQuery\Criteria; class CustomerTable extends AbstractTable { @@ -99,6 +102,47 @@ protected function configure(TableConfiguration $config) return $config; } + /** + * @return string[] + */ + protected function getCsvHeaders(): array + { + return [ + static::COL_ID_CUSTOMER => '#', + static::COL_CREATED_AT => 'Registration Date', + static::COL_EMAIL => 'Email', + static::COL_LAST_NAME => 'Last Name', + static::COL_FIRST_NAME => 'First Name', + static::COL_STATUS => 'Status', + ]; + } + + /** + * @return \Orm\Zed\Customer\Persistence\SpyCustomerQuery + */ + protected function getDownloadQuery(): ModelCriteria + { + $customerQuery = $this->prepareQuery(); + $customerQuery->orderBy(SpyCustomerTableMap::COL_ID_CUSTOMER, Criteria::DESC); + + return $customerQuery; + } + + /** + * @param \Orm\Zed\Customer\Persistence\SpyCustomer $entity + * + * @return array + */ + protected function formatCsvRow(ActiveRecordInterface $entity): array + { + $customerRow = $entity->toArray(); + + $customerRow[static::COL_CREATED_AT] = $this->utilDateTimeService->formatDateTime($entity->getCreatedAt()); + $customerRow[static::COL_STATUS] = $entity->getRegistered() ? 'Verified' : 'Unverified'; + + return $customerRow; + } + /** * @param \Spryker\Zed\Gui\Communication\Table\TableConfiguration $config * diff --git a/src/Spryker/Zed/Customer/Presentation/Index/index.twig b/src/Spryker/Zed/Customer/Presentation/Index/index.twig index 4333331c..3a5ce850 100644 --- a/src/Spryker/Zed/Customer/Presentation/Index/index.twig +++ b/src/Spryker/Zed/Customer/Presentation/Index/index.twig @@ -8,6 +8,7 @@ {% block action %} {{ createActionButton('/customer/add', 'Add Customer' | trans) }} + {{ createActionButton('/customer/download', 'Download as CSV' | trans) }} {% endblock %} {% block content %}