wip: article AI polish, category SEO fields, cover generator, membership plan seeder
This commit is contained in:
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Resources\Articles;
|
||||
|
||||
use App\Filament\Concerns\HasTranslatedLabels;
|
||||
use App\Filament\Resources\Articles\Pages\CreateArticle;
|
||||
use App\Filament\Resources\Articles\Pages\EditArticle;
|
||||
use App\Filament\Resources\Articles\Pages\ListArticles;
|
||||
use App\Filament\Resources\Articles\RelationManagers\CommentsRelationManager;
|
||||
use App\Filament\Resources\Articles\Schemas\ArticleForm;
|
||||
use App\Filament\Resources\Articles\Tables\ArticlesTable;
|
||||
use App\Models\Article;
|
||||
@@ -15,8 +17,6 @@ use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
use App\Filament\Concerns\HasTranslatedLabels;
|
||||
|
||||
class ArticleResource extends Resource
|
||||
{
|
||||
@@ -24,9 +24,6 @@ class ArticleResource extends Resource
|
||||
|
||||
protected static ?string $model = Article::class;
|
||||
|
||||
|
||||
|
||||
|
||||
protected static function navKey(): string
|
||||
{
|
||||
return 'articles';
|
||||
@@ -57,7 +54,7 @@ class ArticleResource extends Resource
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
CommentsRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -19,8 +19,14 @@ class CreateArticle extends CreateRecord
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
$filtered = Hook::filter('filament.article.mutate_before_save', $data, null);
|
||||
$data = is_array($filtered) ? $filtered : $data;
|
||||
|
||||
return is_array($filtered) ? $filtered : $data;
|
||||
$validated = Hook::filter('filament.article.validate_access_restrictions', $data, null);
|
||||
$data = is_array($validated) ? $validated : $data;
|
||||
|
||||
unset($data['paid_content'], $data['membership']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
|
||||
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Resources\Articles\Pages;
|
||||
|
||||
use App\Domain\Ai\Jobs\GenerateArticleCoverJob;
|
||||
use App\Domain\Ai\Jobs\OptimizeArticleContentJob;
|
||||
use App\Domain\Plugin\Hook;
|
||||
use App\Filament\Resources\Articles\ArticleResource;
|
||||
use App\Settings\AiSettings;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Notifications\Notification;
|
||||
@@ -21,6 +23,13 @@ class EditArticle extends EditRecord
|
||||
return [
|
||||
Action::make('aiOptimize')
|
||||
->label(__('admin.messages.ai_optimize'))
|
||||
->visible(function (): bool {
|
||||
try {
|
||||
return (bool) app(AiSettings::class)->content_optimization_enabled;
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
->action(function (): void {
|
||||
OptimizeArticleContentJob::dispatch($this->record->getKey());
|
||||
Notification::make()
|
||||
@@ -29,6 +38,55 @@ class EditArticle extends EditRecord
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
Action::make('applyAiPolish')
|
||||
->label(__('admin.messages.ai_apply_polish'))
|
||||
->color('gray')
|
||||
->requiresConfirmation()
|
||||
->modalHeading(__('admin.messages.ai_apply_polish'))
|
||||
->modalDescription(__('admin.messages.ai_apply_polish_confirm'))
|
||||
->visible(fn (): bool => filled($this->record->ai_polished_content))
|
||||
->action(function (): void {
|
||||
$polished = (string) $this->record->ai_polished_content;
|
||||
|
||||
if ($polished === '') {
|
||||
Notification::make()
|
||||
->title(__('admin.messages.ai_polish_missing'))
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->record->forceFill(['content' => $polished])->save();
|
||||
$this->refreshFormData(['content', 'ai_polished_content', 'ai_summary', 'description']);
|
||||
|
||||
Notification::make()
|
||||
->title(__('admin.messages.ai_apply_polish_done'))
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
Action::make('autoCover')
|
||||
->label(__('admin.messages.auto_cover'))
|
||||
->color('gray')
|
||||
->action(function (): void {
|
||||
GenerateArticleCoverJob::dispatch($this->record->getKey(), 'auto');
|
||||
Notification::make()
|
||||
->title(__('admin.messages.auto_cover_queued'))
|
||||
->body(__('admin.messages.ai_optimize_queue_hint'))
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
Action::make('generateCover')
|
||||
->label(__('admin.messages.generate_cover'))
|
||||
->color('gray')
|
||||
->action(function (): void {
|
||||
GenerateArticleCoverJob::dispatch($this->record->getKey(), 'generate');
|
||||
Notification::make()
|
||||
->title(__('admin.messages.generate_cover_queued'))
|
||||
->body(__('admin.messages.ai_optimize_queue_hint'))
|
||||
->success()
|
||||
->send();
|
||||
}),
|
||||
DeleteAction::make(),
|
||||
...Hook::collect('filament.article.actions'),
|
||||
];
|
||||
@@ -40,6 +98,13 @@ class EditArticle extends EditRecord
|
||||
*/
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
$items = is_array($this->record->ai_suggestions) ? $this->record->ai_suggestions : [];
|
||||
$data['ai_suggestions_text'] = collect($items)
|
||||
->filter(fn ($item) => filled($item))
|
||||
->values()
|
||||
->map(fn ($item, $index) => ($index + 1).'. '.$item)
|
||||
->implode("\n");
|
||||
|
||||
$filtered = Hook::filter('filament.article.mutate_before_fill', $data, $this->record);
|
||||
|
||||
return is_array($filtered) ? $filtered : $data;
|
||||
@@ -51,9 +116,26 @@ class EditArticle extends EditRecord
|
||||
*/
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
$filtered = Hook::filter('filament.article.mutate_before_save', $data, $this->record);
|
||||
if (filled($data['cover_path'] ?? null)) {
|
||||
$path = (string) $data['cover_path'];
|
||||
$data['cover_status'] = 'ready';
|
||||
$data['cover_source'] = str_starts_with($path, 'http://') || str_starts_with($path, 'https://')
|
||||
? 'content_image'
|
||||
: 'manual';
|
||||
$data['cover_disk'] = ($data['cover_source'] === 'content_image')
|
||||
? 'url'
|
||||
: ($this->record->cover_disk ?: config('larablog.attachments_disk', 'attachments'));
|
||||
}
|
||||
|
||||
return is_array($filtered) ? $filtered : $data;
|
||||
$filtered = Hook::filter('filament.article.mutate_before_save', $data, $this->record);
|
||||
$data = is_array($filtered) ? $filtered : $data;
|
||||
|
||||
$validated = Hook::filter('filament.article.validate_access_restrictions', $data, $this->record);
|
||||
$data = is_array($validated) ? $validated : $data;
|
||||
|
||||
unset($data['paid_content'], $data['membership']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function afterSave(): void
|
||||
|
||||
@@ -5,8 +5,8 @@ declare(strict_types=1);
|
||||
namespace App\Filament\Resources\Articles\Pages;
|
||||
|
||||
use App\Filament\Resources\Articles\ArticleResource;
|
||||
use App\Filament\Resources\Pages\ListRecords;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListArticles extends ListRecords
|
||||
{
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Resources\Articles\RelationManagers;
|
||||
|
||||
use App\Filament\Support\AdminTable;
|
||||
use App\Models\Comment;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CommentsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'comments';
|
||||
|
||||
public static function getTitle(Model $ownerRecord, string $pageClass): string
|
||||
{
|
||||
return __('admin.nav.comments');
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextInput::make('author')
|
||||
->label(__('admin.fields.author'))
|
||||
->required(),
|
||||
TextInput::make('url')
|
||||
->label(__('admin.fields.url'))
|
||||
->url(),
|
||||
Textarea::make('content')
|
||||
->label(__('admin.fields.content'))
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
Select::make('moderation_status')
|
||||
->label(__('admin.fields.moderation_status'))
|
||||
->options([
|
||||
Comment::STATUS_PENDING => __('admin.options.moderation.pending'),
|
||||
Comment::STATUS_PENDING_AI => __('admin.options.moderation.pending_ai'),
|
||||
Comment::STATUS_APPROVED => __('admin.options.moderation.approved'),
|
||||
Comment::STATUS_REJECTED => __('admin.options.moderation.rejected'),
|
||||
Comment::STATUS_NEEDS_HUMAN => __('admin.options.moderation.needs_human'),
|
||||
])
|
||||
->required()
|
||||
->default(Comment::STATUS_PENDING),
|
||||
DateTimePicker::make('published_at')
|
||||
->label(__('admin.fields.published_at')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('author')
|
||||
->columns([
|
||||
TextColumn::make('author')
|
||||
->label(__('admin.fields.author'))
|
||||
->searchable(),
|
||||
AdminTable::ellipsis(
|
||||
TextColumn::make('content')
|
||||
->label(__('admin.fields.content'))
|
||||
->html()
|
||||
->formatStateUsing(fn (?string $state): string => trim(html_entity_decode(strip_tags((string) $state), ENT_QUOTES | ENT_HTML5, 'UTF-8'))),
|
||||
),
|
||||
TextColumn::make('moderation_status')
|
||||
->label(__('admin.fields.moderation_status'))
|
||||
->badge(),
|
||||
TextColumn::make('published_at')
|
||||
->label(__('admin.fields.published_at'))
|
||||
->dateTime()
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('moderation_status')
|
||||
->label(__('admin.fields.moderation_status'))
|
||||
->options([
|
||||
Comment::STATUS_PENDING => __('admin.options.moderation.pending'),
|
||||
Comment::STATUS_PENDING_AI => __('admin.options.moderation.pending_ai'),
|
||||
Comment::STATUS_APPROVED => __('admin.options.moderation.approved'),
|
||||
Comment::STATUS_REJECTED => __('admin.options.moderation.rejected'),
|
||||
Comment::STATUS_NEEDS_HUMAN => __('admin.options.moderation.needs_human'),
|
||||
]),
|
||||
])
|
||||
->headerActions([
|
||||
CreateAction::make()
|
||||
->mutateFormDataUsing(function (array $data): array {
|
||||
$data['published_at'] ??= now();
|
||||
$data['moderation_status'] ??= Comment::STATUS_PENDING;
|
||||
|
||||
return $data;
|
||||
})
|
||||
->after(function (): void {
|
||||
$this->getOwnerRecord()->increment('comments_count');
|
||||
}),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
DeleteAction::make()
|
||||
->after(function (): void {
|
||||
$this->getOwnerRecord()->decrement('comments_count');
|
||||
}),
|
||||
])
|
||||
->toolbarActions([
|
||||
DeleteBulkAction::make()
|
||||
->after(function (): void {
|
||||
$this->getOwnerRecord()->forceFill([
|
||||
'comments_count' => $this->getOwnerRecord()->comments()->count(),
|
||||
])->save();
|
||||
}),
|
||||
])
|
||||
->defaultSort('published_at', 'desc');
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ use App\Domain\Plugin\Hook;
|
||||
use App\Settings\GeneralSettings;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
@@ -78,8 +78,33 @@ class ArticleForm
|
||||
TextInput::make('read_password')
|
||||
->label(__('admin.fields.read_password'))
|
||||
->password(),
|
||||
TextInput::make('cover_path')
|
||||
->label(__('admin.fields.cover_path'))
|
||||
->helperText(__('admin.helpers.cover_path'))
|
||||
->columnSpanFull(),
|
||||
TextInput::make('cover_source')
|
||||
->label(__('admin.fields.cover_source'))
|
||||
->disabled()
|
||||
->dehydrated(false),
|
||||
TextInput::make('cover_status')
|
||||
->label(__('admin.fields.cover_status'))
|
||||
->disabled()
|
||||
->dehydrated(false),
|
||||
Textarea::make('ai_summary')
|
||||
->label(__('admin.fields.ai_summary'))
|
||||
->rows(3)
|
||||
->columnSpanFull(),
|
||||
Textarea::make('ai_polished_content')
|
||||
->label(__('admin.fields.ai_polished_content'))
|
||||
->helperText(__('admin.helpers.ai_polished_content'))
|
||||
->rows(12)
|
||||
->columnSpanFull(),
|
||||
Textarea::make('ai_suggestions_text')
|
||||
->label(__('admin.fields.ai_suggestions'))
|
||||
->helperText(__('admin.helpers.ai_suggestions'))
|
||||
->rows(4)
|
||||
->disabled()
|
||||
->dehydrated(false)
|
||||
->columnSpanFull(),
|
||||
...Hook::collect('filament.article.form'),
|
||||
]);
|
||||
|
||||
@@ -5,12 +5,16 @@ declare(strict_types=1);
|
||||
namespace App\Filament\Resources\Articles\Tables;
|
||||
|
||||
use App\Domain\Plugin\Hook;
|
||||
use App\Filament\Support\AdminTable;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ArticlesTable
|
||||
{
|
||||
@@ -18,21 +22,32 @@ class ArticlesTable
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
AdminTable::stickyStart(
|
||||
TextColumn::make('id')
|
||||
->label(__('admin.fields.id'))
|
||||
->sortable(),
|
||||
),
|
||||
TextColumn::make('category.name')
|
||||
->label(__('admin.fields.category'))
|
||||
->searchable(),
|
||||
TextColumn::make('user.name')
|
||||
->label(__('admin.fields.author'))
|
||||
->searchable(),
|
||||
TextColumn::make('title')
|
||||
->label(__('admin.fields.title'))
|
||||
->searchable(),
|
||||
TextColumn::make('description')
|
||||
->label(__('admin.fields.description'))
|
||||
->searchable(),
|
||||
AdminTable::ellipsis(
|
||||
TextColumn::make('title')
|
||||
->label(__('admin.fields.title'))
|
||||
->searchable(),
|
||||
),
|
||||
AdminTable::ellipsis(
|
||||
TextColumn::make('description')
|
||||
->label(__('admin.fields.description'))
|
||||
->searchable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
),
|
||||
TextColumn::make('keywords')
|
||||
->label(__('admin.fields.keywords'))
|
||||
->searchable(),
|
||||
->searchable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('published_at')
|
||||
->label(__('admin.fields.published_at'))
|
||||
->dateTime()
|
||||
@@ -66,15 +81,33 @@ class ArticlesTable
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('slug')
|
||||
->label(__('admin.fields.slug'))
|
||||
->searchable(),
|
||||
->searchable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('content_format')
|
||||
->label(__('admin.fields.content_format'))
|
||||
->searchable(),
|
||||
->searchable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
...Hook::collect('filament.article.table.columns'),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
SelectFilter::make('category_id')
|
||||
->label(__('admin.fields.category'))
|
||||
->relationship('category', 'name')
|
||||
->searchable()
|
||||
->preload(),
|
||||
TernaryFilter::make('visible')
|
||||
->label(__('admin.fields.visible')),
|
||||
TernaryFilter::make('stick')
|
||||
->label(__('admin.fields.stick')),
|
||||
TernaryFilter::make('close_comment')
|
||||
->label(__('admin.fields.close_comment')),
|
||||
...Hook::collect('filament.article.table.filters'),
|
||||
])
|
||||
->modifyQueryUsing(function (Builder $query): Builder {
|
||||
$modified = Hook::filter('filament.article.table.query', $query);
|
||||
|
||||
return $modified instanceof Builder ? $modified : $query;
|
||||
})
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
...Hook::collect('filament.article.actions'),
|
||||
|
||||
Reference in New Issue
Block a user