/** * Store the full information of Career, statistics and references. * Also, save career based upon career type * @param $request * @param $career_type */ public function storeCareer($userProfile, $request, $career_type) { $sportDataMap = $this->getSportDataMap(Session::get(SiteSessions::USER_SPORT_TYPE)); $formRequest = $request->all(); $careerStatisticsType = ""; $careerInformationFillAbles = ""; $careerTypeSportDataMap = ""; $careerCounts = ""; $existingCareer = ""; $existingCareerCount = ""; $fieldPrefix = ""; switch ($career_type) { case SiteConstants::CAREER_TYPE_CLUB: //Store Club Information $careerInformationFillAbles = TalentCareerInformation::$club_fillable; $careerTypeSportDataMap = SportsRepository::getClubDataMap($this->getSportDataMap(Session::get(SiteSessions::USER_SPORT_TYPE))); $careerCounts = count($formRequest["club_club_school_name"]); $fieldPrefix = "club_"; $existingCareer = $userProfile->careerInformation()->where('career_type', '=', SiteConstants::CAREER_TYPE_CLUB)->get(); $existingCareerCount = count($existingCareer); break; case SiteConstants::CAREER_TYPE_SCHOOL: //Store Club Information $careerInformationFillAbles = TalentCareerInformation::$school_fillable; $careerTypeSportDataMap = SportsRepository::getSchoolDataMap($this->getSportDataMap(Session::get(SiteSessions::USER_SPORT_TYPE))); $careerCounts = count($formRequest["school_club_school_name"]); $fieldPrefix = "school_"; $existingCareer = $userProfile->careerInformation()->where('career_type', '=', SiteConstants::CAREER_TYPE_SCHOOL)->get(); $existingCareerCount = count($existingCareer); break; default: return false; } // var_dump($careerTypeSportDataMap); // dd($formRequest); /* * For loop for storing Career Information */ for ($i = 0; $i < $careerCounts; $i++) { $talentCareerInformation = ""; if ($i < $existingCareerCount) { $talentCareerInformation = $existingCareer[$i]; } else { if ($i >= $existingCareerCount) { $talentCareerInformation = new TalentCareerInformation(); } } $talentCareerInformation->career_type = $career_type; foreach ($careerInformationFillAbles as $value) { $requestField = $fieldPrefix . $value; $talentCareerInformation->{$value} = isset($formRequest[$requestField][$i]) ? $formRequest[$requestField][$i] : null; } if ($i >= $existingCareerCount) { $talentCareerInformation = $userProfile->careerInformation()->create($talentCareerInformation->toArray()); } $talentCareerInformation->save(); /* * This code is for storing Sport statistics in the database */ $sportStatisticsCount = count($formRequest[array_values($careerTypeSportDataMap)[0]][$i]); $existingSportStatistics = $talentCareerInformation->careerSportStatistics(Session::get(SiteSessions::USER_SPORT_TYPE))->get(); for ($j = 0; $j < $sportStatisticsCount; $j++) { $careerStatistics = ""; if ($j < count($existingSportStatistics)) { $careerStatistics = $existingSportStatistics[$j]; } else { if ($j >= count($existingSportStatistics)) { $careerStatistics = $this->getCareerStatisticsModelObject(Session::get(SiteSessions::USER_SPORT_TYPE)); } } $careerStatistics->statistic_type = $career_type; foreach ($careerTypeSportDataMap as $key => $value) { $statisticField = $sportDataMap[$key]; $careerStatistics->{$statisticField} = $formRequest[$careerTypeSportDataMap[$key]][$i][$j]; } if ($j >= count($existingSportStatistics)) { $careerStatistics = $talentCareerInformation->careerSportStatistics(Session::get(SiteSessions::USER_SPORT_TYPE))->create($careerStatistics->toArray()); } $careerStatistics->save(); } if (count($existingSportStatistics) > $sportStatisticsCount) { for ($k = $sportStatisticsCount; $k < count($existingSportStatistics); $k++) { $existingSportStatistics[$k]->delete(); } } /* * This code is for storing references in the database */ $clubReferenceCount = count($formRequest[$fieldPrefix . 'name'][$i]); $existingReference = $talentCareerInformation->careerReferences()->get(); for ($j = 0; $j < $clubReferenceCount; $j++) { $careerReference = ""; if ($j < count($existingReference)) { $careerReference = $existingReference[$j]; } else { if ($j >= count($existingReference)) { $careerReference = new TalentCareerReferences(); } } foreach ($careerReference->getFillAbleField() as $value) { $inputField = $fieldPrefix . $value; $careerReference->{$value} = $formRequest[$inputField][$i][$j]; } if ($j >= count($existingReference)) { $careerReference = $talentCareerInformation->careerReferences()->create($careerReference->toArray()); } $careerReference->save(); } if (count($existingReference) > $clubReferenceCount) { for ($k = $clubReferenceCount; $k < count($existingReference); $k++) { $existingReference[$k]->delete(); } } } if ($existingCareerCount > $careerCounts) { for ($i = $careerCounts; $i < $existingCareerCount; $i++) { $existingCareer[$i]->delete(); } } //dd($formRequest); // var_dump($careerTypeSportDataMap); // dd($request->all()); }