Skip to content

Commit

Permalink
Hide mappings table & unique fields when file/destination has been ch…
Browse files Browse the repository at this point in the history
…anged.
  • Loading branch information
duncanmcclean committed Nov 6, 2024
1 parent 51ed25c commit d1f2670
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion resources/js/components/EditImportForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
:name="publishContainer"
:blueprint="fieldset"
:values="values"
:reference="initialReference"
:meta="meta"
:errors="errors"
:track-dirty-state="trackDirtyState"
Expand Down Expand Up @@ -123,6 +122,7 @@ export default {
this.$toast.success(__('Saved'));
clearTimeout(this.trackDirtyStateTimeout);
this.trackDirtyState = false;
this.fieldset = response.data.data.blueprint;
this.meta = response.data.data.meta;
this.values = this.resetValuesFromResponse(response.data.data.values);
this.trackDirtyStateTimeout = setTimeout(() => (this.trackDirtyState = true), 500);
Expand Down
9 changes: 3 additions & 6 deletions src/Http/Controllers/ExtractFromImportFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

trait ExtractFromImportFields
{
protected function extractFromFields($import, $blueprint)
protected function extractFromFields($import, $fields)
{
$fields = $blueprint
->fields()
->addValues($import->config()->all())
->preProcess();
$fields = $fields->preProcess();

$values = $fields->values()->merge([
'name' => $import->name(),
'file' => [basename($import->get('path'))],
'strategy' => array_keys($import->get('strategy')),
]);

return [$values->all(), $fields->meta()];
Expand Down
19 changes: 15 additions & 4 deletions src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public function edit(Request $request, Import $import)
->setParent($import)
->addValues($import->config()->merge([
'name' => $import->name(),
'file' => [basename($import->get('path'))],
])->all())
->preProcess();

Expand Down Expand Up @@ -150,7 +149,7 @@ public function update(Request $request, Import $import)
$type = $import->get('type');
$path = $import->get('path');

if (($file = $request->file[0]) && basename($import->get('path')) !== $request->file[0]) {
if ($request->file && $file = $request->file[0]) {
$type = match (Storage::disk('local')->mimeType("statamic/file-uploads/{$file}")) {
'text/csv', 'application/csv', 'text/plain' => 'csv',
'application/xml', 'text/xml' => 'xml',
Expand Down Expand Up @@ -189,12 +188,16 @@ public function update(Request $request, Import $import)
$import->run();
}

[$values, $meta] = $this->extractFromFields($import, $blueprint);
// We need to refresh the blueprint after saving, so the field conditions are up-to-date.
$blueprint = $import->blueprint();

[$values, $meta] = $this->extractFromFields($import, $fields);

return [
'data' => array_merge((new ImportResource($import->fresh()))->resolve()['data'], [
'values' => $values,
'meta' => $meta,
'blueprint' => $blueprint->setParent($import)->toPublishArray(),
]),
'saved' => $saved,
];
Expand All @@ -209,9 +212,17 @@ public function destroy(Request $request, Import $import)

private function createBlueprint(): Blueprint
{
return Facades\Blueprint::make()->setContents([
$blueprint = Facades\Blueprint::make()->setContents([
'fields' => Arr::get(ImportBlueprint::getBlueprint()->contents(), 'tabs.main.sections.0.fields'),
]);

$blueprint->ensureFieldHasConfig('file', [
'display' => __('File'),
'instructions' => __('Upload a CSV or XML file to import.'),
'required' => true,
]);

return $blueprint;
}

private function ensureJobBatchesTableExists(): bool
Expand Down
34 changes: 28 additions & 6 deletions src/Imports/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ public static function getBlueprint(?Import $import = null): \Statamic\Fields\Bl
'handle' => 'file',
'field' => [
'type' => 'files',
'display' => __('File'),
'instructions' => __('Upload a CSV or XML file to import.'),
'display' => __('Upload a new file'),
'instructions' => __('Upload a CSV or XML file to import. This will replace the current file.'),
'max_files' => 1,
'allowed_extensions' => ['csv', 'xml'],
'validate' => [
'required',
'nullable',
'max:1',
function (string $attribute, mixed $value, Closure $fail) {
$import = request()->route('import');

if ($import && basename($import->get('path')) === $value[0]) {
if (! $value) {
return;
}

Expand Down Expand Up @@ -176,6 +174,7 @@ function (string $attribute, mixed $value, Closure $fail) {
}
},
],
'if' => $import ? static::buildFieldConditions($import) : null,
],
],
[
Expand All @@ -196,6 +195,7 @@ function (string $attribute, mixed $value, Closure $fail) {
}
},
],
'if' => $import ? static::buildFieldConditions($import) : null,
],
],
],
Expand All @@ -205,4 +205,26 @@ function (string $attribute, mixed $value, Closure $fail) {
],
]);
}

private static function buildFieldConditions(Import $import): array
{
$conditions = [
'file' => 'empty',
'destination.type' => $import->get('destination.type'),
];

if ($import->get('destination.collection')) {
$conditions['destination.collection'] = 'contains ' . $import->get('destination.collection');
}

if ($import->get('destination.taxonomy')) {
$conditions['destination.taxonomy'] = 'contains ' . $import->get('destination.taxonomy');
}

if ($import->get('destination.site')) {
$conditions['destination.site'] = 'contains ' . $import->get('destination.site');
}

return $conditions;
}
}
6 changes: 0 additions & 6 deletions tests/Imports/UpdateImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function can_update_an_import()
->actingAs(User::make()->makeSuper()->save())
->patch("/cp/utilities/importer/{$this->import->id()}", [
'name' => 'Old Posts',
'file' => ['posts.csv'],
'destination' => ['type' => 'entries', 'collection' => ['posts']],
'strategy' => ['create', 'update'],
'mappings' => [
Expand Down Expand Up @@ -180,7 +179,6 @@ public function validation_error_is_thrown_without_an_import_strategy()
->actingAs(User::make()->makeSuper()->save())
->patch("/cp/utilities/importer/{$this->import->id()}", [
'name' => 'Posts',
'file' => ['posts.csv'],
'destination' => ['type' => 'entries', 'collection' => ['posts']],
'strategy' => [],
'mappings' => [
Expand All @@ -206,7 +204,6 @@ public function validation_error_is_thrown_without_any_mappings()
->actingAs(User::make()->makeSuper()->save())
->patch("/cp/utilities/importer/{$this->import->id()}", [
'name' => 'Posts',
'file' => ['posts.csv'],
'destination' => ['type' => 'entries', 'collection' => ['posts']],
'strategy' => ['create', 'update'],
'mappings' => [
Expand All @@ -228,7 +225,6 @@ public function throws_validation_errors_for_mapping_fields()
->actingAs(User::make()->makeSuper()->save())
->patch("/cp/utilities/importer/{$this->import->id()}", [
'name' => 'Posts',
'file' => ['posts.csv'],
'destination' => ['type' => 'entries', 'collection' => ['posts']],
'strategy' => ['create', 'update'],
'mappings' => [
Expand All @@ -246,7 +242,6 @@ public function validation_error_is_thrown_without_unique_field()
->actingAs(User::make()->makeSuper()->save())
->patch("/cp/utilities/importer/{$this->import->id()}", [
'name' => 'Posts',
'file' => ['posts.csv'],
'destination' => ['type' => 'entries', 'collection' => ['posts']],
'strategy' => ['create', 'update'],
'mappings' => [
Expand All @@ -268,7 +263,6 @@ public function validation_error_is_thrown_when_no_mapping_is_configured_for_uni
->actingAs(User::make()->makeSuper()->save())
->patch("/cp/utilities/importer/{$this->import->id()}", [
'name' => 'Posts',
'file' => ['posts.csv'],
'destination' => ['type' => 'entries', 'collection' => ['posts']],
'strategy' => ['create', 'update'],
'mappings' => [
Expand Down

0 comments on commit d1f2670

Please sign in to comment.