1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Lang; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Barryvdh\Debugbar\Facade as Debugbar; |
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
|
|
|
/** |
20
|
|
|
* Display the Experience 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
|
|
|
* Show the form for editing the applicant's experience |
32
|
|
|
* |
33
|
|
|
* @param Request $request |
|
|
|
|
34
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
35
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
public function edit(Request $request, Applicant $applicant) |
38
|
|
|
{ |
39
|
|
|
return view('applicant/profile_02_experience', [ |
|
|
|
|
40
|
|
|
'applicant' => $applicant, |
41
|
|
|
'profile' => Lang::get('applicant/profile_experience'), |
42
|
|
|
'degree_types' => DegreeType::all(), |
43
|
|
|
'course_status' => CourseStatus::all(), |
44
|
|
|
'degree_template' => Lang::get('common/degree'), |
45
|
|
|
'course_template' => Lang::get('common/course'), |
46
|
|
|
'work_template' => Lang::get('common/work_experience'), |
47
|
|
|
'form_submit_action' => route('profile.experience.update', $applicant) |
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 update(Request $request, Applicant $applicant) |
59
|
|
|
{ |
60
|
|
|
$input = $request->input(); |
61
|
|
|
|
62
|
|
|
$newDegrees = []; |
63
|
|
|
$newCourses = []; |
64
|
|
|
$newWorkExperiences = []; |
65
|
|
|
|
66
|
|
|
$oldDegrees = []; |
67
|
|
|
$oldCourses = []; |
68
|
|
|
$oldWorkExperiences = []; |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* If an obj exists at $array[$key], return it. |
72
|
|
|
* Otherwise, set $array[$key] to be a new instance of $class, |
73
|
|
|
* set applicant_id, and return it. |
74
|
|
|
* |
75
|
|
|
* @param [type] $array [description] |
|
|
|
|
76
|
|
|
* @param [type] $key [description] |
77
|
|
|
* @param [type] $class [description] |
78
|
|
|
* @return [type] [description] |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
function getModelFromArrayOrSetNew(&$array, $key, $class, $applicant) { |
|
|
|
|
81
|
|
|
if (isset($array[$key]) && $array[$key] != null ) { |
82
|
|
|
$obj = $array[$key]; |
83
|
|
|
} else { |
84
|
|
|
$obj = new $class; |
85
|
|
|
$obj->applicant_id = $applicant->id; |
86
|
|
|
$array[$key] = $obj; |
87
|
|
|
} |
88
|
|
|
return $obj; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
|
|
|
|
92
|
|
|
* For each item in an array, create a new model instance and set the |
93
|
|
|
* specified attribute with the value in the array. |
94
|
|
|
* |
95
|
|
|
* If an instance alread exists in the $instances array that matches |
96
|
|
|
* the $inputArray key, then set the attribute on that instance instead |
97
|
|
|
* of creating a new instance. |
98
|
|
|
* |
99
|
|
|
* @param array $inputArray An array of input key-values |
|
|
|
|
100
|
|
|
* @param array $instances Array of previously created Model instances. |
|
|
|
|
101
|
|
|
* @param string $model Fully qualified class name. |
|
|
|
|
102
|
|
|
* @param string $attribute Name of attribute to set in model. |
|
|
|
|
103
|
|
|
* @param \App\Models\Applicant Owner of the object |
|
|
|
|
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
function setAttrFromInputNew($inputArray, &$instances, $model, $attribute, $applicant) { |
|
|
|
|
106
|
|
|
if (isset($inputArray) && is_array($inputArray)) { |
107
|
|
|
foreach($inputArray as $key => $value) { |
|
|
|
|
108
|
|
|
$obj = getModelFromArrayOrSetNew($instances, $key, $model, $applicant); |
109
|
|
|
$obj->setAttribute($attribute, $value); |
110
|
|
|
Debugbar::info($instances); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
|
|
|
|
116
|
|
|
* For each item in an input array, retrieve the modelName instance with |
117
|
|
|
* id matching input key, and belonging to $applicant, and set $attribute |
118
|
|
|
* on that model instance with the inputArray value. |
119
|
|
|
* |
120
|
|
|
* Retrieved instances are cached in $instances array, so they don't |
121
|
|
|
* have to be retrieved from database multiple times. |
122
|
|
|
* |
123
|
|
|
* @param [type] $inputArray [description] |
|
|
|
|
124
|
|
|
* @param [type] $instances [description] |
|
|
|
|
125
|
|
|
* @param [type] $modelName [description] |
|
|
|
|
126
|
|
|
* @param [type] $attribute [description] |
|
|
|
|
127
|
|
|
*/ |
|
|
|
|
128
|
|
|
function setAttrFromInputOld($input, $inputField, &$instances, $modelName, $attribute, $applicant) { |
|
|
|
|
129
|
|
|
if (isset($input[$inputField]) && |
130
|
|
|
is_array($input[$inputField]) && |
|
|
|
|
131
|
|
|
isset($input[$inputField]['old']) && |
|
|
|
|
132
|
|
|
is_array($input[$inputField]['old']) ) { |
|
|
|
|
133
|
|
|
|
134
|
|
|
$inputArray = $input[$inputField]['old']; |
135
|
|
|
Debugbar::info($inputArray); |
136
|
|
|
foreach($inputArray as $key => $value) { |
|
|
|
|
137
|
|
|
if (isset($instances[$key])) { |
138
|
|
|
$obj = $instances[$key]; |
139
|
|
|
} else { |
140
|
|
|
$model = new $modelName; |
141
|
|
|
$obj = $model->newQuery()->where('applicant_id', $applicant->id) |
142
|
|
|
->where('id', $key)->first(); |
143
|
|
|
$instances[$key] = $obj; |
144
|
|
|
} |
145
|
|
|
if ($obj != null) { |
146
|
|
|
$obj->setAttribute($attribute, $value); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
function get(&$var, $default=null) { |
|
|
|
|
153
|
|
|
return isset($var) ? $var : $default; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// New Entitities |
157
|
|
|
setAttrFromInputNew(get($input['degree_type']['new']), $newDegrees,'App\Models\Degree', 'degree_type_id', $applicant); |
158
|
|
|
setAttrFromInputNew(get($input['degree_area']['new']), $newDegrees,'App\Models\Degree', 'area_of_study', $applicant); |
159
|
|
|
setAttrFromInputNew(get($input['degree_institution']['new']), $newDegrees,'App\Models\Degree', 'institution', $applicant); |
160
|
|
|
setAttrFromInputNew(get($input['degree_thesis']['new']), $newDegrees,'App\Models\Degree', 'thesis', $applicant); |
161
|
|
|
setAttrFromInputNew(get($input['degree_start_date']['new']), $newDegrees,'App\Models\Degree', 'start_date', $applicant); |
162
|
|
|
setAttrFromInputNew(get($input['degree_end_date']['new']), $newDegrees,'App\Models\Degree', 'end_date', $applicant); |
163
|
|
|
|
164
|
|
|
setAttrFromInputNew(get($input['course_name']['new']), $newCourses,'App\Models\Course', 'name', $applicant); |
165
|
|
|
setAttrFromInputNew(get($input['course_institution']['new']), $newCourses,'App\Models\Course', 'institution', $applicant); |
166
|
|
|
setAttrFromInputNew(get($input['course_status']['new']), $newCourses,'App\Models\Course', 'course_status_id', $applicant); |
167
|
|
|
setAttrFromInputNew(get($input['course_start_date']['new']), $newCourses,'App\Models\Course', 'start_date', $applicant); |
168
|
|
|
setAttrFromInputNew(get($input['course_end_date']['new']), $newCourses,'App\Models\Course', 'end_date', $applicant); |
169
|
|
|
|
170
|
|
|
setAttrFromInputNew(get($input['work_role']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'role', $applicant); |
171
|
|
|
setAttrFromInputNew(get($input['work_company']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'company', $applicant); |
172
|
|
|
setAttrFromInputNew(get($input['work_description']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'description', $applicant); |
173
|
|
|
setAttrFromInputNew(get($input['work_start_date']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'start_date', $applicant); |
174
|
|
|
setAttrFromInputNew(get($input['work_end_date']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'end_date', $applicant); |
175
|
|
|
|
176
|
|
|
// PreExisting entities |
177
|
|
|
setAttrFromInputOld($input, 'degree_type', $oldDegrees,'App\Models\Degree', 'degree_type_id', $applicant); |
178
|
|
|
setAttrFromInputOld($input, 'degree_area', $oldDegrees,'App\Models\Degree', 'area_of_study', $applicant); |
179
|
|
|
setAttrFromInputOld($input, 'degree_institution', $oldDegrees,'App\Models\Degree', 'institution', $applicant); |
180
|
|
|
setAttrFromInputOld($input, 'degree_thesis', $oldDegrees,'App\Models\Degree', 'thesis', $applicant); |
181
|
|
|
setAttrFromInputOld($input, 'degree_start_date', $oldDegrees,'App\Models\Degree', 'start_date', $applicant); |
182
|
|
|
setAttrFromInputOld($input, 'degree_end_date', $oldDegrees,'App\Models\Degree', 'end_date', $applicant); |
183
|
|
|
|
184
|
|
|
setAttrFromInputOld($input, 'course_name', $oldCourses,'App\Models\Course', 'name', $applicant); |
185
|
|
|
setAttrFromInputOld($input, 'course_institution', $oldCourses,'App\Models\Course', 'institution', $applicant); |
186
|
|
|
setAttrFromInputOld($input, 'course_status', $oldCourses,'App\Models\Course', 'course_status_id', $applicant); |
187
|
|
|
setAttrFromInputOld($input, 'course_start_date', $oldCourses,'App\Models\Course', 'start_date', $applicant); |
188
|
|
|
setAttrFromInputOld($input, 'course_end_date', $oldCourses,'App\Models\Course', 'end_date', $applicant); |
189
|
|
|
|
190
|
|
|
setAttrFromInputOld($input, 'work_role', $oldWorkExperiences,'App\Models\WorkExperience', 'role', $applicant); |
191
|
|
|
setAttrFromInputOld($input, 'work_company', $oldWorkExperiences,'App\Models\WorkExperience', 'company', $applicant); |
192
|
|
|
setAttrFromInputOld($input, 'work_description', $oldWorkExperiences,'App\Models\WorkExperience', 'description', $applicant); |
193
|
|
|
setAttrFromInputOld($input, 'work_start_date', $oldWorkExperiences,'App\Models\WorkExperience', 'start_date', $applicant); |
194
|
|
|
setAttrFromInputOld($input, 'work_end_date', $oldWorkExperiences,'App\Models\WorkExperience', 'end_date', $applicant); |
195
|
|
|
|
196
|
|
|
//Delete old entities that weren't resubmitted |
197
|
|
|
foreach($applicant->degrees as $degree) { |
|
|
|
|
198
|
|
|
Debugbar::info($oldCourses); |
199
|
|
|
Debugbar::info($degree); |
200
|
|
|
if (!isset($oldDegrees[$degree->id]) || $oldDegrees[$degree->id] === null) { |
201
|
|
|
Debugbar::info('Deleting degree ' . $degree->id); |
202
|
|
|
$degree->delete(); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
foreach($applicant->courses as $course) { |
|
|
|
|
206
|
|
|
if (!isset($oldCourses[$course->id]) || $oldCourses[$course->id] === null) { |
207
|
|
|
$course->delete(); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
foreach($applicant->work_experiences as $work) { |
|
|
|
|
211
|
|
|
if (!isset($oldWorkExperiences[$work->id]) || $oldWorkExperiences[$work->id] === null) { |
212
|
|
|
$work->delete(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
//Save new entities |
217
|
|
|
foreach($newDegrees as $entity) { |
|
|
|
|
218
|
|
|
$entity->save(); |
219
|
|
|
} |
220
|
|
|
foreach($newCourses as $entity) { |
|
|
|
|
221
|
|
|
$entity->save(); |
222
|
|
|
} |
223
|
|
|
foreach($newWorkExperiences as $entity) { |
|
|
|
|
224
|
|
|
$entity->save(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
//Save old entities |
228
|
|
|
foreach($oldDegrees as $entity) { |
|
|
|
|
229
|
|
|
$entity->save(); |
230
|
|
|
} |
231
|
|
|
foreach($oldCourses as $entity) { |
|
|
|
|
232
|
|
|
$entity->save(); |
233
|
|
|
} |
234
|
|
|
foreach($oldWorkExperiences as $entity) { |
|
|
|
|
235
|
|
|
$entity->save(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return redirect( route('profile.experience.edit', $applicant) ); |
|
|
|
|
239
|
|
|
//Debugbar::info($oldDegrees); |
240
|
|
|
// return view('applicant/profile_02_experience', [ |
241
|
|
|
// 'applicant' => $applicant, |
242
|
|
|
// 'profile' => Lang::get('applicant/profile_experience'), |
243
|
|
|
// 'degree_types' => DegreeType::all(), |
244
|
|
|
// 'course_status' => CourseStatus::all(), |
245
|
|
|
// 'degree_template' => Lang::get('common/degree'), |
246
|
|
|
// 'course_template' => Lang::get('common/course'), |
247
|
|
|
// 'work_template' => Lang::get('common/work_experience'), |
248
|
|
|
// 'form_submit_action' => route('profile.experience.update', $applicant), |
249
|
|
|
// ]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|