/**
  * List the latest matches for a tournament
  * @Route("/edit/m/save/plan/{tournamentid}", name="_edit_match_planning_save")
  * @Method("GET")
  */
 public function saveMatchesAction($tournamentid, Request $request)
 {
     $tournament = $this->checkArgs($tournamentid);
     $result = $this->get('planning')->getSchedule($tournament);
     // Only if tournament has not been started we are allowed to wipe the teams
     if ($this->get('tmnt')->getTournamentStatus($tournamentid, new DateTime()) == TournamentSupport::$TMNT_ENROLL) {
         $this->get('tmnt')->wipeMatches($tournamentid);
         $em = $this->getDoctrine()->getEntityManager();
         $em->beginTransaction();
         try {
             /* @var $match MatchPlan */
             foreach ($result['matches'] as $match) {
                 $matchrec = new Match();
                 $matchrec->setMatchno($match->getMatchno());
                 $matchrec->setDate($match->getDate());
                 $matchrec->setTime($match->getTime());
                 $matchrec->setGroup($match->getGroup());
                 $matchrec->setPlayground($match->getPlayground());
                 $resultreqA = new MatchRelation();
                 $resultreqA->setTeam($match->getTeamA());
                 $resultreqA->setAwayteam(MatchSupport::$HOME);
                 $resultreqA->setScorevalid(false);
                 $resultreqA->setScore(0);
                 $resultreqA->setPoints(0);
                 $matchrec->addMatchRelation($resultreqA);
                 $resultreqB = new MatchRelation();
                 $resultreqB->setTeam($match->getTeamB());
                 $resultreqB->setAwayteam(MatchSupport::$AWAY);
                 $resultreqB->setScorevalid(false);
                 $resultreqB->setScore(0);
                 $resultreqB->setPoints(0);
                 $matchrec->addMatchRelation($resultreqB);
                 $em->persist($matchrec);
             }
             $em->flush();
             $em->commit();
         } catch (Exception $e) {
             $em->rollback();
             throw $e;
         }
         $request->getSession()->getFlashBag()->add('data_saved', 'FORM.MATCHPLANNING.PLAN_SAVED');
     } else {
         $request->getSession()->getFlashBag()->add('data_saved', 'FORM.MATCHPLANNING.PLAN_NOT_SAVED');
     }
     return $this->redirect($this->generateUrl("_edit_match_planning_result", array('tournamentid' => $tournament->getId())));
 }
 private function commitImport($parseObj, $date)
 {
     $matchdate = date_create_from_format($this->get('translator')->trans('FORMAT.DATE'), $date);
     $matchtime = date_create_from_format($this->get('translator')->trans('FORMAT.TIME'), $parseObj['time']);
     if ($matchdate === false || $matchtime === false) {
         throw new ValidationException("BADDATE", "date=" . $date . " time=" . $parseObj['time']);
     }
     $matchrec = new Match();
     $matchrec->setMatchno($parseObj['id']);
     $matchrec->setDate(Date::getDate($matchdate));
     $matchrec->setTime(Date::getTime($matchtime));
     $matchrec->setGroup($parseObj['group']);
     $matchrec->setPlayground($parseObj['playground']);
     $resultreqA = new MatchRelation();
     $resultreqA->setTeam($parseObj['teamA']);
     $resultreqA->setAwayteam(false);
     $resultreqA->setScorevalid(false);
     $resultreqA->setScore(0);
     $resultreqA->setPoints(0);
     $matchrec->addMatchRelation($resultreqA);
     $resultreqB = new MatchRelation();
     $resultreqB->setTeam($parseObj['teamB']);
     $resultreqB->setAwayteam(true);
     $resultreqB->setScorevalid(false);
     $resultreqB->setScore(0);
     $resultreqB->setPoints(0);
     $matchrec->addMatchRelation($resultreqB);
     $em = $this->getDoctrine()->getManager();
     $em->persist($matchrec);
     $em->flush();
 }
 private function commitImport($parseObj)
 {
     /* @var $match Match */
     $match = $parseObj['match'];
     $goA = new GroupOrder();
     $goA->setTeam($parseObj['teamA']);
     $goA->setGroup($match->getGroup());
     $resultreqA = new MatchRelation();
     $resultreqA->setTeam($parseObj['teamA']);
     $resultreqA->setAwayteam(false);
     $resultreqA->setScorevalid(false);
     $resultreqA->setScore(0);
     $resultreqA->setPoints(0);
     $match->addMatchRelation($resultreqA);
     $goB = new GroupOrder();
     $goB->setTeam($parseObj['teamB']);
     $goB->setGroup($match->getGroup());
     $resultreqB = new MatchRelation();
     $resultreqB->setTeam($parseObj['teamB']);
     $resultreqB->setAwayteam(true);
     $resultreqB->setScorevalid(false);
     $resultreqB->setScore(0);
     $resultreqB->setPoints(0);
     $match->addMatchRelation($resultreqB);
     $em = $this->getDoctrine()->getManager();
     $em->persist($goA);
     $em->persist($goB);
     $em->persist($resultreqA);
     $em->persist($resultreqB);
 }
 private function isScoreValid(MatchRelation $relA, MatchRelation $relB)
 {
     return $relA->getScorevalid() && $relB->getScorevalid();
 }
 private function chgMatch(MatchForm $matchForm, Match &$match)
 {
     $em = $this->getDoctrine()->getManager();
     $homeRel = $this->get('match')->getMatchRelationByMatch($match->getId(), false);
     if ($homeRel == null) {
         $homeRel = new MatchRelation();
         $homeRel->setAwayteam(false);
         $homeRel->setScorevalid(false);
         $homeRel->setPoints(0);
         $homeRel->setScore(0);
         $match->addMatchRelation($homeRel);
         $em->persist($homeRel);
     }
     $homeRel->setTeam($this->get('entity')->getTeamById($matchForm->getTeamA()));
     $awayRel = $this->get('match')->getMatchRelationByMatch($match->getId(), true);
     if ($awayRel == null) {
         $awayRel = new MatchRelation();
         $awayRel->setAwayteam(true);
         $awayRel->setScorevalid(false);
         $awayRel->setPoints(0);
         $awayRel->setScore(0);
         $match->addMatchRelation($awayRel);
         $em->persist($awayRel);
     }
     $awayRel->setTeam($this->get('entity')->getTeamById($matchForm->getTeamB()));
     $em->flush();
 }