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