Ermöglicht das Kopieren inklusive aller Inhalte und Einstellungen mit fortlaufender Benennung als „Kopie N - …“. Co-authored-by: Cursor <cursoragent@cursor.com>
156 lines
4.9 KiB
PHP
156 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin\Cms;
|
|
|
|
use App\Enums\DisplayVersionType;
|
|
use App\Models\DisplayVersion;
|
|
use App\Support\DisplayModuleSettings;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Livewire\Component;
|
|
|
|
class DisplayVersionList extends Component
|
|
{
|
|
public $showCreateModal = false;
|
|
|
|
public $newName = '';
|
|
|
|
public $newType = '';
|
|
|
|
public function openCreateModal(): void
|
|
{
|
|
$this->newName = '';
|
|
$this->newType = '';
|
|
$this->showCreateModal = true;
|
|
}
|
|
|
|
public function createVersion(): void
|
|
{
|
|
$this->validate([
|
|
'newName' => 'required|string|max:255',
|
|
'newType' => 'required|string|in:video-display,b2in,offers',
|
|
], [
|
|
'newName.required' => 'Bitte geben Sie einen Namen ein.',
|
|
'newType.required' => 'Bitte wählen Sie einen Typ aus.',
|
|
]);
|
|
|
|
$version = DisplayVersion::create([
|
|
'name' => $this->newName,
|
|
'type' => $this->newType,
|
|
'settings' => $this->defaultSettingsForType($this->newType),
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$this->showCreateModal = false;
|
|
$this->newName = '';
|
|
$this->newType = '';
|
|
|
|
session()->flash('success', 'Modul "'.$version->name.'" wurde erstellt!');
|
|
|
|
$this->redirect(
|
|
route('admin.cms.display-module-edit', $version),
|
|
navigate: true
|
|
);
|
|
}
|
|
|
|
public function deleteVersion(int $id): void
|
|
{
|
|
$version = DisplayVersion::query()
|
|
->withCount([
|
|
'playlistItems as displays_count' => fn ($query) => $query
|
|
->join('display_playlists', 'display_playlist_items.display_playlist_id', '=', 'display_playlists.id')
|
|
->select(DB::raw('count(distinct display_playlists.display_id)')),
|
|
])
|
|
->findOrFail($id);
|
|
|
|
if ($version->displays_count > 0) {
|
|
session()->flash('error', 'Modul "'.$version->name.'" wird noch von '.$version->displays_count.' Display(s) verwendet und kann nicht gelöscht werden. Entfernen Sie es zuerst aus den betroffenen Bespielungen.');
|
|
|
|
return;
|
|
}
|
|
|
|
$name = $version->name;
|
|
$version->delete();
|
|
|
|
session()->flash('success', 'Modul "'.$name.'" wurde gelöscht!');
|
|
}
|
|
|
|
public function toggleActive(int $id): void
|
|
{
|
|
$version = DisplayVersion::findOrFail($id);
|
|
$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>
|
|
*/
|
|
private function defaultSettingsForType(string $type): array
|
|
{
|
|
return DisplayModuleSettings::defaults($type);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$versions = DisplayVersion::withCount([
|
|
'items',
|
|
'playlistItems as displays_count' => fn ($query) => $query
|
|
->join('display_playlists', 'display_playlist_items.display_playlist_id', '=', 'display_playlists.id')
|
|
->select(DB::raw('count(distinct display_playlists.display_id)')),
|
|
])
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return view('livewire.admin.cms.display-version-list', [
|
|
'versions' => $versions,
|
|
'types' => DisplayVersionType::cases(),
|
|
]);
|
|
}
|
|
}
|