2026-07-18 17:13:50 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire\Posts;
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
use App\Jobs\PublishPostJob;
|
2026-07-18 17:13:50 +03:00
|
|
|
use App\Models\Post;
|
|
|
|
|
use App\Models\Site;
|
|
|
|
|
use App\Models\SocialAccount;
|
2026-07-21 14:04:45 +03:00
|
|
|
use App\Support\Publishing\PublisherManager;
|
2026-07-18 17:13:50 +03:00
|
|
|
use Livewire\Attributes\Layout;
|
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
#[Layout('components.layouts.app')]
|
|
|
|
|
class Index extends Component
|
|
|
|
|
{
|
|
|
|
|
public bool $showForm = false;
|
|
|
|
|
|
|
|
|
|
public ?int $editingId = null;
|
|
|
|
|
|
|
|
|
|
public string $siteId = '';
|
|
|
|
|
|
|
|
|
|
public string $socialAccountId = '';
|
|
|
|
|
|
|
|
|
|
public string $type = '';
|
|
|
|
|
|
|
|
|
|
public string $title = '';
|
|
|
|
|
|
|
|
|
|
public string $body = '';
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
public string $target = '';
|
|
|
|
|
|
2026-07-18 17:13:50 +03:00
|
|
|
public string $status = 'draft';
|
|
|
|
|
|
|
|
|
|
public string $scheduledAt = '';
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
public ?string $publishNotice = null;
|
|
|
|
|
|
2026-07-18 17:13:50 +03:00
|
|
|
public function render()
|
|
|
|
|
{
|
2026-07-21 14:04:45 +03:00
|
|
|
$posts = Post::with(['site', 'socialAccount'])->orderByDesc('created_at')->get();
|
|
|
|
|
|
2026-07-18 17:13:50 +03:00
|
|
|
return view('livewire.posts.index', [
|
2026-07-21 14:04:45 +03:00
|
|
|
'posts' => $posts,
|
2026-07-18 17:13:50 +03:00
|
|
|
'sites' => Site::orderBy('name')->get(),
|
|
|
|
|
'accounts' => SocialAccount::orderBy('username')->get(),
|
|
|
|
|
'types' => Post::TYPES,
|
|
|
|
|
'statuses' => Post::STATUSES,
|
2026-07-21 14:04:45 +03:00
|
|
|
'hasPublishing' => $posts->contains('status', 'publishing'),
|
2026-07-18 17:13:50 +03:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
public function publish(int $id): void
|
|
|
|
|
{
|
|
|
|
|
$this->publishNotice = null;
|
|
|
|
|
|
|
|
|
|
$post = Post::with('site')->findOrFail($id);
|
|
|
|
|
|
|
|
|
|
if (! app(PublisherManager::class)->resolve($post)) {
|
|
|
|
|
$this->publishNotice = "No publisher available yet for post type '{$post->type}'.";
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$post->update(['status' => 'publishing', 'publish_error' => null]);
|
|
|
|
|
|
|
|
|
|
PublishPostJob::dispatch($post->id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 17:13:50 +03:00
|
|
|
public function create(): void
|
|
|
|
|
{
|
|
|
|
|
$this->resetForm();
|
|
|
|
|
$this->showForm = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(int $id): void
|
|
|
|
|
{
|
|
|
|
|
$post = Post::findOrFail($id);
|
|
|
|
|
$this->editingId = $post->id;
|
|
|
|
|
$this->siteId = (string) $post->site_id;
|
|
|
|
|
$this->socialAccountId = $post->social_account_id ? (string) $post->social_account_id : '';
|
|
|
|
|
$this->type = $post->type;
|
|
|
|
|
$this->title = (string) $post->title;
|
|
|
|
|
$this->body = $post->body;
|
2026-07-21 14:04:45 +03:00
|
|
|
$this->target = (string) $post->target;
|
2026-07-18 17:13:50 +03:00
|
|
|
$this->status = $post->status;
|
|
|
|
|
$this->scheduledAt = $post->scheduled_at?->format('Y-m-d\TH:i') ?? '';
|
|
|
|
|
$this->showForm = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function save(): void
|
|
|
|
|
{
|
|
|
|
|
$validated = $this->validate([
|
|
|
|
|
'siteId' => ['required', 'integer', 'exists:sites,id'],
|
|
|
|
|
'socialAccountId' => ['nullable', 'integer', 'exists:social_accounts,id'],
|
|
|
|
|
'type' => ['required', 'string', 'in:' . implode(',', Post::TYPES)],
|
|
|
|
|
'title' => ['nullable', 'string', 'max:255'],
|
|
|
|
|
'body' => ['required', 'string'],
|
2026-07-21 14:04:45 +03:00
|
|
|
'target' => ['nullable', 'string', 'max:255'],
|
2026-07-18 17:13:50 +03:00
|
|
|
'status' => ['required', 'string', 'in:' . implode(',', Post::STATUSES)],
|
|
|
|
|
'scheduledAt' => ['nullable', 'date'],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Post::updateOrCreate(['id' => $this->editingId], [
|
|
|
|
|
'site_id' => $validated['siteId'],
|
|
|
|
|
'social_account_id' => $validated['socialAccountId'] ?: null,
|
|
|
|
|
'type' => $validated['type'],
|
|
|
|
|
'title' => $validated['title'] ?: null,
|
|
|
|
|
'body' => $validated['body'],
|
2026-07-21 14:04:45 +03:00
|
|
|
'target' => $validated['target'] ?: null,
|
2026-07-18 17:13:50 +03:00
|
|
|
'status' => $validated['status'],
|
|
|
|
|
'scheduled_at' => $validated['scheduledAt'] ?: null,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->resetForm();
|
|
|
|
|
$this->showForm = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete(int $id): void
|
|
|
|
|
{
|
|
|
|
|
Post::findOrFail($id)->delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function resetForm(): void
|
|
|
|
|
{
|
|
|
|
|
$this->editingId = null;
|
|
|
|
|
$this->siteId = '';
|
|
|
|
|
$this->socialAccountId = '';
|
|
|
|
|
$this->type = '';
|
|
|
|
|
$this->title = '';
|
|
|
|
|
$this->body = '';
|
2026-07-21 14:04:45 +03:00
|
|
|
$this->target = '';
|
2026-07-18 17:13:50 +03:00
|
|
|
$this->status = 'draft';
|
|
|
|
|
$this->scheduledAt = '';
|
|
|
|
|
$this->resetValidation();
|
|
|
|
|
}
|
|
|
|
|
}
|