1010use Statamic \Contracts \Forms \FormRepository as FormRepositoryContract ;
1111use Statamic \Contracts \Forms \Submission as SubmissionContract ;
1212use Statamic \Contracts \Forms \SubmissionRepository as SubmissionRepositoryContract ;
13- use Statamic \Eloquent \Forms \Form ;
14- use Statamic \Eloquent \Forms \FormRepository ;
15- use Statamic \Eloquent \ Forms \ Submission ;
16- use Statamic \Eloquent \ Forms \ SubmissionRepository ;
13+ use Statamic \Eloquent \Forms \FormModel ;
14+ use Statamic \Eloquent \Forms \SubmissionModel ;
15+ use Statamic \Facades \ Form ;
16+ use Statamic \Facades \ FormSubmission ;
1717use Statamic \Forms \Form as StacheForm ;
1818use Statamic \Forms \FormRepository as StacheFormRepository ;
1919use Statamic \Forms \Submission as StacheSubmission ;
2020use Statamic \Stache \Repositories \SubmissionRepository as StacheSubmissionRepository ;
21- use Statamic \Statamic ;
2221
2322class ExportForms extends Command
2423{
@@ -29,14 +28,17 @@ class ExportForms extends Command
2928 *
3029 * @var string
3130 */
32- protected $ signature = 'statamic:eloquent:export-forms ' ;
31+ protected $ signature = 'statamic:eloquent:export-forms
32+ {--force : Force the export to run, with all prompts answered "yes"}
33+ {--only-forms : Only export forms}
34+ {--only-submissions : Only export submissions} ' ;
3335
3436 /**
3537 * The console command description.
3638 *
3739 * @var string
3840 */
39- protected $ description = 'Export eloquent based forms to flat files. ' ;
41+ protected $ description = 'Export eloquent based forms and submissions to flat files. ' ;
4042
4143 /**
4244 * Execute the console command.
@@ -47,11 +49,9 @@ public function handle()
4749 {
4850 $ this ->usingDefaultRepositories (function () {
4951 $ this ->exportForms ();
52+ $ this ->exportSubmissions ();
5053 });
5154
52- $ this ->newLine ();
53- $ this ->info ('Forms exported ' );
54-
5555 return 0 ;
5656 }
5757
@@ -62,57 +62,75 @@ private function usingDefaultRepositories(Closure $callback)
6262 Facade::clearResolvedInstance (SubmissionContract::class);
6363 Facade::clearResolvedInstance (SubmissionRepositoryContract::class);
6464
65- app ()->bind (FormContract::class, Form::class);
66- app ()->bind (FormRepositoryContract::class, FormRepository::class);
67- app ()->bind (SubmissionContract::class, Submission::class);
68- app ()->bind (SubmissionRepositoryContract::class, SubmissionRepository::class);
69- app ()->bind (\Statamic \Contracts \Forms \SubmissionQueryBuilder::class, \Statamic \Eloquent \Forms \SubmissionQueryBuilder::class);
65+ app ()->bind (FormContract::class, StacheForm::class);
66+ app ()->bind (FormRepositoryContract::class, StacheFormRepository::class);
67+ app ()->bind (SubmissionContract::class, StacheSubmission::class);
68+ app ()->bind (SubmissionRepositoryContract::class, StacheSubmissionRepository::class);
7069
7170 $ callback ();
7271 }
7372
7473 private function exportForms ()
7574 {
76- $ forms = (new FormRepository )->all ();
75+ if (! $ this ->shouldExportForms ()) {
76+ return ;
77+ }
7778
78- app ()-> bind (FormContract::class, StacheForm::class );
79+ $ forms = FormModel:: all ( );
7980
8081 $ this ->withProgressBar ($ forms , function ($ form ) {
81- $ newForm = (new StacheForm )
82- ->handle ($ form ->handle ())
83- ->title ($ form ->title ())
84- ->store ($ form ->store ())
85- ->email ($ form ->email ())
86- ->honeypot ($ form ->honeypot ());
87-
88- Statamic::repository (FormRepositoryContract::class, StacheFormRepository::class);
89- Facade::clearResolvedInstance (SubmissionRepositoryContract::class);
90-
91- $ newForm ->save ();
82+ Form::make ()
83+ ->handle ($ form ->handle )
84+ ->title ($ form ->title )
85+ ->store ($ form ->settings ['store ' ] ?? null )
86+ ->email ($ form ->settings ['email ' ] ?? null )
87+ ->honeypot ($ form ->settings ['honeypot ' ] ?? null )
88+ ->data ($ form ->settings ['data ' ] ?? [])
89+ ->save ();
90+ });
9291
93- Statamic::repository (FormRepositoryContract::class, FormRepository::class);
94- Facade::clearResolvedInstance (SubmissionRepositoryContract::class);
92+ $ this ->newLine ();
93+ $ this ->info ('Forms exported ' );
94+ }
9595
96- $ form ->submissions ()->each (function ($ submission ) use ($ newForm ) {
97- $ id = $ submission ->date ()->getPreciseTimestamp (4 );
98- $ id = substr ($ id , 0 , -4 ).'. ' .substr ($ id , -4 );
96+ private function exportSubmissions ()
97+ {
98+ if (! $ this ->shouldExportSubmissions ()) {
99+ return ;
100+ }
99101
100- $ newSubmission = (new StacheSubmission )
101- ->id ($ id )
102- ->form ($ newForm )
103- ->data ($ submission ->data ());
102+ $ submissions = SubmissionModel::all ();
104103
105- Statamic::repository (SubmissionRepositoryContract::class, StacheSubmissionRepository::class);
106- Facade::clearResolvedInstance (SubmissionRepositoryContract::class);
104+ $ this ->withProgressBar ($ submissions , function ($ submission ) {
105+ if (! $ form = Form::find ($ submission ->form )) {
106+ return ;
107+ }
107108
108- $ newSubmission ->save ();
109+ $ id = $ submission ->created_at ->getPreciseTimestamp (4 );
110+ $ id = substr ($ id , 0 , -4 ).'. ' .substr ($ id , -4 );
109111
110- Statamic::repository (SubmissionRepositoryContract::class, SubmissionRepository::class);
111- Facade::clearResolvedInstance (SubmissionRepositoryContract::class);
112- });
112+ FormSubmission::make ()
113+ ->id ($ id )
114+ ->form ($ form )
115+ ->data ($ submission ->data )
116+ ->save ();
113117 });
114118
115119 $ this ->newLine ();
116- $ this ->info ('Forms exported ' );
120+ $ this ->info ('Submissions exported ' );
121+ }
122+
123+ private function shouldExportForms (): bool
124+ {
125+ return $ this ->option ('only-forms ' )
126+ || ! $ this ->option ('only-submissions ' )
127+ && ($ this ->option ('force ' ) || $ this ->confirm ('Do you want to export forms? ' ));
128+ }
129+
130+ private function shouldExportSubmissions (): bool
131+ {
132+ return $ this ->option ('only-submissions ' )
133+ || ! $ this ->option ('only-forms ' )
134+ && ($ this ->option ('force ' ) || $ this ->confirm ('Do you want to export submissions? ' ));
117135 }
118136}
0 commit comments