public function getTemplateParameters()
 {
     $cupRoundId = $this->_websoccer->getRequestParameter("roundid");
     $cupGroup = $this->_websoccer->getRequestParameter("group");
     $columns = "C.name AS cup_name, R.name AS round_name";
     $fromTable = $this->_websoccer->getConfig("db_prefix") . "_cup_round AS R";
     $fromTable .= " INNER JOIN " . $this->_websoccer->getConfig("db_prefix") . "_cup AS C ON C.id = R.cup_id";
     $result = $this->_db->querySelect($columns, $fromTable, "R.id = %d", $cupRoundId);
     $round = $result->fetch_array();
     $result->free();
     $matches = MatchesDataService::getMatchesByCupRoundAndGroup($this->_websoccer, $this->_db, $round["cup_name"], $round["round_name"], $cupGroup);
     return array("matches" => $matches, "groupteams" => CupsDataService::getTeamsOfCupGroupInRankingOrder($this->_websoccer, $this->_db, $cupRoundId, $cupGroup));
 }
 public static function checkIfMatchIsLastMatchOfGroupRoundAndCreateFollowingMatches(WebSoccer $websoccer, DbConnection $db, SimulationMatch $match)
 {
     if (!strlen($match->cupRoundGroup)) {
         return;
     }
     // check if there are any open matches in this round
     $result = $db->querySelect('COUNT(*) AS hits', $websoccer->getConfig('db_prefix') . '_spiel', 'berechnet = \'0\' AND pokalname = \'%s\' AND pokalrunde = \'%s\' AND id != %d', array($match->cupName, $match->cupRoundName, $match->id));
     $openMatches = $result->fetch_array();
     $result->free();
     // there are still matches to simulate, so wait until all potential opponents are available
     if (isset($openMatches['hits']) && $openMatches['hits']) {
         return;
     }
     // get configurations for next round
     $columns = array();
     $columns['N.cup_round_id'] = 'round_id';
     $columns['N.groupname'] = 'groupname';
     $columns['N.rank'] = 'rank';
     $columns['N.target_cup_round_id'] = 'target_cup_round_id';
     $fromTable = $websoccer->getConfig('db_prefix') . '_cup_round_group_next AS N';
     $fromTable .= ' INNER JOIN ' . $websoccer->getConfig('db_prefix') . '_cup_round AS R ON N.cup_round_id = R.id';
     $fromTable .= ' INNER JOIN ' . $websoccer->getConfig('db_prefix') . '_cup AS C ON R.cup_id = C.id';
     $result = $db->querySelect($columns, $fromTable, 'C.name = \'%s\' AND R.name = \'%s\'', array($match->cupName, $match->cupRoundName));
     $nextConfigs = array();
     while ($nextConfig = $result->fetch_array()) {
         $nextConfigs[$nextConfig['groupname']]['' . $nextConfig['rank']] = $nextConfig['target_cup_round_id'];
         $roundId = $nextConfig['round_id'];
     }
     $result->free();
     // get teams of each group for next rounds
     $nextRoundTeams = array();
     foreach ($nextConfigs as $groupName => $rankings) {
         $teamsInGroup = CupsDataService::getTeamsOfCupGroupInRankingOrder($websoccer, $db, $roundId, $groupName);
         for ($teamRank = 1; $teamRank <= count($teamsInGroup); $teamRank++) {
             $configIndex = '' . $teamRank;
             if (isset($rankings[$configIndex])) {
                 $team = $teamsInGroup[$teamRank - 1];
                 $targetRound = $rankings[$configIndex];
                 $nextRoundTeams[$targetRound][] = $team['id'];
             }
         }
     }
     // create matches
     $matchTable = $websoccer->getConfig('db_prefix') . '_spiel';
     $type = 'Pokalspiel';
     foreach ($nextRoundTeams as $nextRoundId => $teamIds) {
         // get round info
         $result = $db->querySelect('name,firstround_date,secondround_date', $websoccer->getConfig('db_prefix') . '_cup_round', 'id = %d', $nextRoundId);
         $roundInfo = $result->fetch_array();
         $result->free();
         if (!$roundInfo) {
             continue;
         }
         $teams = $teamIds;
         shuffle($teams);
         while (count($teams) > 1) {
             $homeTeam = array_pop($teams);
             $guestTeam = array_pop($teams);
             // create first round
             $db->queryInsert(array('spieltyp' => $type, 'pokalname' => $match->cupName, 'pokalrunde' => $roundInfo['name'], 'home_verein' => $homeTeam, 'gast_verein' => $guestTeam, 'datum' => $roundInfo['firstround_date']), $matchTable);
             // create second round
             if ($roundInfo['secondround_date']) {
                 $db->queryInsert(array('spieltyp' => $type, 'pokalname' => $match->cupName, 'pokalrunde' => $roundInfo['name'], 'home_verein' => $guestTeam, 'gast_verein' => $homeTeam, 'datum' => $roundInfo['secondround_date']), $matchTable);
             }
         }
     }
 }