/**
  * 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 makeUpdMatchForm(MatchForm $matchForm, Match $match)
 {
     /* @var $qmh QMatchRelation */
     $qmh = $this->get('match')->getQMatchRelationByMatch($match->getId(), false);
     $teamListA = $this->get('orderTeams')->sortGroup($qmh->getGroup()->getId());
     $teamnamesA = array();
     foreach ($teamListA as $team) {
         $teamnamesA[$team->id] = $team->name;
     }
     $teamA = $teamListA[$qmh->getRank() - 1];
     $matchForm->setTeamA($teamA->id);
     /* @var $qma QMatchRelation */
     $qma = $this->get('match')->getQMatchRelationByMatch($match->getId(), true);
     $teamListB = $this->get('orderTeams')->sortGroup($qma->getGroup()->getId());
     $teamnamesB = array();
     foreach ($teamListB as $team) {
         $teamnamesB[$team->id] = $team->name;
     }
     $teamB = $teamListB[$qma->getRank() - 1];
     $matchForm->setTeamB($teamB->id);
     $show = true;
     $extshow = $show && !$this->get('match')->isMatchResultValid($matchForm->getId());
     $formDef = $this->createFormBuilder($matchForm);
     $formDef->add('teamA', 'choice', array('label' => 'FORM.MATCH.HOME', 'choices' => $teamnamesA, 'empty_value' => 'FORM.MATCH.DEFAULT', 'required' => false, 'disabled' => !$extshow, 'translation_domain' => 'admin'));
     $formDef->add('teamB', 'choice', array('label' => 'FORM.MATCH.AWAY', 'choices' => $teamnamesB, 'empty_value' => 'FORM.MATCH.DEFAULT', 'required' => false, 'disabled' => !$extshow, 'translation_domain' => 'admin'));
     $formDef->add('cancel', 'submit', array('label' => 'FORM.MATCH.CANCEL.CHG', 'translation_domain' => 'admin', 'buttontype' => 'btn btn-default', 'icon' => 'fa fa-times'));
     $formDef->add('save', 'submit', array('label' => 'FORM.MATCH.SUBMIT.CHG', 'translation_domain' => 'admin', 'icon' => 'fa fa-check'));
     return $formDef->getForm();
 }
 private function copyMatchForm(Match $match)
 {
     $matchForm = new MatchForm();
     $matchForm->setId($match->getId());
     $matchForm->setPid($match->getGroup()->getId());
     $matchForm->setMatchno($match->getMatchno());
     $matchdate = Date::getDateTime($match->getDate(), $match->getTime());
     $dateformat = $this->get('translator')->trans('FORMAT.DATE');
     $matchForm->setDate(date_format($matchdate, $dateformat));
     $timeformat = $this->get('translator')->trans('FORMAT.TIME');
     $matchForm->setTime(date_format($matchdate, $timeformat));
     $matchForm->setPlayground($match->getPlayground());
     /* @var $homeRel QMatchRelation */
     $homeRel = $this->get('match')->getQMatchRelationByMatch($match->getId(), false);
     if ($homeRel != null) {
         $matchForm->setGroupA($homeRel->getGroup()->getId());
         $matchForm->setRankA($homeRel->getRank());
     }
     /* @var $awayRel QMatchRelation */
     $awayRel = $this->get('match')->getQMatchRelationByMatch($match->getId(), true);
     if ($awayRel != null) {
         $matchForm->setGroupB($awayRel->getGroup()->getId());
         $matchForm->setRankB($awayRel->getRank());
     }
     return $matchForm;
 }
 private function getDetails(Match $match, $away)
 {
     $detail = array();
     $details = $this->get('match')->getMatchRelationDetails($match->getId(), $away);
     if ($details) {
         $detail['id'] = $details['id'];
         $detail['name'] = $this->get('logic')->getTeamName($details['team'], $details['division']);
         $detail['country'] = $details['country'];
     }
     /* @var $qrel QMatchRelation */
     $qrel = $this->get('match')->getQMatchRelationByMatch($match->getId(), $away);
     if ($qrel) {
         $group = $qrel->getGroup();
         if ($group->getClassification() > 0) {
             $groupname = $this->get('translator')->trans('GROUPCLASS.' . $group->getClassification(), array(), 'tournament');
         } else {
             $groupname = $this->get('translator')->trans('GROUP', array(), 'tournament');
         }
         $rankTxt = $this->get('translator')->transChoice('RANK', $qrel->getRank(), array('%rank%' => $qrel->getRank(), '%group%' => strtolower($groupname) . ' ' . $group->getName()), 'tournament');
         if (!array_key_exists('id', $detail)) {
             $detail['id'] = -1;
         }
         $detail['rank'] = $rankTxt;
         $detail['rgrp'] = $group->getId();
     }
     return count($detail) > 0 ? $detail : null;
 }