Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the current file in the edit form #47

Merged
merged 2 commits into from
Nov 29, 2024
Merged
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
3 changes: 1 addition & 2 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
'destination_site_instructions' => 'Which site should the entries be imported into?',
'destination_taxonomy_instructions' => 'Select the taxonomy to import terms into.',
'destination_type_instructions' => 'Choose what type of data are you importing.',
'import_file_instructions' => 'Upload a CSV or XML file to import. This will replace the current file.',
'import_file_instructions_create' => 'Upload a CSV or XML file to import.',
'import_file_instructions' => 'Upload a CSV or XML file to import.',
'import_name_instructions' => 'Name this import so you can identify it later.',
'mapping_instructions' => 'Map the fields from your import to the fields in your blueprint.',
'strategy_instructions' => 'Choose what should happen when importing.',
Expand Down
1 change: 1 addition & 0 deletions src/Http/Controllers/ExtractFromImportFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ protected function extractFromFields($import, $blueprint)
->setParent($import)
->addValues($import->config()->merge([
'name' => $import->name(),
'file' => [basename($import->get('path'))],
])->all())
->preProcess();

Expand Down
13 changes: 3 additions & 10 deletions src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ 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 @@ -146,7 +147,7 @@ public function update(Request $request, Import $import)
$type = $import->get('type');
$path = $import->get('path');

if ($request->file && $file = $request->file[0]) {
if (($request->file && $file = $request->file[0]) && $file !== basename($path)) {
$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 @@ -207,17 +208,9 @@ public function destroy(Request $request, Import $import)

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

$blueprint->ensureFieldHasConfig('file', [
'display' => __('File'),
'instructions' => __('importer::messages.import_file_instructions_create'),
'required' => true,
]);

return $blueprint;
}

private function ensureJobBatchesTableExists(): bool
Expand Down
12 changes: 8 additions & 4 deletions src/Imports/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ public static function getBlueprint(?Import $import = null): \Statamic\Fields\Bl
'handle' => 'file',
'field' => [
'type' => 'files',
'display' => __('Upload a new file'),
'display' => __('File'),
'instructions' => __('importer::messages.import_file_instructions'),
'max_files' => 1,
'allowed_extensions' => ['csv', 'xml'],
'validate' => [
'nullable',
'required',
'max:1',
function (string $attribute, mixed $value, Closure $fail) {
function (string $attribute, mixed $value, Closure $fail) use ($import) {
if (! $value) {
return;
}

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

$path = "statamic/file-uploads/{$value[0]}";

if (! Storage::disk('local')->exists($path)) {
Expand Down Expand Up @@ -239,7 +243,7 @@ private static function getSourceFields(?Import $import = null): array
private static function buildFieldConditions(Import $import): array
{
$conditions = [
'file' => 'empty',
'file' => 'contains '.basename($import->get('path')),
'destination.type' => $import->get('destination.type'),
];

Expand Down
6 changes: 6 additions & 0 deletions tests/Imports/UpdateImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ 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'],
'source' => ['csv_delimiter' => ','],
Expand Down Expand Up @@ -181,6 +182,7 @@ 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,6 +208,7 @@ 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 @@ -227,6 +230,7 @@ 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 @@ -244,6 +248,7 @@ 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 @@ -265,6 +270,7 @@ 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