Skip to content

Commit

Permalink
Fix issues with importing and exporting forms (#390)
Browse files Browse the repository at this point in the history
* Ensure shouldimport doesnt run multiple times

* Fix import issues

* Fix exports too
  • Loading branch information
ryanmitchell authored Dec 12, 2024
1 parent d5874bb commit 52df13a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/Commands/ExportForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use Statamic\Contracts\Forms\Form as FormContract;
use Statamic\Contracts\Forms\FormRepository as FormRepositoryContract;
use Statamic\Contracts\Forms\Submission as SubmissionContract;
use Statamic\Contracts\Forms\SubmissionRepository as SubmissionRepositoryContract;
use Statamic\Eloquent\Forms\Form;
use Statamic\Eloquent\Forms\FormRepository;
use Statamic\Eloquent\Forms\Submission;
use Statamic\Eloquent\Forms\SubmissionRepository;
use Statamic\Forms\Form as StacheForm;
use Statamic\Forms\FormRepository as StacheFormRepository;
use Statamic\Forms\Submission as StacheSubmission;
use Statamic\Stache\Repositories\SubmissionRepository as StacheSubmissionRepository;
use Statamic\Statamic;

class ExportForms extends Command
Expand Down Expand Up @@ -55,10 +58,15 @@ public function handle()
private function usingDefaultRepositories(Closure $callback)
{
Facade::clearResolvedInstance(FormContract::class);
Facade::clearResolvedInstance(FormRepositoryContract::class);
Facade::clearResolvedInstance(SubmissionContract::class);
Facade::clearResolvedInstance(SubmissionRepositoryContract::class);

app()->bind(FormContract::class, Form::class);
app()->bind(FormRepositoryContract::class, FormRepository::class);
app()->bind(SubmissionContract::class, Submission::class);
app()->bind(SubmissionRepositoryContract::class, SubmissionRepository::class);
app()->bind(\Statamic\Contracts\Forms\SubmissionQueryBuilder::class, \Statamic\Eloquent\Forms\SubmissionQueryBuilder::class);

$callback();
}
Expand All @@ -78,10 +86,12 @@ private function exportForms()
->honeypot($form->honeypot());

Statamic::repository(FormRepositoryContract::class, StacheFormRepository::class);
Facade::clearResolvedInstance(SubmissionRepositoryContract::class);

$newForm->save();

Statamic::repository(FormRepositoryContract::class, FormRepository::class);
Facade::clearResolvedInstance(SubmissionRepositoryContract::class);

$form->submissions()->each(function ($submission) use ($newForm) {
$id = $submission->date()->getPreciseTimestamp(4);
Expand All @@ -92,7 +102,13 @@ private function exportForms()
->form($newForm)
->data($submission->data());

Statamic::repository(SubmissionRepositoryContract::class, StacheSubmissionRepository::class);
Facade::clearResolvedInstance(SubmissionRepositoryContract::class);

$newSubmission->save();

Statamic::repository(SubmissionRepositoryContract::class, SubmissionRepository::class);
Facade::clearResolvedInstance(SubmissionRepositoryContract::class);
});
});

Expand Down
20 changes: 15 additions & 5 deletions src/Commands/ImportForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use Illuminate\Support\Facades\Facade;
use Statamic\Console\RunsInPlease;
use Statamic\Contracts\Forms\Form as FormContract;
use Statamic\Contracts\Forms\FormRepository as FormRepositoryContract;
use Statamic\Contracts\Forms\Submission as SubmissionContract;
use Statamic\Contracts\Forms\SubmissionRepository as SubmissionRepositoryContract;
use Statamic\Eloquent\Forms\Form;
use Statamic\Facades\File;
use Statamic\Forms\Form as StacheForm;
use Statamic\Forms\FormRepository;
use Statamic\Forms\Submission as StacheSubmission;
use Statamic\Stache\Repositories\SubmissionRepository;

class ImportForms extends Command
{
Expand Down Expand Up @@ -50,26 +53,33 @@ public function handle(): int
private function useDefaultRepositories(): void
{
Facade::clearResolvedInstance(FormContract::class);
Facade::clearResolvedInstance(FormRepositoryContract::class);
Facade::clearResolvedInstance(SubmissionContract::class);
Facade::clearResolvedInstance(SubmissionRepositoryContract::class);

app()->bind(FormContract::class, StacheForm::class);
app()->bind(FormRepositoryContract::class, FormRepository::class);
app()->bind(SubmissionContract::class, StacheSubmission::class);
app()->bind(\Statamic\Eloquent\Forms\SubmissionQueryBuilder::class, \Statamic\Stache\Query\SubmissionQueryBuilder::class);
app()->bind(SubmissionRepositoryContract::class, SubmissionRepository::class);
app()->bind(\Statamic\Contracts\Forms\SubmissionQueryBuilder::class, \Statamic\Stache\Query\SubmissionQueryBuilder::class);
}

private function importForms(): void
{
$this->withProgressBar((new FormRepository)->all(), function ($form) {
if ($this->shouldImportForms()) {
$shouldImportForms = $this->shouldImportForms();
$shouldImportSubmissions = $this->shouldImportFormSubmissions();

$this->withProgressBar((new FormRepository)->all(), function ($form) use ($shouldImportForms, $shouldImportSubmissions) {
if ($shouldImportForms) {
$lastModified = Carbon::createFromTimestamp(File::lastModified($form->path()));

Form::makeModelFromContract($form)
->fill(['created_at' => $lastModified, 'updated_at' => $lastModified])
->save();
}

if ($this->shouldImportFormSubmissions()) {
$form->submissions()->each(function ($submission) use ($form) {
if ($shouldImportSubmissions) {
$form->querySubmissions()->lazy()->each(function ($submission) use ($form) {
$timestamp = app('statamic.eloquent.form_submissions.model')::make()->fromDateTime($submission->date());

app('statamic.eloquent.form_submissions.model')::firstOrNew(['created_at' => $timestamp])
Expand Down

0 comments on commit 52df13a

Please sign in to comment.