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