public function executeAction($parameters)
 {
     // check if feature is enabled
     if (!$this->_websoccer->getConfig("youth_enabled") || !$this->_websoccer->getConfig("youth_matchrequests_enabled")) {
         return NULL;
     }
     $user = $this->_websoccer->getUser();
     $clubId = $user->getClubId($this->_websoccer, $this->_db);
     // check if user has a club
     if ($clubId < 1) {
         throw new Exception($this->_i18n->getMessage("error_action_required_team"));
     }
     // check if date is valid (might be manipulated)
     $tooLateBoundary = $this->_websoccer->getNowAsTimestamp() + 3600 * 24 * (1 + $this->_websoccer->getConfig("youth_matchrequest_max_futuredays"));
     $validTimes = explode(",", $this->_websoccer->getConfig("youth_matchrequest_allowedtimes"));
     // check valid times (remove white spaces)
     $timeIsValid = FALSE;
     $matchTime = date("H:i", $parameters["matchdate"]);
     foreach ($validTimes as $validTime) {
         if ($matchTime == trim($validTime)) {
             $timeIsValid = TRUE;
             break;
         }
     }
     if (!$timeIsValid || $parameters["matchdate"] > $tooLateBoundary) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_create_err_invaliddate"));
     }
     // check maximum number of open requests
     $fromTable = $this->_websoccer->getConfig("db_prefix") . "_youthmatch_request";
     $result = $this->_db->querySelect("COUNT(*) AS hits", $fromTable, "team_id = %d", $clubId);
     $requests = $result->fetch_array();
     $result->free();
     $maxNoOfRequests = (int) $this->_websoccer->getConfig("youth_matchrequest_max_open_requests");
     if ($requests && $requests["hits"] >= $maxNoOfRequests) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_create_err_too_many_open_requests", $maxNoOfRequests));
     }
     // check if reward can be paid
     if ($parameters["reward"]) {
         $team = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $clubId);
         if ($team["team_budget"] <= $parameters["reward"]) {
             throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_create_err_budgetnotenough"));
         }
     }
     // check if enough youth players
     if (YouthPlayersDataService::countYouthPlayersOfTeam($this->_websoccer, $this->_db, $clubId) < 11) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_create_err_notenoughplayers"));
     }
     // check maximum number of matches per day constraint
     $maxMatchesPerDay = $this->_websoccer->getConfig("youth_match_maxperday");
     if (YouthMatchesDataService::countMatchesOfTeamOnSameDay($this->_websoccer, $this->_db, $clubId, $parameters["matchdate"]) >= $maxMatchesPerDay) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_err_maxperday_violated", $maxMatchesPerDay));
     }
     // create request
     $columns = array("team_id" => $clubId, "matchdate" => $parameters["matchdate"], "reward" => $parameters["reward"]);
     $this->_db->queryInsert($columns, $fromTable);
     // create success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("youthteam_matchrequest_create_success"), ""));
     return "youth-matchrequests";
 }
 public function getTemplateParameters()
 {
     $clubId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db);
     $count = YouthMatchesDataService::countMatchesOfTeam($this->_websoccer, $this->_db, $clubId);
     $eps = $this->_websoccer->getConfig("entries_per_page");
     $paginator = new Paginator($count, $eps, $this->_websoccer);
     $matches = YouthMatchesDataService::getMatchesOfTeam($this->_websoccer, $this->_db, $clubId, $paginator->getFirstIndex(), $eps);
     return array("matches" => $matches, "paginator" => $paginator);
 }
 public function executeAction($parameters)
 {
     $user = $this->_websoccer->getUser();
     $teamId = $user->getClubId($this->_websoccer, $this->_db);
     // next match
     $matchinfo = YouthMatchesDataService::getYouthMatchinfoById($this->_websoccer, $this->_db, $this->_i18n, $parameters["matchid"]);
     // check if home or guest team (or else it is an invalid match)
     if ($matchinfo["home_team_id"] == $teamId) {
         $teamPrefix = "home";
     } elseif ($matchinfo["guest_team_id"] == $teamId) {
         $teamPrefix = "guest";
     } else {
         // ID has been entered manually, hence message not important
         throw new Exception($this->_i18n->getMessage(MSG_KEY_ERROR_PAGENOTFOUND));
     }
     // check if expired
     if ($matchinfo["matchdate"] < $this->_websoccer->getNowAsTimestamp() || $matchinfo["simulated"]) {
         throw new Exception($this->_i18n->getMessage("youthformation_err_matchexpired"));
     }
     // get team players and check whether provided IDs are valid players (ceck for duplicate players only, for now)
     $this->validatePlayer($parameters["player1"]);
     $this->validatePlayer($parameters["player2"]);
     $this->validatePlayer($parameters["player3"]);
     $this->validatePlayer($parameters["player4"]);
     $this->validatePlayer($parameters["player5"]);
     $this->validatePlayer($parameters["player6"]);
     $this->validatePlayer($parameters["player7"]);
     $this->validatePlayer($parameters["player8"]);
     $this->validatePlayer($parameters["player9"]);
     $this->validatePlayer($parameters["player10"]);
     $this->validatePlayer($parameters["player11"]);
     $this->validatePlayer($parameters["bench1"]);
     $this->validatePlayer($parameters["bench2"]);
     $this->validatePlayer($parameters["bench3"]);
     $this->validatePlayer($parameters["bench4"]);
     $this->validatePlayer($parameters["bench5"]);
     // validate substitutions
     $validSubstitutions = array();
     for ($subNo = 1; $subNo <= 3; $subNo++) {
         $playerIn = $parameters["sub" . $subNo . "_in"];
         $playerOut = $parameters["sub" . $subNo . "_out"];
         $playerMinute = $parameters["sub" . $subNo . "_minute"];
         if ($playerIn != null && $playerIn > 0 && $playerOut != null && $playerOut > 0 && $playerMinute != null && $playerMinute > 0) {
             $this->validateSubstitution($playerIn, $playerOut, $playerMinute);
             $validSubstitutions[] = $subNo;
         }
     }
     // save formation
     $this->saveFormation($teamId, $parameters, $validSubstitutions, $matchinfo, $teamPrefix);
     // create success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("saved_message_title"), ""));
     return null;
 }
 public function getTemplateParameters()
 {
     $clubId = $this->_websoccer->getUser()->getClubId($this->_websoccer, $this->_db);
     // next match
     $matchinfo = YouthMatchesDataService::getYouthMatchinfoById($this->_websoccer, $this->_db, $this->_i18n, $this->_websoccer->getRequestParameter("matchid"));
     // check if home or guest team (or else it is an invalid match)
     if ($matchinfo["home_team_id"] == $clubId) {
         $teamPrefix = "home";
     } elseif ($matchinfo["guest_team_id"] == $clubId) {
         $teamPrefix = "guest";
     } else {
         // ID has been entered manually, hence message not important
         throw new Exception($this->_i18n->getMessage(MSG_KEY_ERROR_PAGENOTFOUND));
     }
     // check if expired
     if ($matchinfo["matchdate"] <= $this->_websoccer->getNowAsTimestamp() || $matchinfo["simulated"]) {
         throw new Exception($this->_i18n->getMessage("youthformation_err_matchexpired"));
     }
     // get team players
     $players = null;
     if ($clubId > 0) {
         $players = YouthPlayersDataService::getYouthPlayersOfTeamByPosition($this->_websoccer, $this->_db, $clubId, "DESC");
     }
     // get previously saved formation and tactic
     $formation = $this->_getFormation($teamPrefix, $matchinfo);
     // override by request parameters
     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] = "";
             }
         }
     }
     $setup = $this->getFormationSetup($formation);
     for ($playerNo = 1; $playerNo <= 11; $playerNo++) {
         // set player from request
         if ($this->_websoccer->getRequestParameter("player" . $playerNo)) {
             $formation["player" . $playerNo] = $this->_websoccer->getRequestParameter("player" . $playerNo);
             $formation["player" . $playerNo . "_pos"] = $this->_websoccer->getRequestParameter("player" . $playerNo . "_pos");
             // set to 0 if no previous formation is available
         } else {
             if (!isset($formation["player" . $playerNo])) {
                 $formation["player" . $playerNo] = "";
                 $formation["player" . $playerNo . "_pos"] = "";
             }
         }
     }
     return array("matchinfo" => $matchinfo, "players" => $players, "formation" => $formation, "setup" => $setup, "youthFormation" => TRUE);
 }
 public function executeAction($parameters)
 {
     // check if feature is enabled
     if (!$this->_websoccer->getConfig("youth_enabled") || !$this->_websoccer->getConfig("youth_matchrequests_enabled")) {
         return NULL;
     }
     $user = $this->_websoccer->getUser();
     $clubId = $user->getClubId($this->_websoccer, $this->_db);
     // get request info
     $fromTable = $this->_websoccer->getConfig("db_prefix") . "_youthmatch_request";
     $result = $this->_db->querySelect("*", $fromTable, "id = %d", $parameters["id"]);
     $request = $result->fetch_array();
     $result->free();
     if (!$request) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_cancel_err_notfound"));
     }
     // check if own request
     if ($clubId == $request["team_id"]) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_accept_err_ownrequest"));
     }
     // check if team has enough youth players
     if (YouthPlayersDataService::countYouthPlayersOfTeam($this->_websoccer, $this->_db, $clubId) < 11) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_create_err_notenoughplayers"));
     }
     // check maximum number of matches on same day
     $maxMatchesPerDay = $this->_websoccer->getConfig("youth_match_maxperday");
     if (YouthMatchesDataService::countMatchesOfTeamOnSameDay($this->_websoccer, $this->_db, $clubId, $request["matchdate"]) >= $maxMatchesPerDay) {
         throw new Exception($this->_i18n->getMessage("youthteam_matchrequest_err_maxperday_violated", $maxMatchesPerDay));
     }
     $homeTeam = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $request["team_id"]);
     $guestTeam = TeamsDataService::getTeamSummaryById($this->_websoccer, $this->_db, $clubId);
     // deduct/credit transfer reward
     if ($request["reward"]) {
         BankAccountDataService::debitAmount($this->_websoccer, $this->_db, $request["team_id"], $request["reward"], "youthteam_matchrequest_reward_subject", $guestTeam["team_name"]);
         BankAccountDataService::creditAmount($this->_websoccer, $this->_db, $clubId, $request["reward"], "youthteam_matchrequest_reward_subject", $homeTeam["team_name"]);
     }
     // create match
     $this->_db->queryInsert(array("matchdate" => $request["matchdate"], "home_team_id" => $request["team_id"], "guest_team_id" => $clubId), $this->_websoccer->getConfig("db_prefix") . "_youthmatch");
     // delete match request
     $this->_db->queryDelete($fromTable, "id = %d", $parameters["id"]);
     // send notification to user
     NotificationsDataService::createNotification($this->_websoccer, $this->_db, $homeTeam["user_id"], "youthteam_matchrequest_accept_notification", array("team" => $guestTeam["team_name"], "date" => $this->_websoccer->getFormattedDatetime($request["matchdate"])), "youthmatch_accept", "youth-matches", null, $request["team_id"]);
     // create success message
     $this->_websoccer->addFrontMessage(new FrontMessage(MESSAGE_TYPE_SUCCESS, $this->_i18n->getMessage("youthteam_matchrequest_accept_success"), $this->_i18n->getMessage("youthteam_matchrequest_accept_success_details")));
     return "youth-matches";
 }
 /**
  * @see ISimulationObserver::onPenaltyShoot()
  */
 public function onPenaltyShoot(SimulationMatch $match, SimulationPlayer $player, SimulationPlayer $goaly, $successful)
 {
     if ($successful) {
         YouthMatchesDataService::createMatchReportItem($this->_websoccer, $this->_db, $match->id, $match->minute, 'ymreport_penalty_success', array('player' => $player->name), $player->team->id == $match->homeTeam->id);
     } else {
         YouthMatchesDataService::createMatchReportItem($this->_websoccer, $this->_db, $match->id, $match->minute, 'ymreport_penalty_failure', array('player' => $player->name), $player->team->id == $match->homeTeam->id);
     }
 }
 /**
  * Create a match report item.
  * 
  * @see ISimulatorObserver::onSubstitution()
  */
 public function onSubstitution(SimulationMatch $match, SimulationSubstitution $substitution)
 {
     YouthMatchesDataService::createMatchReportItem($this->_websoccer, $this->_db, $match->id, $match->minute, 'ymreport_substitution', array('in' => $substitution->playerIn->name, 'out' => $substitution->playerOut->name), $substitution->playerIn->team->id == $match->homeTeam->id);
 }