Display-Module in der Modulliste duplizieren.
Ermöglicht das Kopieren inklusive aller Inhalte und Einstellungen mit fortlaufender Benennung als „Kopie N - …“. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
bebfa63e87
commit
6c2da9bdac
3 changed files with 116 additions and 0 deletions
|
|
@ -80,6 +80,55 @@ class DisplayVersionList extends Component
|
|||
$version->update(['is_active' => ! $version->is_active]);
|
||||
}
|
||||
|
||||
public function copyVersion(int $id): void
|
||||
{
|
||||
$source = DisplayVersion::query()->with('items')->findOrFail($id);
|
||||
|
||||
$copy = DB::transaction(function () use ($source) {
|
||||
$copy = DisplayVersion::create([
|
||||
'name' => $this->copyNameFor($source->name),
|
||||
'type' => $source->type,
|
||||
'settings' => $source->settings ?? [],
|
||||
'is_active' => $source->is_active,
|
||||
]);
|
||||
|
||||
foreach ($source->items as $item) {
|
||||
$copy->items()->create([
|
||||
'item_type' => $item->item_type,
|
||||
'content' => $item->content,
|
||||
'sort_order' => $item->sort_order,
|
||||
'is_active' => $item->is_active,
|
||||
]);
|
||||
}
|
||||
|
||||
return $copy;
|
||||
});
|
||||
|
||||
session()->flash('success', 'Modul "'.$source->name.'" wurde als "'.$copy->name.'" kopiert!');
|
||||
}
|
||||
|
||||
private function copyNameFor(string $sourceName): string
|
||||
{
|
||||
$baseName = preg_match('/^Kopie \d+ - (.+)$/', $sourceName, $matches) === 1
|
||||
? $matches[1]
|
||||
: $sourceName;
|
||||
|
||||
$maxCopyNumber = 0;
|
||||
|
||||
DisplayVersion::query()
|
||||
->where('name', 'like', 'Kopie % - '.$baseName)
|
||||
->pluck('name')
|
||||
->each(function (string $name) use ($baseName, &$maxCopyNumber): void {
|
||||
if (preg_match('/^Kopie (\d+) - '.preg_quote($baseName, '/').'$/', $name, $matches) !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$maxCopyNumber = max($maxCopyNumber, (int) $matches[1]);
|
||||
});
|
||||
|
||||
return 'Kopie '.($maxCopyNumber + 1).' - '.$baseName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -79,6 +79,14 @@
|
|||
icon="pencil">
|
||||
</flux:button>
|
||||
|
||||
<flux:button wire:click="copyVersion({{ $version->id }})"
|
||||
wire:confirm="Möchten Sie dieses Modul wirklich kopieren? Es wird eine Kopie mit allen Inhalten angelegt."
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
icon="document-duplicate"
|
||||
title="{{ __('Modul kopieren') }}">
|
||||
</flux:button>
|
||||
|
||||
<flux:button wire:click="deleteVersion({{ $version->id }})"
|
||||
wire:confirm="Möchten Sie dieses Modul wirklich löschen? Alle zugehörigen Inhalte werden ebenfalls gelöscht."
|
||||
size="sm"
|
||||
|
|
|
|||
|
|
@ -132,6 +132,65 @@ test('can toggle display version active status', function () {
|
|||
expect($version->fresh()->is_active)->toBeFalse();
|
||||
});
|
||||
|
||||
test('can copy a display version with all items and settings', function () {
|
||||
$user = User::factory()->create();
|
||||
$source = DisplayVersion::factory()->create([
|
||||
'name' => 'B2In Immobilien Dark',
|
||||
'type' => DisplayVersionType::B2in,
|
||||
'settings' => ['theme' => 'dark', 'show_footer' => false],
|
||||
'is_active' => true,
|
||||
]);
|
||||
DisplayVersionItem::factory()->create([
|
||||
'display_version_id' => $source->id,
|
||||
'item_type' => 'media',
|
||||
'content' => ['headline' => 'Test Headline'],
|
||||
'sort_order' => 0,
|
||||
]);
|
||||
DisplayVersionItem::factory()->create([
|
||||
'display_version_id' => $source->id,
|
||||
'item_type' => 'media',
|
||||
'content' => ['headline' => 'Zweites Medium'],
|
||||
'sort_order' => 1,
|
||||
]);
|
||||
|
||||
Livewire::actingAs($user)
|
||||
->test(DisplayVersionList::class)
|
||||
->call('copyVersion', $source->id);
|
||||
|
||||
$copy = DisplayVersion::query()->where('name', 'Kopie 1 - B2In Immobilien Dark')->first();
|
||||
|
||||
expect($copy)->not->toBeNull()
|
||||
->and($copy->type)->toBe(DisplayVersionType::B2in)
|
||||
->and($copy->settings)->toBe(['theme' => 'dark', 'show_footer' => false])
|
||||
->and($copy->is_active)->toBeTrue()
|
||||
->and($copy->items)->toHaveCount(2)
|
||||
->and($copy->items->pluck('content.headline')->all())->toBe(['Test Headline', 'Zweites Medium']);
|
||||
});
|
||||
|
||||
test('copying a display version increments the copy number', function () {
|
||||
$user = User::factory()->create();
|
||||
$source = DisplayVersion::factory()->create(['name' => 'Schaufenster Video']);
|
||||
|
||||
Livewire::actingAs($user)
|
||||
->test(DisplayVersionList::class)
|
||||
->call('copyVersion', $source->id)
|
||||
->call('copyVersion', $source->id);
|
||||
|
||||
expect(DisplayVersion::where('name', 'Kopie 1 - Schaufenster Video')->exists())->toBeTrue()
|
||||
->and(DisplayVersion::where('name', 'Kopie 2 - Schaufenster Video')->exists())->toBeTrue();
|
||||
});
|
||||
|
||||
test('copying an existing copy keeps the original base name in the title', function () {
|
||||
$user = User::factory()->create();
|
||||
$copy = DisplayVersion::factory()->create(['name' => 'Kopie 1 - Angebote Schauraum']);
|
||||
|
||||
Livewire::actingAs($user)
|
||||
->test(DisplayVersionList::class)
|
||||
->call('copyVersion', $copy->id);
|
||||
|
||||
expect(DisplayVersion::where('name', 'Kopie 2 - Angebote Schauraum')->exists())->toBeTrue();
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// DisplayVersionEditor
|
||||
// ========================================
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue