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