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;
}
}