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 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";
 }