From 9d0539211a95562da641ebeb455ee5f547ae0304 Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:19:20 +0000 Subject: [PATCH 1/9] update composer.json added maatwebsite/excel --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3367398..3edcb82 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,8 @@ "description": "Statamic Export", "require": { "statamic/cms": "^3.0.0", - "league/csv": "^9.0.0" + "league/csv": "^9.0.0", + "maatwebsite/excel": "^3.1" }, "autoload": { "psr-4": { From f8b65128b7202b12d8fe2694256f57c2001ee2dd Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:20:06 +0000 Subject: [PATCH 2/9] Update ServiceProvider.php Added configuration file and publishing --- src/ServiceProvider.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 10e9708..d3f0034 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -8,6 +8,12 @@ class ServiceProvider extends AddonServiceProvider { public function register() { + $this->mergeConfigFrom(__DIR__ . '/../config/statamic-export.php', 'statamic-export'); + + $this->publishes([ + __DIR__ . '/../config/statamic-export.php' => config_path('statamic-export.php'), + ], 'statamic-export-config'); + Export::register(); } } From 7e13222b8557689213468b2967a1a8c58224d3fe Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:21:31 +0000 Subject: [PATCH 3/9] Create statamic-export.php Configuration file with defaults and per collection option, along with excluded columns --- config/statamic-export.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 config/statamic-export.php diff --git a/config/statamic-export.php b/config/statamic-export.php new file mode 100644 index 0000000..50eef84 --- /dev/null +++ b/config/statamic-export.php @@ -0,0 +1,14 @@ + 'csv', + 'collections' => [ + // 'collection_name' => 'xlsx' + ], + 'excluded_columns' => [ + 'blueprint', + 'updated_by', + ] +]; From 6eb29c1f383007e548691fa3c2167b73c92ec4b7 Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:22:50 +0000 Subject: [PATCH 4/9] Update Export.php Excluded columns moved to config. Option to export via csv, json or xlsx. Defaults and collections options read from config. --- src/Export.php | 64 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/src/Export.php b/src/Export.php index 69e9584..1ebc20a 100644 --- a/src/Export.php +++ b/src/Export.php @@ -9,28 +9,22 @@ use League\Csv\CannotInsertRecord; use League\Csv\Exception; use League\Csv\Writer; +use Maatwebsite\Excel\Facades\Excel; use Statamic\Actions\Action; +use Throwable; class Export extends Action { - /** - * Exclude statamic fields - */ - const EXCLUDED_COLUMNS = [ - 'blueprint', - 'updated_by', - ]; - /** * Title */ public static function title() { - return __('CSV Export'); + return __('Export'); } /** - * Download items as CSV. + * Download items, based on configuration for specific collections. Defaults to CSV. * * @param $items * @param $values @@ -40,16 +34,34 @@ public static function title() */ public function download($items, $values) { + $export_type = $this::getExportType(); + $headers = $this::getHeaders($items); $entries = $this::getEntries($headers, $items); - - $csv = Writer::createFromString(''); - $csv->insertOne($headers->toArray()); - $csv->insertAll($entries->toArray()); - - return new Response($csv->toString(), 200, [ - 'Content-Disposition' => 'attachment; filename="' . $this::getFileName() . '"', - ]); + switch ($export_type): + case "csv": + $csv = Writer::createFromString(''); + $csv->insertOne($headers->toArray()); + $csv->insertAll($entries->toArray()); + + return new Response($csv->toString(), 200, [ + 'Content-Disposition' => 'attachment; filename="' . $this::getFileName($export_type) . '"', + ]); + + break; + case "json": + $data = $items->map(function ($item) { + return $item->data()->except(config("statamic-export.excluded_columns", [])); + }); + + return response() + ->json($data) + ->header('Content-Disposition', 'attachment; filename="' . $this::getFileName($export_type) . '"'); + break; + case "xlsx": + return Excel::download(new ArrayExport($headers->toArray(), $entries->toArray()), $this::getFileName($export_type)); + break; + endswitch; } public function authorize($user, $item) @@ -72,7 +84,7 @@ private static function getHeaders(Collection $items) foreach ($items as $item) { foreach ($item->data()->keys() as $key) { - if (in_array($key, static::EXCLUDED_COLUMNS)) { + if (in_array($key, config("statamic-export.excluded_columns", []))) { continue; } @@ -117,8 +129,18 @@ private static function getEntries(Collection $headers, Collection $items) * * @return string */ - private static function getFileName() + private static function getFileName($export_type) { - return sprintf('Export %s.csv', Carbon::now()->toDateTimeString()); + return sprintf('Export %s.%s', Carbon::now()->toDateTimeString(), $export_type); + } + + private static function getExportType() + { + try { + $export_type = config("statamic-export.collections.{$items[0]->collection->handle}", config("statamic-export.default", "csv")); + } catch (Throwable $t) { + $export_type = config("statamic-export.default", "csv"); + } + return $export_type; } } From 1b8a92393f6909ed36186c8af39c5b91feb109fd Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:23:25 +0000 Subject: [PATCH 5/9] Create ArrayExport.php Required file to Export to XLSX from array values --- src/ArrayExport.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/ArrayExport.php diff --git a/src/ArrayExport.php b/src/ArrayExport.php new file mode 100644 index 0000000..79c8ad1 --- /dev/null +++ b/src/ArrayExport.php @@ -0,0 +1,28 @@ +headings = $headings; + $this->entries = $entries; + } + + public function array(): array + { + return $this->entries; + } + + public function headings(): array + { + return $this->headings; + } +} From 4be1420f4f166785529d93512b92e39ac5ca2600 Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:33:27 +0000 Subject: [PATCH 6/9] Update README.md Added the configuration file publishing --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 065a5ed..b7b3a61 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,15 @@ This plugin easily exports your entries to CSV. Select the entries you like to export, and select "Export" from the actions. ## Installation -Add the package using composer. And you're done! 😎 +Add the package using composer. ```bash composer require youfront/statamic-export ``` +Publish the configuration file. And you're done! 😎 +```bash +php artisan vendor:publish --tag=statamic-export-config +``` + # License This plugin is published under the MIT license. Feel free to use it and remember to spread love 😍 From d85788bb4aad6573e2ab00ed1044556548d2ef3d Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:38:39 +0000 Subject: [PATCH 7/9] Update Export.php --- src/Export.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Export.php b/src/Export.php index 1ebc20a..60afea3 100644 --- a/src/Export.php +++ b/src/Export.php @@ -34,7 +34,11 @@ public static function title() */ public function download($items, $values) { - $export_type = $this::getExportType(); + try { + $export_type = config("statamic-export.collections.{$items[0]->collection->handle}", config("statamic-export.default", "csv")); + } catch (Throwable $t) { + $export_type = config("statamic-export.default", "csv"); + } $headers = $this::getHeaders($items); $entries = $this::getEntries($headers, $items); @@ -133,14 +137,4 @@ private static function getFileName($export_type) { return sprintf('Export %s.%s', Carbon::now()->toDateTimeString(), $export_type); } - - private static function getExportType() - { - try { - $export_type = config("statamic-export.collections.{$items[0]->collection->handle}", config("statamic-export.default", "csv")); - } catch (Throwable $t) { - $export_type = config("statamic-export.default", "csv"); - } - return $export_type; - } } From 73baf0a000f5d5db19b3a6b14428e5dede5e081a Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:46:16 +0000 Subject: [PATCH 8/9] Update Export.php Make sure the button only appears when there are entries. Fixes the error of trying to export forms from the form list which is not possible. --- src/Export.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Export.php b/src/Export.php index 60afea3..3670bdd 100644 --- a/src/Export.php +++ b/src/Export.php @@ -11,6 +11,7 @@ use League\Csv\Writer; use Maatwebsite\Excel\Facades\Excel; use Statamic\Actions\Action; +use Statamic\Entries\Entry; use Throwable; class Export extends Action @@ -99,6 +100,11 @@ private static function getHeaders(Collection $items) return $headers = $headers->unique(); } + public function visibleTo($item) + { + return $item instanceof Entry; + } + /** * Get entries values by headers. * From ab76435f8af85ffd024e9951b69db3e2cf8c03d1 Mon Sep 17 00:00:00 2001 From: Gustavo Silva Date: Tue, 7 Feb 2023 17:52:21 +0000 Subject: [PATCH 9/9] Update Export.php Fix for Form Submissions along with Collection Entries --- src/Export.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Export.php b/src/Export.php index 3670bdd..0576622 100644 --- a/src/Export.php +++ b/src/Export.php @@ -12,6 +12,7 @@ use Maatwebsite\Excel\Facades\Excel; use Statamic\Actions\Action; use Statamic\Entries\Entry; +use Statamic\Forms\Submission; use Throwable; class Export extends Action @@ -102,7 +103,7 @@ private static function getHeaders(Collection $items) public function visibleTo($item) { - return $item instanceof Entry; + return $item instanceof Entry || $item instanceof Submission; } /**