1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Lang; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use App\Models\Skill; |
10
|
|
|
use App\Models\Applicant; |
11
|
|
|
use App\Models\Reference; |
12
|
|
|
use App\Models\Project; |
13
|
|
|
use App\Models\Lookup\Relationship; |
14
|
|
|
use App\Services\Validation\Requests\UpdateReferenceValidator; |
15
|
|
|
|
16
|
|
|
class ReferencesController extends Controller |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Show the form for editing the logged-in applicant's references |
21
|
|
|
* |
22
|
|
|
* @param Request $request |
|
|
|
|
23
|
|
|
* @return \Illuminate\Http\RedirectResponse |
24
|
|
|
*/ |
25
|
1 |
|
public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse |
26
|
|
|
{ |
27
|
1 |
|
$applicant = $request->user()->applicant; |
28
|
1 |
|
return redirect(route('profile.references.edit', $applicant)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show the form for editing the applicant's references |
33
|
|
|
* |
34
|
|
|
* @param Request $request Incoming request object. |
35
|
|
|
* @param Applicant $applicant Incoming applicant object. |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
38
|
|
|
*/ |
39
|
1 |
|
public function edit(Request $request, Applicant $applicant) |
40
|
|
|
{ |
41
|
1 |
|
$applicant->load([ |
42
|
1 |
|
'references.projects', |
43
|
|
|
'skill_declarations.skill', |
44
|
|
|
]); |
45
|
|
|
|
46
|
1 |
|
return view('applicant/profile_04_references', [ |
47
|
1 |
|
'applicant' => $applicant, |
48
|
1 |
|
'profile' => Lang::get('applicant/profile_references'), |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Update or create a reference with the supplied data. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Http\Request $request The incoming request object. |
56
|
|
|
* @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created. |
57
|
|
|
* |
58
|
|
|
* @return |
|
|
|
|
59
|
|
|
*/ |
60
|
2 |
|
public function update(Request $request, ?Reference $reference = null) |
|
|
|
|
61
|
|
|
{ |
62
|
2 |
|
$validator = new UpdateReferenceValidator(); |
63
|
2 |
|
$validator->validate($request->input()); |
64
|
|
|
|
65
|
2 |
|
if ($reference === null) { |
66
|
1 |
|
$reference = new Reference(); |
67
|
1 |
|
$reference->applicant_id = $request->user()->applicant->id; |
68
|
|
|
} |
69
|
2 |
|
$reference->fill([ |
70
|
2 |
|
'name' => $request->input('name'), |
71
|
2 |
|
'email' => $request->input('email'), |
72
|
2 |
|
'relationship_id' => $request->input('relationship_id'), |
73
|
2 |
|
'description' => $request->input('description'), |
74
|
|
|
]); |
75
|
2 |
|
$reference->save(); |
76
|
|
|
|
77
|
2 |
|
$reference->load('projects'); |
78
|
|
|
|
79
|
|
|
//TODO: As soon as you can interact with projects outside of references, |
80
|
|
|
// this will become a dangerous operation |
81
|
2 |
|
$reference->projects()->delete(); |
82
|
|
|
|
83
|
2 |
|
$newProjects = []; |
84
|
2 |
|
if ($request->input('projects')) { |
85
|
2 |
|
foreach ($request->input('projects') as $projectInput) { |
86
|
2 |
|
$project = new Project(); |
87
|
2 |
|
$project->applicant_id = $reference->applicant_id; |
88
|
2 |
|
$project->fill([ |
89
|
2 |
|
'name' => $projectInput['name'], |
90
|
2 |
|
'start_date' => $projectInput['start_date'], |
91
|
2 |
|
'end_date' => $projectInput['end_date'], |
92
|
|
|
]); |
93
|
2 |
|
$project->save(); |
94
|
2 |
|
$newProjects[] = $project->id; |
95
|
|
|
// $reference->projects()->attach($project); |
96
|
|
|
} |
97
|
|
|
} |
98
|
2 |
|
$reference->projects()->sync($newProjects); |
99
|
|
|
|
100
|
|
|
//Attach relatives |
101
|
2 |
|
$skillIds = $this->getRelativeIds($request->input(), 'skills'); |
102
|
2 |
|
$reference->skill_declarations()->sync($skillIds); |
103
|
|
|
|
104
|
|
|
// if an ajax request, return the new object |
105
|
2 |
|
if ($request->ajax()) { |
106
|
|
|
$reference->load('relationship'); |
107
|
|
|
$reference->load('projects'); |
108
|
|
|
return $reference->toJson(); |
109
|
|
|
} else { |
110
|
2 |
|
return redirect()->back(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Delete the particular reference from storage. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
118
|
|
|
* @param \App\Models\Reference $reference |
|
|
|
|
119
|
|
|
* @return \Illuminate\Http\Response |
120
|
|
|
*/ |
121
|
1 |
|
public function destroy(Request $request, Reference $reference) |
|
|
|
|
122
|
|
|
{ |
123
|
1 |
|
$this->authorize('delete', $reference); |
124
|
|
|
|
125
|
|
|
//TODO: when projects exist independently on profile, delete seperatley |
126
|
1 |
|
$reference->projects()->delete(); |
127
|
|
|
|
128
|
1 |
|
$reference->delete(); |
129
|
|
|
|
130
|
1 |
|
if ($request->ajax()) { |
131
|
|
|
return [ |
|
|
|
|
132
|
|
|
"message" => 'Reference deleted' |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
return redirect()->back(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|