viewdocumentpage
unknown
plain_text
10 months ago
4.4 kB
13
Indexable
<?php
namespace App\Livewire\Pages\Document;
use Livewire\Component;
use App\Models\ClientFile;
use App\Facades\OpenAIClient;
use Livewire\Attributes\Layout;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use RalphJSmit\Livewire\Urls\Facades\Url as LivewireUrl;
#[Layout('layouts.app-viewer')]
class ViewDocumentPage extends Component
{
public $activeSubprojectId;
public $activeFileId;
protected $listeners = ['translateText'];
public function translateText($text)
{
try {
$result = OpenAIClient::chat()->create([
'model' => env('OPENAI_DEFAULT_MODEL', 'gpt-4'),
'messages' => [
['role' => 'system', 'content' => 'You are a helpful translator. Translate the given text to Indonesian.'],
['role' => 'user', 'content' => $text],
],
'temperature' => 0.3,
]);
$translated = $result->choices[0]->message->content;
Log::info("Translation result: " . $translated);
// Dispatch event ke JS
$this->dispatch('translatedText', translated: $translated);
} catch (\Exception $e) {
$this->dispatch('translatedText', translated: 'Translation failed: ' . $e->getMessage());
}
}
public function mount()
{
$currentRoute = explode('/', LivewireUrl::current());
$activeSubprojectId = $currentRoute[3];
$activeFileId = $currentRoute[6];
$this->activeSubprojectId = $activeSubprojectId;
$this->activeFileId = $activeFileId;
}
public function nextFile()
{
$file = ClientFile::where('basename', $this->activeFileId)->first();
$file = ClientFile::where('parent', $file->parent)
->where('index', '>', $file->index)
->orderBy('index', 'asc')
->first();
$this->redirectRoute('client.document.view', ['subprojectId' => $file->subproject_id, 'id' => $file->basename], navigate: true);
}
public function previousFile()
{
$file = ClientFile::where('basename', $this->activeFileId)->first();
$file = ClientFile::where('parent', $file->parent)
->where('index', '<', $file->index)
->orderBy('index', 'desc')
->first();
$this->redirectRoute('client.document.view', ['subprojectId' => $file->subproject_id, 'id' => $file->basename], navigate: true);
}
public function render()
{
$file = ClientFile::where('basename', $this->activeFileId)->first();
$fileCount = ClientFile::where('parent', $file->parent)
->where('status', true)
->orderBy('index', 'asc')
->count();
$filePosition = ClientFile::where('parent', $file->parent)
->where('status', true)
->where('index', '<', $file->index)
->orderBy('index', 'asc')
->count();
$file_path = Storage::path($file->subproject_id . "/" . $file->basename);
if (!Storage::exists($file_path)) {
return view('livewire.pages.document.view-document-page', [
'file' => $file,
'fileCount' => $fileCount,
'filePosition' => $filePosition,
'viewerType' => 'notexist'
]);
}
$ext = strtolower(pathinfo($file->name, PATHINFO_EXTENSION));
$viewerType = '';
if ($ext === 'pdf') {
$viewerType = 'pdf';
} elseif (in_array($ext, ['jpg', 'jpeg', 'png', 'svg', 'gif', 'bmp', 'tiff', 'webp'])) {
$viewerType = 'image';
} elseif (in_array($ext, ['doc', 'docx'])) {
$viewerType = 'document';
} elseif (in_array($ext, ['ppt', 'pptx', 'pot', 'potx', 'pps', 'ppsx'])) {
$viewerType = 'powerpoint';
} elseif (in_array($ext, ['xls', 'xlsx', 'xlsm', 'csv', 'ods'])) {
$viewerType = 'excel';
}
return view('livewire.pages.document.view-document-page', [
'file' => $file,
'fileCount' => $fileCount,
'filePosition' => $filePosition,
'viewerType' => $viewerType
]);
}
}Editor is loading...
Leave a Comment