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());
     }
 }
 /**
  * @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);
 }
 /**
  * Creates the controls when the editor is in its fixture view
  *
  */
 private function CreateFixtureControls(Match $match, XhtmlElement $match_box)
 {
     $css_class = 'TournamentEdit';
     if ($this->GetCssClass()) {
         $css_class .= ' ' . $this->GetCssClass();
     }
     $match_outer_1 = new XhtmlElement('div');
     $match_outer_1->SetCssClass($css_class);
     $this->SetCssClass('');
     $match_outer_1->SetXhtmlId($this->GetNamingPrefix());
     $match_outer_2 = new XhtmlElement('div');
     $this->AddControl($match_outer_1);
     $match_outer_1->AddControl($match_outer_2);
     $match_outer_2->AddControl($match_box);
     if ($match->GetId()) {
         $heading = "Edit tournament";
     } else {
         $heading = "Add your tournament";
     }
     if ($this->show_step_number) {
         $heading .= ' &#8211; step 1 of 3';
     }
     $o_title_inner_1 = new XhtmlElement('span', $heading);
     $o_title_inner_2 = new XhtmlElement('span', $o_title_inner_1);
     $o_title_inner_3 = new XhtmlElement('span', $o_title_inner_2);
     $match_box->AddControl(new XhtmlElement('h2', $o_title_inner_3, "large"));
     # Tournament title
     $suggested_title = $match->GetTitle();
     if (isset($this->context_season)) {
         $suggested_title = $this->GetContextSeason()->GetCompetition()->GetName();
         if (strpos(strtolower($suggested_title), 'tournament') === false) {
             $suggested_title .= ' tournament';
         }
     } else {
         if (isset($this->context_team)) {
             $suggested_title = $this->GetContextTeam()->GetName();
             if (strpos(strtolower($suggested_title), 'tournament') === false) {
                 $suggested_title .= ' tournament';
             }
         }
     }
     if ($suggested_title == "To be confirmed tournament") {
         $suggested_title = "";
     }
     if ($suggested_title == "To be confirmed v To be confirmed") {
         $suggested_title = "";
     }
     $title = new TextBox($this->GetNamingPrefix() . 'Title', $suggested_title, $this->IsValidSubmit());
     $title->SetMaxLength(200);
     $match_box->AddControl(new FormPart('Tournament name', $title));
     # Open or invite?
     require_once 'xhtml/forms/radio-button.class.php';
     $qualify_set = new XhtmlElement('fieldset');
     $qualify_set->SetCssClass('formPart radioButtonList');
     $qualify_set->AddControl(new XhtmlElement('legend', 'Who can play?', 'formLabel'));
     $qualify_radios = new XhtmlElement('div', null, 'formControl');
     $qualify_set->AddControl($qualify_radios);
     $qualify_radios->AddControl(new RadioButton($this->GetNamingPrefix() . 'Open', $this->GetNamingPrefix() . 'Qualify', 'any team may enter', MatchQualification::OPEN_TOURNAMENT, $match->GetQualificationType() === MatchQualification::OPEN_TOURNAMENT or !$match->GetId(), $this->IsValidSubmit()));
     $qualify_radios->AddControl(new RadioButton($this->GetNamingPrefix() . 'Qualify', $this->GetNamingPrefix() . 'Qualify', 'only invited or qualifying teams can enter', MatchQualification::CLOSED_TOURNAMENT, $match->GetQualificationType() === MatchQualification::CLOSED_TOURNAMENT, $this->IsValidSubmit()));
     $match_box->AddControl($qualify_set);
     # Player type
     $suggested_type = 2;
     if (isset($this->context_season)) {
         $suggested_type = $this->context_season->GetCompetition()->GetPlayerType();
     } elseif (isset($this->context_team)) {
         $suggested_type = $this->context_team->GetPlayerType();
     }
     if (!is_null($match->GetPlayerType())) {
         $suggested_type = $match->GetPlayerType();
     }
     # Saved value overrides suggestion
     $player_set = new XhtmlElement('fieldset');
     $player_set->SetCssClass('formPart radioButtonList');
     $player_set->AddControl(new XhtmlElement('legend', 'Type of teams', 'formLabel'));
     $player_radios = new XhtmlElement('div', null, 'formControl');
     $player_set->AddControl($player_radios);
     $player_radios_1 = new XhtmlElement('div', null, 'column');
     $player_radios_2 = new XhtmlElement('div', null, 'column');
     $player_radios->AddControl($player_radios_1);
     $player_radios->AddControl($player_radios_2);
     $player_radios_1->AddControl(new RadioButton($this->GetNamingPrefix() . 'Ladies', $this->GetNamingPrefix() . 'PlayerType', 'Ladies', 2, $suggested_type === 2, $this->IsValidSubmit()));
     $player_radios_1->AddControl(new RadioButton($this->GetNamingPrefix() . 'Mixed', $this->GetNamingPrefix() . 'PlayerType', 'Mixed', 1, $suggested_type === 1, $this->IsValidSubmit()));
     $player_radios_2->AddControl(new RadioButton($this->GetNamingPrefix() . 'Girls', $this->GetNamingPrefix() . 'PlayerType', 'Junior girls', 5, $suggested_type === 5, $this->IsValidSubmit()));
     $player_radios_2->AddControl(new RadioButton($this->GetNamingPrefix() . 'Children', $this->GetNamingPrefix() . 'PlayerType', 'Junior mixed', 4, $suggested_type === 6, $this->IsValidSubmit()));
     $match_box->AddControl($player_set);
     # How many?
     $per_side_box = new XhtmlSelect($this->GetNamingPrefix() . "Players", null, $this->IsValid());
     $per_side_box->SetBlankFirst(true);
     for ($i = 6; $i <= 16; $i++) {
         $per_side_box->AddControl(new XhtmlOption($i));
     }
     if ($match->GetIsMaximumPlayersPerTeamKnown()) {
         $per_side_box->SelectOption($match->GetMaximumPlayersPerTeam());
     } else {
         if (!$match->GetId()) {
             # Use eight as sensible default for new tournaments
             $per_side_box->SelectOption(8);
         }
     }
     $players_per_team = new XhtmlElement("label", $per_side_box);
     $players_per_team->AddAttribute("for", $this->GetNamingPrefix() . "Players");
     $players_per_team->AddControl(" players per team");
     $players_part = new FormPart("How many players?", $players_per_team);
     $players_part->AddCssClass("playersPerTeam");
     $match_box->AddControl($players_part);
     # Overs
     $overs_box = new XhtmlSelect($this->GetNamingPrefix() . "Overs", null, $this->IsValid());
     $overs_box->SetBlankFirst(true);
     for ($i = 2; $i <= 8; $i++) {
         $overs_box->AddControl(new XhtmlOption($i));
     }
     if ($match->GetIsOversKnown()) {
         $overs_box->SelectOption($match->GetOvers());
     }
     $overs_label = new XhtmlElement("label", "Overs per innings");
     $overs_label->AddAttribute("for", $overs_box->GetXhtmlId());
     $overs_part = new FormPart($overs_label, new XhtmlElement("div", $overs_box));
     $overs_part->AddCssClass("overs");
     $match_box->AddControl($overs_part);
     # Start date and time
     if (!$match->GetStartTime()) {
         # if no date set, use specified default
         if ($this->i_default_time) {
             $match->SetStartTime($this->i_default_time);
         } else {
             # if no date set and no default, default to today at 10.30am BST
             # NOTE that if this is a new tournament in an old season, this date won't be selected because the available
             # dates will be limited below and won't include today. It'll be the same day in the relevant year though.
             $i_now = gmdate('U');
             $match->SetStartTime(gmmktime(9, 30, 00, (int) gmdate('n', $i_now), (int) gmdate('d', $i_now), (int) gmdate('Y', $i_now)));
             $match->SetIsStartTimeKnown(true);
         }
     }
     $o_date = new DateControl($this->GetNamingPrefix() . 'Start', $match->GetStartTime(), $match->GetIsStartTimeKnown(), $this->IsValidSubmit());
     $o_date->SetShowTime(true);
     $o_date->SetRequireTime(false);
     $o_date->SetMinuteInterval(5);
     # if only one season to choose from, limit available dates to the length of that season
     if ($this->context_season instanceof Season) {
         if ($this->context_season->GetStartYear() == $this->context_season->GetEndYear()) {
             $i_mid_season = gmmktime(0, 0, 0, 6, 30, $this->context_season->GetStartYear());
         } else {
             $i_mid_season = gmmktime(0, 0, 0, 12, 31, $this->context_season->GetStartYear());
         }
         $season_dates = Season::SeasonDates($i_mid_season);
         $season_start_month = gmdate('n', $season_dates[0]);
         $season_end_month = gmdate('n', $season_dates[1]);
         if ($season_start_month) {
             $o_date->SetMonthStart($season_start_month);
         }
         if ($season_end_month) {
             $o_date->SetMonthEnd($season_end_month);
         }
         $season_start_year = $this->context_season->GetStartYear();
         $season_end_year = $this->context_season->GetEndYear();
         if ($season_start_year) {
             $o_date->SetYearStart($season_start_year);
         }
         if ($season_end_year) {
             $o_date->SetYearEnd($season_end_year);
         }
     }
     $o_date_part = new FormPart('When?', $o_date);
     $o_date_part->SetIsFieldset(true);
     $match_box->AddControl($o_date_part);
     # Where?
     $o_ground_list = new XhtmlSelect($this->GetNamingPrefix() . 'Ground');
     $o_ground_list->AddControl(new XhtmlOption("Don't know", -1));
     $o_ground_list->AddControl(new XhtmlOption('Not listed (type the address in the notes field)', -2));
     # Promote the most likely grounds to the top of the list
     $likely_ground_ids = array();
     if ($match->GetGroundId()) {
         $likely_ground_ids[] = $match->GetGroundId();
     }
     foreach ($this->probable_teams as $o_team) {
         $likely_ground_ids[] = $o_team->GetGround()->GetId();
     }
     if (isset($this->context_season)) {
         foreach ($this->context_season->GetTeams() as $o_team) {
             $likely_ground_ids[] = $o_team->GetGround()->GetId();
         }
     }
     if (isset($this->context_team) and is_object($this->context_team->GetGround())) {
         $likely_ground_ids[] = $this->context_team->GetGround()->GetId();
     }
     $likely_grounds = array();
     $a_other_grounds = array();
     /* @var $o_ground Ground */
     foreach ($this->grounds->GetItems() as $o_ground) {
         if (array_search($o_ground->GetId(), $likely_ground_ids) > -1) {
             $likely_grounds[] = $o_ground;
         } else {
             $a_other_grounds[] = $o_ground;
         }
     }
     # Add home grounds
     foreach ($likely_grounds as $o_ground) {
         $option = new XhtmlOption($o_ground->GetNameAndTown(), $o_ground->GetId());
         $option->SetGroupName('Likely grounds');
         $o_ground_list->AddControl($option);
     }
     # Add away grounds
     foreach ($a_other_grounds as $o_ground) {
         $option = new XhtmlOption($o_ground->GetNameAndTown(), $o_ground->GetId());
         $option->SetGroupName('Other grounds');
         $o_ground_list->AddControl($option);
     }
     # Select ground
     if ($match->GetGroundId()) {
         $o_ground_list->SelectOption($match->GetGroundId());
     } elseif (isset($this->context_team)) {
         $o_ground_list->SelectOption($this->context_team->GetGround()->GetId());
     }
     $o_ground_part = new FormPart('Where?', $o_ground_list);
     $match_box->AddControl($o_ground_part);
     # Notes
     $o_notes = new TextBox($this->GetNamingPrefix() . 'Notes', $match->GetNotes());
     $o_notes->SetMode(TextBoxMode::MultiLine());
     $o_notes_part = new FormPart('Notes<br />(remember to include contact details)', $o_notes);
     $match_box->AddControl($o_notes_part);
     # Remember short URL
     $o_short_url = new TextBox($this->GetNamingPrefix() . 'ShortUrl', $match->GetShortUrl());
     $o_short_url->SetMode(TextBoxMode::Hidden());
     $match_box->AddControl($o_short_url);
     # Note the context team to be added to the tournament by default
     if (isset($this->context_team)) {
         $context_team_box = new TextBox($this->GetNamingPrefix() . 'ContextTeam', $this->context_team->GetId());
         $context_team_box->SetMode(TextBoxMode::Hidden());
         $match_box->AddControl($context_team_box);
     }
     # Change Save button to "Next" button
     if ($this->show_step_number) {
         $this->SetButtonText('Next &raquo;');
     }
 }
 /**
  * @return void
  * @param Season $o_season
  * @desc Merge details from supplied season into this season. Where both copies have info, this copy is assumed correct.
  */
 function Merge($o_season)
 {
     if (!$this->i_id) {
         $this->SetId($o_season->GetId());
     }
     if (!$this->s_name) {
         $this->SetName($o_season->GetName());
     }
     if (!$this->i_start_year) {
         $this->SetStartYear($o_season->GetStartYear());
     }
     if (!$this->i_end_year) {
         $this->SetEndYear($o_season->GetEndYear());
     }
     if (!$this->s_intro) {
         $this->SetIntro($o_season->GetIntro());
     }
     if (!$this->s_results) {
         $this->SetResults($o_season->GetResults());
     }
     if (!count($this->a_teams)) {
         $this->a_teams = $o_season->GetTeams();
     }
     if (!is_object($this->o_competition)) {
         $this->SetCompetition($o_season->GetCompetition());
     }
 }