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