libraryItem tagging added

This commit is contained in:
HolgerHatGarKeineNode
2023-02-11 20:25:39 +01:00
parent b528f509dd
commit dc1edcde08
2 changed files with 52 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ use App\Enums\LibraryItemType;
use App\Models\Country; use App\Models\Country;
use App\Models\Library; use App\Models\Library;
use App\Models\LibraryItem; use App\Models\LibraryItem;
use App\Models\Tag;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Livewire\Component; use Livewire\Component;
use Livewire\WithFileUploads; use Livewire\WithFileUploads;
@@ -21,6 +22,7 @@ class LibraryItemForm extends Component
public $library; public $library;
public $image; public $image;
public $file; public $file;
public array $selectedTags = [];
public bool $lecturer = false; public bool $lecturer = false;
public ?string $fromUrl = ''; public ?string $fromUrl = '';
@@ -37,6 +39,8 @@ class LibraryItemForm extends Component
'library' => 'required', 'library' => 'required',
'selectedTags' => 'array|min:1',
'libraryItem.lecturer_id' => 'required', 'libraryItem.lecturer_id' => 'required',
'libraryItem.name' => 'required', 'libraryItem.name' => 'required',
'libraryItem.type' => 'required', 'libraryItem.type' => 'required',
@@ -66,9 +70,14 @@ class LibraryItemForm extends Component
]); ]);
if ($this->lecturer) { if ($this->lecturer) {
$this->library = Library::query() $this->library = Library::query()
->firstWhere('name', '=', 'Dozentenmaterial')?->id; ->firstWhere('name', '=', 'Dozentenmaterial')?->id;
} }
} else { } else {
$this->selectedTags = $this->libraryItem->tags()
->where('type', 'library_item')
->get()
->map(fn($tag) => $tag->name)
->toArray();
$this->library = $this->libraryItem->libraries() $this->library = $this->libraryItem->libraries()
->first() ->first()
->id; ->id;
@@ -84,6 +93,11 @@ class LibraryItemForm extends Component
$this->libraryItem->save(); $this->libraryItem->save();
$this->libraryItem->setStatus('published'); $this->libraryItem->setStatus('published');
$this->libraryItem->syncTagsWithType(
$this->selectedTags,
'library_item'
);
if ($this->image) { if ($this->image) {
$this->libraryItem->addMedia($this->image) $this->libraryItem->addMedia($this->image)
->toMediaCollection('main'); ->toMediaCollection('main');
@@ -100,6 +114,18 @@ class LibraryItemForm extends Component
return to_route('library.table.libraryItems', ['country' => $this->country]); return to_route('library.table.libraryItems', ['country' => $this->country]);
} }
public function selectTag($name)
{
$selectedTags = collect($this->selectedTags);
if ($selectedTags->contains($name)) {
$selectedTags = $selectedTags->filter(fn($tag) => $tag !== $name);
} else {
$selectedTags->push($name);
}
$this->selectedTags = $selectedTags->values()
->toArray();
}
public function render() public function render()
{ {
return view('livewire.library.form.library-item-form', [ return view('livewire.library.form.library-item-form', [
@@ -117,6 +143,9 @@ class LibraryItemForm extends Component
'name' => $library->name, 'name' => $library->name,
]) ])
->toArray(), ->toArray(),
'tags' => Tag::query()
->where('type', 'library_item')
->get(),
]); ]);
} }
} }

View File

@@ -71,7 +71,28 @@
/> />
</x-input.group> </x-input.group>
@if($libraryItem->lecturer_id && $libraryItem->type && $library) <x-input.group :for="md5('selectedTags')" :label="__('Tags')">
<div class="py-2 flex flex-wrap items-center space-x-1">
@foreach($tags as $tag)
<div class="cursor-pointer" wire:key="tag{{ $loop->index }}"
wire:click="selectTag('{{ $tag->name }}')">
@if(collect($selectedTags)->contains($tag->name))
<x-badge
amber>
{{ $tag->name }}
</x-badge>
@else
<x-badge
black>
{{ $tag->name }}
</x-badge>
@endif
</div>
@endforeach
</div>
</x-input.group>
@if($libraryItem->lecturer_id && $libraryItem->type && $library && count($selectedTags) > 0)
<x-input.group :for=" md5('image')" :label="__('Main picture')"> <x-input.group :for=" md5('image')" :label="__('Main picture')">
<div class="py-4"> <div class="py-4">
@if ($image) @if ($image)