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
|
|
|
|
14
|
|
|
class ReferencesController extends Controller |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Display the Skills page associated with the applicant. |
19
|
|
|
* |
20
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
21
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public function show(Applicant $applicant) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
// |
26
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Show the form for editing the applicant's references |
31
|
|
|
* |
32
|
|
|
* @param Request $request |
|
|
|
|
33
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
34
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function edit(Request $request, Applicant $applicant) |
37
|
|
|
{ |
38
|
|
|
return view('applicant/profile_04_references', [ |
|
|
|
|
39
|
|
|
'applicant' => $applicant, |
40
|
|
|
'profile' => Lang::get('applicant/profile_references'), |
41
|
|
|
'form_submit_action' => route('profile.references.update', $applicant), |
42
|
|
|
]); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Update all the applicant's references in storage. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
49
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
50
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function updateAll(Request $request, Applicant $applicant) |
53
|
|
|
{ |
54
|
|
|
$input = $request->input(); |
55
|
|
|
|
56
|
|
|
$references = $input['references']; |
57
|
|
|
|
58
|
|
|
//Delete old references that weren't resubmitted |
59
|
|
|
//Note: this must be done before adding new references, so we don't delete |
60
|
|
|
// them right after adding them |
61
|
|
|
foreach($applicant->references as $oldReference) { |
|
|
|
|
62
|
|
|
//Check if no references were resubmitted, or if this specific one wasn't |
63
|
|
|
if (!isset($references['old']) || |
64
|
|
|
!isset($references['old'][$oldReference->id])) { |
|
|
|
|
65
|
|
|
$oldReference->delete(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//Save new references |
70
|
|
|
if (isset($references['new'])) { |
71
|
|
|
foreach($references['new'] as $referenceInput) { |
|
|
|
|
72
|
|
|
$reference = new Reference(); |
73
|
|
|
$reference->applicant_id = $applicant->id; |
74
|
|
|
$reference->fill([ |
|
|
|
|
75
|
|
|
'name' => $referenceInput['name'], |
76
|
|
|
'email' => $referenceInput['email'], |
77
|
|
|
'relationship_id' => $referenceInput['relationship_id'], |
78
|
|
|
'description' => $referenceInput['description'], |
79
|
|
|
]); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$reference->save(); |
82
|
|
|
|
83
|
|
|
$projectIds = []; |
84
|
|
|
$projects = $referenceInput['projects']; |
85
|
|
|
if (isset($projects['new'])) { |
86
|
|
|
foreach($projects['new'] as $projectInput) { |
|
|
|
|
87
|
|
|
$project = new Project(); |
88
|
|
|
$project->applicant_id = $applicant->id; |
89
|
|
|
$project->fill([ |
|
|
|
|
90
|
|
|
'name' => $projectInput['name'], |
91
|
|
|
'start_date' => $projectInput['start_date'], |
92
|
|
|
'end_date' => $projectInput['end_date'], |
93
|
|
|
]); |
|
|
|
|
94
|
|
|
$project->save(); |
95
|
|
|
$projectIds[] = $project->id; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
//Sync attaches the specified ids, and detaches all others |
99
|
|
|
$reference->projects()->sync($projectIds); |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
$skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); |
103
|
|
|
$reference->skill_declarations()->sync($skillDeclarationIds); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//Update old references |
108
|
|
|
if (isset($references['old'])) { |
109
|
|
|
foreach($references['old'] as $id=>$referenceInput) { |
|
|
|
|
110
|
|
|
//Ensure this reference belongs to this applicant |
111
|
|
|
$reference = $applicant->references->firstWhere('id', $id); |
112
|
|
|
if ($reference != null) { |
113
|
|
|
$reference->fill([ |
|
|
|
|
114
|
|
|
'name' => $referenceInput['name'], |
115
|
|
|
'email' => $referenceInput['email'], |
116
|
|
|
'relationship_id' => $referenceInput['relationship_id'], |
117
|
|
|
'description' => $referenceInput['description'], |
118
|
|
|
]); |
|
|
|
|
119
|
|
|
$reference->save(); |
120
|
|
|
|
121
|
|
|
$projectIds = []; |
122
|
|
|
$projects = $referenceInput['projects']; |
123
|
|
|
if (isset($projects['new'])) { |
124
|
|
|
foreach($projects['new'] as $projectInput) { |
|
|
|
|
125
|
|
|
$project = new Project(); |
126
|
|
|
$project->applicant_id = $applicant->id; |
127
|
|
|
$project->fill([ |
|
|
|
|
128
|
|
|
'name' => $projectInput['name'], |
129
|
|
|
'start_date' => $projectInput['start_date'], |
130
|
|
|
'end_date' => $projectInput['end_date'], |
131
|
|
|
]); |
|
|
|
|
132
|
|
|
$project->save(); |
133
|
|
|
$projectIds[] = $project->id; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
if (isset($projects['old'])) { |
137
|
|
|
foreach($projects['old'] as $projectId=>$projectInput) { |
|
|
|
|
138
|
|
|
//Ensure this project belongs to this applicant |
139
|
|
|
$project = $applicant->projects->firstWhere('id', $projectId); |
140
|
|
|
if ($project != null) { |
141
|
|
|
$project->fill([ |
|
|
|
|
142
|
|
|
'name' => $projectInput['name'], |
143
|
|
|
'start_date' => $projectInput['start_date'], |
144
|
|
|
'end_date' => $projectInput['end_date'], |
145
|
|
|
]); |
|
|
|
|
146
|
|
|
$project->save(); |
147
|
|
|
$projectIds[] = $project->id; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
//TODO: when projects exists independpently on profile, don't delete them Here |
152
|
|
|
// Delete projects that will be detached from this reference |
153
|
|
|
foreach($reference->projects as $project) { |
|
|
|
|
154
|
|
|
if (!in_array($project->id, $projectIds)) { |
155
|
|
|
$project->delete(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
//Sync attaches the specified ids, and detaches all others |
160
|
|
|
$reference->projects()->sync($projectIds); |
161
|
|
|
|
162
|
|
|
$skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); |
163
|
|
|
$reference->skill_declarations()->sync($skillDeclarationIds); |
164
|
|
|
} else { |
165
|
|
|
Log::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return redirect( route('profile.references.edit', $applicant) ); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Update or create a reference with the supplied data. |
175
|
|
|
* |
176
|
|
|
* @param \Illuminate\Http\Request $request The incoming request object. |
177
|
|
|
* @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created. |
178
|
|
|
* |
179
|
|
|
* @return |
|
|
|
|
180
|
|
|
*/ |
181
|
|
|
public function update(Request $request, ?Reference $reference = null) |
182
|
|
|
{ |
183
|
|
|
if ($reference === null) { |
184
|
|
|
$reference = new Reference(); |
185
|
|
|
$reference->applicant_id = $request->user()->applicant->id; |
186
|
|
|
} |
187
|
|
|
$reference->fill([ |
|
|
|
|
188
|
|
|
'name' => $request->input('name'), |
189
|
|
|
'email' => $request->input('email'), |
190
|
|
|
'relationship_id' => $request->input('relationship_id'), |
191
|
|
|
'description' => $request->input('description'), |
192
|
|
|
]); |
|
|
|
|
193
|
|
|
$reference->save(); |
194
|
|
|
|
195
|
|
|
$reference->load('projects'); |
196
|
|
|
|
197
|
|
|
//TODO: As soon as you can interact with projects outside of references, |
198
|
|
|
// this will become a dangerous operation |
199
|
|
|
foreach ($reference->projects as $project) { |
200
|
|
|
$project->delete(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$newProjects = []; |
204
|
|
|
if ($request->input('projects')) { |
205
|
|
|
foreach ($request->input('projects') as $projectInput) { |
206
|
|
|
$project = new Project(); |
207
|
|
|
$project->applicant_id = $reference->applicant_id; |
208
|
|
|
$project->fill([ |
|
|
|
|
209
|
|
|
'name' => $projectInput['name'], |
210
|
|
|
'start_date' => $projectInput['start_date'], |
211
|
|
|
'end_date' => $projectInput['end_date'], |
212
|
|
|
]); |
|
|
|
|
213
|
|
|
$project->save(); |
214
|
|
|
$newProjects[] = $project->id; |
215
|
|
|
// $reference->projects()->attach($project); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
$reference->projects()->sync($newProjects); |
219
|
|
|
$reference->save(); |
220
|
|
|
|
221
|
|
|
// if an ajax request, return the new object |
222
|
|
|
if ($request->ajax()) { |
223
|
|
|
return $reference->toJson(); |
224
|
|
|
} else { |
225
|
|
|
return redirect()->back(); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Delete the particular reference from storage. |
231
|
|
|
* |
232
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
233
|
|
|
* @param \App\Models\Reference $reference |
|
|
|
|
234
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
235
|
|
|
*/ |
236
|
|
|
public function destroy(Request $request, Reference $reference) |
237
|
|
|
{ |
238
|
|
|
$this->authorize('delete', $reference); |
239
|
|
|
|
240
|
|
|
//TODO: when projects exist independently on profile, delete seperatley |
241
|
|
|
foreach($reference->projects as $project) { |
|
|
|
|
242
|
|
|
$project->delete(); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$reference->delete(); |
246
|
|
|
|
247
|
|
|
if($request->ajax()) { |
|
|
|
|
248
|
|
|
return [ |
|
|
|
|
249
|
|
|
"message" => 'Reference deleted' |
250
|
|
|
]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return redirect()->back(); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|