-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocumentUpload.php
143 lines (119 loc) · 3.52 KB
/
DocumentUpload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace App\Livewire;
use App\Models\Document;
use App\Models\DocumentCategory;
use App\Models\DocumentState;
use App\Models\User;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;
use Livewire\WithFileUploads;
class DocumentUpload extends Component
{
use HasCommentSection, WithFileUploads;
/**
* @var string
*/
public $displayName;
/**
* @var string
*/
public $category;
/**
* @var UploadedFile
*/
public $file;
public bool $disabled = false;
public string $message = '';
public User $user;
/**
* @var string[]
*/
private $defaultRules = [
'file' => 'required|mimes:pdf|file|max:10240',
];
/**
* @var string[]
*/
private $pictureRules = [
'file' => 'required|image|file|max:10240',
];
/**
* @return array<string>
*/
public function messages(): array
{
return [
'file.required' => strval(__('registration.upload-no-file-selected')),
];
}
public function mount(): void
{
$user = Auth::user();
if ($user == null) {
abort(401);
}
$this->user = $user;
$this->message = $this->getStringForDocumentState();
$this->document = $user->documentBy(DocumentCategory::read($this->category));
$this->comments = $this->document->comments;
}
private function getStringForDocumentState(): string
{
$document = $this->user->documentBy(DocumentCategory::read($this->category));
if ($document == null || $document->path == null) {
return strval(__('document.state_not_uploaded'));
}
$state = $document->state;
$string = '';
switch ($state) {
case DocumentState::Submitted:
$string = __('document.state_submitted');
break;
case DocumentState::Approved:
$string = __('document.state_approved');
break;
case DocumentState::Declined:
$string = __('document.state_declined');
break;
}
return strval($string);
}
public function render(): Factory|View|Application
{
return view('livewire.document-upload', [
'displayName' => $this->displayName,
]);
}
public function save(): void
{
$this->validate();
$clientOriginalName = $this->file->getClientOriginalName();
$extension = $this->file->getClientOriginalExtension();
$path = 'documents/'.$this->user->uuid;
Storage::disk()->putFileAs($path, $this->file, $this->category.'.'.$extension);
$dbDoc = $this->user->documentBy(DocumentCategory::read($this->category));
$filePath = $path.'/'.$this->category.'.'.$extension;
$dbDoc->name = $clientOriginalName;
$dbDoc->state = DocumentState::Submitted;
$dbDoc->path = $filePath;
$dbDoc->type = Document::TYPE_DIGITAL;
$dbDoc->save();
$this->message = strval(__('registration.upload-success'));
}
/**
* @return string[]
*/
public function getRules(): array
{
if ($this->category == DocumentCategory::Picture->value) {
return $this->pictureRules;
} else {
return $this->defaultRules;
}
}
}