/**
  * Get league
  *
  * @return \sma\models\User
  */
 public function getLeague()
 {
     if (!$this->league) {
         $this->league = current(League::get($this->leagueId));
     }
     return $this->league;
 }
 public static function register()
 {
     Controller::requirePermissions(["RegisterTeamsForOwnOrganization", "RegisterTeamsForAnyOrganization"], "any");
     if (empty($_POST)) {
         $teams = User::getVisitor()->organizationId ? TeamModel::get(null, User::getVisitor()->organizationId) : null;
         View::load("team/register_form.twig", ["organizations" => Organization::get(), "designations" => TeamModel::getValidDesignations(), "leagues" => League::get(), "teams" => $teams]);
     } else {
         // add the team
         if (User::getVisitor()->checkPermissions(["RegisterTeamsForAnyOrganization"])) {
             $organizationId = $_POST["organization-id"];
         } else {
             $organizationId = User::getVisitor()->organizationId;
         }
         try {
             if (ALLOW_TEAM_REGISTRANTS_TO_SELECT_LEAGUE) {
                 $teamId = TeamModel::add($organizationId, $_POST["designation"], User::getVisitor()->id, $_POST["league-id"]);
             } else {
                 $teamId = TeamModel::add($organizationId, $_POST["designation"], User::getVisitor()->id);
             }
         } catch (DuplicateException $e) {
             Controller::addAlert(new Alert("danger", "You cannot register more than one team with the same name. " . "To edit an existing team please use the edit button beside the team in the Registered Teams box."));
             Controller::redirect("/team/register");
         }
         // add the players
         $exemptsAdded = 0;
         for ($i = 1; array_key_exists("player" . $i, $_POST); $i++) {
             if ($_POST["player" . $i]) {
                 if (isset($_POST["player" . $i . "e"])) {
                     if ($exemptsAdded < MAX_EXEMPTS) {
                         $makeExempt = true;
                         $exemptsAdded++;
                     } else {
                         $makeExempt = false;
                         Controller::addAlert(new Alert("warning", "You attempted to star " . $_POST["player" . $i] . " but you had already starred " . MAX_EXEMPTS . " other players, which is the maxmimum allowed, thus " . $_POST["player" . $i] . "was not starred"));
                     }
                 } else {
                     $makeExempt = false;
                 }
                 try {
                     Player::add($_POST["player" . $i], $teamId, $makeExempt);
                 } catch (DuplicateException $e) {
                     Controller::addAlert(new Alert("info", "You entered the name " . $_POST["player" . $i] . " more than once, only the first entry was" . "added to the database"));
                 }
             }
         }
         Controller::addAlert(new Alert("success", "You have successfully registered your team and its details are shown below. You can come back to this area up until the freeze date and make changes."));
         Controller::redirect("/team/edit?id=" . $teamId);
     }
 }
 public static function manage()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminTeams"]);
     if (!empty($_POST)) {
         if (!array_key_exists("section", $_POST)) {
             $_POST["section"] = null;
         }
         TeamModel::update($_POST["id"], null, $_POST["designation"], $_POST["section"], $_POST["league"]);
         Controller::addAlert(new Alert("success", "Team details updated successfully"));
     }
     $team = current(TeamModel::get($_GET["id"]));
     $sections = $team->leagueId ? LeagueSection::get(null, $team->leagueId) : null;
     $data = $team->constructMatchParticipationData();
     View::load("acp/team_manage.twig", ["team" => $team, "leagues" => League::get(), "sections" => $sections, "matches" => $data->matches, "players" => $data->players]);
 }
 public static function delete()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminAllLeagues", "PerformDeletionOperations"]);
     if (!array_key_exists("id", $_GET)) {
         Controller::redirect("/acp/league");
     }
     $leagues = LeagueModel::get($_GET["id"]);
     if (!empty($leagues)) {
         current($leagues)->delete();
         Controller::addAlert(new Alert("success", "League deleted successfully"));
     } else {
         Controller::addAlert(new Alert("danger", "The league you attempted to delete does not exist"));
     }
     Controller::redirect("/acp/league");
 }
 public static function add()
 {
     Controller::requirePermissions(["AdminAccessDashboard"]);
     if (empty($_POST)) {
         Controller::redirect("/acp/league");
     }
     $league = current(League::get($_POST["league_id"]));
     // check permissions
     $visitor = User::getVisitor();
     if ($visitor->id != $league->managerId) {
         Controller::requirePermissions(["AdminAllLeagues"]);
     }
     LeagueSectionModel::add($_POST["league_id"]);
     Controller::addAlert(new Alert("success", "League section added successfully"));
     Controller::redirect("/acp/league/manage?id=" . $_POST["league_id"]);
 }
 public static function add()
 {
     Controller::requireFields("post", ["date", "type"], "/acp/league/manage?id=" . $_POST["league"]);
     Controller::requirePermissions(["AdminAccessDashboard"]);
     // check permissions
     $visitor = UserModel::getVisitor();
     if ($visitor->id != current(LeagueModel::get($_POST["league"]))->managerId) {
         Controller::requirePermissions(["AdminAllLeagues"]);
     }
     // check date
     $dt = \DateTime::createFromFormat("Y-m-d", $_POST["date"]);
     if ($dt === false || array_sum($dt->getLastErrors())) {
         Controller::addAlert(new Alert("danger", "The provided date was invalid"));
         Controller::redirect("/acp/league/manage?id=" . $_POST["league"]);
     }
     FixtureModel::add($_POST["type"], $_POST["date"], $_POST["league"], $_POST["home-team-id"], $_POST["away-team-id"], $_POST["home-team-number"], $_POST["away-team-number"]);
     Controller::addAlert(new Alert("success", "Fixture added successfully"));
     Controller::redirect("/acp/league/manage?id=" . $_POST["league"]);
 }
 public static function index()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminMatches"]);
     $reports = MatchReport::get();
     if (array_key_exists("league", $_GET) && $_GET["league"] != 0) {
         foreach ($reports as $key => $report) {
             if ($reports[$key]->getMatch()->leagueId != $_GET["league"]) {
                 unset($reports[$key]);
             }
         }
     }
     if (array_key_exists("status", $_GET) && $_GET["status"] !== "") {
         foreach ($reports as $key => $report) {
             if ($reports[$key]->getMatch()->status != $_GET["status"]) {
                 unset($reports[$key]);
             }
         }
     }
     View::load("acp/match.twig", ["objects" => $reports, "leagues" => League::get(), "selectedLeagueId" => array_key_exists("league", $_GET) ? $_GET["league"] : 0, "selectedStatus" => array_key_exists("status", $_GET) && $_GET["status"] !== '' ? $_GET["status"] : -1]);
 }
 public static function index()
 {
     $league = current(LeagueModel::get($_GET["id"]));
     View::load("league.twig", ["league" => $league, "fixtures" => $league->constructFixtures(), "matches" => Match::get(null, null, $league->id, null, null, Match::STATUS_RECONCILED, 10)]);
 }
 /**
  * Get league
  *
  * @return \sma\models\League league
  */
 public function getLeague()
 {
     return current(League::get($this->leagueId));
 }
 public static function index()
 {
     $includeRestricted = false;
     View::load("index.twig", ["info" => Setting::get("info_box_content"), "leagues" => League::get(), "downloads" => Download::get(null, Download::TYPE_HOMEPAGE, null, $includeRestricted)]);
 }
 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);
     }
 }