Files
nuhodds_fi4/app/Livewire/BoardDispatcher.php
T
2026-09-05 08:19:49 +02:00

142 lines
4.3 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
class BoardDispatcher extends Component
{
public array $lists = [];
public string $boardVersion = '';
public function mount()
{
$this->boardVersion = uniqid();
$this->lists['source-1'] = [
['id' => '1', 'name' => 'Source Item A', 'origin' => 'source-1'],
['id' => '2', 'name' => 'Source Item B', 'origin' => 'source-1'],
];
$this->lists['source-2'] = [
['id' => '3', 'name' => 'Source Item C', 'origin' => 'source-2'],
];
$this->lists['source-3'] = [
['id' => '4', 'name' => 'Source Item D', 'origin' => 'source-3'],
['id' => '5', 'name' => 'Source Item E', 'origin' => 'source-3'],
];
$this->lists['source-4'] = [
['id' => '6', 'name' => 'Source Item F', 'origin' => 'source-4'],
];
for ($i = 1; $i <= 21; $i++) {
$this->lists["dest-{$i}"] = [];
}
}
public function moveItem($itemId, $fromList, $toList, $newIndex)
{
$itemId = (string) $itemId;
$newIndex = (int) $newIndex;
if (! $toList || $fromList === $toList || ! isset($this->lists[$fromList]) || ! isset($this->lists[$toList])) {
$this->boardVersion = uniqid();
return;
}
// Limit-Guard: Wenn das Ziel bereits ein Element enthält, abbrechen
if (count($this->lists[$toList]) >= 1) {
$this->boardVersion = uniqid();
return;
}
$foundItem = null;
foreach ($this->lists[$fromList] as $key => $item) {
if ((string) $item['id'] === $itemId) {
$foundItem = $item;
unset($this->lists[$fromList][$key]);
$this->lists[$fromList] = array_values($this->lists[$fromList]);
break;
}
}
if ($foundItem) {
array_splice($this->lists[$toList], $newIndex, 0, [$foundItem]);
}
$this->boardVersion = uniqid();
}
public function returnToOrigin($itemId, $currentListId)
{
$itemId = (string) $itemId;
if (! isset($this->lists[$currentListId])) {
$this->boardVersion = uniqid();
return;
}
$foundItem = null;
foreach ($this->lists[$currentListId] as $key => $item) {
if ((string) $item['id'] === $itemId) {
$foundItem = $item;
unset($this->lists[$currentListId][$key]);
$this->lists[$currentListId] = array_values($this->lists[$currentListId]);
break;
}
}
if ($foundItem && isset($this->lists[$foundItem['origin']])) {
// Nur zurücklegen, wenn die Quelle wirklich frei ist
if (count($this->lists[$foundItem['origin']]) < 1) {
array_unshift($this->lists[$foundItem['origin']], $foundItem);
} else {
// Ausfallsicherheit gegen Datenverlust: Zurück in die Bucht schieben
array_unshift($this->lists[$currentListId], $foundItem);
}
}
$this->boardVersion = uniqid();
}
public function resetBoard()
{
// Alle 21 Zielbays durchlaufen
for ($i = 1; $i <= 21; $i++) {
$bayKey = "dest-{$i}";
if (isset($this->lists[$bayKey]) && ! empty($this->lists[$bayKey])) {
foreach ($this->lists[$bayKey] as $item) {
$targetOrigin = $item['origin'];
// Element zurück in sein Original-Quell-Tab schieben
if (isset($this->lists[$targetOrigin])) {
array_unshift($this->lists[$targetOrigin], $item);
}
}
// Die Zielbucht komplett leeren
$this->lists[$bayKey] = [];
}
}
// Falls Sie die boardVersion-Logik nutzen, erneuern wir sie hier für den DOM-Refresh:
if (property_exists($this, 'boardVersion')) {
$this->boardVersion = uniqid();
}
// Falls Sie die Javascript-Event/ignore-Logik nutzen, feuern wir das Refresh-Event:
$this->dispatch('refreshBoard', lists: $this->lists);
}
public function render()
{
return view('livewire.board-dispatcher');
}
}