function OnPageLoad()
    {
        echo "<h1>Statistics for " . htmlentities($this->season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false) . "</h1>";
        require_once "_summary-data-found.php";
        require_once 'xhtml/navigation/tabs.class.php';
        $tabs = array('Summary' => $this->season->GetNavigateUrl());
        if ($this->season->MatchTypes()->Contains(MatchType::LEAGUE)) {
            $tabs['Table'] = $this->season->GetTableUrl();
        }
        if (count($this->season->GetTeams())) {
            $tabs['Map'] = $this->season->GetMapUrl();
        }
        $tabs['Statistics'] = '';
        echo new Tabs($tabs);
        ?>

        <div class="box tab-box">
            <div class="dataFilter"></div>
            <div class="box-content">
        <?php 
        if (!$has_player_stats) {
            echo "<p>There aren't any statistics for the " . htmlentities($this->season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false) . ' yet.</p>
			<p>To find out how to add them, see <a href="/play/manage/website/matches-and-results-why-you-should-add-yours/">Matches and results - why you should add yours</a>.</p>' . "<p>You can also view the <a href=\"" . htmlentities($this->season->GetNavigateUrl(), ENT_QUOTES, "UTF-8", false) . "\">" . htmlentities($this->season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false) . " page</a>.</p>";
        } else {
            require_once "_summary-controls.php";
        }
        # Check for other seasons. Check is >2 becuase current season is there twice - added above
        if (count($this->competition->GetSeasons()) > 2) {
            require_once "stoolball/season-list-control.class.php";
            echo new XhtmlElement('h2', htmlentities('More statistics for the ' . $this->competition->GetName(), ENT_QUOTES, "UTF-8", false));
            $season_list = new SeasonListControl($this->competition->GetSeasons());
            $season_list->SetExcludedSeasons(array($this->season));
            $season_list->SetUrlMethod('GetStatisticsUrl');
            # Override XHTML to add link to competition stats
            $season_list_xhtml = $season_list->__toString();
            $season_list_xhtml = str_replace("<ul>", "<ul><li><a href=\"" . htmlentities($this->competition->GetStatisticsUrl(), ENT_QUOTES, "UTF-8", false) . "\">All seasons</a></li>", (string) $season_list_xhtml);
            echo $season_list_xhtml;
        }
        ?>
        </div>
        </div>
        <?php 
    }
    function OnPageLoad()
    {
        echo new XhtmlElement('h1', Html::Encode('Delete season: ' . $this->data_object->GetCompetitionName()));
        if ($this->deleted) {
            ?>
			<p>The season has been deleted.</p>
			<p><a href="<?php 
            echo Html::Encode($this->data_object->GetCompetition()->GetNavigateUrl());
            ?>
">View <?php 
            echo Html::Encode($this->data_object->GetCompetition()->GetName());
            ?>
</a></p>
			<?php 
        } else {
            # Only offer delete option if there's more than one season. Don't want to delete last season because
            # that leaves an empty competition which won't display. Instead, must delete whole competition with its one remaining season.
            if ($this->has_permission and $this->seasons_in_competition > 1) {
                ?>
				<p>Deleting a season cannot be undone. Matches will not be deleted, but they will no longer be a part of this season.</p>
				<p>Are you sure you want to delete this season?</p>
				<form action="<?php 
                echo Html::Encode($this->data_object->GetDeleteSeasonUrl());
                ?>
" method="post" class="deleteButtons">
				<div>
				<input type="submit" value="Delete season" name="delete" />
				<input type="submit" value="Cancel" name="cancel" />
				</div>
				</form>
				<?php 
                $this->AddSeparator();
                require_once 'stoolball/user-edit-panel.class.php';
                $panel = new UserEditPanel($this->GetSettings(), 'this season');
                $panel->AddLink('view this season', $this->data_object->GetNavigateUrl());
                $panel->AddLink('edit this season', $this->data_object->GetEditSeasonUrl());
                echo $panel;
            } else {
                ?>
				<p>Sorry, you're not allowed to delete this season.</p>
				<p><a href="<?php 
                echo Html::Encode($this->data_object->GetNavigateUrl());
                ?>
">Go back to the season</a></p>
				<?php 
            }
        }
    }
 function OnPostback()
 {
     $this->season = $this->edit->GetDataObject();
     if (!$this->season->GetId()) {
         $existing_season_id = $this->season_manager->CheckIfSeasonExists($this->season->GetCompetition()->GetId(), $this->season->GetStartYear(), $this->season->GetEndYear());
         if ($existing_season_id) {
             require_once 'data/validation/required-field-validator.class.php';
             $season = new Season($this->GetSettings());
             $season->SetId($existing_season_id);
             $validator = new RequiredFieldValidator('This_Validator_Will_Fail', "The season you're adding already exists &#8211; <a href=\"" . $season->GetNavigateUrl() . "\">edit season</a>");
             $validator->SetValidIfNotFound(false);
             $this->edit->AddValidator($validator);
         }
     }
     # Get the competition. This is used to build the page title and, when saving, the short URL.
     # It is also re-indexed in search below.
     $this->competition_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     $this->competition_manager->ReadById(array($this->season->GetCompetition()->GetId()));
     $this->season->SetCompetition($this->competition_manager->GetFirst());
     unset($this->competition_manager);
     # save data if valid
     if ($this->IsValid()) {
         $b_saved_new = !(bool) $this->season->GetId();
         $id = $this->season_manager->SaveSeason($this->season);
         $this->season->SetId($id);
         # Add the competition to the search  engine. Re-request so we have team names as well as IDs.
         require_once "search/competition-search-adapter.class.php";
         $this->SearchIndexer()->DeleteFromIndexById("competition" . $this->season->GetCompetition()->GetId());
         $adapter = new CompetitionSearchAdapter($this->season->GetCompetition());
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         # If just saved a new season, redirect to load this page from scratch
         # (When just loading data on this page, didn't load correctly into aggregated editors)
         if ($b_saved_new) {
             $this->Redirect($this->season->GetEditSeasonUrl());
         }
         $this->Redirect($this->season->GetNavigateUrl());
     }
 }
 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);
 }
 function OnLoadPageData()
 {
     /* @var $o_last_match Match */
     /* @var $season Season */
     /* @var $team Team */
     # First best guess at where user came from is the page field posted back,
     # second best is tournaments page. Either can be tampered with, so there will be
     # a check later before they're used for redirection. If there's a context
     # season or team its URL will be read from the db and overwrite this later.
     if (isset($_POST['page'])) {
         $this->destination_url_if_cancelled = $_POST['page'];
     } else {
         $this->destination_url_if_cancelled = "/tournaments";
     }
     # new data manager
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     $match_manager->FilterByMatchType(array(MatchType::TOURNAMENT));
     # Check whether cancel was clicked
     if ($this->IsPostback() and $this->editor->CancelClicked()) {
         # new tournament, nothing saved yet, so just send user back where they came from
         $this->Cancel();
     }
     # Save match
     if ($this->IsPostback() and $this->IsValid()) {
         # Get posted match
         $this->tournament = $this->editor->GetDataObject();
         # Save match
         $match_manager->SaveFixture($this->tournament);
         if (count($this->tournament->GetAwayTeams())) {
             $match_manager->SaveTeams($this->tournament);
         }
         if ($this->season instanceof Season) {
             $this->tournament->Seasons()->Add($this->season);
             $match_manager->SaveSeasons($this->tournament, true);
         }
         http_response_code(303);
         $this->Redirect($this->tournament->AddTournamentTeamsUrl());
     }
     if (isset($this->season)) {
         $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $season_manager->ReadById(array($this->season->GetId()));
         $this->season = $season_manager->GetFirst();
         $this->editor->SetContextSeason($this->season);
         $this->destination_url_if_cancelled = $this->season->GetNavigateUrl();
         unset($season_manager);
         # If we're adding a match to a season, get last game in season for its date
         $match_manager->ReadLastInSeason($this->season->GetId());
     }
     if (isset($this->team)) {
         # Get more information about the team itself
         require_once 'stoolball/team-manager.class.php';
         $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
         $team_manager->ReadById(array($this->team->GetId()));
         $this->team = $team_manager->GetFirst();
         $this->editor->SetContextTeam($this->team);
         $this->destination_url_if_cancelled = $this->team->GetNavigateUrl();
         # Get the last game already scheduled for the team to use its date
         $match_manager->ReadLastForTeam($this->team->GetId());
         # Read teams played in the last year in order to get their grounds, which will be put at the top of the select list
         $team_manager->ReadRecentOpponents(array($this->team->GetId()), 12);
         $this->editor->ProbableTeams()->SetItems($team_manager->GetItems());
         unset($team_manager);
     }
     # Use the date of the most recent tournament if it was this year, otherwise just use the default of today
     $o_last_match = $match_manager->GetFirst();
     if (is_object($o_last_match) and gmdate('Y', $o_last_match->GetStartTime()) == gmdate('Y')) {
         if ($o_last_match->GetIsStartTimeKnown()) {
             # If the last match has a time, use it
             $this->editor->SetDefaultTime($o_last_match->GetStartTime());
         } else {
             # If the last match has no time, use 11am BST
             $this->editor->SetDefaultTime(gmmktime(10, 0, 0, gmdate('m', $o_last_match->GetStartTime()), gmdate('d', $o_last_match->GetStartTime()), gmdate('Y', $o_last_match->GetStartTime())));
         }
     }
     unset($match_manager);
     # Get grounds
     $o_ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
     $o_ground_manager->ReadAll();
     $a_grounds = $o_ground_manager->GetItems();
     $this->editor->Grounds()->SetItems($a_grounds);
     unset($o_ground_manager);
 }
 function OnLoadPageData()
 {
     /* @var $o_match Match */
     /* @var $o_team Team */
     # new data manager
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     # create repeater control, and save any posted data
     $this->repeater = new DataEditRepeater($this, 'CreateEditControl');
     $this->repeater->SetCssClass('matchResults');
     $this->repeater->SetPersistedParameters(array('team', 'season', "tournament"));
     $this->repeater->SetButtonText('Save all results');
     $this->repeater->SetShowButtonsAtTop(true);
     if ($this->IsPostback() and !$this->IsRefresh() and $this->IsValid()) {
         require_once 'forums/topic-manager.class.php';
         require_once 'forums/review-item.class.php';
         require_once 'forums/subscription-manager.class.php';
         $topic_manager = new TopicManager($this->GetSettings(), $this->GetDataConnection());
         foreach ($this->repeater->GetDataObjects() as $o_current_match) {
             /* @var $o_current_match Match */
             $match_manager->SaveResult($o_current_match);
             $match_manager->ExpandMatchUrl($o_current_match);
             $match_manager->NotifyMatchModerator($o_current_match->GetId());
             if (trim($o_current_match->GetNewComment())) {
                 $item_to_comment_on = new ReviewItem($this->GetSettings());
                 $item_to_comment_on->SetType(ContentType::STOOLBALL_MATCH);
                 $item_to_comment_on->SetId($o_current_match->GetId());
                 $item_to_comment_on->SetNavigateUrl("https://" . $this->GetSettings()->GetDomain() . $o_current_match->GetNavigateUrl());
                 $message = $topic_manager->SaveComment($item_to_comment_on, $o_current_match->GetNewComment());
                 # send subscription emails - new object each time to reset list of who's already recieved an email
                 $subs_manager = new SubscriptionManager($this->GetSettings(), $this->GetDataConnection());
                 $subs_manager->SendCommentsSubscriptions($item_to_comment_on, $message);
                 unset($subs_manager);
             }
         }
         $this->b_saved = true;
     }
     # get matches
     if (!is_null($this->i_team_id)) {
         $a_season_times = Season::SeasonDates();
         $match_manager->FilterByTeam(array($this->i_team_id));
         $match_manager->FilterByDateStart($a_season_times[0]);
         $match_manager->FilterByDateEnd($a_season_times[1]);
         $match_manager->ReadMatchSummaries();
     } else {
         if (!is_null($this->i_season_id)) {
             $match_manager->ReadBySeasonId(array($this->i_season_id), true);
         } else {
             if (!is_null($this->tournament_id)) {
                 $match_manager->FilterByTournament($this->tournament_id);
                 $this->a_matches = $match_manager->ReadMatchSummaries();
             }
         }
     }
     $this->a_matches = $match_manager->GetItems();
     # Make sure we have some matches
     if (count($this->a_matches)) {
         # If it's matches for a team, get the team
         if (!is_null($this->i_team_id)) {
             # Should only need to look at the first match, because the team must be a
             # part of that match in order for the match to be in this list
             $o_match =& $this->a_matches[0];
             $o_team = $o_match->GetHomeTeam();
             if ($o_team instanceof Team and $o_team->GetId() == $this->i_team_id) {
                 $this->team = $o_team;
             } else {
                 foreach ($o_match->GetAwayTeams() as $o_team) {
                     if ($o_team->GetId() == $this->i_team_id) {
                         $this->team = $o_team;
                         break;
                     }
                 }
             }
             if (!is_object($this->team)) {
                 $this->Redirect();
             }
             # Now that we have short URL data, if data has just been saved for team, redirect back to team
             if ($this->b_saved) {
                 $this->Redirect($this->team->GetNavigateUrl());
             }
         } else {
             if (!is_null($this->i_season_id)) {
                 # get details of the season
                 require_once 'stoolball/competition-manager.class.php';
                 $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
                 $o_comp_manager->ReadById(null, array($this->i_season_id));
                 $o_competition = $o_comp_manager->GetFirst();
                 unset($o_comp_manager);
                 if ($o_competition instanceof Competition) {
                     $this->season = $o_competition->GetWorkingSeason();
                 }
                 if (!$this->season instanceof Season) {
                     $this->Redirect();
                 }
                 # Now that we have short URL data, if data has just been saved for season, redirect back to season
                 if ($this->b_saved) {
                     $this->Redirect($this->season->GetNavigateUrl());
                 }
             } else {
                 if (!is_null($this->tournament_id)) {
                     $match_manager->ReadByMatchId(array($this->tournament_id));
                     $this->tournament = $match_manager->GetFirst();
                     if (!is_null($this->tournament)) {
                         # If the tournament has just been saved, now we have its URL so redirect to the thanks page
                         if ($this->b_saved) {
                             $this->Redirect($this->tournament->GetNavigateUrl());
                         }
                     }
                 }
             }
         }
     } else {
         $this->Redirect();
     }
     unset($match_manager);
 }