| 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\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Models\Skill; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Models\Applicant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use App\Models\Reference; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use App\Models\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use App\Services\Validation\Requests\UpdateReferenceValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ReferencesController extends Controller | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Display the Skills page associated with the applicant. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param  \App\Models\Applicant  $applicant | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function show(Applicant $applicant) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Show the form for editing the applicant's references | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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_04_references', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'applicant' => $applicant, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'profile' => Lang::get('applicant/profile_references'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'form_submit_action' => route('profile.references.update', $applicant), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Update all the applicant's references in storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param  \Illuminate\Http\Request  $request | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param  \App\Models\Applicant  $applicant | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function updateAll(Request $request, Applicant $applicant) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $input = $request->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $references = $input['references']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         //Delete old references that weren't resubmitted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         //Note: this must be done before adding new references, so we don't delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         // them right after adding them | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         foreach($applicant->references as $oldReference) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             //Check if no references were resubmitted, or if this specific one wasn't | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             if (!isset($references['old']) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 !isset($references['old'][$oldReference->id])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $oldReference->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         //Save new references | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if (isset($references['new'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             foreach($references['new'] as $referenceInput) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $reference = new Reference(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $reference->applicant_id = $applicant->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $reference->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     'name' => $referenceInput['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     'email' => $referenceInput['email'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     'relationship_id' => $referenceInput['relationship_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     'description' => $referenceInput['description'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $reference->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $projectIds = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $projects = $referenceInput['projects']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 if (isset($projects['new'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     foreach($projects['new'] as $projectInput) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                         $project = new Project(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                         $project->applicant_id = $applicant->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                         $project->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                             'name' => $projectInput['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                             'start_date' => $projectInput['start_date'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                             'end_date' => $projectInput['end_date'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                         ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                         $project->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                         $projectIds[] = $project->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 //Sync attaches the specified ids, and detaches all others | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $reference->projects()->sync($projectIds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $reference->skill_declarations()->sync($skillDeclarationIds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         //Update old references | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if (isset($references['old'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             foreach($references['old'] as $id=>$referenceInput) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 //Ensure this reference belongs to this applicant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $reference = $applicant->references->firstWhere('id', $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 if ($reference != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     $reference->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                         'name' => $referenceInput['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                         'email' => $referenceInput['email'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                         'relationship_id' => $referenceInput['relationship_id'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                         'description' => $referenceInput['description'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $reference->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     $projectIds = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     $projects = $referenceInput['projects']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     if (isset($projects['new'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                         foreach($projects['new'] as $projectInput) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                             $project = new Project(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                             $project->applicant_id = $applicant->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                             $project->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                                 'name' => $projectInput['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                                 'start_date' => $projectInput['start_date'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                                 'end_date' => $projectInput['end_date'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                             ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                             $project->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                             $projectIds[] = $project->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     if (isset($projects['old'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                         foreach($projects['old'] as $projectId=>$projectInput) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                             //Ensure this project belongs to this applicant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                             $project = $applicant->projects->firstWhere('id', $projectId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                             if ($project != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                                 $project->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                                     'name' => $projectInput['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                                     'start_date' => $projectInput['start_date'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                                     'end_date' => $projectInput['end_date'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                                 ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                                 $project->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                                 $projectIds[] = $project->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                     //TODO: when projects exists independpently on profile, don't delete them Here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                     // Delete projects that will be detached from this reference | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                     foreach($reference->projects as $project) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                         if (!in_array($project->id, $projectIds)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                             $project->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                     //Sync attaches the specified ids, and detaches all others | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                     $reference->projects()->sync($projectIds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                     $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                     $reference->skill_declarations()->sync($skillDeclarationIds); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                     Log::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return redirect( route('profile.references.edit', $applicant) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Update or create a reference with the supplied data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param \Illuminate\Http\Request   $request   The incoming request object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @return | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |     public function update(Request $request, ?Reference $reference = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |         $validator = new UpdateReferenceValidator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |         $validator->validate($request->input()); | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |         if ($reference === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |             $reference = new Reference(); | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |             $reference->applicant_id = $request->user()->applicant->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |         $reference->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |             'name' => $request->input('name'), | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |             'email' => $request->input('email'), | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |             'relationship_id' => $request->input('relationship_id'), | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |             'description' => $request->input('description'), | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |         ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |         $reference->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |         $reference->load('projects'); | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |         //TODO: As soon as you can interact with projects outside of references, | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |         //  this will become a dangerous operation | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |         foreach ($reference->projects as $project) { | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |             $project->delete(); | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |         $newProjects = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |         if ($request->input('projects')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |             foreach ($request->input('projects') as $projectInput) { | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  |                 $project = new Project(); | 
            
                                                                        
                            
            
                                    
            
            
                | 211 |  |  |                 $project->applicant_id = $reference->applicant_id; | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  |                 $project->fill([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  |                     'name' => $projectInput['name'], | 
            
                                                                        
                            
            
                                    
            
            
                | 214 |  |  |                     'start_date' => $projectInput['start_date'], | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |                     'end_date' => $projectInput['end_date'], | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |                 ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |                 $project->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 218 |  |  |                 $newProjects[] = $project->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 219 |  |  |                 // $reference->projects()->attach($project); | 
            
                                                                        
                            
            
                                    
            
            
                | 220 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 221 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 222 |  |  |         $reference->projects()->sync($newProjects); | 
            
                                                                        
                            
            
                                    
            
            
                | 223 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 224 |  |  |         //Attach relatives | 
            
                                                                        
                            
            
                                    
            
            
                | 225 |  |  |         $skillIds = $this->getRelativeIds($request->input(), 'skills'); | 
            
                                                                        
                            
            
                                    
            
            
                | 226 |  |  |         $reference->skill_declarations()->sync($skillIds); | 
            
                                                                        
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 228 |  |  |         // if an ajax request, return the new object | 
            
                                                                        
                            
            
                                    
            
            
                | 229 |  |  |         if ($request->ajax()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 230 |  |  |             $reference->load('relationship'); | 
            
                                                                        
                            
            
                                    
            
            
                | 231 |  |  |             $reference->load('projects'); | 
            
                                                                        
                            
            
                                    
            
            
                | 232 |  |  |             return $reference->toJson(); | 
            
                                                                        
                            
            
                                    
            
            
                | 233 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 234 |  |  |             return redirect()->back(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      * Delete the particular reference from storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |      * @param  \Illuminate\Http\Request  $request | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |      * @param  \App\Models\Reference  $reference | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * @return \Illuminate\Http\Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |     public function destroy(Request $request, Reference $reference) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         $this->authorize('delete', $reference); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         //TODO: when projects exist independently on profile, delete seperatley | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         foreach($reference->projects as $project) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             $project->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         $reference->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         if($request->ajax()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             return [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |                 "message" => 'Reference deleted' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |         return redirect()->back(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 265 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 266 |  |  |  |