/**
  * (non-PHPdoc)
  * @see IModel::getTemplateParameters()
  */
 public function getTemplateParameters()
 {
     $matchId = (int) $this->_websoccer->getRequestParameter('id');
     if ($matchId < 1) {
         throw new Exception($this->_i18n->getMessage(MSG_KEY_ERROR_PAGENOTFOUND));
     }
     $match = MatchesDataService::getMatchSubstitutionsById($this->_websoccer, $this->_db, $matchId);
     if ($match['match_simulated']) {
         throw new Exception($this->_i18n->getMessage('match_details_match_completed'));
     }
     $teamId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db);
     if ($match['match_home_id'] !== $teamId && $match['match_guest_id'] !== $teamId) {
         $teamId = NationalteamsDataService::getNationalTeamManagedByCurrentUser($this->_websoccer, $this->_db);
     }
     if ($teamId !== $match['match_home_id'] && $match['match_guest_id'] !== $teamId) {
         throw new Exception('illegal match');
     }
     $teamPrefix = $teamId == $match['match_home_id'] ? 'home' : 'guest';
     $players = MatchesDataService::getMatchPlayerRecordsByField($this->_websoccer, $this->_db, $matchId, $teamId);
     $playersOnField = $players['field'];
     $playersOnBench = isset($players['bench']) ? $players['bench'] : array();
     $formation = array();
     if ($this->_websoccer->getRequestParameter('freekickplayer')) {
         $formation['freekickplayer'] = $this->_websoccer->getRequestParameter('freekickplayer');
     } else {
         $formation['freekickplayer'] = $match['match_' . $teamPrefix . '_freekickplayer'];
     }
     if ($this->_websoccer->getRequestParameter('offensive')) {
         $formation['offensive'] = $this->_websoccer->getRequestParameter('offensive');
     } else {
         $formation['offensive'] = $match['match_' . $teamPrefix . '_offensive'];
     }
     if ($this->_websoccer->getRequestParameter('longpasses')) {
         $formation['longpasses'] = $this->_websoccer->getRequestParameter('longpasses');
     } else {
         $formation['longpasses'] = $match['match_' . $teamPrefix . '_longpasses'];
     }
     if ($this->_websoccer->getRequestParameter('counterattacks')) {
         $formation['counterattacks'] = $this->_websoccer->getRequestParameter('counterattacks');
     } else {
         $formation['counterattacks'] = $match['match_' . $teamPrefix . '_counterattacks'];
     }
     // get existing formation
     $playerNo = 0;
     foreach ($playersOnField as $player) {
         $playerNo++;
         $formation['player' . $playerNo] = $player['id'];
         $formation['player' . $playerNo . '_pos'] = $player['match_position_main'];
     }
     // set setup
     $setup = array('defense' => 6, 'dm' => 3, 'midfield' => 4, 'om' => 3, 'striker' => 2, 'outsideforward' => 2);
     $setupMainMapping = array('LV' => 'defense', 'RV' => 'defense', 'IV' => 'defense', 'DM' => 'dm', 'LM' => 'midfield', 'ZM' => 'midfield', 'RM' => 'midfield', 'OM' => 'om', 'LS' => 'outsideforward', 'MS' => 'striker', 'RS' => 'outsideforward');
     $setupPosMapping = array('Abwehr' => 'defense', 'Mittelfeld' => 'midfield', 'Sturm' => 'striker');
     // override formation by user input and count setup
     for ($playerNo = 1; $playerNo <= 11; $playerNo++) {
         if ($this->_websoccer->getRequestParameter('player' . $playerNo) > 0) {
             $formation['player' . $playerNo] = $this->_websoccer->getRequestParameter('player' . $playerNo);
             $formation['player' . $playerNo . '_pos'] = $this->_websoccer->getRequestParameter('player' . $playerNo . '_pos');
         }
     }
     // bench
     $benchNo = 0;
     foreach ($playersOnBench as $player) {
         $benchNo++;
         $formation['bench' . $benchNo] = $player['id'];
     }
     for ($benchNo = 1; $benchNo <= 5; $benchNo++) {
         if ($this->_websoccer->getRequestParameter('bench' . $benchNo)) {
             $formation['bench' . $benchNo] = $this->_websoccer->getRequestParameter('bench' . $benchNo);
         } else {
             if (!isset($formation['bench' . $benchNo])) {
                 $formation['bench' . $benchNo] = '';
             }
         }
     }
     // subs
     for ($subNo = 1; $subNo <= 3; $subNo++) {
         if ($this->_websoccer->getRequestParameter('sub' . $subNo . '_out')) {
             $formation['sub' . $subNo . '_out'] = $this->_websoccer->getRequestParameter('sub' . $subNo . '_out');
             $formation['sub' . $subNo . '_in'] = $this->_websoccer->getRequestParameter('sub' . $subNo . '_in');
             $formation['sub' . $subNo . '_minute'] = $this->_websoccer->getRequestParameter('sub' . $subNo . '_minute');
             $formation['sub' . $subNo . '_condition'] = $this->_websoccer->getRequestParameter('sub' . $subNo . '_condition');
             $formation['sub' . $subNo . '_position'] = $this->_websoccer->getRequestParameter('sub' . $subNo . '_position');
         } else {
             if (isset($match[$teamPrefix . '_sub' . $subNo . '_out'])) {
                 $formation['sub' . $subNo . '_out'] = $match[$teamPrefix . '_sub' . $subNo . '_out'];
                 $formation['sub' . $subNo . '_in'] = $match[$teamPrefix . '_sub' . $subNo . '_in'];
                 $formation['sub' . $subNo . '_minute'] = $match[$teamPrefix . '_sub' . $subNo . '_minute'];
                 $formation['sub' . $subNo . '_condition'] = $match[$teamPrefix . '_sub' . $subNo . '_condition'];
                 $formation['sub' . $subNo . '_position'] = $match[$teamPrefix . '_sub' . $subNo . '_position'];
             } else {
                 $formation['sub' . $subNo . '_out'] = '';
                 $formation['sub' . $subNo . '_in'] = '';
                 $formation['sub' . $subNo . '_minute'] = '';
                 $formation['sub' . $subNo . '_condition'] = '';
                 $formation['sub' . $subNo . '_position'] = '';
             }
         }
     }
     return array('setup' => $setup, 'players' => $players, 'formation' => $formation, 'minute' => $match['match_minutes']);
 }
 /**
  * (non-PHPdoc)
  * @see IActionController::executeAction()
  */
 public function executeAction($parameters)
 {
     $user = $this->_websoccer->getUser();
     $teamId = $user->getClubId($this->_websoccer, $this->_db);
     $nationalTeamId = NationalteamsDataService::getNationalTeamManagedByCurrentUser($this->_websoccer, $this->_db);
     $matchId = $parameters['id'];
     // check and get match data
     $matchinfo = MatchesDataService::getMatchSubstitutionsById($this->_websoccer, $this->_db, $matchId);
     if (!isset($matchinfo['match_id'])) {
         throw new Exception($this->_i18n->getMessage('formation_err_nonextmatch'));
     }
     // check whether user is one of the team managers
     if ($matchinfo['match_home_id'] != $teamId && $matchinfo['match_guest_id'] != $teamId && $matchinfo['match_home_id'] != $nationalTeamId && $matchinfo['match_guest_id'] != $nationalTeamId) {
         throw new Exception('nice try');
     }
     // is already completed?
     if ($matchinfo['match_simulated']) {
         throw new Exception($this->_i18n->getMessage('match_details_match_completed'));
     }
     // update match fields
     $columns = array();
     $teamPrefix = $matchinfo['match_home_id'] == $teamId || $matchinfo['match_home_id'] == $nationalTeamId ? 'home' : 'guest';
     $teamPrefixDb = $matchinfo['match_home_id'] == $teamId || $matchinfo['match_home_id'] == $nationalTeamId ? 'home' : 'gast';
     // consider already executed subs
     $occupiedSubPos = array();
     $existingFutureSubs = array();
     for ($subNo = 1; $subNo <= 3; $subNo++) {
         $existingMinute = (int) $matchinfo[$teamPrefix . '_sub' . $subNo . '_minute'];
         if ($existingMinute > 0 && $existingMinute <= $matchinfo['match_minutes']) {
             $occupiedSubPos[$subNo] = TRUE;
         } elseif ($existingMinute > 0) {
             $existingFutureSubs[$matchinfo[$teamPrefix . '_sub' . $subNo . '_out']] = array('minute' => $matchinfo[$teamPrefix . '_sub' . $subNo . '_minute'], 'in' => $matchinfo[$teamPrefix . '_sub' . $subNo . '_in'], 'condition' => $matchinfo[$teamPrefix . '_sub' . $subNo . '_condition'], 'position' => $matchinfo[$teamPrefix . '_sub' . $subNo . '_position'], 'slot' => $subNo);
         }
     }
     // save subs
     if (count($occupiedSubPos) < 3) {
         // a substitution must be announced at least number of minutes of interval, otherwise no chance of execution
         $nextPossibleMinute = $matchinfo['match_minutes'] + $this->_websoccer->getConfig('sim_interval') + 1;
         for ($subNo = 1; $subNo <= 3; $subNo++) {
             $newOut = (int) $parameters['sub' . $subNo . '_out'];
             $newIn = (int) $parameters['sub' . $subNo . '_in'];
             $newMinute = (int) $parameters['sub' . $subNo . '_minute'];
             $newCondition = $parameters['sub' . $subNo . '_condition'];
             $newPosition = $parameters['sub' . $subNo . '_position'];
             $slot = FALSE;
             $saveSub = TRUE;
             // replace existing sub
             if (isset($existingFutureSubs[$newOut]) && $newIn == $existingFutureSubs[$newOut]['in'] && $newCondition == $existingFutureSubs[$newOut]['condition'] && $newMinute == $existingFutureSubs[$newOut]['minute'] && $newPosition == $existingFutureSubs[$newOut]['position']) {
                 $saveSub = FALSE;
             }
             // get first free slot
             for ($slotNo = 1; $slotNo <= 3; $slotNo++) {
                 if (!isset($occupiedSubPos[$slotNo])) {
                     $slot = $slotNo;
                     break;
                 }
             }
             if ($slot && $newOut && $newIn && $newMinute) {
                 if ($saveSub && $newMinute < $nextPossibleMinute) {
                     $newMinute = $nextPossibleMinute;
                     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_WARNING, '', $this->_i18n->getMessage('match_details_changes_too_late_altered', $subNo)));
                 }
                 $columns[$teamPrefixDb . '_w' . $slot . '_raus'] = $newOut;
                 $columns[$teamPrefixDb . '_w' . $slot . '_rein'] = $newIn;
                 $columns[$teamPrefixDb . '_w' . $slot . '_minute'] = $newMinute;
                 $columns[$teamPrefixDb . '_w' . $slot . '_condition'] = $newCondition;
                 $columns[$teamPrefixDb . '_w' . $slot . '_position'] = $newPosition;
                 $occupiedSubPos[$slot] = TRUE;
             }
         }
     }
     // update tactics
     $prevOffensive = $matchinfo['match_' . $teamPrefix . '_offensive'];
     $prevLongpasses = $matchinfo['match_' . $teamPrefix . '_longpasses'];
     $prevCounterattacks = $matchinfo['match_' . $teamPrefix . '_counterattacks'];
     if (!$prevLongpasses) {
         $prevLongpasses = '0';
     }
     if (!$prevCounterattacks) {
         $prevCounterattacks = '0';
     }
     if ($prevOffensive !== $parameters['offensive'] || $prevLongpasses !== $parameters['longpasses'] || $prevCounterattacks !== $parameters['counterattacks']) {
         $alreadyChanged = $matchinfo['match_' . $teamPrefix . '_offensive_changed'];
         if ($alreadyChanged >= $this->_websoccer->getConfig('sim_allow_offensivechanges')) {
             throw new Exception($this->_i18n->getMessage('match_details_changes_too_often', $this->_websoccer->getConfig('sim_allow_offensivechanges')));
         }
         $columns[$teamPrefixDb . '_offensive'] = $parameters['offensive'];
         $columns[$teamPrefixDb . '_longpasses'] = $parameters['longpasses'];
         $columns[$teamPrefixDb . '_counterattacks'] = $parameters['counterattacks'];
         $columns[$teamPrefixDb . '_offensive_changed'] = $alreadyChanged + 1;
         $this->_createMatchReportMessage($user, $matchId, $matchinfo['match_minutes'], $teamPrefix == 'home');
     }
     // free kick taker
     $prevFreekickPlayer = $matchinfo['match_' . $teamPrefix . '_freekickplayer'];
     if ($parameters['freekickplayer'] && $parameters['freekickplayer'] != $prevFreekickPlayer) {
         $columns[$teamPrefixDb . '_freekickplayer'] = $parameters['freekickplayer'];
     }
     // execute update
     if (count($columns)) {
         $fromTable = $this->_websoccer->getConfig('db_prefix') . '_spiel';
         $whereCondition = 'id = %d';
         $this->_db->queryUpdate($columns, $fromTable, $whereCondition, $matchId);
     }
     $this->_updatePlayerPosition($parameters, $matchId, $teamId);
     // create success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage('saved_message_title'), ''));
     return "match";
 }