diff --git a/src/app/Livewire/DocumentEditor.php b/src/app/Livewire/DocumentEditor.php index cc8ccfc..f6abc43 100644 --- a/src/app/Livewire/DocumentEditor.php +++ b/src/app/Livewire/DocumentEditor.php @@ -2,30 +2,44 @@ namespace App\Livewire; +use App\Http\Middleware\SetLocale; use App\Models\Document; use App\Services\DocumentService; -use Livewire\Component; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Auth; +use Livewire\Component; class DocumentEditor extends Component { public ?Document $document = null; - public $title = ''; - public $content = ''; - public $directory = ''; - public $isEditMode = false; + public string $title = ''; + public string $content = ''; + public string $editingLocale = ''; + public bool $isEditMode = false; + public bool $isNewLocale = false; + public array $availableLocales = []; - public function mount(?Document $document = null) + public function mount(?Document $document = null, ?string $locale = null) { if ($document) { $this->authorize('update', $document); - - $this->document = $document; - $this->title = $document->title; - $this->content = $document->content; - $this->directory = $document->directory; + $this->document = $document->load('translations'); $this->isEditMode = true; + $this->availableLocales = $document->availableLocales(); + $this->editingLocale = $locale ?: ($document->default_locale ?? App::getLocale()); + + $translation = $document->translations->firstWhere('locale', $this->editingLocale); + if ($translation) { + $this->title = $translation->title; + $this->content = $translation->content; + $this->isNewLocale = false; + } else { + $this->title = ''; + $this->content = ''; + $this->isNewLocale = true; + } } else { + $this->editingLocale = App::getLocale(); $titleParam = request()->query('title'); if ($titleParam) { $this->title = $titleParam; @@ -35,53 +49,96 @@ public function mount(?Document $document = null) public function save(DocumentService $documentService) { - $this->validate([ + $validated = $this->validate([ 'title' => 'required|string|max:255', 'content' => 'required|string', + 'editingLocale' => ['required', 'string', 'in:' . implode(',', array_keys(SetLocale::SUPPORTED_LOCALES))], ]); try { if ($this->isEditMode && $this->document) { $this->authorize('update', $this->document); - $this->document = $documentService->updateDocument( - $this->document, - $this->title, - $this->content, - Auth::id() - ); + if ($this->isNewLocale) { + $documentService->addTranslation( + $this->document, + $this->editingLocale, + $this->title, + $this->content, + Auth::id(), + ); + $this->document->refresh()->load('translations'); + } else { + $this->document = $documentService->updateDocument( + $this->document, + $this->title, + $this->content, + Auth::id(), + $this->editingLocale, + ); + } - session()->flash('message', 'Document updated successfully!'); + session()->flash('message', __('messages.documents.update_success')); return $this->redirect(route('documents.show', $this->document)); } else { $this->document = $documentService->createDocument( $this->title, $this->content, Auth::id(), - $this->directory ?: null + $this->editingLocale, ); - - session()->flash('message', 'Document created successfully!'); + session()->flash('message', __('messages.documents.create_success')); return $this->redirect(route('documents.show', $this->document)); } + } catch (\InvalidArgumentException $e) { + session()->flash('error', $e->getMessage()); } catch (\Exception $e) { session()->flash('error', 'Error saving document: ' . $e->getMessage()); } } + public function deleteTranslation(DocumentService $documentService) + { + if (!$this->isEditMode || !$this->document || $this->isNewLocale) { + return; + } + $this->authorize('update', $this->document); + + try { + $documentService->deleteTranslation($this->document, $this->editingLocale); + session()->flash('message', __('messages.documents.translation_deleted')); + return $this->redirect(route('documents.show', $this->document)); + } catch (\InvalidArgumentException $e) { + session()->flash('error', $e->getMessage()); + } + } + + public function setAsDefault(DocumentService $documentService) + { + if (!$this->isEditMode || !$this->document) { + return; + } + $this->authorize('update', $this->document); + + try { + $this->document = $documentService->setDefaultLocale($this->document, $this->editingLocale); + session()->flash('message', __('messages.documents.update_success')); + return $this->redirect(route('documents.show', $this->document)); + } catch (\InvalidArgumentException $e) { + session()->flash('error', $e->getMessage()); + } + } + public function delete(DocumentService $documentService) { if (!$this->isEditMode || !$this->document) { return; } - $this->authorize('delete', $this->document); try { $documentService->deleteDocument($this->document); - session()->flash('message', 'Document deleted successfully!'); - - // Try to redirect to home document, or root if not found + session()->flash('message', __('messages.documents.delete_success')); $homeDocument = Document::where('slug', 'home')->first(); if ($homeDocument) { return redirect()->route('documents.show', $homeDocument); @@ -96,7 +153,9 @@ public function render() { return view('livewire.document-editor') ->layout('layouts.knowledge-base', [ - 'title' => $this->isEditMode ? 'Edit: ' . $this->title : 'New Document' + 'title' => $this->isEditMode + ? __('messages.documents.edit_document') . ': ' . $this->title + : __('messages.documents.new_document'), ]); } } diff --git a/src/resources/views/livewire/document-editor.blade.php b/src/resources/views/livewire/document-editor.blade.php index 5b1bbe9..f687fa1 100644 --- a/src/resources/views/livewire/document-editor.blade.php +++ b/src/resources/views/livewire/document-editor.blade.php @@ -56,6 +56,65 @@ class="inline-flex items-center justify-center px-3 sm:px-4 py-2 bg-indigo-600 b + @if($isEditMode && $document) +