[c6b84df] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Requests\Dashboard;
|
---|
| 4 |
|
---|
| 5 | use App\Models\Department;
|
---|
| 6 | use App\Models\FileType;
|
---|
| 7 | use App\Models\Folder;
|
---|
| 8 | use Illuminate\Foundation\Http\FormRequest;
|
---|
| 9 |
|
---|
| 10 | class FolderRequest 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 | {
|
---|
| 19 | if ($this->isMethod("patch")) {
|
---|
| 20 | $folder = Folder::find($this->route("id"));
|
---|
| 21 | return auth()->user()->hasPermission("edit_all_folders") || ($folder->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 | {
|
---|
| 34 | $rules = [
|
---|
| 35 | "arch_id" => [
|
---|
| 36 | "required",
|
---|
| 37 | "unique:folders,arch_id,$this->id,id",
|
---|
| 38 | function ($attribute, $value, $fail) {
|
---|
| 39 |
|
---|
| 40 | try {
|
---|
| 41 | $arch_id = $this->request->get('arch_id');
|
---|
| 42 |
|
---|
[94f05dc] | 43 | $deptCode = explode('/', $arch_id)[0];
|
---|
[c6b84df] | 44 | $archNum = explode('/', $arch_id)[1];
|
---|
| 45 |
|
---|
| 46 | if (empty($archNum)) {
|
---|
| 47 | $fail("Please enter folders Archive ID");
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[94f05dc] | 50 | if ($deptCode != Department::find($this->request->get('department'))->code) {
|
---|
[c6b84df] | 51 | $fail("Folder Archive ID field format is invalid");
|
---|
| 52 | }
|
---|
[94f05dc] | 53 | }
|
---|
| 54 | catch (\Exception $e) {
|
---|
[c6b84df] | 55 | $fail("Please enter folders Archive ID");
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | ],
|
---|
| 59 | "name" => "required|min:2|max:30",
|
---|
| 60 | "department" => "required|integer|exists:departments,id",
|
---|
| 61 | ];
|
---|
| 62 |
|
---|
| 63 | $mimes = FileType::find("1")->mimes;
|
---|
| 64 | $maxSize = FileType::find("1")->max_size;
|
---|
| 65 |
|
---|
| 66 | if ($this->isMethod("patch")) {
|
---|
| 67 | $fileRules = [
|
---|
| 68 | "file_item.*" => "mimes:{$mimes}|max:{$maxSize}"
|
---|
| 69 | ];
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | else {
|
---|
| 73 | $fileRules = [
|
---|
| 74 | "file_item.*" => "mimes:{$mimes}|max:{$maxSize}"
|
---|
| 75 | ];
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | $rules = array_merge(
|
---|
| 79 | $rules,
|
---|
| 80 | $fileRules,
|
---|
| 81 | );
|
---|
| 82 |
|
---|
| 83 | return $rules;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|