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\Degree; |
9
|
|
|
use App\Models\Applicant; |
10
|
|
|
use App\Models\Course; |
11
|
|
|
use App\Models\WorkExperience; |
12
|
|
|
use App\Models\Lookup\DegreeType; |
13
|
|
|
use App\Models\Lookup\CourseStatus; |
14
|
|
|
use App\Http\Controllers\Controller; |
15
|
|
|
|
16
|
|
|
class ExperienceController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Show the form for editing the logged-in applicant's experience |
20
|
|
|
* |
21
|
|
|
* @param Request $request |
|
|
|
|
22
|
|
|
* @return \Illuminate\Http\RedirectResponse |
23
|
|
|
*/ |
24
|
|
|
public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse |
25
|
|
|
{ |
26
|
|
|
$applicant = $request->user()->applicant; |
27
|
|
|
return redirect(route('profile.experience.edit', $applicant)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Show the form for editing the applicant's experience |
32
|
|
|
* |
33
|
|
|
* @param \Illuminate\Http\Request $request Incoming request object. |
34
|
|
|
* @param \App\Models\Applicant $applicant Incoming applicant object. |
35
|
|
|
* |
36
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
37
|
|
|
*/ |
38
|
|
|
public function edit(Request $request, Applicant $applicant) |
39
|
|
|
{ |
40
|
|
|
return view('applicant/profile_02_experience', [ |
41
|
|
|
'applicant' => $applicant, |
42
|
|
|
'profile' => Lang::get('applicant/profile_experience'), |
43
|
|
|
'degree_types' => DegreeType::all(), |
44
|
|
|
'course_status' => CourseStatus::all(), |
45
|
|
|
'degree_template' => Lang::get('common/degree'), |
46
|
|
|
'course_template' => Lang::get('common/course'), |
47
|
|
|
'work_template' => Lang::get('common/work_experience'), |
48
|
|
|
'form_submit_action' => route('profile.experience.update', $applicant) |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Update the applicant's profile in storage. |
54
|
|
|
* |
55
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
56
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
57
|
|
|
* @return \Illuminate\Http\Response |
58
|
|
|
*/ |
59
|
|
|
public function update(Request $request, Applicant $applicant) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$input = $request->input(); |
62
|
|
|
|
63
|
|
|
$degrees = $input['degrees']; |
64
|
|
|
|
65
|
|
|
//Delete old degrees that weren't resubmitted |
66
|
|
|
//Note: this must be done before adding new degrees, so we don't delete |
67
|
|
|
// them right after adding them |
68
|
|
|
foreach ($applicant->degrees as $oldDegree) { |
69
|
|
|
//Check if no degrees were resubmitted, or if this specific one wasn't |
70
|
|
|
if (!isset($degrees['old']) || |
71
|
|
|
!isset($degrees['old'][$oldDegree->id])) { |
72
|
|
|
$oldDegree->delete(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
//Save new degrees |
77
|
|
|
if (isset($degrees['new'])) { |
78
|
|
|
foreach ($degrees['new'] as $degreeInput) { |
79
|
|
|
$degree = new Degree(); |
80
|
|
|
$degree->applicant_id = $applicant->id; |
81
|
|
|
$degree->fill([ |
82
|
|
|
'degree_type_id' => $degreeInput['degree_type_id'], |
83
|
|
|
'area_of_study' => $degreeInput['area_of_study'], |
84
|
|
|
'institution' => $degreeInput['institution'], |
85
|
|
|
'thesis' => $degreeInput['thesis'], |
86
|
|
|
'start_date' => $degreeInput['start_date'], |
87
|
|
|
'end_date' => $degreeInput['end_date'] |
88
|
|
|
]); |
89
|
|
|
$degree->save(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//Update old degrees |
94
|
|
|
if (isset($degrees['old'])) { |
95
|
|
|
foreach ($degrees['old'] as $id => $degreeInput) { |
96
|
|
|
//Ensure this degree belongs to this applicant |
97
|
|
|
$degree = $applicant->degrees->firstWhere('id', $id); |
98
|
|
|
if ($degree != null) { |
99
|
|
|
$degree->fill([ |
100
|
|
|
'degree_type_id' => $degreeInput['degree_type_id'], |
101
|
|
|
'area_of_study' => $degreeInput['area_of_study'], |
102
|
|
|
'institution' => $degreeInput['institution'], |
103
|
|
|
'thesis' => $degreeInput['thesis'], |
104
|
|
|
'start_date' => $degreeInput['start_date'], |
105
|
|
|
'end_date' => $degreeInput['end_date'] |
106
|
|
|
]); |
107
|
|
|
$degree->save(); |
108
|
|
|
} else { |
109
|
|
|
Log::warning('Applicant '.$applicant->id.' attempted to update degree with invalid id '.$id); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$courses = $input['courses']; |
115
|
|
|
|
116
|
|
|
//Delete old courses that weren't resubmitted |
117
|
|
|
//Note: this must be done before adding new ones, so we don't delete |
118
|
|
|
// them right after adding them |
119
|
|
|
foreach ($applicant->courses as $oldCourse) { |
120
|
|
|
//Check if no courses were resubmitted, or if this specific one wasn't |
121
|
|
|
if (!isset($courses['old']) || |
122
|
|
|
!isset($courses['old'][$oldCourse->id])) { |
123
|
|
|
$oldCourse->delete(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//Save new courses |
128
|
|
|
if (isset($courses['new'])) { |
129
|
|
|
foreach ($courses['new'] as $courseInput) { |
130
|
|
|
$course = new Course(); |
131
|
|
|
$course->applicant_id = $applicant->id; |
132
|
|
|
$course->fill([ |
133
|
|
|
'name' => $courseInput['name'], |
134
|
|
|
'institution' => $courseInput['institution'], |
135
|
|
|
'course_status_id' => $courseInput['course_status_id'], |
136
|
|
|
'start_date' => $courseInput['start_date'], |
137
|
|
|
'end_date' => $courseInput['end_date'] |
138
|
|
|
]); |
139
|
|
|
$course->save(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
//Update old courses |
144
|
|
|
if (isset($courses['old'])) { |
145
|
|
|
foreach ($courses['old'] as $id => $courseInput) { |
146
|
|
|
//Ensure this course belongs to this applicant |
147
|
|
|
$course = $applicant->courses->firstWhere('id', $id); |
148
|
|
|
if ($course != null) { |
149
|
|
|
$course->fill([ |
150
|
|
|
'name' => $courseInput['name'], |
151
|
|
|
'institution' => $courseInput['institution'], |
152
|
|
|
'course_status_id' => $courseInput['course_status_id'], |
153
|
|
|
'start_date' => $courseInput['start_date'], |
154
|
|
|
'end_date' => $courseInput['end_date'] |
155
|
|
|
]); |
156
|
|
|
$course->save(); |
157
|
|
|
} else { |
158
|
|
|
Log::warning('Applicant '.$applicant->id.' attempted to update course with invalid id '.$id); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$work_experiences = $input['work_experiences'] ; |
164
|
|
|
|
165
|
|
|
//Delete old work_experiences that weren't resubmitted |
166
|
|
|
//Note: this must be done before adding new ones, so we don't delete |
167
|
|
|
// them right after adding them |
168
|
|
|
foreach ($applicant->work_experiences as $oldWorkExperience) { |
169
|
|
|
//Check if no work_experiences were resubmitted, or if this specific one wasn't |
170
|
|
|
if (!isset($work_experiences['old']) || |
171
|
|
|
!isset($work_experiences['old'][$oldWorkExperience->id])) { |
172
|
|
|
$oldWorkExperience->delete(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
//Save new work_experiences |
177
|
|
|
if (isset($work_experiences['new'])) { |
178
|
|
|
foreach ($work_experiences['new'] as $workExperienceInput) { |
179
|
|
|
$workExperience = new WorkExperience(); |
180
|
|
|
$workExperience->applicant_id = $applicant->id; |
181
|
|
|
$workExperience->fill([ |
182
|
|
|
'role' => $workExperienceInput['role'], |
183
|
|
|
'company' => $workExperienceInput['company'], |
184
|
|
|
'description' => $workExperienceInput['description'], |
185
|
|
|
'start_date' => $workExperienceInput['start_date'], |
186
|
|
|
'end_date' => $workExperienceInput['end_date'] |
187
|
|
|
]); |
188
|
|
|
$workExperience->save(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
//Update old work_experiences |
193
|
|
|
if (isset($work_experiences['old'])) { |
194
|
|
|
foreach ($work_experiences['old'] as $id => $workExperienceInput) { |
195
|
|
|
//Ensure this work_experience belongs to this applicant |
196
|
|
|
$workExperience = $applicant->work_experiences->firstWhere('id', $id); |
197
|
|
|
if ($workExperience != null) { |
198
|
|
|
$workExperience->fill([ |
199
|
|
|
'role' => $workExperienceInput['role'], |
200
|
|
|
'company' => $workExperienceInput['company'], |
201
|
|
|
'description' => $workExperienceInput['description'], |
202
|
|
|
'start_date' => $workExperienceInput['start_date'], |
203
|
|
|
'end_date' => $workExperienceInput['end_date'] |
204
|
|
|
]); |
205
|
|
|
$workExperience->save(); |
206
|
|
|
} else { |
207
|
|
|
Log::warning('Applicant '.$applicant->id.' attempted to update work_experience with invalid id '.$id); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return redirect(route('profile.experience.edit', $applicant)); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|