Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export as CSV, XLSX or JSON, specified by configuration file with defaults and per Collection configuration #6

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 😍
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 14 additions & 0 deletions config/statamic-export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// either: csv, json, xlsx

return [
'default' => 'csv',
'collections' => [
// 'collection_name' => 'xlsx'
],
'excluded_columns' => [
'blueprint',
'updated_by',
]
];
28 changes: 28 additions & 0 deletions src/ArrayExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Youfront\Export;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ArrayExport implements FromArray, WithHeadings
{
protected $headings;
protected $entries;

public function __construct(array $headings, array $entries)
{
$this->headings = $headings;
$this->entries = $entries;
}

public function array(): array
{
return $this->entries;
}

public function headings(): array
{
return $this->headings;
}
}
65 changes: 44 additions & 21 deletions src/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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;
}

Expand All @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}