function OnPreRender()
 {
     /* @var $o_team Team */
     /* @var $o_match Match */
     $a_data_to_bind = array();
     $i_played = 1;
     $i_won = 2;
     $i_lost = 3;
     $i_tie = 4;
     $i_noresult = 5;
     $i_runs_for = 6;
     $i_runs_against = 7;
     $i_points = 8;
     # Build an array of teams, and initiate an array of data for each team
     foreach ($this->o_season->GetTeams() as $o_team) {
         if ($o_team instanceof Team and !is_object($this->o_season->TeamsWithdrawnFromLeague()->GetItemByProperty('GetId', $o_team->GetId()))) {
             $a_team_data = array();
             $a_team_data[0] = new XhtmlAnchor(htmlentities($o_team->GetName(), ENT_QUOTES, "UTF-8", false), $o_team->GetNavigateUrl());
             $a_team_data[$i_played] = 0;
             $a_team_data[$i_won] = 0;
             $a_team_data[$i_lost] = 0;
             $a_team_data[$i_tie] = 0;
             $a_team_data[$i_noresult] = 0;
             if ($this->o_season->GetShowTableRunsScored()) {
                 $a_team_data[$i_runs_for] = 0;
             }
             if ($this->o_season->GetShowTableRunsConceded()) {
                 $a_team_data[$i_runs_against] = 0;
             }
             $a_team_data[$i_points] = 0;
             $a_data_to_bind[$o_team->GetId()] =& $a_team_data;
             unset($a_team_data);
         }
     }
     # Look at matches to build data for each team
     foreach ($this->o_season->GetMatches() as $o_match) {
         # Discount matches in the future
         if ($o_match->GetStartTime() >= gmdate('U')) {
             break;
         }
         # Discount non-league matches
         if ($o_match->GetMatchType() != MatchType::LEAGUE) {
             continue;
         }
         # Discount postponed matches
         if ($o_match->Result()->GetResultType() == MatchResult::POSTPONED) {
             continue;
         }
         # Discount matches where a team has withdrawn from the league
         if (is_object($this->o_season->TeamsWithdrawnFromLeague()->GetItemByProperty('GetId', $o_match->GetHomeTeamId()))) {
             continue;
         }
         if (is_object($this->o_season->TeamsWithdrawnFromLeague()->GetItemByProperty('GetId', $o_match->GetAwayTeamId()))) {
             continue;
         }
         # Make a note of missing results, to excuse inaccuracies
         if ($o_match->Result()->GetResultType() == MatchResult::UNKNOWN) {
             $this->a_results_missing[] = '<a href="' . Html::Encode($o_match->GetNavigateUrl()) . '">' . Html::Encode($o_match->GetTitle()) . '</a> &#8211; ' . Html::Encode($o_match->GetStartTimeFormatted());
             continue;
         }
         # Home team
         $i_home = $o_match->GetHomeTeamId();
         if (array_key_exists($i_home, $a_data_to_bind)) {
             $a_data_to_bind[$i_home][$i_played]++;
             if ($o_match->Result()->GetIsHomeWin()) {
                 $a_data_to_bind[$i_home][$i_won]++;
             } else {
                 if ($o_match->Result()->GetIsAwayWin()) {
                     $a_data_to_bind[$i_home][$i_lost]++;
                 } else {
                     if ($o_match->Result()->GetIsEqualResult()) {
                         $a_data_to_bind[$i_home][$i_tie]++;
                     } else {
                         if ($o_match->Result()->GetIsNoResult()) {
                             $a_data_to_bind[$i_home][$i_noresult]++;
                         } else {
                             $a_data_to_bind[$i_home][$i_played]--;
                         }
                     }
                 }
             }
             // safeguard - shouldn't get here
             if ($this->o_season->GetShowTableRunsScored()) {
                 $a_data_to_bind[$i_home][$i_runs_for] = $a_data_to_bind[$i_home][$i_runs_for] + $o_match->Result()->GetHomeRuns();
             }
             if ($this->o_season->GetShowTableRunsConceded()) {
                 $a_data_to_bind[$i_home][$i_runs_against] = $a_data_to_bind[$i_home][$i_runs_against] + $o_match->Result()->GetAwayRuns();
             }
             $a_data_to_bind[$i_home][$i_points] = $a_data_to_bind[$i_home][$i_points] + $o_match->Result()->GetHomePoints();
         }
         # Away team
         $i_away = $o_match->GetAwayTeamId();
         if (array_key_exists($i_away, $a_data_to_bind)) {
             $a_data_to_bind[$i_away][$i_played]++;
             if ($o_match->Result()->GetIsHomeWin()) {
                 $a_data_to_bind[$i_away][$i_lost]++;
             } else {
                 if ($o_match->Result()->GetIsAwayWin()) {
                     $a_data_to_bind[$i_away][$i_won]++;
                 } else {
                     if ($o_match->Result()->GetIsEqualResult()) {
                         $a_data_to_bind[$i_away][$i_tie]++;
                     } else {
                         if ($o_match->Result()->GetIsNoResult()) {
                             $a_data_to_bind[$i_away][$i_noresult]++;
                         } else {
                             $a_data_to_bind[$i_away][$i_played]--;
                         }
                     }
                 }
             }
             // safeguard - shouldn't get here
             if ($this->o_season->GetShowTableRunsScored()) {
                 $a_data_to_bind[$i_away][$i_runs_for] = $a_data_to_bind[$i_away][$i_runs_for] + $o_match->Result()->GetAwayRuns();
             }
             if ($this->o_season->GetShowTableRunsConceded()) {
                 $a_data_to_bind[$i_away][$i_runs_against] = $a_data_to_bind[$i_away][$i_runs_against] + $o_match->Result()->GetHomeRuns();
             }
             $a_data_to_bind[$i_away][$i_points] = $a_data_to_bind[$i_away][$i_points] + $o_match->Result()->GetAwayPoints();
         }
     }
     # Apply points adjustments
     foreach ($this->o_season->PointsAdjustments()->GetItems() as $o_point) {
         /* @var $o_point PointsAdjustment */
         $a_data_to_bind[$o_point->GetTeam()->GetId()][$i_points] += $o_point->GetPoints();
     }
     # Sort the teams so that the highest points come first
     $a_control_array = array();
     foreach ($a_data_to_bind as $a_team_data) {
         $a_control_array[] = $a_team_data[$i_points];
     }
     $a_control_subarray = array();
     foreach ($a_data_to_bind as $a_team_data) {
         $a_control_subarray[] = $a_team_data[$i_played];
     }
     array_multisort($a_control_array, SORT_DESC, $a_control_subarray, SORT_DESC, $a_data_to_bind);
     # Display the data
     $this->BindArray($a_data_to_bind);
     # Add withdrawn teams at the end of the table
     foreach ($this->o_season->TeamsWithdrawnFromLeague() as $team) {
         /* @var $team Team */
         $withdrawn_row = new XhtmlRow(array(new XhtmlAnchor(htmlentities($team->GetName(), ENT_QUOTES, "UTF-8", false), $team->GetNavigateUrl()), 'Withdrew from league'));
         $withdrawn_row->SetCssClass('withdrawn');
         $this->AddRow($withdrawn_row);
     }
     parent::OnPreRender();
 }
    function OnPageLoad()
    {
        require_once 'xhtml/navigation/tabs.class.php';
        require_once 'stoolball/match-list-control.class.php';
        require_once 'stoolball/season-list-control.class.php';
        /* @var $season Season */
        $season = $this->competition->GetWorkingSeason();
        echo new XhtmlElement('h1', Html::Encode($this->season->GetCompetitionName()));
        $tabs = array('Summary' => '');
        if ($season->MatchTypes()->Contains(MatchType::LEAGUE)) {
            $tabs['Table'] = $season->GetTableUrl();
        }
        if (count($season->GetTeams())) {
            $tabs['Map'] = $season->GetMapUrl();
        }
        $tabs['Statistics'] = $season->GetStatisticsUrl();
        echo new Tabs($tabs);
        ?>

        <div class="box tab-box">
            <div class="dataFilter"></div>
            <div class="box-content">
  
        <?php 
        # Add intro
        if ($this->competition->GetIntro()) {
            $intro = htmlentities($this->competition->GetIntro(), ENT_QUOTES, "UTF-8", false);
            $intro = XhtmlMarkup::ApplyParagraphs($intro);
            $intro = XhtmlMarkup::ApplyLists($intro);
            $intro = XhtmlMarkup::ApplySimpleXhtmlTags($intro, false);
            $intro = XhtmlMarkup::ApplyLinks($intro);
            echo $intro;
        }
        # Add season intro
        if ($season->GetIntro()) {
            $intro = htmlentities($season->GetIntro(), ENT_QUOTES, "UTF-8", false);
            $intro = XhtmlMarkup::ApplyCharacterEntities($intro);
            $intro = XhtmlMarkup::ApplyParagraphs($intro);
            $intro = XhtmlMarkup::ApplyLinks($intro);
            $intro = XhtmlMarkup::ApplyLists($intro);
            $intro = XhtmlMarkup::ApplySimpleTags($intro);
            $intro = XhtmlMarkup::ApplyTables($intro);
            echo $intro;
        }
        # Add not active, if relevant
        if (!$this->competition->GetIsActive()) {
            echo new XhtmlElement('p', new XhtmlElement('strong', 'This competition isn\'t played any more.'));
        }
        # Add matches
        $a_matches = $season->GetMatches();
        $i_matches = count($a_matches);
        if ($i_matches > 0) {
            echo new XhtmlElement('h2', 'Matches in ' . htmlentities($season->GetName(), ENT_QUOTES, "UTF-8", false) . ' season');
            $o_matches = new MatchListControl($a_matches);
            if ($season->MatchTypes()->Contains(MatchType::LEAGUE)) {
                $o_matches->SetMatchTypesToLabel(array(MatchType::FRIENDLY, MatchType::CUP, MatchType::PRACTICE));
            } else {
                if ($season->MatchTypes()->Contains(MatchType::CUP)) {
                    $o_matches->SetMatchTypesToLabel(array(MatchType::FRIENDLY, MatchType::PRACTICE));
                } else {
                    $o_matches->SetMatchTypesToLabel(array(MatchType::PRACTICE));
                }
            }
            echo $o_matches;
        }
        # Add teams
        $a_teams = $season->GetTeams();
        if (count($a_teams) > 0) {
            require_once 'stoolball/team-list-control.class.php';
            echo new XhtmlElement('h2', 'Teams playing in ' . htmlentities($season->GetName(), ENT_QUOTES, "UTF-8", false) . ' season');
            echo new TeamListControl($a_teams);
        }
        # Add results
        if ($season->GetResults()) {
            $s_results = htmlentities($season->GetResults(), ENT_QUOTES, "UTF-8", false);
            $s_results = XhtmlMarkup::ApplyCharacterEntities($s_results);
            $s_results = XhtmlMarkup::ApplyParagraphs($s_results);
            $s_results = XhtmlMarkup::ApplyLinks($s_results);
            $s_results = XhtmlMarkup::ApplyLists($s_results);
            $s_results = XhtmlMarkup::ApplySimpleTags($s_results);
            $s_results = XhtmlMarkup::ApplyTables($s_results);
            echo $s_results;
        }
        # Add contact details
        $s_contact = $this->competition->GetContact();
        $s_website = $this->competition->GetWebsiteUrl();
        if ($s_contact or $s_website) {
            echo new XhtmlElement('h2', 'Contact details');
        }
        if ($s_contact) {
            $s_contact = htmlentities($s_contact, ENT_QUOTES, "UTF-8", false);
            $s_contact = XhtmlMarkup::ApplyCharacterEntities($s_contact);
            require_once 'email/email-address-protector.class.php';
            $protector = new EmailAddressProtector($this->GetSettings());
            $s_contact = $protector->ApplyEmailProtection($s_contact, AuthenticationManager::GetUser()->IsSignedIn());
            $s_contact = XhtmlMarkup::ApplyParagraphs($s_contact);
            $s_contact = XhtmlMarkup::ApplyLists($s_contact);
            $s_contact = XhtmlMarkup::ApplySimpleXhtmlTags($s_contact, false);
            $s_contact = XhtmlMarkup::ApplyLinks($s_contact);
            echo $s_contact;
        }
        if ($s_website) {
            echo new XhtmlAnchor("Visit the " . htmlentities($this->competition->GetName(), ENT_QUOTES, "UTF-8", false) . ' website', $s_website);
        }
        # 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', 'Other seasons in the ' . htmlentities($this->competition->GetName(), ENT_QUOTES, "UTF-8", false), "screen");
            $season_list = new SeasonListControl($this->competition->GetSeasons());
            $season_list->SetExcludedSeasons(array($this->season));
            $season_list->AddCssClass("screen");
            echo $season_list;
        }
        ?>
        </div>
        </div>
        <?php 
        $this->ShowSocial();
        $this->AddSeparator();
        # Panel for updates
        $you_can = new UserEditPanel($this->GetSettings(), 'this season');
        $you_can->AddCssClass("with-tabs");
        if (AuthenticationManager::GetUser()->Permissions()->HasPermission(PermissionType::MANAGE_COMPETITIONS)) {
            $you_can->AddLink('edit this competition', $this->competition->GetEditCompetitionUrl());
            $you_can->AddLink('delete this competition', $this->competition->GetDeleteCompetitionUrl());
            $you_can->AddLink('edit this season', $this->season->GetEditSeasonUrl());
            # 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 (count($this->competition->GetSeasons()) > 1) {
                $you_can->AddLink('delete this season', $this->season->GetDeleteSeasonUrl());
            }
        }
        foreach ($this->season->MatchTypes() as $i_type) {
            if ($i_type != MatchType::PRACTICE and $i_type != MatchType::TOURNAMENT_MATCH) {
                $you_can->AddLink('add a ' . MatchType::Text($i_type), $this->season->GetNewMatchNavigateUrl($i_type));
            }
        }
        if (count($this->season->GetMatches())) {
            # Make sure there's at least one match which is not a tournament or a practice
            foreach ($this->season->GetMatches() as $o_match) {
                /* @var $o_match Match */
                if ($o_match->GetMatchType() == MatchType::PRACTICE or $o_match->GetMatchType() == MatchType::TOURNAMENT or $o_match->GetMatchType() == MatchType::TOURNAMENT_MATCH) {
                    continue;
                } else {
                    $you_can->AddLink('update results', $this->season->GetResultsNavigateUrl());
                    break;
                }
            }
            $you_can->AddLink('add matches to your calendar', $this->season->GetCalendarNavigateUrl());
        }
        echo $you_can;
        if ($this->has_player_stats) {
            require_once 'stoolball/statistics-highlight-table.class.php';
            echo new StatisticsHighlightTable($this->best_batting, $this->most_runs, $this->best_bowling, $this->most_wickets, $this->most_catches, $this->season->GetName() . " season");
        }
    }