public static function logout()
 {
     if (array_key_exists("target", $_GET) && $_GET["target"] == "all") {
         UserModel::logoutAll();
     } else {
         UserModel::logout();
     }
     Controller::redirect("");
 }
 public static function delete()
 {
     Controller::requireFields("get", ["id"], "/acp/team");
     Controller::requirePermissions(["AdminAccessDashboard", "AdminTeams", "AdminPlayers", "PerformDeletionOperations"]);
     $team = current(TeamModel::get($_GET["id"]));
     $team->delete();
     Controller::addAlert(new Alert("success", "Team deleted successfully"));
     Controller::redirect("/acp/team");
 }
 public static function delete()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminUsers", "PerformDeletionOperations"]);
     if (!array_key_exists("id", $_GET)) {
         Controller::redirect("/acp/user");
     }
     $users = UserModel::get($_GET["id"]);
     if (!empty($users)) {
         current($users)->delete();
         Controller::addAlert(new Alert("success", "User deleted successfully"));
     } else {
         Controller::addAlert(new Alert("danger", "The user you attempted to delete does not exist"));
     }
     Controller::redirect("/acp/user");
 }
 public static function delete()
 {
     Controller::requirePermissions(["AdminAccessDashboard"]);
     $fixture = current(FixtureModel::get($_GET["id"]));
     if (!$fixture) {
         Controller::addAlert(new Alert("success", "The specified fixture does not exist"));
         Controller::redirect("/acp/league");
     }
     $league = $fixture->getLeague();
     // check permissions
     $visitor = UserModel::getVisitor();
     if ($visitor->id != $league->managerId) {
         Controller::requirePermissions(["AdminAllLeagues"]);
     }
     $fixture->delete();
     Controller::addAlert(new Alert("success", "Fixture deleted successfully"));
     Controller::redirect("/acp/league/manage?id=" . $league->id);
 }
 public static function delete()
 {
     Controller::requirePermissions(["AdminAccessDashboard"]);
     if (!array_key_exists("id", $_GET)) {
         Controller::redirect("/acp/league");
     }
     $section = current(LeagueSectionModel::get($_GET["id"]));
     // check permissions
     $visitor = User::getVisitor();
     if ($visitor->id != $section->getLeague()->managerId) {
         Controller::requirePermissions(["AdminAllLeagues"]);
     }
     try {
         $section->delete();
         Controller::addAlert(new Alert("success", "League section deleted successfully"));
     } catch (ObjectCannotBeDeletedException $e) {
         Controller::addAlert(new Alert("danger", "You cannot delete a section which has teams assigned to it. Please reassign the teams to an alternative section first"));
     }
     Controller::redirect("/acp/league/manage?id=" . $section->getLeague()->id);
 }
 public static function delete()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminUserGroups", "PerformDeletionOperations"]);
     if (!array_key_exists("id", $_GET)) {
         Controller::redirect("/acp/group");
     }
     $group = current(UserGroupModel::get($_GET["id"]));
     if (!$group) {
         Controller::addAlert(new Alert("danger", "The specified group does not exist"));
     } else {
         if ($group->special) {
             Controller::addAlert(new Alert("danger", "The specified group is a special group and cannot be deleted as it would break core functionality"));
         } else {
             if (($count = count($group->getUsers())) > 0) {
                 Controller::addAlert(new Alert("danger", "There are " . $count . " users currently in " . "the specified group, you must assign them to a different group before you can delete this group"));
             } else {
                 $group->delete();
                 Controller::addAlert(new Alert("success", "User group deleted successfully"));
             }
         }
     }
     Controller::redirect("/acp/group");
 }
require_once __DIR__ . "/config.php";
if (!defined("ORGANIZATION_WORD")) {
    define("ORGANIZATION_WORD", "organization");
}
require_once __DIR__ . "/app/Autoloader.php";
require_once __DIR__ . "/vendor/autoload.php";
use sma\Autoloader;
use sma\Router;
use sma\Controller;
use sma\Installer;
date_default_timezone_set(TIMEZONE);
ini_set("display_errors", DISPLAY_ERRORS ? "On" : "Off");
error_reporting(E_ALL);
if (!array_key_exists("PATH_INFO", $_SERVER)) {
    $_SERVER["PATH_INFO"] = "";
}
Autoloader::setup();
Twig_Autoloader::register();
Controller::loadAlertsFromCookie();
$dbStatus = Installer::getDatabaseStatus();
if (strpos($_SERVER["PATH_INFO"], "install") === false) {
    if ($dbStatus == Installer::DATABASE_STATUS_NOT_INSTALLED) {
        Controller::redirect("/install/install");
    } else {
        if ($dbStatus == Installer::DATABASE_STATUS_NOT_UP_TO_DATE) {
            Controller::redirect("/install/upgrade");
        }
    }
}
Router::setRoutes(["/install/install" => ["Installer", "install"], "/install/upgrade" => ["Installer", "upgrade"], "/install" => ["Installer", "index"], "/download/trigger" => ["Download", "trigger"], "/league" => ["League", "index"], "/user/login" => ["User", "login"], "/user/logout" => ["User", "logout"], "/user" => ["User", "index"], "/team/register" => ["Team", "register"], "/team/edit" => ["Team", "edit"], "/team/addplayer" => ["Team", "addplayer"], "/team/updateplayer" => ["Team", "updateplayer"], "/match/submitted" => ["Match", "submitted"], "/match/submit" => ["Match", "submit"], "/match/record" => ["Match", "record"], "/ajax/teams" => ["Ajax", "teams"], "/ajax/players" => ["Ajax", "players"], "/acp/match/manage" => ["acp\\Match", "manage"], "/acp/match/delete" => ["acp\\Match", "delete"], "/acp/match/correct" => ["acp\\Match", "correct"], "/acp/match/alter" => ["acp\\Match", "alter"], "/acp/match" => ["acp\\Match", "index"], "/acp/organization/add" => ["acp\\Organization", "add"], "/acp/organization/edit" => ["acp\\Organization", "edit"], "/acp/organization/delete" => ["acp\\Organization", "delete"], "/acp/organization" => ["acp\\Organization", "index"], "/acp/user/add" => ["acp\\User", "add"], "/acp/user/edit" => ["acp\\User", "edit"], "/acp/user/delete" => ["acp\\User", "delete"], "/acp/user" => ["acp\\User", "index"], "/acp/group/add" => ["acp\\UserGroup", "add"], "/acp/group/manage" => ["acp\\UserGroup", "manage"], "/acp/group/delete" => ["acp\\UserGroup", "delete"], "/acp/group" => ["acp\\UserGroup", "index"], "/acp/league/add" => ["acp\\League", "add"], "/acp/league/manage" => ["acp\\League", "manage"], "/acp/league/delete" => ["acp\\League", "delete"], "/acp/league" => ["acp\\League", "index"], "/acp/section/add" => ["acp\\LeagueSection", "add"], "/acp/section/delete" => ["acp\\LeagueSection", "delete"], "/acp/team/assign" => ["acp\\Team", "assign"], "/acp/team/manage" => ["acp\\Team", "manage"], "/acp/team/delete" => ["acp\\Team", "delete"], "/acp/team" => ["acp\\Team", "index"], "/acp/player/add" => ["acp\\Player", "add"], "/acp/player/delete" => ["acp\\Player", "delete"], "/acp/player/update" => ["acp\\Player", "update"], "/acp/player/exempt" => ["acp\\Player", "exempt"], "/acp/fixture/add" => ["acp\\Fixture", "add"], "/acp/fixture/delete" => ["acp\\Fixture", "delete"], "/acp" => ["acp\\Dashboard", "index"], "" => ["Index", "index"]]);
Router::route();
 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);
 }
 public static function alter()
 {
     Controller::requirePermissions(["AdminAccessDashboard", "AdminMatches"]);
     $id = $_POST["id"];
     $match = current(MatchModel::get($_POST["id"]));
     if (array_key_exists("date", $_POST)) {
         try {
             $id = $match->correctDate($match->id, $_POST["date"]);
             Controller::addAlert(new Alert("success", "Correction completed"));
         } catch (DuplicateException $e) {
             Controller::addAlert(new Alert("danger", "The report cannot be moved to the specified date as there is already another report filed for the team for the match on that date"));
         }
     } else {
         if (array_key_exists("home_team_id", $_POST)) {
             try {
                 $homeTeamId = $_POST["home_team_id"] != 0 ? $_POST["home_team_id"] : null;
                 $awayTeamId = $_POST["away_team_id"] != 0 ? $_POST["away_team_id"] : null;
                 $id = $match->correctTeams($match->id, $homeTeamId, $awayTeamId);
                 Controller::addAlert(new Alert("success", "Correction completed"));
             } catch (DuplicateException $e) {
                 Controller::addAlert(new Alert("danger", "The report cannot be updated with those team(s) as there is already another report filed for the team for the match on that date"));
             }
         }
     }
     Controller::redirect("/acp/match/manage?id=" . $id);
 }
 public static function index()
 {
     Controller::redirect("/install/install");
 }
 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);
     }
 }