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();
 }
 private function CreateWicketsRow(Match $match, $wickets_taken)
 {
     $wickets_header = new XhtmlCell(true, "Wickets");
     $wickets_header->SetColumnSpan(4);
     $wickets = new XhtmlSelect("batWickets", null, $this->IsValidSubmit());
     $wickets->SetBlankFirst(true);
     $max_wickets = $match->GetMaximumPlayersPerTeam() - 2;
     $season_dates = Season::SeasonDates($match->GetStartTime());
     # working with GMT
     if (Date::Year($season_dates[0]) != Date::Year($season_dates[1])) {
         # outdoor needs maximum-2, but indoor needs maximum-1 cos last batter can play on.
         # if there's any chance it's indoor use maximum-1
         $max_wickets = $match->GetMaximumPlayersPerTeam() - 1;
     }
     for ($i = 0; $i <= $max_wickets; $i++) {
         $wickets->AddControl(new XhtmlOption($i));
     }
     $wickets->AddControl(new XhtmlOption('all out', -1));
     if ($this->IsValidSubmit() and !is_null($wickets_taken)) {
         $wickets->SelectOption($wickets_taken);
     }
     $balls_column = new XhtmlCell(false, null);
     $wickets_row = new XhtmlRow(array($wickets_header, $wickets, $balls_column));
     $wickets_row->SetCssClass("totals");
     return $wickets_row;
 }
 /**
  * Creates an extras/totals row at the bottom of the batting card
  * @param XhtmlTable $table
  * @param string $label
  * @param string $class
  * @param int $value
  * @param bool $is_minimal_scorecard
  * @return void
  */
 private function CreateExtrasRow(XhtmlTable $table, $label, $class, $value, $is_minimal_scorecard = false)
 {
     $extras_header = new XhtmlCell(true, htmlentities($label, ENT_QUOTES, "UTF-8", false));
     $extras_header->SetColumnSpan(3);
     $extras_data = new XhtmlCell(false, htmlentities($value, ENT_QUOTES, "UTF-8", false));
     $extras_data->SetCssClass("numeric runs");
     if ($is_minimal_scorecard) {
         $extras_row = new XhtmlRow(array($extras_header, $extras_data));
     } else {
         $balls_column = new XhtmlCell(false, null);
         $extras_row = new XhtmlRow(array($extras_header, $extras_data, $balls_column));
     }
     $extras_row->SetCssClass($class);
     $table->AddRow($extras_row);
 }
 /**
  * Adds populated controls as a new table row
  *
  * @param XhtmlElement[] $a_controls
  * @param bool $b_enabled
  * @return XhtmlRow
  */
 protected function AddRowToTable($a_controls, $b_enabled = true)
 {
     # Add action button to controls for table
     $button = new XhtmlElement('input');
     if (!$b_enabled) {
         $button->AddAttribute('disabled', 'disabled');
     }
     $last_cell_data = new Placeholder($button);
     $s_id = '';
     if (!is_null($this->current_data_object)) {
         # Get methods for data objects
         $s_id_method = $this->s_id_method;
         $s_date_modified_method = $this->s_date_modified_method;
         # Update button
         $button->AddAttribute('value', 'Delete');
         $button->SetXhtmlId($this->GetNamingPrefix() . 'DeleteRelated' . $this->i_current_row_identifier);
         # Hidden - id of related item
         $s_id = $this->current_data_object->{$s_id_method}();
         $s_id = $s_id ? $s_id : $this->i_current_row_identifier;
         $id_box = new TextBox($this->GetNamingPrefix() . 'RelatedId' . $this->i_current_row_identifier, $s_id);
         $id_box->SetMode(TextBoxMode::Hidden());
         $last_cell_data->AddControl($id_box);
         # Hidden - when was relationship to item updated?
         $b_track_modified_date = ($s_date_modified_method and method_exists($this->current_data_object, $s_date_modified_method));
         if ($b_track_modified_date) {
             $updated_box = new TextBox($this->GetNamingPrefix() . 'RelatedUpdated' . $this->i_current_row_identifier, $this->current_data_object->{$s_date_modified_method}());
             $updated_box->SetMode(TextBoxMode::Hidden());
             $last_cell_data->AddControl($updated_box);
         }
         # Hidden - check whether item updated
         $check_box = new TextBox($this->GetNamingPrefix() . 'Check' . $this->i_current_row_identifier, $this->GetDataObjectHash($this->current_data_object));
         $check_box->SetMode(TextBoxMode::Hidden());
         $last_cell_data->AddControl($check_box);
     } else {
         $button->AddAttribute('value', 'Add');
         $button->SetXhtmlId($this->GetNamingPrefix() . 'AddRelated');
     }
     $button->AddAttribute('type', 'submit');
     $button->SetEmpty(true);
     $button->AddAttribute('name', $button->GetXhtmlId());
     $this->RegisterInternalButton($button->GetXhtmlId());
     # Create table row and add to table
     $a_controls[] = $last_cell_data;
     $row = new XhtmlRow($a_controls);
     if (!$b_enabled) {
         $row->SetCssClass('unavailable');
     }
     $this->table->AddRow($row);
     return $row;
 }