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 Barryvdh\Debugbar\Facade as Debugbar; |
9
|
|
|
use App\Models\Skill; |
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
|
|
|
|
16
|
|
|
class SkillsController 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 skills |
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
|
|
|
$skills = Skill::all(); |
41
|
|
|
|
42
|
|
|
return view('applicant/profile_03_skills', [ |
|
|
|
|
43
|
|
|
'applicant' => $applicant, |
44
|
|
|
'profile' => Lang::get('applicant/profile_skills'), |
45
|
|
|
'form_submit_action' => route('profile.skills.update', $applicant), |
|
|
|
|
46
|
|
|
'skills' => $skills |
47
|
|
|
]); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Update the applicant's profile in storage. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
54
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
55
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
public function update(Request $request, Applicant $applicant) |
58
|
|
|
{ |
59
|
|
|
$input = $request->input(); |
60
|
|
|
|
61
|
|
|
$validator = new BulkSkillDeclarationValidator($applicant, $input); |
62
|
|
|
$validator->validate($input); |
63
|
|
|
|
64
|
|
|
$skillDeclarations = $request->input('skill_declarations'); |
65
|
|
|
$claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
66
|
|
|
|
67
|
|
|
//Delete old skill declarations that weren't resubmitted |
68
|
|
|
//Note: this must be done before adding new ones, so we don't delete |
69
|
|
|
// them right after adding them |
70
|
|
|
foreach($applicant->skill_declarations as $oldDeclaration) { |
|
|
|
|
71
|
|
|
//Check if none were resubmitted, or if this specific one wasn't |
72
|
|
|
$type = $oldDeclaration->skill->skill_type->name; |
73
|
|
|
if (!isset($skillDeclarations['old']) || |
74
|
|
|
!isset($skillDeclarations['old'][$type]) || |
|
|
|
|
75
|
|
|
!isset($skillDeclarations['old'][$type][$oldDeclaration->id])) { |
|
|
|
|
76
|
|
|
$oldDeclaration->delete(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
//Save new skill declarartions |
81
|
|
|
if (isset($skillDeclarations['new'])) { |
82
|
|
|
foreach($skillDeclarations['new'] as $skillType => $typeInput) { |
|
|
|
|
83
|
|
|
foreach($typeInput as $skillDeclarationInput) { |
|
|
|
|
84
|
|
|
$skillDeclaration = new SkillDeclaration(); |
85
|
|
|
$skillDeclaration->applicant_id = $applicant->id; |
86
|
|
|
$skillDeclaration->skill_id = $skillDeclarationInput['skill_id']; |
87
|
|
|
$skillDeclaration->skill_status_id = $claimedStatusId; |
88
|
|
|
$skillDeclaration->fill([ |
|
|
|
|
89
|
|
|
'description' => $skillDeclarationInput['description'], |
90
|
|
|
'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
91
|
|
|
]); |
|
|
|
|
92
|
|
|
$skillDeclaration->save(); |
93
|
|
|
|
94
|
|
|
$referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
95
|
|
|
$skillDeclaration->references()->sync($referenceIds); |
96
|
|
|
|
97
|
|
|
$sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
98
|
|
|
$skillDeclaration->work_samples()->sync($sampleIds); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
//Update old declarations |
104
|
|
|
if (isset($skillDeclarations['old'])) { |
105
|
|
|
foreach($skillDeclarations['old'] as $skillType => $typeInput) { |
|
|
|
|
106
|
|
|
foreach($typeInput as $id=>$skillDeclarationInput) { |
|
|
|
|
107
|
|
|
//Ensure this declaration belongs to this applicant |
108
|
|
|
$skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id); |
109
|
|
|
if ($skillDeclaration != null) { |
110
|
|
|
//skill_id and skill_status cannot be changed |
111
|
|
|
$skillDeclaration->fill([ |
|
|
|
|
112
|
|
|
'description' => $skillDeclarationInput['description'], |
113
|
|
|
'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
114
|
|
|
]); |
|
|
|
|
115
|
|
|
$skillDeclaration->save(); |
116
|
|
|
|
117
|
|
|
$referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
118
|
|
|
$skillDeclaration->references()->sync($referenceIds); |
119
|
|
|
|
120
|
|
|
$sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
121
|
|
|
$skillDeclaration->work_samples()->sync($sampleIds); |
122
|
|
|
} else { |
123
|
|
|
Log::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
return redirect( route('profile.skills.edit', $applicant) ); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Delete the particular skill declaration in storage. |
135
|
|
|
* |
136
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
137
|
|
|
* @param \App\Models\SkillDeclaration $skillDeclaration |
|
|
|
|
138
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
139
|
|
|
*/ |
140
|
|
|
public function destroy(Request $request, SkillDeclaration $skillDeclaration) |
141
|
|
|
{ |
142
|
|
|
$this->authorize('delete', $skillDeclaration); |
143
|
|
|
$skillDeclaration->delete(); |
144
|
|
|
|
145
|
|
|
if($request->ajax()) { |
|
|
|
|
146
|
|
|
return ['message' => 'Skill deleted']; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return redirect()->back(); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|