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\Models\Skill; |
9
|
|
|
use App\Models\Lookup\SkillLevel; |
10
|
|
|
use App\Models\Lookup\SkillStatus; |
11
|
|
|
use App\Models\SkillDeclaration; |
12
|
|
|
use App\Models\Applicant; |
13
|
|
|
use App\Http\Controllers\Controller; |
14
|
|
|
use App\Services\Validation\BulkSkillDeclarationValidator; |
15
|
|
|
use App\Services\Validation\Rules\UniqueApplicantSkillRule; |
16
|
|
|
use App\Services\Validation\Rules\ApplicantHasRelationRule; |
17
|
|
|
use Illuminate\Validation\Rule; |
18
|
|
|
|
19
|
|
|
class SkillsController extends Controller |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Display the Skills page associated with the applicant. |
24
|
|
|
* |
25
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
26
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
public function show(Applicant $applicant) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
// |
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Show the form for editing the applicant's skills |
36
|
|
|
* |
37
|
|
|
* @param Request $request |
|
|
|
|
38
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
39
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function edit(Request $request, Applicant $applicant) |
42
|
|
|
{ |
43
|
|
|
$skills = Skill::all(); |
44
|
|
|
|
45
|
|
|
return view('applicant/profile_03_skills', [ |
|
|
|
|
46
|
|
|
'applicant' => $applicant, |
47
|
|
|
'profile' => Lang::get('applicant/profile_skills'), |
48
|
|
|
'form_submit_action' => route('profile.skills.update', $applicant), |
|
|
|
|
49
|
|
|
'skills' => $skills |
50
|
|
|
]); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Update the applicant's profile in storage. |
55
|
|
|
* |
56
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
57
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
58
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
public function updateAll(Request $request, Applicant $applicant) |
61
|
|
|
{ |
62
|
|
|
$input = $request->input(); |
63
|
|
|
|
64
|
|
|
$validator = new BulkSkillDeclarationValidator($applicant, $input); |
65
|
|
|
$validator->validate($input); |
66
|
|
|
|
67
|
|
|
$skillDeclarations = $request->input('skill_declarations'); |
68
|
|
|
$claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
69
|
|
|
|
70
|
|
|
//Delete old skill declarations that weren't resubmitted |
71
|
|
|
//Note: this must be done before adding new ones, so we don't delete |
72
|
|
|
// them right after adding them |
73
|
|
|
foreach($applicant->skill_declarations as $oldDeclaration) { |
|
|
|
|
74
|
|
|
//Check if none were resubmitted, or if this specific one wasn't |
75
|
|
|
$type = $oldDeclaration->skill->skill_type->name; |
76
|
|
|
if (!isset($skillDeclarations['old']) || |
77
|
|
|
!isset($skillDeclarations['old'][$type]) || |
|
|
|
|
78
|
|
|
!isset($skillDeclarations['old'][$type][$oldDeclaration->id])) { |
|
|
|
|
79
|
|
|
$oldDeclaration->delete(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
//Save new skill declarartions |
84
|
|
|
if (isset($skillDeclarations['new'])) { |
85
|
|
|
foreach($skillDeclarations['new'] as $skillType => $typeInput) { |
|
|
|
|
86
|
|
|
foreach($typeInput as $skillDeclarationInput) { |
|
|
|
|
87
|
|
|
$skillDeclaration = new SkillDeclaration(); |
88
|
|
|
$skillDeclaration->applicant_id = $applicant->id; |
89
|
|
|
$skillDeclaration->skill_id = $skillDeclarationInput['skill_id']; |
90
|
|
|
$skillDeclaration->skill_status_id = $claimedStatusId; |
91
|
|
|
$skillDeclaration->fill([ |
|
|
|
|
92
|
|
|
'description' => $skillDeclarationInput['description'], |
93
|
|
|
'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
94
|
|
|
]); |
|
|
|
|
95
|
|
|
$skillDeclaration->save(); |
96
|
|
|
|
97
|
|
|
$referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
98
|
|
|
$skillDeclaration->references()->sync($referenceIds); |
99
|
|
|
|
100
|
|
|
$sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
101
|
|
|
$skillDeclaration->work_samples()->sync($sampleIds); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
//Update old declarations |
107
|
|
|
if (isset($skillDeclarations['old'])) { |
108
|
|
|
foreach($skillDeclarations['old'] as $skillType => $typeInput) { |
|
|
|
|
109
|
|
|
foreach($typeInput as $id=>$skillDeclarationInput) { |
|
|
|
|
110
|
|
|
//Ensure this declaration belongs to this applicant |
111
|
|
|
$skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id); |
112
|
|
|
if ($skillDeclaration != null) { |
113
|
|
|
//skill_id and skill_status cannot be changed |
114
|
|
|
$skillDeclaration->fill([ |
|
|
|
|
115
|
|
|
'description' => $skillDeclarationInput['description'], |
116
|
|
|
'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
117
|
|
|
]); |
|
|
|
|
118
|
|
|
$skillDeclaration->save(); |
119
|
|
|
|
120
|
|
|
$referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
121
|
|
|
$skillDeclaration->references()->sync($referenceIds); |
122
|
|
|
|
123
|
|
|
$sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
124
|
|
|
$skillDeclaration->work_samples()->sync($sampleIds); |
125
|
|
|
} else { |
126
|
|
|
Log::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
return redirect( route('profile.skills.edit', $applicant) ); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create the particular skill declaration in storage. |
138
|
|
|
* |
139
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
140
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
141
|
|
|
*/ |
142
|
|
|
public function create(Request $request) |
143
|
|
|
{ |
144
|
|
|
$this->authorize('create', SkillDeclaration::class); |
145
|
|
|
|
146
|
|
|
$user = $request->user(); |
147
|
|
|
$applicant = $user->applicant; |
148
|
|
|
|
149
|
|
|
$skill_level_ids = SkillLevel::all()->pluck('id'); |
150
|
|
|
$skill_ids = Skill::all()->pluck('id'); |
151
|
|
|
$uniqueSkillRule = new UniqueApplicantSkillRule($applicant); |
152
|
|
|
$applicantHasSkillRule = new ApplicantHasRelationRule($applicant, 'skill_declarations'); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$request->validate([ |
|
|
|
|
155
|
|
|
'skill_id' => [ |
156
|
|
|
'required', |
157
|
|
|
Rule::in($skill_ids->toArray()), |
158
|
|
|
$uniqueSkillRule, |
159
|
|
|
], |
160
|
|
|
'skill_level_id' => [ |
161
|
|
|
'required', |
162
|
|
|
Rule::in($skill_level_ids->toArray()), |
163
|
|
|
], |
164
|
|
|
'description' => 'string|required', |
165
|
|
|
'fake' => 'string', |
166
|
|
|
'fake_2' => 'numeric|min:10' |
167
|
|
|
]); |
|
|
|
|
168
|
|
|
|
169
|
|
|
//Get the default claim status id |
170
|
|
|
$claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
171
|
|
|
|
172
|
|
|
// Create a new Skill Declaration |
173
|
|
|
$skillDeclaration = new SkillDeclaration(); |
174
|
|
|
$skillDeclaration->applicant_id = $applicant->id; |
175
|
|
|
$skillDeclaration->skill_id = $request->input('skill_id'); |
176
|
|
|
$skillDeclaration->skill_status_id = $claimedStatusId; |
177
|
|
|
|
178
|
|
|
//Update variable fields in skill declaration |
179
|
|
|
return $this->updateSkillDeclaration($request, $skillDeclaration); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Update the particular skill declaration in storage. |
184
|
|
|
* |
185
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
186
|
|
|
* @param \App\Models\SkillDeclaration $skillDeclaration |
|
|
|
|
187
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
188
|
|
|
*/ |
189
|
|
|
public function update(Request $request, SkillDeclaration $skillDeclaration) |
190
|
|
|
{ |
191
|
|
|
$this->authorize('update', $skillDeclaration); |
192
|
|
|
|
193
|
|
|
$skill_level_ids = SkillLevel::all()->pluck('id'); |
194
|
|
|
|
195
|
|
|
$request->validate([ |
|
|
|
|
196
|
|
|
'skill_level_id' => [ |
197
|
|
|
'required', |
198
|
|
|
Rule::in($skill_level_ids->toArray()), |
199
|
|
|
], |
200
|
|
|
'description' => 'string|required', |
201
|
|
|
'fake' => 'string', |
202
|
|
|
'fake_2' => 'numeric|min:10' |
203
|
|
|
]); |
|
|
|
|
204
|
|
|
|
205
|
|
|
return $this->updateSkillDeclaration($request, $skillDeclaration); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
protected function updateSkillDeclaration(Request $request, SkillDeclaration $skillDeclaration) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
//Fill variable values |
211
|
|
|
$skillDeclaration->fill([ |
|
|
|
|
212
|
|
|
'description' => $request->input('description'), |
213
|
|
|
'skill_level_id' => $request->input('skill_level_id'), |
214
|
|
|
]); |
|
|
|
|
215
|
|
|
|
216
|
|
|
//Save this skill declaration |
217
|
|
|
$skillDeclaration->save(); |
218
|
|
|
|
219
|
|
|
//Attach relatives |
220
|
|
|
$referenceIds = $this->getRelativeIds($request->input(), 'references'); |
221
|
|
|
$skillDeclaration->references()->sync($referenceIds); |
222
|
|
|
|
223
|
|
|
$sampleIds = $this->getRelativeIds($request->input(), 'samples'); |
224
|
|
|
$skillDeclaration->work_samples()->sync($sampleIds); |
225
|
|
|
|
226
|
|
|
// If an ajax request, return the new object |
227
|
|
|
if($request->ajax()) { |
|
|
|
|
228
|
|
|
$skillDeclaration->load('references'); |
229
|
|
|
$skillDeclaration->load('work_samples'); |
230
|
|
|
$skillDeclaration->load('skill'); |
231
|
|
|
$skillDeclaration->load('skill_status'); |
232
|
|
|
return $skillDeclaration->toJson(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return redirect()->back(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Delete the particular skill declaration in storage. |
240
|
|
|
* |
241
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
242
|
|
|
* @param \App\Models\SkillDeclaration $skillDeclaration |
|
|
|
|
243
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
244
|
|
|
*/ |
245
|
|
|
public function destroy(Request $request, SkillDeclaration $skillDeclaration) |
246
|
|
|
{ |
247
|
|
|
$this->authorize('delete', $skillDeclaration); |
248
|
|
|
$skillDeclaration->delete(); |
249
|
|
|
|
250
|
|
|
if($request->ajax()) { |
|
|
|
|
251
|
|
|
return ['message' => 'Skill deleted']; |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
return redirect()->back(); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|