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\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
|
||||
|
||||
Reference in New Issue
Block a user