Skip to content
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
2 changes: 2 additions & 0 deletions resources/js/components/forms/SubmissionListing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export default {
listingKey: 'submissions',
preferencesPrefix: `forms.${this.form}`,
requestUrl: cp_url(`forms/${this.form}/submissions`),
pushQuery: true,
previousFilters: null,
}
},

Expand Down
73 changes: 50 additions & 23 deletions src/Forms/Exporters/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@

namespace Statamic\Forms\Exporters;

use Illuminate\Support\Uri;
use League\Csv\Writer;
use SplTempFileObject;
use Statamic\Facades\Scope;
use Statamic\Fields\Field;
use Statamic\Support\Arr;

class CsvExporter extends Exporter
{
private Writer $writer;
protected static string $title = 'CSV';

public function export(): string
public function export(string $path): void
{
$this->writer = Writer::createFromFileObject(new SplTempFileObject);
$this->writer = Writer::createFromPath($path);
$this->writer->setDelimiter(Arr::get($this->config, 'delimiter', config('statamic.forms.csv_delimiter', ',')));

$this->insertHeaders();

$this->insertData();

return (string) $this->writer;
$this->query()
->lazy(10)
->each(fn ($submission) => $this->writer->insertOne(
collect($submission->toArray())
->except('id')
->map(fn ($value) => (is_array($value)) ? implode(', ', $value) : ((string) $value))
->all()
));
}

private function insertHeaders()
Expand All @@ -35,25 +41,46 @@ private function insertHeaders()
$this->writer->insertOne($headers);
}

private function insertData()
public function extension(): string
{
$data = $this->form->submissions()->map(function ($submission) {
$submission = $submission->toArray();

$submission['date'] = (string) $submission['date'];

unset($submission['id']);

return collect($submission)->map(function ($value) {
return (is_array($value)) ? implode(', ', $value) : $value;
})->all();
})->all();

$this->writer->insertAll($data);
return 'csv';
}

public function extension(): string
private function query()
{
return 'csv';
$form = $this->form;
$query = $form->querySubmissions();

if ($url = request()->headers->get('referer')) {
$url = Uri::of($url);

if ($search = $url->query()->get('search')) {
$query->where('date', 'like', '%'.$search.'%');

$form->blueprint()
->fields()
->all()
->filter(fn (Field $field) => in_array($field->type(), ['text', 'textarea', 'integer']))
->each(fn (Field $field) => $query->orWhere($field->handle(), 'like', '%'.$search.'%'));
}

if ($filters = $url->query()->get('filters')) {
$filters = base64_decode($filters);
$filters = json_validate($filters) ? json_decode($filters, true) : [];
collect($filters)
->map(fn ($values, $handle) => (object) [
'filterInstance' => Scope::find($handle, ['form' => $form->handle()]),
'values' => $values,
])
->filter(fn ($filter) => $filter->filterInstance !== null)
->each(fn ($filter) => $filter->filterInstance->apply($query, $filter->values));
}

$sortField = $url->query()->get('sort', 'date');
$sortDirection = $url->query()->get('order', $sortField === 'date' ? 'desc' : 'asc');
$query->orderBy($sortField, $sortDirection);
}

return $query;
}
}
10 changes: 4 additions & 6 deletions src/Forms/Exporters/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Statamic\Forms\Exporters;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
use Statamic\Contracts\Forms\Form;
use Statamic\Facades\File;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

use function Statamic\trans as __;
Expand All @@ -16,7 +16,7 @@ abstract class Exporter
protected string $handle;
protected Form $form;

abstract public function export(): string;
abstract public function export(string $path): void;

public function setHandle(string $handle)
{
Expand Down Expand Up @@ -75,11 +75,9 @@ public function response(): Response

public function download(): BinaryFileResponse
{
$content = $this->export();

$path = storage_path('statamic/tmp/forms/'.$this->form->handle().'-'.time().'.'.$this->extension());

File::put($path, $content);
File::put($path, '');
$this->export($path);

return response()->download($path)->deleteFileAfterSend();
}
Expand Down
67 changes: 64 additions & 3 deletions src/Forms/Exporters/JsonExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,38 @@

namespace Statamic\Forms\Exporters;

use Illuminate\Support\Uri;
use Statamic\Facades\Scope;
use Statamic\Fields\Field;

class JsonExporter extends Exporter
{
protected static string $title = 'JSON';

public function export(): string
public function export(string $path): void
{
$submissions = $this->form->submissions()->toArray();
$file = fopen($path, 'w');
if (! $file) {
return;
}

fwrite($file, '[');

$first = true;
$this->query()
->lazy(10)
->each(function ($submission) use ($file, &$first) {
if (! $first) {
fwrite($file, ',');
}

fwrite($file, json_encode($submission));

return json_encode($submissions);
$first = false;
});

fwrite($file, ']');
fclose($file);
}

public function contentType(): string
Expand All @@ -22,4 +45,42 @@ public function extension(): string
{
return 'json';
}

private function query()
{
$form = $this->form;
$query = $form->querySubmissions();

if ($url = request()->headers->get('referer')) {
$url = Uri::of($url);

if ($search = $url->query()->get('search')) {
$query->where('date', 'like', '%'.$search.'%');

$form->blueprint()
->fields()
->all()
->filter(fn (Field $field) => in_array($field->type(), ['text', 'textarea', 'integer']))
->each(fn (Field $field) => $query->orWhere($field->handle(), 'like', '%'.$search.'%'));
}

if ($filters = $url->query()->get('filters')) {
$filters = base64_decode($filters);
$filters = json_validate($filters) ? json_decode($filters, true) : [];
collect($filters)
->map(fn ($values, $handle) => (object) [
'filterInstance' => Scope::find($handle, ['form' => $form->handle()]),
'values' => $values,
])
->filter(fn ($filter) => $filter->filterInstance !== null)
->each(fn ($filter) => $filter->filterInstance->apply($query, $filter->values));
}

$sortField = $url->query()->get('sort', 'date');
$sortDirection = $url->query()->get('order', $sortField === 'date' ? 'desc' : 'asc');
$query->orderBy($sortField, $sortDirection);
}

return $query;
}
}