From b44f182ae537c93eb57007fdf1b3c491b75e0b6b Mon Sep 17 00:00:00 2001 From: Luigi Foscari Date: Thu, 20 May 2021 16:39:32 +0200 Subject: [PATCH 1/2] switched to JSON from CSV --- src/Export.php | 66 ++++++-------------------------------------------- 1 file changed, 8 insertions(+), 58 deletions(-) diff --git a/src/Export.php b/src/Export.php index 4f9d6c1..34d73a7 100644 --- a/src/Export.php +++ b/src/Export.php @@ -6,31 +6,26 @@ use Illuminate\Http\Response; use Illuminate\Support\Arr; use Illuminate\Support\Collection; -use League\Csv\Writer; use Statamic\Actions\Action; class Export extends Action { /** - * Download items as CSV. + * Download items as JSON. * * @param $items * @param $values * @return false|Response - * @throws \League\Csv\CannotInsertRecord */ public function download($items, $values) { - $headers = $this::getHeaders($items); - $entries = $this::getEntries($headers, $items); + $data = $items->map(function($item) { + return $item->data(); + }); - $csv = Writer::createFromString(''); - $csv->insertOne($headers->toArray()); - $csv->insertAll($entries->toArray()); - - return new Response($csv->getContent(), 200, [ - 'Content-Disposition' => 'attachment; filename="' . $this::getFileName() . '"', - ]); + return response() + ->json($data) + ->header('Content-Disposition', 'attachment; filename="' . $this::getFileName() . '"'); } public function authorize($user, $item) @@ -38,51 +33,6 @@ public function authorize($user, $item) return $user->can('view', $item); } - /** - * Get array keys as headers. - * - * @param Collection $items - * - * @return Collection - */ - private static function getHeaders(Collection $items) - { - $headers = new Collection; - - foreach ($items as $item) { - foreach ($item->data()->keys() as $key) { - $headers->push($key); - } - } - - return $headers = $headers->unique(); - } - - /** - * Get entries values by headers. - * - * @param Collection $headers - * @param Collection $items - * - * @return Collection - */ - private static function getEntries(Collection $headers, Collection $items) - { - $data = new Collection; - - foreach ($items as $item) { - $itemData = []; - - foreach ($headers as $header) { - Arr::set($itemData, $header, Arr::get($item->data(), $header)); - } - - $data->push($itemData); - } - - return $data; - } - /** * Get export file name. * @@ -90,6 +40,6 @@ private static function getEntries(Collection $headers, Collection $items) */ private static function getFileName() { - return sprintf('Export %s.csv', Carbon::now()->toDateTimeString()); + return sprintf('Export %s.json', Carbon::now()->toDateTimeString()); } } From 715a6af6e2abf1726876053bf33d263ef19ffa3e Mon Sep 17 00:00:00 2001 From: Luigi Foscari Date: Thu, 20 May 2021 17:01:44 +0200 Subject: [PATCH 2/2] updated readme and deps --- README.md | 4 ++-- composer.json | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 065a5ed..4799349 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

-This plugin easily exports your entries to CSV. Select the entries you like to export, and select "Export" from the actions. +This plugin easily exports your entries to JSON. Select the entries you like to export, and select "Export" from the actions. ## Installation Add the package using composer. And you're done! 😎 @@ -11,5 +11,5 @@ Add the package using composer. And you're done! 😎 composer require youfront/statamic-export ``` -# License +# License This plugin is published under the MIT license. Feel free to use it and remember to spread love 😍 diff --git a/composer.json b/composer.json index 3367398..9c785ec 100644 --- a/composer.json +++ b/composer.json @@ -2,8 +2,7 @@ "name": "youfront/statamic-export", "description": "Statamic Export", "require": { - "statamic/cms": "^3.0.0", - "league/csv": "^9.0.0" + "statamic/cms": "^3.0.0" }, "autoload": { "psr-4": {