| Total Complexity | 22 |
| Total Lines | 209 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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', [ |
||
|
1 ignored issue
–
show
|
|||
| 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) ); |
||
|
1 ignored issue
–
show
|
|||
| 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); |
||
|
1 ignored issue
–
show
|
|||
| 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) |
||
| 173 | } |
||
| 174 | |||
| 175 | protected function updateSkillDeclaration(Request $request, SkillDeclaration $skillDeclaration) |
||
| 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) |
||
| 226 | } |
||
| 227 | |||
| 229 |