public static function delete() { Controller::requireFields("get", ["id"], "/acp/team"); Controller::requirePermissions(["AdminAccessDashboard", "AdminTeams", "AdminPlayers", "PerformDeletionOperations"]); $player = current(PlayerModel::get($_GET["id"])); $player->delete(); Controller::addAlert(new Alert("success", "Player deleted successfully")); Controller::redirect("/acp/team/manage?id=" . $player->teamId); }
public static function players() { $teamId = array_key_exists("team", $_GET) ? $_GET["team"] : null; $players = Player::get(null, null, $teamId); $return = []; foreach ($players as $player) { $data = new \stdClass(); $data->id = $player->id; $data->name = $player->fullName; $return[] = $data; } echo json_encode($return); }
/** * Get all players who have played for this team at any point during the season or are registered * as a part of it * * @return \sma\models\Player[] */ public function getAllRelatedPlayers() { $players = $this->getPlayers(); $q = (new SelectQuery(Database::getConnection()))->from("matches_players")->fields(["player_id"])->where("team_id = ?", $this->id); $stmt = $q->prepare(); $stmt->execute(); $participatingPlayerIds = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); $participatingPlayerIds = array_unique($participatingPlayerIds, SORT_NUMERIC); $additionalPlayers = Player::get($participatingPlayerIds); foreach ($additionalPlayers as $player) { if (!array_key_exists($player->id, $players)) { $players[] = $player; } } return $players; }
public static function updateplayer() { Controller::requireFields("get", ["id"], "/acp/team"); $player = current(Player::get($_GET["id"])); if (!User::getVisitor()->checkPermissions(["RegisterTeamsForAnyOrganization"])) { Controller::requirePermissions(["RegisterTeamsForOwnOrganization"]); if ($player->getTeam()->organizationId != User::getVisitor()->organizationId) { ErrorHandler::forbidden(); } } if ($_GET["exempt"] == 1 && !$player->exempt) { if ($player->getTeam()->getNumberOfExemptPlayers() >= MAX_EXEMPTS) { Controller::addAlert(new Alert("danger", "You have already starred the maximum number of players")); Controller::redirect("/team/edit?id=" . $player->getTeam()->id); } } Player::update($player->id, null, (bool) $_GET["exempt"]); Controller::addAlert(new Alert("success", "Player updated successfully")); Controller::redirect("/team/edit?id=" . $player->getTeam()->id); }
/** * Add player to match * * @param int $teamId team * @param int $playerId player id * @param int $playerName player name if id unknown/inapplicable */ public function addParticipatingPlayer($teamId, $playerId, $playerName = null) { if (!$playerId) { $players = Player::get(null, $playerName); if ($players) { $playerId = current($players)->id; } else { $playerId = Player::add($playerName, null, false); } } (new InsertQuery(Database::getConnection()))->into("matches_players")->fields(["match_id", "team_id", "player_id"])->values("(?,?,?)", [$this->id, $teamId, $playerId])->prepare()->execute(); }
public static function submit() { Controller::requirePermissions(["SubmitMatchReports"]); if (empty($_POST)) { View::load("match/submit.twig", ["leagues" => League::get(), "players" => Player::get()]); } else { // basic input validation Controller::requireFields("post", ["date", "league", "reporter-team", "reporter-score", "opposing-team", "opposing-score"], "/match/submit"); $datetime = DateTime::createFromFormat("Y-m-d", $_POST["date"]); $epoch = $datetime->getTimestamp(); if ($datetime === false || array_sum($datetime->getLastErrors()) || $epoch > time() || time() - $epoch > 3600 * 24 * 365) { Controller::addAlert(new Alert("danger", "You did not enter a valid date, please try again.")); Controller::redirect("/match/submit"); } // check authorization of user to file reports on behalf of reporting team $reporterTeam = current(Team::get($_POST["reporter-team"])); $visitor = User::getVisitor(); if ($visitor->organizationId != $reporterTeam->organizationId) { Controller::requirePermissions(["SubmitMatchReportsForAnyTeam"]); } // start determining the data for insertion if ($_POST["location"] == "home") { // reporting team is home $homeTeamId = $_POST["reporter-team"]; $homeScore = $_POST["reporter-score"]; $awayTeamId = $_POST["opposing-team"]; $awayScore = $_POST["opposing-score"]; } else { $awayTeamId = $_POST["reporter-team"]; $awayScore = $_POST["reporter-score"]; $homeTeamId = $_POST["opposing-team"]; $homeScore = $_POST["opposing-score"]; } // transaction Database::getConnection()->beginTransaction(); // attempt to pull an existing match record or add a new one $match = current(MatchModel::get(null, $_POST["date"], $_POST["league"], $homeTeamId, $awayTeamId)); if ($match) { $matchId = $match->id; } else { $matchId = MatchModel::add($_POST["date"], $_POST["league"], $homeTeamId, $awayTeamId); } try { MatchReport::add($matchId, $_POST["reporter-team"], $visitor->id, $homeScore, $awayScore); } catch (DuplicateException $e) { Database::getConnection()->rollBack(); Controller::addAlert(new Alert("danger", "You have already submitted a report for that match!")); Controller::redirect("/match/submit"); } if (!$match) { $match = current(MatchModel::get($matchId)); } $players = $reporterTeam->getPlayers(); foreach ($players as $player) { if (array_key_exists("player" . $player->id, $_POST)) { $match->addParticipatingPlayer($reporterTeam->id, $player->id); } } for ($i = 1; $i <= 8; $i++) { if (array_key_exists("additional-player" . $i, $_POST) && $_POST["additional-player" . $i]) { $match->addParticipatingPlayer($reporterTeam->id, null, $_POST["additional-player" . $i]); } } // commit Database::getConnection()->commit(); // attempt reconciliation $matches = MatchModel::get($matchId); current($matches)->attemptReportReconciliation(); Controller::addAlert(new Alert("success", "Match report submitted successfully!")); Controller::redirect("/match/record?id=" . $matchId); } }