/** * @param $filename string file from where to import the data */ private function importData($filename, Event $event) { $xml = simplexml_load_file($filename); // see <http://stackoverflow.com/questions/4411340/> $date = \DateTime::createFromFormat('Y-m-d\\TH:i:sP', $xml["stand"]); $clubs = array(); $races = array(); $boats = array(); $athletes = array(); foreach ($xml->vereine->verein as $club) { $x = new Club($club); $clubs[$x->drv_id] = $x; } foreach ($xml->meldungen->rennen as $r) { $race = new Race($r); $races[$race->number] = $race; foreach ($r->meldung as $registration) { $boat = new Boat($registration); $boats[$boat->id] = $boat; foreach ($registration->mannschaft->position as $crewmember) { $position = (int) (string) $crewmember['nr']; $is_cox = 'true' == (string) $crewmember['st']; foreach ($crewmember->athlet as $athlete) { $a = new Athlete($athlete); $athletes[$a->drv_id] = $a; $boat->add($a, $position, $is_cox); } } $race->add($boat); } } $em = $this->getDoctrine()->getManager(); /** @var \AppBundle\Repository\ClubRepository $clubRepo */ $clubRepo = $em->getRepository('AppBundle:Club'); foreach ($clubs as $club) { $clubRepo->createOrUpdate($club, $this->get('logger')); } // save into DB $em->flush(); /** @var \AppBundle\Repository\CompetitorRepository $athleteRepo */ $athleteRepo = $em->getRepository('AppBundle:Competitor'); /** @var \AppBundle\Repository\MembershipRepository $membershipRepo */ $membershipRepo = $em->getRepository('AppBundle:Membership'); // cache $membershipPerAthlete = array("_" => ""); foreach ($athletes as $athlete) { try { $a = $athleteRepo->createOrUpdate($athlete, $this->get('logger')); $m = $membershipRepo->createOrUpdate($a, $athlete->club_id, $date, $this->get('logger')); $membershipPerAthlete[$athlete->drv_id] = $m; } catch (\Exception $e) { $this->addFlash('error', $e->getMessage()); } } // save into DB $em->flush(); /** @var \AppBundle\Repository\RaceRepository $raceRepo */ $raceRepo = $em->getRepository('AppBundle:Race'); /** @var \AppBundle\Repository\TeamRepository $teamRepository */ $teamRepository = $em->getRepository('AppBundle:Team'); /** @var \AppBundle\DRV_Import\Race $race */ foreach ($races as $race) { $r = $raceRepo->createOrUpdate($race, $event, $this->get('logger')); if (null != $r) { /** @var \AppBundle\DRV_Import\Boat $boat */ foreach ($race->getBoats() as $boat) { $b = $teamRepository->createOrUpdate($boat, $r, $this->get('logger')); // map athletes->boat $positions = $boat->getPositions(); foreach (array_keys($positions) as $i) { $a_id = $positions[$i]['athlete']; if (null == $a_id) { $message = "No athlete at position {$i} in group {$b->getName()}"; $this->get('logger')->warning($message); echo $message . "\n"; } else { if (!array_key_exists($a_id, $membershipPerAthlete)) { $message = "No membership for athlete {$a_id}"; $this->get('logger')->warning($message); echo $message . "\n"; } else { $m = $membershipPerAthlete[$a_id]; // FIXME use createOrUpdate to avoid duplicates $posInTeam = new TeamPosition(); $posInTeam->setTeam($b)->setPosition($i)->setIsCox($positions[$i]['is_cox'])->setMembership($m); $em->persist($posInTeam); } } } } $em->flush(); } else { $this->addFlash('error', "Could not find race for [{$race->specification}, {$race->number}]"); } } }
/** * Creates a new Team entity. * * @Route("/team/new", name="team_new") * @Method({"GET", "POST"}) */ public function newAction(Request $request) { /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); $maxTeamSize = 9; $minAge = 0; $maxAge = 100; $gender = Competitor::GENDER_BOTH; //$withCox = true; /** @var Race $race */ $race = $request->get('race', null); if (!is_null($race)) { /** @var RaceRepository $raceRepo */ $raceRepo = $em->getRepository('AppBundle:Race'); /** @var Race|null $race */ $race = $raceRepo->find(intval($race)); if (!is_null($race)) { $maxTeamSize = $race->getTeamsize(); $minAge = $race->getAgeMin(); $maxAge = $race->getAgeMax(); $gender = $race->getGender(); } } // get all potential candidates /** @var MembershipRepository $memberRepo */ $memberRepo = $em->getRepository('AppBundle:Membership'); $memberships = $memberRepo->findAllCurrent($gender, $minAge, $maxAge); if (0 == count($memberships)) { $this->addFlash('error', 'Keine passenden Sportler gefunden, um neue Mannschaft anzulegen!'); return $this->redirect($request->headers->get('referer')); } else { /** @var ClubRepository $clubRepo */ $clubRepo = $em->getRepository('AppBundle:Club'); $clubs = $clubRepo->findAll(); usort($clubs, function ($a, $b) { /** @var Club $a */ $left = $a->getCity() . '_' . $a->getAbbreviation(); /** @var Club $b */ $right = $b->getCity() . '_' . $b->getAbbreviation(); return strcmp($left, $right); }); // no model $data = array(); $fb = $this->createFormBuilder($data); $fb->add('club', ChoiceType::class, array('label' => 'Meldender Club', 'required' => true, 'expanded' => false, 'multiple' => false, 'choices' => $clubs, 'choice_label' => function ($club, $key, $index) { /** @var Club $club */ return $club->getName() . ' (' . $club->getCity() . ')'; }, 'group_by' => function ($club, $key, $index) { /** @var Club $club */ return substr($club->getCity(), 0, 1); })); // team members at the positions for ($i = 1; $i <= $maxTeamSize; $i++) { $fb->add('members_' . $i, ChoiceType::class, array('label' => 'Platz ' . $i, 'required' => true, 'expanded' => false, 'multiple' => false, 'choices' => $memberships, 'choice_label' => function ($m, $key, $index) use($gender) { /** @var Membership $m */ /** @var Competitor $p */ $p = $m->getPerson(); $result = $p->getFirstName() . ' ' . strtoupper($p->getLastName()) . ' (' . $p->getYearOfBirth() . ', ' . $p->getAge(); if (Competitor::GENDER_BOTH == $gender) { $result .= ', ' . $p->getGenderSymbol(); } $result .= ')'; return $result; }, 'group_by' => function ($m, $key, $index) { /** @var Membership $m*/ if (!empty(trim($m->getClub()->getShortname()))) { return $m->getClub()->getShortname(); } else { return $m->getClub()->getName(); } })); } $form = $fb->getForm(); $form->handleRequest($request); if ($form->isSubmitted()) { $isGood = true; if (!$form->isValid()) { foreach ($form->getErrors() as $error) { $this->addFlash('error', $error->getMessage()); } $isGood = false; } if ($isGood) { /** @var Club $club */ $club = $form->get('club')->getData(); if (is_null($club)) { $this->addFlash('error', 'Kein Club angegeben!'); $isGood = false; } } $team = null; if ($isGood) { $team = new Team(); $team->setClub($club); $team->setName($club->getName()); $em->persist($team); } $found[] = array(); for ($i = 1; $i <= $maxTeamSize; $i++) { /** @var Membership $m */ $m = $form->get('members_' . $i)->getData(); if (is_null($m)) { $this->addFlash('error', sprintf("Kein Sportler an Position %d angegeben!", $i)); $isGood = false; } else { if (in_array($m->getId(), $found)) { $this->addFlash('error', sprintf("Sportler an Position %d mehrfach angegeben!", $i)); $isGood = false; } else { $found[] = $m->getId(); $posInTeam = new TeamPosition(); $posInTeam->setTeam($team)->setPosition($i)->setIsCox(false)->setMembership($m); $em->persist($posInTeam); } } } if ($isGood) { $em->flush(); $this->addFlash('notice', 'Mannschaft erfolgreich angelegt!'); } } return $this->render('team/new.html.twig', array('race' => $race, 'form' => $form->createView())); } }