source: app/Http/Requests/Dashboard/FolderRequest.php@ 507ade0

develop
Last change on this file since 507ade0 was 94f05dc, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

  • Property mode set to 100644
File size: 2.3 KB
Line 
1<?php
2
3namespace App\Http\Requests\Dashboard;
4
5use App\Models\Department;
6use App\Models\FileType;
7use App\Models\Folder;
8use Illuminate\Foundation\Http\FormRequest;
9
10class 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
43 $deptCode = explode('/', $arch_id)[0];
44 $archNum = explode('/', $arch_id)[1];
45
46 if (empty($archNum)) {
47 $fail("Please enter folders Archive ID");
48 }
49
50 if ($deptCode != Department::find($this->request->get('department'))->code) {
51 $fail("Folder Archive ID field format is invalid");
52 }
53 }
54 catch (\Exception $e) {
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}
Note: See TracBrowser for help on using the repository browser.