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 😍 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": { 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', + ] +]; 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; + } +} diff --git a/src/Export.php b/src/Export.php index 69e9584..0576622 100644 --- a/src/Export.php +++ b/src/Export.php @@ -9,28 +9,24 @@ use League\Csv\CannotInsertRecord; use League\Csv\Exception; use League\Csv\Writer; +use Maatwebsite\Excel\Facades\Excel; use Statamic\Actions\Action; +use Statamic\Entries\Entry; +use Statamic\Forms\Submission; +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 +36,38 @@ public static function title() */ public function download($items, $values) { + 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); - - $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 +90,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; } @@ -83,6 +101,11 @@ private static function getHeaders(Collection $items) return $headers = $headers->unique(); } + public function visibleTo($item) + { + return $item instanceof Entry || $item instanceof Submission; + } + /** * Get entries values by headers. * @@ -117,8 +140,8 @@ 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); } } 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(); } }