tags creation added

This commit is contained in:
HolgerHatGarKeineNode
2023-03-17 13:36:59 +01:00
parent f87bf151da
commit d7ff201301
8 changed files with 220 additions and 34 deletions

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Console\Commands\Database;
use App\Models\LibraryItem;
use Illuminate\Console\Command;
class AddTagsToNewsArticles extends Command
{
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'news:tags';
/**
* The console command description.
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): void
{
LibraryItem::query()
->where('news', true)
->get()
->each(fn(LibraryItem $libraryItem) => $libraryItem->syncTagsWithType(['News'],
'library_item'));
}
}

View File

@@ -7,6 +7,7 @@ use App\Models\Country;
use App\Models\Library;
use App\Models\LibraryItem;
use App\Models\Tag;
use App\Traits\HasTagsTrait;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;
@@ -14,6 +15,7 @@ use Spatie\LaravelOptions\Options;
class LibraryItemForm extends Component
{
use HasTagsTrait;
use WithFileUploads;
public Country $country;
@@ -26,8 +28,6 @@ class LibraryItemForm extends Component
public $file;
public array $selectedTags = [];
public bool $lecturer = false;
public ?string $fromUrl = '';
@@ -122,18 +122,6 @@ class LibraryItemForm extends Component
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()
{
return view('livewire.library.form.library-item-form', [
@@ -150,9 +138,6 @@ class LibraryItemForm extends Component
'name' => $library->name,
])
->toArray(),
'tags' => Tag::query()
->where('type', 'library_item')
->get(),
]);
}
}

View File

@@ -3,12 +3,14 @@
namespace App\Http\Livewire\News\Form;
use App\Models\LibraryItem;
use App\Traits\HasTagsTrait;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Livewire\WithFileUploads;
class NewsArticleForm extends Component
{
use HasTagsTrait;
use WithFileUploads;
public ?LibraryItem $libraryItem = null;
@@ -36,13 +38,17 @@ class NewsArticleForm extends Component
return [
'image' => [Rule::requiredIf(!$this->libraryItem->id), 'nullable', 'mimes:jpeg,png,jpg,gif', 'max:10240'],
'selectedTags' => 'array|min:1',
'libraryItem.lecturer_id' => 'required',
'libraryItem.name' => 'required',
'libraryItem.type' => 'required',
'libraryItem.language_code' => 'required',
'libraryItem.value' => 'required',
'libraryItem.value_to_be_paid' => [Rule::requiredIf($this->libraryItem->sats > 0), 'nullable', 'string',],
'libraryItem.sats' => [Rule::requiredIf($this->libraryItem->sats > 0), 'nullable', 'numeric',],
'libraryItem.sats' => [
Rule::requiredIf($this->libraryItem->sats > 0), 'nullable', 'numeric',
],
'libraryItem.subtitle' => 'string|nullable',
'libraryItem.excerpt' => 'required',
'libraryItem.main_image_caption' => 'string|nullable',
@@ -68,6 +74,7 @@ class NewsArticleForm extends Component
'language_code' => 'de',
'approved' => false,
]);
$this->selectedTags[] = 'News';
}
if (!$this->fromUrl) {
$this->fromUrl = url()->previous();
@@ -93,6 +100,11 @@ class NewsArticleForm extends Component
$this->validate();
$this->libraryItem->save();
$this->libraryItem->syncTagsWithType(
$this->selectedTags,
'library_item'
);
if ($this->image) {
$this->libraryItem->addMedia($this->image)
->usingFileName(md5($this->image->getClientOriginalName()).'.'.$this->image->getClientOriginalExtension())

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Rules;
use App\Models\Tag;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class TagUniqueRule implements ValidationRule
{
/**
* Indicates whether the rule should be implicit.
*
* @var bool
*/
public $implicit = true;
public function __construct(public string $type = 'library_item')
{
}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$tag = Tag::findFromString($value, $this->type);
if ($tag) {
$fail(__('Tags must be unique', ['attribute' => $attribute]));
}
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Traits;
use App\Models\Tag;
use App\Rules\TagUniqueRule;
trait HasTagsTrait
{
public array $tags = [];
public array $selectedTags = [];
public bool $addTag = false;
public $newTag;
public function mountHasTagsTrait()
{
$this->tags = Tag::query()
->where('type', 'library_item')
->get()
->map(fn($tag) => ['id' => $tag->id, 'name' => $tag->name])
->toArray();
}
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 addTag()
{
$this->validateOnly('newTag', [
'newTag' => ['required', 'string', new TagUniqueRule()],
]);
Tag::create(['name' => $this->newTag, 'type' => 'library_item']);
$this->tags = Tag::query()
->where('type', 'library_item')
->get()
->map(fn($tag) => ['id' => $tag->id, 'name' => $tag->name])
->toArray();
$this->newTag = '';
$this->addTag = false;
}
}

View File

@@ -74,19 +74,42 @@
</x-input.group>
<x-input.group :for="md5('selectedTags')" :label="__('Tags')">
<x-slot name="label">
<div class="flex flex-row space-x-4 items-center">
<div>
{{ __('Tags') }}
</div>
@if(!$addTag)
<x-button
xs
wire:click="$set('addTag', true)"
>
<i class="fa fa-thin fa-plus"></i>
{{ __('Add') }}
</x-button>
@else
<x-input label="" wire:model.debounce="newTag" placeholder="{{ __('New tag') }}"/>
<x-button
xs
wire:click="addTag">
<i class="text-xl fa-thin fa-save"></i>
</x-button>
@endif
</div>
</x-slot>
<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))
wire:click="selectTag('{{ $tag['name'] }}')">
@if(collect($selectedTags)->contains($tag['name']))
<x-badge
amber>
{{ $tag->name }}
{{ $tag['name'] }}
</x-badge>
@else
<x-badge
black>
{{ $tag->name }}
{{ $tag['name']}}
</x-badge>
@endif
</div>

View File

@@ -93,10 +93,10 @@
</div>
<div class="flex flex-1 flex-col justify-between bg-21gray p-6">
<div class="flex-1">
<p class="text-sm font-medium text-amber-600">
<div
class="text-amber-500">{{ $libraryItem->tags->pluck('name')->join(', ') }}</div>
</p>
<div class="text-sm font-medium text-amber-600">
<div
class="text-amber-500">{{ $libraryItem->tags->pluck('name')->join(', ') }}</div>
</div>
<a href="{{ $link }}"
class="mt-2 block">
<p class="text-xl font-semibold text-gray-200">{{ $libraryItem->name }}</p>

View File

@@ -43,7 +43,8 @@
@if($paid)
<x-input.group :for="md5('libraryItem.sats')" :label="__('sats')">
<x-inputs.number min="21" autocomplete="off" wire:model.debounce="libraryItem.sats"
:placeholder="__('sats')" :hint="__('How many sats to read this article?')"/>
:placeholder="__('sats')"
:hint="__('How many sats to read this article?')"/>
</x-input.group>
@endif
@@ -77,6 +78,50 @@
/>
</x-input.group>
<x-input.group :for="md5('selectedTags')" :label="__('Tags')">
<x-slot name="label">
<div class="flex flex-row space-x-4 items-center">
<div>
{{ __('Tags') }}
</div>
@if(!$addTag)
<x-button
xs
wire:click="$set('addTag', true)"
>
<i class="fa fa-thin fa-plus"></i>
{{ __('Add') }}
</x-button>
@else
<x-input label="" wire:model.debounce="newTag" placeholder="{{ __('New tag') }}"/>
<x-button
xs
wire:click="addTag">
<i class="text-xl fa-thin fa-save"></i>
</x-button>
@endif
</div>
</x-slot>
<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)
<x-input.group :for="md5('image')" :label="__('Main picture')">
<div class="py-4">
@@ -124,7 +169,8 @@
/>
</x-input.group>
<x-input.group :for="md5('libraryItem.value')" :label="$paid ? __('Free part of the Article as Markdown') : __('Article as Markdown')">
<x-input.group :for="md5('libraryItem.value')"
:label="$paid ? __('Free part of the Article as Markdown') : __('Article as Markdown')">
<div
class="text-amber-500 text-xs py-2">{{ __('For images in Markdown, please use eg. Imgur or another provider.') }}</div>
<x-input.simple-mde wire:model.defer="libraryItem.value"/>
@@ -132,12 +178,14 @@
</x-input.group>
@if($paid)
<x-input.group :for="md5('libraryItem.value_to_be_paid')" :label="__('Part of the article to be paid')">
<div
class="text-amber-500 text-xs py-2">{{ __('For images in Markdown, please use eg. Imgur or another provider.') }}</div>
<x-input.simple-mde wire:model.defer="libraryItem.value_to_be_paid"/>
@error('libraryItem.value_to_be_paid') <span class="text-red-500 py-2">{{ $message }}</span> @enderror
</x-input.group>
<x-input.group :for="md5('libraryItem.value_to_be_paid')"
:label="__('Part of the article to be paid')">
<div
class="text-amber-500 text-xs py-2">{{ __('For images in Markdown, please use eg. Imgur or another provider.') }}</div>
<x-input.simple-mde wire:model.defer="libraryItem.value_to_be_paid"/>
@error('libraryItem.value_to_be_paid') <span
class="text-red-500 py-2">{{ $message }}</span> @enderror
</x-input.group>
@endif
<x-input.group :for="md5('libraryItem.read_time')" :label="__('Time to read')">