/**
  * @return void
  * @desc Re-build from data posted by this control the data object this control is editing
  */
 function BuildPostedDataObject()
 {
     /* @var $o_season Season */
     $o_season = new Season($this->GetSettings());
     if (isset($_POST['start']) and is_numeric($_POST['start'])) {
         $o_season->SetStartYear($_POST['start']);
     }
     $i_year_span = isset($_POST['when']) ? (int) $_POST['when'] : 0;
     $o_season->SetEndYear($o_season->GetStartYear() + $i_year_span);
     $o_season->SetName($o_season->GetYears());
     if (isset($_POST['item'])) {
         $o_season->SetId($_POST['item']);
     } else {
         $this->b_saving_new = true;
         $this->match_types_editor->SetMinimumItems(0);
         # because will be populated from previous season (if there is one)
     }
     $o_season->SetIntro(ucfirst(trim($_POST['intro'])));
     $o_season->SetShortUrl($_POST[$this->GetNamingPrefix() . 'ShortUrl']);
     # Get the competition short URL and generate a season URL, rather than providing
     # a direct interface for season URLs.
     $o_comp = new Competition($this->GetSettings());
     $o_comp->SetId($_POST['competition']);
     $o_season->SetCompetition($o_comp);
     # Match types - get from aggregated editor
     $selected_match_types = $this->match_types_editor->DataObjects()->GetItems();
     foreach ($selected_match_types as $id_value) {
         $o_season->MatchTypes()->Add($id_value->GetId());
     }
     # Results and rules
     if (isset($_POST['results'])) {
         $o_season->SetResults($_POST['results']);
     }
     foreach ($this->result_types as $o_result) {
         /*@var $o_result MatchResult */
         $s_key = $this->GetNamingPrefix() . 'Result' . $o_result->GetResultType() . 'Home';
         if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
             $o_result->SetHomePoints($_POST[$s_key]);
         }
         $s_key = $this->GetNamingPrefix() . 'Result' . $o_result->GetResultType() . 'Away';
         if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
             $o_result->SetAwayPoints($_POST[$s_key]);
         }
         if (!is_null($o_result->GetHomePoints()) or !is_null($o_result->GetAwayPoints())) {
             $o_season->PossibleResults()->Add($o_result);
         }
     }
     # Points adjustments - get from aggregated editor
     $o_season->PointsAdjustments()->SetItems($this->adjustments_editor->DataObjects()->GetItems());
     # Show league table?
     $o_season->SetShowTable(isset($_POST['showTable']));
     $o_season->SetShowTableRunsScored(isset($_POST['runsScored']));
     $o_season->SetShowTableRunsConceded(isset($_POST['runsConceded']));
     # Teams - get from aggregated editor
     $a_teams_in_season = $this->teams_editor->DataObjects()->GetItems();
     foreach ($a_teams_in_season as $team_in_season) {
         /* @var $team_in_season TeamInSeason */
         $o_season->AddTeam($team_in_season->GetTeam());
         if ($team_in_season->GetWithdrawnFromLeague()) {
             $o_season->TeamsWithdrawnFromLeague()->Add($team_in_season->GetTeam());
         }
     }
     $this->SetDataObject($o_season);
 }
 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();
 }
 /**
  * Populates the collection of business objects from raw data
  *
  * @return bool
  * @param MySqlRawData $o_result
  */
 protected function BuildItems(MySqlRawData $o_result)
 {
     /* @var $o_competition Competition */
     # use CollectionBuilder to handle duplicates
     $o_comp_builder = new CollectionBuilder();
     $o_season_builder = new CollectionBuilder();
     $o_team_builder = new CollectionBuilder();
     $o_points_builder = new CollectionBuilder();
     $o_matchtype_builder = new CollectionBuilder();
     $o_competition = null;
     $o_season = null;
     while ($o_row = $o_result->fetch()) {
         # check whether this is a new competition
         if (!$o_comp_builder->IsDone($o_row->competition_id)) {
             # store any exisiting competition and reset
             if ($o_competition != null) {
                 if ($o_season != null) {
                     $o_competition->AddSeason($o_season, true);
                 }
                 $o_season = null;
                 $o_matchtype_builder->Reset();
                 $this->Add($o_competition);
                 $o_season_builder->Reset();
             }
             # create the new competition
             $o_competition = new Competition($this->o_settings);
             $o_competition->SetId($o_row->competition_id);
             $o_competition->SetName($o_row->competition_name);
             if (isset($o_row->intro)) {
                 $o_competition->SetIntro($o_row->intro);
             }
             if (isset($o_row->contact)) {
                 $o_competition->SetContact($o_row->contact);
             }
             if (isset($o_row->notification_email)) {
                 $o_competition->SetNotificationEmail($o_row->notification_email);
             }
             if (isset($o_row->website)) {
                 $o_competition->SetWebsiteUrl($o_row->website);
             }
             if (isset($o_row->short_url)) {
                 $o_competition->SetShortUrl($o_row->short_url);
             }
             if (isset($o_row->active)) {
                 $o_competition->SetIsActive($o_row->active);
             }
             if (isset($o_row->players_per_team)) {
                 $o_competition->SetMaximumPlayersPerTeam($o_row->players_per_team);
             }
             if (isset($o_row->overs)) {
                 $o_competition->SetOvers($o_row->overs);
             }
             $o_competition->SetPlayerType($o_row->player_type_id);
             if (isset($o_row->update_search) and $o_row->update_search == 1) {
                 $o_competition->SetSearchUpdateRequired();
             }
             if (isset($o_row->category_id) && !is_null($o_row->category_id)) {
                 $cat = new Category();
                 $cat->SetId($o_row->category_id);
                 if (isset($o_row->category_name)) {
                     $cat->SetName($o_row->category_name);
                 }
                 if (isset($o_row->code)) {
                     $cat->SetUrl($o_row->code);
                 }
                 $o_competition->SetCategory($cat);
             }
         }
         # Seasons are the first cause of multiple rows (first in sort order after competition)
         if (isset($o_row->season_id)) {
             if (!$o_season_builder->IsDone($o_row->season_id)) {
                 if ($o_season != null) {
                     $o_competition->AddSeason($o_season, true);
                 }
                 $o_season = new Season($this->o_settings);
                 $o_season->SetId($o_row->season_id);
                 $o_season->SetName($o_row->season_name);
                 $o_season->SetIsLatest($o_row->is_latest);
                 $o_season->SetStartYear($o_row->start_year);
                 $o_season->SetEndYear($o_row->end_year);
                 if (isset($o_row->season_intro)) {
                     $o_season->SetIntro($o_row->season_intro);
                 }
                 if (isset($o_row->results)) {
                     $o_season->SetResults($o_row->results);
                 }
                 if (isset($o_row->show_table)) {
                     $o_season->SetShowTable($o_row->show_table);
                 }
                 if (isset($o_row->show_runs_scored)) {
                     $o_season->SetShowTableRunsScored($o_row->show_runs_scored);
                 }
                 if (isset($o_row->show_runs_conceded)) {
                     $o_season->SetShowTableRunsConceded($o_row->show_runs_conceded);
                 }
                 if (isset($o_row->season_short_url)) {
                     $o_season->SetShortUrl($o_row->season_short_url);
                 }
             }
             # Team only present if there is a season
             if (isset($o_row->team_id)) {
                 if (!$o_team_builder->IsDone($o_row->team_id)) {
                     if (isset($o_team)) {
                         unset($o_team);
                     }
                     $o_team = new Team($this->GetSettings());
                     $o_team->SetId($o_row->team_id);
                     $o_team->SetName($o_row->team_name);
                     $o_team->GetGround()->SetId($o_row->ground_id);
                     if (isset($o_row->team_short_url)) {
                         $o_team->SetShortUrl($o_row->team_short_url);
                     }
                     $o_season->AddTeam($o_team);
                     if (isset($o_row->withdrawn_league) and (bool) $o_row->withdrawn_league) {
                         $o_season->TeamsWithdrawnFromLeague()->Add($o_team);
                     }
                 }
                 # Points adjustments - should come with team and in order of team
                 if (isset($o_row->point_id) and !$o_points_builder->IsDone($o_row->point_id)) {
                     $o_point = new PointsAdjustment($o_row->point_id, $o_row->points, $o_team, $o_row->reason, $o_row->points_date);
                     $o_season->PointsAdjustments()->Add($o_point);
                 }
             }
             # Match types come with a season
             if (isset($o_row->match_type) and !$o_matchtype_builder->IsDone($o_row->match_type)) {
                 $o_season->MatchTypes()->Add((int) $o_row->match_type);
             }
         }
     }
     # store final competition
     if ($o_competition != null) {
         if ($o_season != null) {
             $o_competition->AddSeason($o_season, true);
         }
         $this->Add($o_competition);
     }
 }