87 lines
2.5 KiB
PHP
87 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Feedback\Pages;
|
|
|
|
use App\Filament\Resources\Feedback\FeedbackResource;
|
|
use Filament\Resources\Pages\CreateRecord;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class CreateFeedback extends CreateRecord
|
|
{
|
|
protected static string $resource = FeedbackResource::class;
|
|
|
|
protected array $pendingAttachments = [];
|
|
protected string $pendingCollection = 'general';
|
|
|
|
protected function mutateFormDataBeforeCreate(array $data): array
|
|
{
|
|
if (($data['is_general'] ?? false) === true) {
|
|
$data['customer_product_id'] = null;
|
|
}
|
|
|
|
$this->pendingAttachments = $data['attachments'] ?? [];
|
|
$this->pendingCollection = $data['attachment_collection'] ?? 'general';
|
|
|
|
unset($data['is_general'], $data['attachment_collection'], $data['attachments']);
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function afterCreate(): void
|
|
{
|
|
$feedback = $this->getRecord();
|
|
|
|
$feedback->interactions()->create([
|
|
'user_id' => auth()->id(),
|
|
'type' => 'note',
|
|
'content' => __('feedback.created_via', ['channel' => $feedback->feedbackChannel?->name ?? __('feedback.unknown_channel')]),
|
|
'changes' => ['status' => ['old' => null, 'new' => $feedback->status]],
|
|
]);
|
|
|
|
if (empty($this->pendingAttachments)) {
|
|
return;
|
|
}
|
|
|
|
$disk = Storage::disk('public');
|
|
|
|
foreach ($this->pendingAttachments as $path) {
|
|
if (! $disk->exists($path)) {
|
|
continue;
|
|
}
|
|
|
|
if ($feedback->attachments()->where('path', $path)->exists()) {
|
|
continue;
|
|
}
|
|
|
|
$feedback->attachments()->create([
|
|
'uuid' => (string) \Illuminate\Support\Str::uuid(),
|
|
'user_id' => auth()->id(),
|
|
'name' => basename($path),
|
|
'path' => $path,
|
|
'disk' => 'public',
|
|
'collection' => $this->pendingCollection,
|
|
'mime_type' => $this->safeMimeType($disk, $path),
|
|
'size' => $this->safeFileSize($disk, $path),
|
|
]);
|
|
}
|
|
}
|
|
|
|
protected function safeMimeType($disk, string $path): ?string
|
|
{
|
|
try {
|
|
return $disk->mimeType($path);
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
protected function safeFileSize($disk, string $path): ?int
|
|
{
|
|
try {
|
|
return $disk->size($path);
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|