[24a616f] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Requests\Dashboard;
|
---|
| 4 |
|
---|
[e6c1f87] | 5 | use App\Models\Department;
|
---|
[24a616f] | 6 | use App\Models\Document;
|
---|
[d795fa6] | 7 | use App\Models\FileType;
|
---|
[24a616f] | 8 | use Illuminate\Foundation\Http\FormRequest;
|
---|
| 9 |
|
---|
| 10 | class DocumentRequest extends FormRequest
|
---|
| 11 | {
|
---|
| 12 | /**
|
---|
| 13 | * Determine if the user is authorized to make this request.
|
---|
| 14 | *
|
---|
| 15 | * @return bool
|
---|
| 16 | */
|
---|
| 17 | public function authorize()
|
---|
| 18 | {
|
---|
[bd9e8e3] | 19 | if ($this->isMethod("patch")) {
|
---|
[24a616f] | 20 | $document = Document::find($this->route("id"));
|
---|
| 21 | return auth()->user()->hasPermission("edit_all_documents") || ($document->user->id == auth()->user()->id);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | return true;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Get the validation rules that apply to the request.
|
---|
| 29 | *
|
---|
| 30 | * @return array
|
---|
| 31 | */
|
---|
| 32 | public function rules()
|
---|
| 33 | {
|
---|
[d795fa6] | 34 |
|
---|
[24a616f] | 35 | $rules = [
|
---|
[bd9e8e3] | 36 | "arch_id" => [
|
---|
| 37 | "required",
|
---|
| 38 | function($attribute, $value, $fail) {
|
---|
| 39 | $arch_id = $this->request->get('arch_id');
|
---|
| 40 | $deptId = explode('/', $arch_id)[0];
|
---|
[e6c1f87] | 41 | if ($deptId !== Department::find($this->request->get('department'))->code) {
|
---|
| 42 | $fail("Document Archive ID field format is invalid");
|
---|
[bd9e8e3] | 43 | }
|
---|
| 44 | }
|
---|
| 45 | ],
|
---|
[24a616f] | 46 | "name" => "required|min:10|max:255",
|
---|
| 47 | "department" => "required|integer|exists:departments,id",
|
---|
| 48 | "description" => "required|min:30",
|
---|
| 49 | ];
|
---|
| 50 |
|
---|
[bd9e8e3] | 51 | if ($this->isMethod("patch")) {
|
---|
[24a616f] | 52 | $fileRules = [
|
---|
| 53 | "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
|
---|
[bd9e8e3] | 54 | ];
|
---|
[24a616f] | 55 | } else {
|
---|
| 56 | $fileRules = [
|
---|
| 57 | "file_item.*" => "mimes:jpg,jpeg,png|max:4096"
|
---|
| 58 | ];
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | $rules = array_merge(
|
---|
| 62 | $rules,
|
---|
| 63 | $fileRules,
|
---|
| 64 | );
|
---|
| 65 |
|
---|
| 66 | return $rules;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|