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\Http\Controllers\Controller; |
9
|
|
|
use App\Models\Skill; |
10
|
|
|
use App\Models\Applicant; |
11
|
|
|
use App\Models\Reference; |
12
|
|
|
use App\Models\Project; |
13
|
|
|
|
14
|
|
|
class ReferencesController extends Controller |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Display the Skills page associated with the applicant. |
19
|
|
|
* |
20
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
21
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
22
|
|
|
*/ |
23
|
|
|
public function show(Applicant $applicant) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
// |
26
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Show the form for editing the applicant's references |
31
|
|
|
* |
32
|
|
|
* @param Request $request |
|
|
|
|
33
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
34
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function edit(Request $request, Applicant $applicant) |
37
|
|
|
{ |
38
|
|
|
return view('applicant/profile_04_references', [ |
|
|
|
|
39
|
|
|
'applicant' => $applicant, |
40
|
|
|
'profile' => Lang::get('applicant/profile_references'), |
41
|
|
|
'form_submit_action' => route('profile.references.update', $applicant), |
|
|
|
|
42
|
|
|
]); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Update the applicant's references in storage. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
49
|
|
|
* @param \App\Models\Applicant $applicant |
|
|
|
|
50
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function update(Request $request, Applicant $applicant) |
53
|
|
|
{ |
54
|
|
|
$input = $request->input(); |
55
|
|
|
|
56
|
|
|
Debugbar::info($input); |
57
|
|
|
|
58
|
|
|
$references = $input['references']; |
59
|
|
|
|
60
|
|
|
//Delete old references that weren't resubmitted |
61
|
|
|
//Note: this must be done before adding new references, so we don't delete |
62
|
|
|
// them right after adding them |
63
|
|
|
foreach($applicant->references as $oldReference) { |
|
|
|
|
64
|
|
|
//Check if no references were resubmitted, or if this specific one wasn't |
65
|
|
|
if (!isset($references['old']) || |
66
|
|
|
!isset($references['old'][$oldReference->id])) { |
|
|
|
|
67
|
|
|
$oldReference->delete(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
//Save new references |
72
|
|
|
if (isset($references['new'])) { |
73
|
|
|
foreach($references['new'] as $referenceInput) { |
|
|
|
|
74
|
|
|
$reference = new Reference(); |
75
|
|
|
$reference->applicant_id = $applicant->id; |
76
|
|
|
$reference->fill([ |
|
|
|
|
77
|
|
|
'name' => $referenceInput['name'], |
78
|
|
|
'email' => $referenceInput['email'], |
79
|
|
|
'relationship_id' => $referenceInput['relationship_id'], |
80
|
|
|
'description' => $referenceInput['description'], |
81
|
|
|
]); |
|
|
|
|
82
|
|
|
|
83
|
|
|
$reference->save(); |
84
|
|
|
|
85
|
|
|
$projectIds = []; |
86
|
|
|
$projects = $referenceInput['projects']; |
87
|
|
|
if (isset($projects['new'])) { |
88
|
|
|
foreach($projects['new'] as $projectInput) { |
|
|
|
|
89
|
|
|
$project = new Project(); |
90
|
|
|
$project->applicant_id = $applicant->id; |
91
|
|
|
$project->fill([ |
|
|
|
|
92
|
|
|
'name' => $projectInput['name'], |
93
|
|
|
'start_date' => $projectInput['start_date'], |
94
|
|
|
'end_date' => $projectInput['end_date'], |
95
|
|
|
]); |
|
|
|
|
96
|
|
|
$project->save(); |
97
|
|
|
$projectIds[] = $project->id; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
//Sync attaches the specified ids, and detaches all others |
101
|
|
|
$reference->projects()->sync($projectIds); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); |
105
|
|
|
$reference->skill_declarations()->sync($skillDeclarationIds); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
//Update old references |
110
|
|
|
if (isset($references['old'])) { |
111
|
|
|
foreach($references['old'] as $id=>$referenceInput) { |
|
|
|
|
112
|
|
|
//Ensure this reference belongs to this applicant |
113
|
|
|
$reference = $applicant->references->firstWhere('id', $id); |
114
|
|
|
if ($reference != null) { |
115
|
|
|
$reference->fill([ |
|
|
|
|
116
|
|
|
'name' => $referenceInput['name'], |
117
|
|
|
'email' => $referenceInput['email'], |
118
|
|
|
'relationship_id' => $referenceInput['relationship_id'], |
119
|
|
|
'description' => $referenceInput['description'], |
120
|
|
|
]); |
|
|
|
|
121
|
|
|
$reference->save(); |
122
|
|
|
|
123
|
|
|
$projectIds = []; |
124
|
|
|
$projects = $referenceInput['projects']; |
125
|
|
|
if (isset($projects['new'])) { |
126
|
|
|
foreach($projects['new'] as $projectInput) { |
|
|
|
|
127
|
|
|
$project = new Project(); |
128
|
|
|
$project->applicant_id = $applicant->id; |
129
|
|
|
$project->fill([ |
|
|
|
|
130
|
|
|
'name' => $projectInput['name'], |
131
|
|
|
'start_date' => $projectInput['start_date'], |
132
|
|
|
'end_date' => $projectInput['end_date'], |
133
|
|
|
]); |
|
|
|
|
134
|
|
|
$project->save(); |
135
|
|
|
$projectIds[] = $project->id; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
if (isset($projects['old'])) { |
139
|
|
|
foreach($projects['old'] as $projectId=>$projectInput) { |
|
|
|
|
140
|
|
|
//Ensure this project belongs to this applicant |
141
|
|
|
$project = $applicant->projects->firstWhere('id', $projectId); |
142
|
|
|
if ($project != null) { |
143
|
|
|
$project->fill([ |
|
|
|
|
144
|
|
|
'name' => $projectInput['name'], |
145
|
|
|
'start_date' => $projectInput['start_date'], |
146
|
|
|
'end_date' => $projectInput['end_date'], |
147
|
|
|
]); |
|
|
|
|
148
|
|
|
$project->save(); |
149
|
|
|
$projectIds[] = $project->id; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
//TODO: when projects exists independpently on profile, don't delete them Here |
154
|
|
|
// Delete projects that will be detached from this reference |
155
|
|
|
foreach($reference->projects as $project) { |
|
|
|
|
156
|
|
|
if (!in_array($project->id, $projectIds)) { |
157
|
|
|
$project->delete(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
//Sync attaches the specified ids, and detaches all others |
162
|
|
|
$reference->projects()->sync($projectIds); |
163
|
|
|
|
164
|
|
|
$skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); |
165
|
|
|
$reference->skill_declarations()->sync($skillDeclarationIds); |
166
|
|
|
} else { |
167
|
|
|
Debugbar::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return redirect( route('profile.references.edit', $applicant) ); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Delete the particular reference from storage. |
177
|
|
|
* |
178
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
179
|
|
|
* @param \App\Models\Reference $reference |
|
|
|
|
180
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
181
|
|
|
*/ |
182
|
|
|
public function destroy(Request $request, Reference $reference) |
183
|
|
|
{ |
184
|
|
|
$this->authorize('delete', $reference); |
185
|
|
|
|
186
|
|
|
//TODO: when projects exist independently on profile, delete seperatley |
187
|
|
|
foreach($reference->projects as $project) { |
|
|
|
|
188
|
|
|
$project->delete(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$reference->delete(); |
192
|
|
|
|
193
|
|
|
if($request->ajax()) { |
|
|
|
|
194
|
|
|
return [ |
|
|
|
|
195
|
|
|
"message" => 'Reference deleted' |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return redirect()->back(); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|