resetPage(); } public function updatedStatusFilter(): void { $this->resetPage(); } public function updatedPerPage(): void { $this->resetPage(); } public function sort(string $column): void { if ($this->sortBy === $column) { $this->sortDir = $this->sortDir === 'asc' ? 'desc' : 'asc'; } else { $this->sortBy = $column; $this->sortDir = 'asc'; } } public function clearFilters(): void { $this->search = ''; $this->statusFilter = ''; $this->resetPage(); } public function togglePublish(int $id): void { DB::transaction(function () use ($id) { $post = Post::lockForUpdate()->find($id); if (! $post) { session()->flash('error', __('posts.post_not_found')); return; } $oldStatus = $post->status->value; $newStatus = $post->status === PostStatus::Published ? PostStatus::Draft : PostStatus::Published; $post->update([ 'status' => $newStatus, 'published_at' => $newStatus === PostStatus::Published ? now() : null, ]); AdminLog::create([ 'admin_id' => auth()->id(), 'action' => 'status_change', 'target_type' => 'post', 'target_id' => $post->id, 'old_values' => ['status' => $oldStatus], 'new_values' => ['status' => $newStatus->value], 'ip_address' => request()->ip(), 'created_at' => now(), ]); session()->flash('success', __('posts.post_status_updated')); }); } public function delete(int $id): void { $this->postToDelete = Post::find($id); if (! $this->postToDelete) { session()->flash('error', __('posts.post_not_found')); return; } $this->showDeleteModal = true; } public function confirmDelete(): void { if (! $this->postToDelete) { return; } DB::transaction(function () { $post = Post::lockForUpdate()->find($this->postToDelete->id); if (! $post) { session()->flash('error', __('posts.post_not_found')); return; } AdminLog::create([ 'admin_id' => auth()->id(), 'action' => 'delete', 'target_type' => 'post', 'target_id' => $post->id, 'old_values' => $post->toArray(), 'ip_address' => request()->ip(), 'created_at' => now(), ]); $post->delete(); session()->flash('success', __('posts.post_deleted')); }); $this->showDeleteModal = false; $this->postToDelete = null; } public function cancelDelete(): void { $this->showDeleteModal = false; $this->postToDelete = null; } public function with(): array { return [ 'posts' => Post::query() ->when($this->search, fn ($q) => $q->where(function ($q) { $q->whereRaw("JSON_EXTRACT(title, '$.\"ar\"') LIKE ?", ["%{$this->search}%"]) ->orWhereRaw("JSON_EXTRACT(title, '$.\"en\"') LIKE ?", ["%{$this->search}%"]) ->orWhereRaw("JSON_EXTRACT(body, '$.\"ar\"') LIKE ?", ["%{$this->search}%"]) ->orWhereRaw("JSON_EXTRACT(body, '$.\"en\"') LIKE ?", ["%{$this->search}%"]); })) ->when($this->statusFilter, fn ($q) => $q->where('status', $this->statusFilter)) ->orderBy($this->sortBy, $this->sortDir) ->paginate($this->perPage), 'statuses' => PostStatus::cases(), ]; } }; ?>
{{ __('posts.posts_description') }}
{{ __('posts.no_posts') }}
{{ __('posts.deleting_post', ['title' => $postToDelete->getTitle()]) }}
@endif