mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
tags creation added
This commit is contained in:
33
app/Console/Commands/Database/AddTagsToNewsArticles.php
Normal file
33
app/Console/Commands/Database/AddTagsToNewsArticles.php
Normal 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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ 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 App\Models\Tag;
|
||||||
|
use App\Traits\HasTagsTrait;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Livewire\WithFileUploads;
|
use Livewire\WithFileUploads;
|
||||||
@@ -14,6 +15,7 @@ use Spatie\LaravelOptions\Options;
|
|||||||
|
|
||||||
class LibraryItemForm extends Component
|
class LibraryItemForm extends Component
|
||||||
{
|
{
|
||||||
|
use HasTagsTrait;
|
||||||
use WithFileUploads;
|
use WithFileUploads;
|
||||||
|
|
||||||
public Country $country;
|
public Country $country;
|
||||||
@@ -26,8 +28,6 @@ class LibraryItemForm extends Component
|
|||||||
|
|
||||||
public $file;
|
public $file;
|
||||||
|
|
||||||
public array $selectedTags = [];
|
|
||||||
|
|
||||||
public bool $lecturer = false;
|
public bool $lecturer = false;
|
||||||
|
|
||||||
public ?string $fromUrl = '';
|
public ?string $fromUrl = '';
|
||||||
@@ -122,18 +122,6 @@ 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', [
|
||||||
@@ -150,9 +138,6 @@ class LibraryItemForm extends Component
|
|||||||
'name' => $library->name,
|
'name' => $library->name,
|
||||||
])
|
])
|
||||||
->toArray(),
|
->toArray(),
|
||||||
'tags' => Tag::query()
|
|
||||||
->where('type', 'library_item')
|
|
||||||
->get(),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
namespace App\Http\Livewire\News\Form;
|
namespace App\Http\Livewire\News\Form;
|
||||||
|
|
||||||
use App\Models\LibraryItem;
|
use App\Models\LibraryItem;
|
||||||
|
use App\Traits\HasTagsTrait;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Livewire\WithFileUploads;
|
use Livewire\WithFileUploads;
|
||||||
|
|
||||||
class NewsArticleForm extends Component
|
class NewsArticleForm extends Component
|
||||||
{
|
{
|
||||||
|
use HasTagsTrait;
|
||||||
use WithFileUploads;
|
use WithFileUploads;
|
||||||
|
|
||||||
public ?LibraryItem $libraryItem = null;
|
public ?LibraryItem $libraryItem = null;
|
||||||
@@ -36,13 +38,17 @@ class NewsArticleForm extends Component
|
|||||||
return [
|
return [
|
||||||
'image' => [Rule::requiredIf(!$this->libraryItem->id), 'nullable', 'mimes:jpeg,png,jpg,gif', 'max:10240'],
|
'image' => [Rule::requiredIf(!$this->libraryItem->id), 'nullable', 'mimes:jpeg,png,jpg,gif', 'max:10240'],
|
||||||
|
|
||||||
|
'selectedTags' => 'array|min:1',
|
||||||
|
|
||||||
'libraryItem.lecturer_id' => 'required',
|
'libraryItem.lecturer_id' => 'required',
|
||||||
'libraryItem.name' => 'required',
|
'libraryItem.name' => 'required',
|
||||||
'libraryItem.type' => 'required',
|
'libraryItem.type' => 'required',
|
||||||
'libraryItem.language_code' => 'required',
|
'libraryItem.language_code' => 'required',
|
||||||
'libraryItem.value' => 'required',
|
'libraryItem.value' => 'required',
|
||||||
'libraryItem.value_to_be_paid' => [Rule::requiredIf($this->libraryItem->sats > 0), 'nullable', 'string',],
|
'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.subtitle' => 'string|nullable',
|
||||||
'libraryItem.excerpt' => 'required',
|
'libraryItem.excerpt' => 'required',
|
||||||
'libraryItem.main_image_caption' => 'string|nullable',
|
'libraryItem.main_image_caption' => 'string|nullable',
|
||||||
@@ -68,6 +74,7 @@ class NewsArticleForm extends Component
|
|||||||
'language_code' => 'de',
|
'language_code' => 'de',
|
||||||
'approved' => false,
|
'approved' => false,
|
||||||
]);
|
]);
|
||||||
|
$this->selectedTags[] = 'News';
|
||||||
}
|
}
|
||||||
if (!$this->fromUrl) {
|
if (!$this->fromUrl) {
|
||||||
$this->fromUrl = url()->previous();
|
$this->fromUrl = url()->previous();
|
||||||
@@ -93,6 +100,11 @@ class NewsArticleForm extends Component
|
|||||||
$this->validate();
|
$this->validate();
|
||||||
$this->libraryItem->save();
|
$this->libraryItem->save();
|
||||||
|
|
||||||
|
$this->libraryItem->syncTagsWithType(
|
||||||
|
$this->selectedTags,
|
||||||
|
'library_item'
|
||||||
|
);
|
||||||
|
|
||||||
if ($this->image) {
|
if ($this->image) {
|
||||||
$this->libraryItem->addMedia($this->image)
|
$this->libraryItem->addMedia($this->image)
|
||||||
->usingFileName(md5($this->image->getClientOriginalName()).'.'.$this->image->getClientOriginalExtension())
|
->usingFileName(md5($this->image->getClientOriginalName()).'.'.$this->image->getClientOriginalExtension())
|
||||||
|
|||||||
34
app/Rules/TagUniqueRule.php
Normal file
34
app/Rules/TagUniqueRule.php
Normal 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]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
51
app/Traits/HasTagsTrait.php
Normal file
51
app/Traits/HasTagsTrait.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -74,19 +74,42 @@
|
|||||||
</x-input.group>
|
</x-input.group>
|
||||||
|
|
||||||
<x-input.group :for="md5('selectedTags')" :label="__('Tags')">
|
<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">
|
<div class="py-2 flex flex-wrap items-center space-x-1">
|
||||||
@foreach($tags as $tag)
|
@foreach($tags as $tag)
|
||||||
<div class="cursor-pointer" wire:key="tag{{ $loop->index }}"
|
<div class="cursor-pointer" wire:key="tag{{ $loop->index }}"
|
||||||
wire:click="selectTag('{{ $tag->name }}')">
|
wire:click="selectTag('{{ $tag['name'] }}')">
|
||||||
@if(collect($selectedTags)->contains($tag->name))
|
@if(collect($selectedTags)->contains($tag['name']))
|
||||||
<x-badge
|
<x-badge
|
||||||
amber>
|
amber>
|
||||||
{{ $tag->name }}
|
{{ $tag['name'] }}
|
||||||
</x-badge>
|
</x-badge>
|
||||||
@else
|
@else
|
||||||
<x-badge
|
<x-badge
|
||||||
black>
|
black>
|
||||||
{{ $tag->name }}
|
{{ $tag['name']}}
|
||||||
</x-badge>
|
</x-badge>
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -93,10 +93,10 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="flex flex-1 flex-col justify-between bg-21gray p-6">
|
<div class="flex flex-1 flex-col justify-between bg-21gray p-6">
|
||||||
<div class="flex-1">
|
<div class="flex-1">
|
||||||
<p class="text-sm font-medium text-amber-600">
|
<div class="text-sm font-medium text-amber-600">
|
||||||
<div
|
<div
|
||||||
class="text-amber-500">{{ $libraryItem->tags->pluck('name')->join(', ') }}</div>
|
class="text-amber-500">{{ $libraryItem->tags->pluck('name')->join(', ') }}</div>
|
||||||
</p>
|
</div>
|
||||||
<a href="{{ $link }}"
|
<a href="{{ $link }}"
|
||||||
class="mt-2 block">
|
class="mt-2 block">
|
||||||
<p class="text-xl font-semibold text-gray-200">{{ $libraryItem->name }}</p>
|
<p class="text-xl font-semibold text-gray-200">{{ $libraryItem->name }}</p>
|
||||||
|
|||||||
@@ -43,7 +43,8 @@
|
|||||||
@if($paid)
|
@if($paid)
|
||||||
<x-input.group :for="md5('libraryItem.sats')" :label="__('sats')">
|
<x-input.group :for="md5('libraryItem.sats')" :label="__('sats')">
|
||||||
<x-inputs.number min="21" autocomplete="off" wire:model.debounce="libraryItem.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>
|
</x-input.group>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
@@ -77,6 +78,50 @@
|
|||||||
/>
|
/>
|
||||||
</x-input.group>
|
</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)
|
@if($libraryItem->lecturer_id)
|
||||||
<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">
|
||||||
@@ -124,7 +169,8 @@
|
|||||||
/>
|
/>
|
||||||
</x-input.group>
|
</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
|
<div
|
||||||
class="text-amber-500 text-xs py-2">{{ __('For images in Markdown, please use eg. Imgur or another provider.') }}</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"/>
|
<x-input.simple-mde wire:model.defer="libraryItem.value"/>
|
||||||
@@ -132,12 +178,14 @@
|
|||||||
</x-input.group>
|
</x-input.group>
|
||||||
|
|
||||||
@if($paid)
|
@if($paid)
|
||||||
<x-input.group :for="md5('libraryItem.value_to_be_paid')" :label="__('Part of the article to be paid')">
|
<x-input.group :for="md5('libraryItem.value_to_be_paid')"
|
||||||
<div
|
:label="__('Part of the article to be paid')">
|
||||||
class="text-amber-500 text-xs py-2">{{ __('For images in Markdown, please use eg. Imgur or another provider.') }}</div>
|
<div
|
||||||
<x-input.simple-mde wire:model.defer="libraryItem.value_to_be_paid"/>
|
class="text-amber-500 text-xs py-2">{{ __('For images in Markdown, please use eg. Imgur or another provider.') }}</div>
|
||||||
@error('libraryItem.value_to_be_paid') <span class="text-red-500 py-2">{{ $message }}</span> @enderror
|
<x-input.simple-mde wire:model.defer="libraryItem.value_to_be_paid"/>
|
||||||
</x-input.group>
|
@error('libraryItem.value_to_be_paid') <span
|
||||||
|
class="text-red-500 py-2">{{ $message }}</span> @enderror
|
||||||
|
</x-input.group>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
<x-input.group :for="md5('libraryItem.read_time')" :label="__('Time to read')">
|
<x-input.group :for="md5('libraryItem.read_time')" :label="__('Time to read')">
|
||||||
|
|||||||
Reference in New Issue
Block a user