post = $post; $this->title_ar = $post->title['ar'] ?? ''; $this->title_en = $post->title['en'] ?? ''; $this->body_ar = $post->body['ar'] ?? ''; $this->body_en = $post->body['en'] ?? ''; $this->status = $post->status->value; } public function rules(): array { return [ 'title_ar' => ['required', 'string', 'max:255'], 'title_en' => ['required', 'string', 'max:255'], 'body_ar' => ['required', 'string'], 'body_en' => ['required', 'string'], 'status' => ['required', 'in:draft,published'], ]; } public function messages(): array { return [ 'title_ar.required' => __('posts.title_ar_required'), 'title_en.required' => __('posts.title_en_required'), 'body_ar.required' => __('posts.body_ar_required'), 'body_en.required' => __('posts.body_en_required'), ]; } public function save(): void { $validated = $this->validate(); $oldValues = $this->post->toArray(); $this->post->update([ 'title' => [ 'ar' => $validated['title_ar'], 'en' => $validated['title_en'], ], 'body' => [ 'ar' => clean($validated['body_ar']), 'en' => clean($validated['body_en']), ], 'status' => $validated['status'], 'published_at' => $validated['status'] === 'published' && ! $this->post->published_at ? now() : $this->post->published_at, ]); AdminLog::create([ 'admin_id' => auth()->id(), 'action' => 'update', 'target_type' => 'post', 'target_id' => $this->post->id, 'old_values' => $oldValues, 'new_values' => $this->post->fresh()->toArray(), 'ip_address' => request()->ip(), 'created_at' => now(), ]); session()->flash('success', __('posts.post_saved')); } public function saveDraft(): void { $this->status = 'draft'; $this->save(); } public function publish(): void { $this->status = 'published'; $this->save(); } public function autoSave(): void { if ($this->status === 'draft') { $this->post->update([ 'title' => [ 'ar' => $this->title_ar, 'en' => $this->title_en, ], 'body' => [ 'ar' => clean($this->body_ar), 'en' => clean($this->body_en), ], ]); } } public function preview(): void { $this->showPreview = true; } public function closePreview(): void { $this->showPreview = false; } }; ?>
{{ __('posts.last_updated') }}: {{ $post->updated_at->diffForHumans() }} @if($status === 'draft') ({{ __('posts.auto_save_enabled') }}) @endif