public function OnLoadPageData()
 {
     require_once 'stoolball/competition-manager.class.php';
     $manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     if ($this->season_id) {
         $manager->ReadById(null, array($this->season_id));
     } else {
         $manager->ReadById(array($this->competition_id));
     }
     $this->competition = $manager->GetFirst();
     $this->season = $this->competition->GetWorkingSeason();
     unset($manager);
     # If the competition was requested, redirect to the current season
     if ($this->competition_id) {
         http_response_code(303);
         header("Location: " . $this->season->GetMapUrl());
         return;
     }
     $this->has_map = count($this->season->GetTeams());
     # Get other seasons
     require_once 'stoolball/season-manager.class.php';
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_other_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons(array());
     foreach ($a_other_seasons as $season) {
         if ($season->GetId() == $this->season->GetId()) {
             $this->competition->AddSeason($this->season, true);
         } else {
             $this->competition->AddSeason($season, false);
         }
     }
 }
 function OnLoadPageData()
 {
     /* @var $competition Competition */
     # check parameter
     if (isset($_GET['competition']) and is_numeric($_GET['competition'])) {
         $comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
         $comp_manager->ReadById(array($_GET['competition']), null);
         $this->competition = $comp_manager->GetFirst();
         $this->season = $this->competition->GetWorkingSeason();
         unset($comp_manager);
     } else {
         $this->Redirect("/competitions/");
     }
     # must have found a competition
     if (!$this->competition instanceof Competition) {
         $this->Redirect("/competitions/");
     }
     # Get seasons in the competition
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons($a_seasons);
     unset($o_season_manager);
     # Get stats highlights
     $this->statistics["querystring"] = "?competition=" . $this->competition->GetId();
     require_once 'stoolball/statistics/statistics-manager.class.php';
     $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
     $statistics_manager->FilterByCompetition(array($this->competition->GetId()));
     require_once "_summary-data-query.php";
     unset($statistics_manager);
 }
 function OnLoadPageData()
 {
     # get item to be deleted
     if (!is_object($this->data_object)) {
         $id = $this->manager->GetItemId($this->data_object);
         $this->manager->ReadById(array($id));
         $this->data_object = $this->manager->GetFirst();
         # How many seasons in this competition?
         if (is_object($this->data_object) and $this->data_object->GetCompetition() instanceof Competition and $this->data_object->GetCompetition()->GetId()) {
             $this->manager->Clear();
             $this->manager->ReadByCompetitionId(array($this->data_object->GetCompetition()->GetId()));
             $this->seasons_in_competition = $this->manager->GetCount();
         }
     }
     # tidy up
     unset($this->manager);
 }
 function OnLoadPageData()
 {
     /* @var $o_competition Competition */
     # check parameter
     if (!isset($_GET['season']) or !is_numeric($_GET['season'])) {
         http_response_code(400);
         exit;
     }
     if (isset($_GET['season']) and is_numeric($_GET['season'])) {
         $comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
         $comp_manager->ReadById(null, array($_GET['season']));
         $this->competition = $comp_manager->GetFirst();
         unset($comp_manager);
     }
     # must have found a competition
     if (!$this->competition instanceof Competition) {
         http_response_code(404);
         exit;
     }
     $this->season = $this->competition->GetWorkingSeason();
     if (is_object($this->season)) {
         # get matches
         $o_match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
         $o_match_manager->ReadBySeasonId(array($this->season->GetId()));
         $a_matches = $o_match_manager->GetItems();
         $this->season->SetMatches($a_matches);
         # Get other seasons
         $a_comp_ids = array($this->competition->GetId());
         $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $o_season_manager->ReadByCompetitionId($a_comp_ids);
         $a_other_seasons = $o_season_manager->GetItems();
         $this->competition->SetSeasons(array());
         foreach ($a_other_seasons as $season) {
             if ($season->GetId() == $this->season->GetId()) {
                 $this->competition->AddSeason($this->season, true);
             } else {
                 $this->competition->AddSeason($season, false);
             }
         }
         unset($o_season_manager);
     } else {
         # Must have a season
         http_response_code(404);
         exit;
     }
 }
 function OnLoadPageData()
 {
     /* @var $o_competition Competition */
     # check parameter
     if (!isset($_GET['item']) or !is_numeric($_GET['item'])) {
         http_response_code(400);
         exit;
     }
     # new data managers
     $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     # get competition
     $latest = (isset($_GET['latest']) and $_GET['latest'] == '1');
     if ($latest) {
         $o_comp_manager->ReadById(array($_GET['item']), null);
     } else {
         $o_comp_manager->ReadById(null, array($_GET['item']));
     }
     $this->competition = $o_comp_manager->GetFirst();
     $this->season = $this->competition->GetWorkingSeason();
     # must have found a competition
     if (!$this->competition instanceof Competition or !$this->season instanceof Season) {
         http_response_code(404);
         exit;
     }
     # If the competition was requested, redirect to the current season
     if ($latest) {
         http_response_code(303);
         header("Location: " . $this->season->GetNavigateUrl());
         return;
     }
     # Update search engine. Only do this for the latest season as then we have the right teams already.
     if ($this->competition->GetSearchUpdateRequired() and $latest) {
         $this->SearchIndexer()->DeleteFromIndexById("competition" . $this->competition->GetId());
         require_once "search/competition-search-adapter.class.php";
         $adapter = new CompetitionSearchAdapter($this->competition);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         $o_comp_manager->SearchUpdated($this->competition->GetId());
     }
     unset($o_comp_manager);
     # get matches
     $o_match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     $o_match_manager->ReadBySeasonId(array($this->season->GetId()));
     $a_matches = $o_match_manager->GetItems();
     $this->season->SetMatches($a_matches);
     # While we're here, check if there are any outstanding notifications to be sent
     $o_match_manager->NotifyMatchModerator();
     unset($o_match_manager);
     # Get stats highlights
     require_once 'stoolball/statistics/statistics-manager.class.php';
     $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
     $statistics_manager->FilterBySeason(array($this->season->GetId()));
     $statistics_manager->FilterMaxResults(1);
     $this->best_batting = $statistics_manager->ReadBestBattingPerformance();
     $this->best_bowling = $statistics_manager->ReadBestBowlingPerformance();
     $this->most_runs = $statistics_manager->ReadBestPlayerAggregate("runs_scored");
     $this->most_wickets = $statistics_manager->ReadBestPlayerAggregate("wickets");
     $this->most_catches = $statistics_manager->ReadBestPlayerAggregate("catches");
     # See what stats we've got available
     $best_batting_count = count($this->best_batting);
     $best_bowling_count = count($this->best_bowling);
     $best_batters = count($this->most_runs);
     $best_bowlers = count($this->most_wickets);
     $best_catchers = count($this->most_catches);
     $this->has_player_stats = ($best_batting_count or $best_batters or $best_bowling_count or $best_bowlers or $best_catchers);
     if (!$this->has_player_stats) {
         $player_of_match = $statistics_manager->ReadBestPlayerAggregate("player_of_match");
         $this->has_player_stats = (bool) count($player_of_match);
     }
     unset($statistics_manager);
     # Get other seasons
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_other_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons(array());
     foreach ($a_other_seasons as $season) {
         if ($season->GetId() == $this->season->GetId()) {
             $this->competition->AddSeason($this->season, true);
         } else {
             $this->competition->AddSeason($season, false);
         }
     }
     unset($o_season_manager);
 }
 /**
  * @return int
  * @param Competition $o_competition
  * @desc Save the supplied Competition to the database, and return the id
  */
 function SaveCompetition($o_competition)
 {
     # Set up short URL manager
     require_once 'http/short-url-manager.class.php';
     $o_url_manager = new ShortUrlManager($this->GetSettings(), $this->GetDataConnection());
     $new_short_url = $o_url_manager->EnsureShortUrl($o_competition);
     # build query
     $category_id = is_null($o_competition->GetCategory()) ? null : $o_competition->GetCategory()->GetId();
     $allowed_html = array('p', 'br', 'strong', 'em', 'a[href]', 'ul', 'ol', 'li');
     # if no id, it's a new Competition; otherwise update the Competition
     $is_new = !$o_competition->GetId();
     if ($is_new) {
         $s_sql = 'INSERT INTO ' . $this->GetSettings()->GetTable('Competition') . ' SET ' . "competition_name = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetName()) . ", " . "category_id = " . Sql::ProtectNumeric($category_id, true, false) . ', ' . "intro = " . $this->SqlHtmlString($o_competition->GetIntro(), $allowed_html) . ", " . "contact = " . $this->SqlHtmlString($o_competition->GetContact(), $allowed_html) . ", " . "notification_email = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetNotificationEmail()) . ", " . "website = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetWebsiteUrl()) . ", " . 'active = ' . Sql::ProtectBool($o_competition->GetIsActive()) . ', ' . 'player_type_id = ' . Sql::ProtectNumeric($o_competition->GetPlayerType()) . ", " . 'players_per_team = ' . Sql::ProtectNumeric($o_competition->GetMaximumPlayersPerTeam()) . ", " . 'overs = ' . Sql::ProtectNumeric($o_competition->GetOvers()) . ", " . "short_url = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetShortUrl()) . ", " . "update_search = 1, " . 'date_added = ' . gmdate('U') . ', ' . 'date_changed = ' . gmdate('U');
         # run query
         $o_result = $this->GetDataConnection()->query($s_sql);
         # get autonumber
         $o_competition->SetId($this->GetDataConnection()->insertID());
         # create a default season
         require_once 'stoolball/season-manager.class.php';
         $o_season = new Season($this->GetSettings());
         $o_season->SetCompetition($o_competition);
         $o_season->SetStartYear(gmdate('Y', gmdate('U')));
         $o_season->SetEndYear(gmdate('Y', gmdate('U')));
         $o_season->SetIsLatest(true);
         $o_season_mgr = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $o_season_mgr->SaveSeason($o_season);
         unset($o_season_mgr);
     } else {
         $s_sql = 'UPDATE ' . $this->GetSettings()->GetTable('Competition') . ' SET ' . "competition_name = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetName()) . ", " . "category_id = " . Sql::ProtectNumeric($category_id, true, false) . ', ' . "intro = " . $this->SqlHtmlString($o_competition->GetIntro(), $allowed_html) . ", " . "contact = " . $this->SqlHtmlString($o_competition->GetContact(), $allowed_html) . ", " . "notification_email = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetNotificationEmail()) . ", " . "website = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetWebsiteUrl()) . ", " . 'active = ' . Sql::ProtectBool($o_competition->GetIsActive()) . ', ' . 'player_type_id = ' . Sql::ProtectNumeric($o_competition->GetPlayerType()) . ", " . 'players_per_team = ' . Sql::ProtectNumeric($o_competition->GetMaximumPlayersPerTeam()) . ", " . 'overs = ' . Sql::ProtectNumeric($o_competition->GetOvers()) . ", " . "short_url = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetShortUrl()) . ", " . "update_search = 1, " . 'date_changed = ' . gmdate('U') . ' ' . 'WHERE competition_id = ' . Sql::ProtectNumeric($o_competition->GetId());
         # run query
         $this->GetDataConnection()->query($s_sql);
     }
     # Request search update for related objects which mention the competition
     $seasons = array();
     $sql = "SELECT season_id FROM nsa_season WHERE competition_id = " . SQL::ProtectNumeric($o_competition->GetId(), false);
     $result = $this->GetDataConnection()->query($sql);
     while ($row = $result->fetch()) {
         $seasons[] = $row->season_id;
     }
     $result->closeCursor();
     $seasons = implode(", ", $seasons);
     $sql = "UPDATE nsa_team SET update_search = 1 WHERE team_id IN \n                ( \n                    SELECT team_id FROM nsa_team_season WHERE season_id IN ({$seasons})\n                )";
     $this->GetDataConnection()->query($sql);
     $sql = "UPDATE nsa_match SET update_search = 1 WHERE match_id IN \n                ( \n                    SELECT match_id FROM nsa_season_match WHERE season_id IN ({$seasons})\n                )";
     $this->GetDataConnection()->query($sql);
     # Regenerate short URLs
     if (is_object($new_short_url)) {
         $new_short_url->SetParameterValuesFromObject($o_competition);
         $o_url_manager->Save($new_short_url);
         # season URLs are generated from the competition, so regenerate those too
         if (!$is_new) {
             $o_season_mgr = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
             $o_season_mgr->ReadByCompetitionId(array($o_competition->GetId()));
             $seasons = $o_season_mgr->GetItems();
             unset($o_season_mgr);
             foreach ($seasons as $season) {
                 /* @var $season Season */
                 $new_short_url = $o_url_manager->EnsureShortUrl($season, true);
                 if (is_object($new_short_url)) {
                     $s_sql = "UPDATE " . $this->GetSettings()->GetTable('Season') . " SET short_url = " . Sql::ProtectString($this->GetDataConnection(), $new_short_url->GetShortUrl()) . " WHERE season_id = " . Sql::ProtectNumeric($season->GetId());
                     $this->GetDataConnection()->query($s_sql);
                     $new_short_url->SetParameterValuesFromObject($season);
                     $o_url_manager->Save($new_short_url);
                 }
             }
         }
     }
     unset($o_url_manager);
     return $o_competition->GetId();
 }