Skip to content

Commit

Permalink
Allow custom form exporters to be defined in the forms config
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Oct 13, 2023
1 parent 2316439 commit 413473e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
22 changes: 22 additions & 0 deletions config/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,26 @@

'csv_headers' => 'handle',

/*
|--------------------------------------------------------------------------
| Exporters
|--------------------------------------------------------------------------
|
| The export option you want to provide on your forms under the
| Export Submission button. These classes should implement
| Statamic\Forms\Exporters\AbstractExporter
|
*/

'exporters' => [
'csv' => [
'class' => Statamic\Forms\Exporters\CsvExporter::class,
'label' => 'Export as CSV',
],
'json' => [
'class' => Statamic\Forms\Exporters\JsonExporter::class,
'label' => 'Export as JSON',
],
]

];
7 changes: 5 additions & 2 deletions resources/views/forms/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
@endcan
</dropdown-list>

@if (count($exporters = config('statamic.forms.exporters', [])))
<dropdown-list>
<button class="btn" slot="trigger">{{ __('Export Submissions') }}</button>
<dropdown-item :text="__('Export as CSV')" redirect="{{ cp_route('forms.export', ['type' => 'csv', 'form' => $form->handle()]) }}?download=true"></dropdown-item>
<dropdown-item :text="__('Export as JSON')" redirect="{{ cp_route('forms.export', ['type' => 'json', 'form' => $form->handle()]) }}?download=true"></dropdown-item>
@foreach ($exporters as $type => $exporter)
<dropdown-item :text="__('{{ $exporter['label'] ?? __('Export as '.strtoupper($type)) }}')" redirect="{{ cp_route('forms.export', ['type' => $type, 'form' => $form->handle()]) }}?download=true"></dropdown-item>
@endforeach
</dropdown-list>
@endif
</div>
</header>

Expand Down
10 changes: 9 additions & 1 deletion src/Contracts/Forms/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ interface Exporter
*/
public function export();

/**
* Get or set the config.
*
* @param array|null $config
* @return array|void
*/
public function config($config = null);

/**
* Get or set the form.
*
* @param Statamic\Contracts\Forms\Form|null $form
* @return Statamic\Contracts\Forms\Form
* @return Statamic\Contracts\Forms\Form|void
*/
public function form($form = null);

Expand Down
20 changes: 20 additions & 0 deletions src/Forms/Exporters/AbstractExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@

abstract class AbstractExporter implements Exporter
{
/**
* @var array
*/
private $config;

/**
* @var Statamic\Contracts\Forms\Form
*/
private $form;

/**
* Get or set the config.
*
* @param array|null $config
* @return array|void
*/
public function config($config = null)
{
if (is_null($config)) {
return $this->$config;
}

$this->config = $config;
}

/**
* Get or set the form.
*
Expand Down
12 changes: 9 additions & 3 deletions src/Http/Controllers/CP/Forms/FormExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Exceptions\FatalException;
use Statamic\Facades\File;
use Statamic\Http\Controllers\CP\CpController;
use Statamic\Support\Arr;
use Statamic\Support\Str;

class FormExportController extends CpController
Expand All @@ -13,13 +14,18 @@ public function export($form, $type)
{
$this->authorize('view', $form);

$exporter = 'Statamic\Forms\Exporters\\'.Str::studly($type).'Exporter';
$exporters = config('statamic.forms.exporters', []);

if (! class_exists($exporter)) {
if (! $exporterConfig = Arr::get($exporters, $type)) {
throw new FatalException("Exporter of type [$type] is not defined.");
}

if (! isset($exporterConfig['class']) && class_exists($exporter['class'])) {
throw new FatalException("Exporter of type [$type] does not exist.");
}

$exporter = new $exporter;
$exporter = new $exporterConfig['class'];
$exporter->config(Arr::except($exporterConfig, ['class']));
$exporter->form($form);

$content = $exporter->export();
Expand Down

0 comments on commit 413473e

Please sign in to comment.