public function __construct(Match $match)
 {
     $this->match = $match;
     $this->searchable = new SearchItem("match", "match" . $match->GetId(), $match->GetNavigateUrl());
     $this->searchable->Title($match->GetTitle() . ", " . $match->GetStartTimeFormatted(true, false));
     $this->searchable->Description($this->GetSearchDescription());
     $this->searchable->FullText($match->GetNotes());
     $this->searchable->RelatedLinksHtml('<ul>' . '<li><a href="' . $match->GetNavigateUrl() . '/statistics">Statistics</a></li>' . '<li><a href="' . $match->GetCalendarNavigateUrl() . '">Add to calendar</a></li>' . '<li><a href="' . $match->GetEditNavigateUrl() . '">Update result</a></li>' . '</ul>');
     # Assign more weight to newer matches
     $weight = $match->GetStartTime() / 60 / 60 / 24 / 365;
     $this->searchable->WeightWithinType($weight);
 }
 /**
  * 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;');
     }
 }
 /**
  * Checks whether the given match fixture data is already in the database; returns the duplicate match or null
  *
  * @param Match $match_to_compare
  * @param bool $b_user_is_match_admin
  * @return Match
  */
 private function GetDuplicateFixture(Match $match_to_compare, $b_user_is_match_admin)
 {
     # Make sure it's not a duplicate. If it is, return the duplicate match.
     $is_duplicate = false;
     $is_new_match = !(bool) $match_to_compare->GetId();
     $id_of_duplicate = null;
     $s_match = $this->GetSettings()->GetTable('Match');
     $s_mt = $this->GetSettings()->GetTable('MatchTeam');
     $i_tournament = is_null($match_to_compare->GetTournament()) ? null : $match_to_compare->GetTournament()->GetId();
     $i_ground = $match_to_compare->GetGroundId() > 0 ? $match_to_compare->GetGroundId() : null;
     $s_sql = "SELECT {$s_match}.match_id FROM {$s_match} INNER JOIN {$s_mt} ON {$s_match}.match_id = {$s_mt}.match_id AND {$s_mt}.team_role = " . TeamRole::Home();
     $s_where = $this->SqlAddCondition('', 'tournament_match_id' . Sql::ProtectNumeric($i_tournament, true, true));
     $s_where = $this->SqlAddCondition($s_where, 'ground_id' . Sql::ProtectNumeric($i_ground, true, true));
     $s_where = $this->SqlAddCondition($s_where, 'start_time = ' . Sql::ProtectNumeric($match_to_compare->GetStartTime()));
     $s_where = $this->SqlAddCondition($s_where, 'start_time_known = ' . Sql::ProtectBool($match_to_compare->GetIsStartTimeKnown()));
     $s_where = $this->SqlAddCondition($s_where, 'match_notes = ' . Sql::ProtectString($this->GetDataConnection(), $match_to_compare->GetNotes()));
     $s_where = $this->SqlAddCondition($s_where, "{$s_mt}.team_id" . Sql::ProtectNumeric($match_to_compare->GetHomeTeamId(), true, true));
     # If it's a new match we want to check that it's not a duplicate match type of one already added.
     # If it's an updated match only an admin can change that. If not an admin, data's not even available so don't compare.
     if ($is_new_match or $b_user_is_match_admin) {
         $s_where = $this->SqlAddCondition($s_where, 'match_type = ' . Sql::ProtectNumeric($match_to_compare->GetMatchType()));
     }
     # If it's a tournament the player type should be specified by the user so check whether it's changed.
     # For any other match it's inferred automatically from other metadata about the match as it's saved,
     # and we're already checking whether that other data has changed so don't check for player type.
     if ($match_to_compare->GetMatchType() == MatchType::TOURNAMENT) {
         $s_where = $this->SqlAddCondition($s_where, 'player_type_id' . Sql::ProtectNumeric($match_to_compare->GetPlayerType(), true, true));
     }
     # If there's an id we're comparing whether the existing match has changed.
     # If no id we're looking whether the match has already been added, probably by someone else.
     if (!$is_new_match) {
         $s_where = $this->SqlAddCondition($s_where, "{$s_match}.match_id" . Sql::ProtectNumeric($match_to_compare->GetId(), false, true));
         # Only compare title properties for an update to a match, not for a match being added. If a new match is being added
         # the same match may exist with a different title because a result has been filled in - we still want to treat that as
         # a duplicate and not add the new match. But if the match is being updated it can only come from the match editing screen,
         # where the existing title is visible. The title may have changed due to an updated result, but OK to treat this as a changed
         # fixture in that event - an email about the match update is going to be sent and the match marked as updated anyway, so no problem.
         $s_where = $this->SqlAddCondition($s_where, 'match_title = ' . Sql::ProtectString($this->GetDataConnection(), $match_to_compare->GetTitle(), false));
         $s_where = $this->SqlAddCondition($s_where, 'custom_title' . Sql::ProtectBool($match_to_compare->GetUseCustomTitle(), false, true));
         # A new match would have either no or a newly-generated short URL, which inevitably would be different from the existing match
         # had it already been entered. But if we're updating the match the short URL may have been changed deliberately and we want to
         # recognise that change, not throw it away as a duplicate.
         $s_where = $this->SqlAddCondition($s_where, 'short_url = ' . Sql::ProtectString($this->GetDataConnection(), $match_to_compare->GetShortUrl(), false));
     }
     $s_sql = $this->SqlAddWhereClause($s_sql, $s_where);
     $result = $this->GetDataConnection()->query($s_sql);
     if ($o_row = $result->fetch()) {
         $id_of_duplicate = $o_row->match_id;
         # if it's a new match, this gives info on the match as it already exists
         $is_duplicate = true;
     }
     $result->closeCursor();
     # If basic match details appear to be a duplicate, does it have the same away team(s) too?
     if ($is_duplicate) {
         $s_sql = "SELECT team_id FROM {$s_mt} WHERE match_id =  " . Sql::ProtectNumeric($id_of_duplicate) . " AND team_role = " . TeamRole::Away();
         $result = $this->GetDataConnection()->query($s_sql);
         $existing_away_team_ids = array();
         while ($row = $result->fetch()) {
             $existing_away_team_ids[] = (int) $row->team_id;
         }
         $result->closeCursor();
         if (count($existing_away_team_ids) != count($match_to_compare->GetAwayTeams())) {
             # Different number of teams...
             $is_duplicate = false;
         }
         # Same number of away teams... now are those away teams the same ones as in the match we're comparing?
         if ($is_duplicate) {
             foreach ($match_to_compare->GetAwayTeams() as $team) {
                 /* @var $team Team */
                 if (!in_array($team->GetId(), $existing_away_team_ids, true)) {
                     $is_duplicate = false;
                     break;
                 }
             }
         }
     }
     # If basic match details and away teams appear to be a duplicate, does it have the same season(s) too?
     if ($is_duplicate) {
         # 1. If the user isn't admin and is updating match, don't check. They're not allowed to change season data so it's not even available.
         # 2. If the user isn't admin and it's a new match, is the existing match in the given season - don't care if it's in others too
         # 3. If the user is an admin, we want to look for an exact match
         if ($b_user_is_match_admin or $is_new_match) {
             $s_sql = "SELECT season_id FROM " . $this->GetSettings()->GetTable('SeasonMatch') . " WHERE match_id =  " . Sql::ProtectNumeric($id_of_duplicate);
             $result = $this->GetDataConnection()->query($s_sql);
             $existing_season_ids = array();
             while ($row = $result->fetch()) {
                 $existing_season_ids[] = (int) $row->season_id;
             }
             $result->closeCursor();
             if (count($existing_season_ids) == $match_to_compare->Seasons()->GetCount() or !$b_user_is_match_admin and $is_new_match) {
                 # Same number of seasons...
             } else {
                 # Different number of seasons...
                 $is_duplicate = false;
             }
             # Disposed of result, now are those seasons are the same ones as in the match we're comparing?
             if ($is_duplicate) {
                 foreach ($match_to_compare->Seasons() as $season) {
                     /* @var $season Season */
                     if (!in_array($season->GetId(), $existing_season_ids, true)) {
                         $is_duplicate = false;
                         break;
                     }
                 }
             }
         }
     }
     # Return the duplicate if one was found, otherwise return null
     if ($is_duplicate) {
         $match_to_compare->SetId($id_of_duplicate);
         return $match_to_compare;
     } else {
         return null;
     }
 }
 protected function CreateControls()
 {
     $o_match = $this->GetDataObject();
     if (is_null($o_match)) {
         $o_match = new Match($this->GetSettings());
     }
     /* @var $o_match Match */
     /* @var $o_team Team */
     $b_got_home = !is_null($o_match->GetHomeTeam());
     $b_got_away = !is_null($o_match->GetAwayTeam());
     $b_is_new_match = !(bool) $o_match->GetId();
     $b_is_tournament_match = false;
     if ($this->i_match_type == MatchType::TOURNAMENT_MATCH) {
         $b_is_tournament_match = $this->tournament instanceof Match;
     } else {
         if ($o_match->GetMatchType() == MatchType::TOURNAMENT_MATCH and $o_match->GetTournament() instanceof Match) {
             $this->SetTournament($o_match->GetTournament());
             $this->SetMatchType($o_match->GetMatchType());
             $b_is_tournament_match = true;
         }
     }
     $o_match_outer_1 = new XhtmlElement('div');
     $o_match_outer_1->SetCssClass('MatchFixtureEdit');
     $o_match_outer_1->AddCssClass($this->GetCssClass());
     $this->SetCssClass('');
     $o_match_outer_1->SetXhtmlId($this->GetNamingPrefix());
     $o_match_outer_2 = new XhtmlElement('div');
     $o_match_box = new XhtmlElement('div');
     $this->AddControl($o_match_outer_1);
     $o_match_outer_1->AddControl($o_match_outer_2);
     $o_match_outer_2->AddControl($o_match_box);
     if ($this->GetShowHeading()) {
         $s_heading = str_replace('{0}', MatchType::Text($this->i_match_type), $this->GetHeading());
         # Add match type if required
         $o_title_inner_1 = new XhtmlElement('span', htmlentities($s_heading, ENT_QUOTES, "UTF-8", false));
         $o_title_inner_2 = new XhtmlElement('span', $o_title_inner_1);
         $o_title_inner_3 = new XhtmlElement('span', $o_title_inner_2);
         $o_match_box->AddControl(new XhtmlElement('h2', $o_title_inner_3, "medium large"));
     }
     # Offer choice of season if appropriate
     $season_count = $this->seasons->GetCount();
     if ($season_count == 1 and $this->i_match_type != MatchType::PRACTICE) {
         $o_season_id = new TextBox($this->GetNamingPrefix() . 'Season', $this->seasons->GetFirst()->GetId());
         $o_season_id->SetMode(TextBoxMode::Hidden());
         $o_match_box->AddControl($o_season_id);
     } elseif ($season_count > 1 and $this->i_match_type != MatchType::PRACTICE) {
         $o_season_id = new XhtmlSelect($this->GetNamingPrefix() . 'Season', '', $this->IsValidSubmit());
         foreach ($this->Seasons()->GetItems() as $season) {
             $o_season_id->AddControl(new XhtmlOption($season->GetCompetitionName(), $season->GetId()));
         }
         $o_match_box->AddControl(new FormPart('Competition', $o_season_id));
     }
     # Start date and time
     $match_time_known = (bool) $o_match->GetStartTime();
     if (!$match_time_known) {
         # if no date set, use specified default
         if ($this->i_default_time) {
             $o_match->SetStartTime($this->i_default_time);
             if ($b_is_tournament_match) {
                 $o_match->SetIsStartTimeKnown(false);
             }
         } else {
             # if no date set and no default, default to today at 6.30pm BST
             $i_now = gmdate('U');
             $o_match->SetStartTime(gmmktime(17, 30, 00, (int) gmdate('n', $i_now), (int) gmdate('d', $i_now), (int) gmdate('Y', $i_now)));
         }
     }
     $o_date = new DateControl($this->GetNamingPrefix() . 'Start', $o_match->GetStartTime(), $o_match->GetIsStartTimeKnown(), $this->IsValidSubmit());
     $o_date->SetShowTime(true);
     $o_date->SetRequireTime(false);
     $o_date->SetMinuteInterval(5);
     # if no date set and only one season to choose from, limit available dates to the length of that season
     if (!$match_time_known and $season_count == 1) {
         if ($this->Seasons()->GetFirst()->GetStartYear() == $this->Seasons()->GetFirst()->GetEndYear()) {
             $i_mid_season = gmmktime(0, 0, 0, 6, 30, $this->Seasons()->GetFirst()->GetStartYear());
         } else {
             $i_mid_season = gmmktime(0, 0, 0, 12, 31, $this->Seasons()->GetFirst()->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 + 1);
         }
         // TODO: need a better way to handle this, allowing overlap. Shirley has indoor matches until early April.
         $season_start_year = $this->Seasons()->GetFirst()->GetStartYear();
         $season_end_year = $this->Seasons()->GetFirst()->GetEndYear();
         if ($season_start_year) {
             $o_date->SetYearStart($season_start_year);
         }
         if ($season_end_year) {
             $o_date->SetYearEnd($season_end_year);
         }
     }
     if ($b_is_tournament_match) {
         $o_date->SetShowDate(false);
         $o_date->SetShowTime(false);
         $o_match_box->AddControl($o_date);
     } else {
         $o_date_part = new FormPart('When?', $o_date);
         $o_date_part->SetIsFieldset(true);
         $o_match_box->AddControl($o_date_part);
     }
     # Who's playing?
     if ($this->i_match_type == MatchType::PRACTICE and isset($this->context_team)) {
         $home_id = new TextBox($this->GetNamingPrefix() . 'Home', $this->context_team->GetId());
         $home_id->SetMode(TextBoxMode::Hidden());
         $away_id = new TextBox($this->GetNamingPrefix() . 'Away', $this->context_team->GetId());
         $away_id->SetMode(TextBoxMode::Hidden());
         $o_match_box->AddControl($home_id);
         $o_match_box->AddControl($away_id);
     } else {
         $o_home_list = new XhtmlSelect($this->GetNamingPrefix() . 'Home');
         $o_away_list = new XhtmlSelect($this->GetNamingPrefix() . 'Away');
         $first_real_team_index = 0;
         if ($this->b_user_is_admin) {
             # Option of not specifying teams is currently admin-only
             # Value of 0 is important because PHP sees it as boolean negative, but it can be used as the indexer of an array in JavaScript
             $o_home_list->AddControl(new XhtmlOption("Don't know yet", '0'));
             $o_away_list->AddControl(new XhtmlOption("Don't know yet", '0'));
             $first_real_team_index = 1;
         }
         foreach ($this->a_teams as $group_name => $teams) {
             foreach ($teams as $o_team) {
                 $home_option = new XhtmlOption($o_team->GetName(), $o_team->GetId());
                 if (is_string($group_name) and $group_name) {
                     $home_option->SetGroupName($group_name);
                 }
                 $o_home_list->AddControl($home_option);
                 $away_option = new XhtmlOption($o_team->GetName(), $o_team->GetId());
                 if (is_string($group_name) and $group_name) {
                     $away_option->SetGroupName($group_name);
                 }
                 $o_away_list->AddControl($away_option);
             }
         }
         $o_home_part = new FormPart('Home team', $o_home_list);
         $o_away_part = new FormPart('Away team', $o_away_list);
         $o_match_box->AddControl($o_home_part);
         $o_match_box->AddControl($o_away_part);
         if ($b_got_home) {
             $o_home_list->SelectOption($o_match->GetHomeTeamId());
         }
         if (!$b_got_home and $b_is_new_match) {
             // if no home team data, select the first team by default
             // unless editing a match, in which case it may be correct to have no teams (eg cup final)
             $o_home_list->SelectIndex($first_real_team_index);
         }
         if (!$b_got_away and $b_is_new_match) {
             // if no away team data, select the second team as the away team so that it's not the same as the first
             // unless editing a match, in which case it may be correct to have no teams (eg cup final).
             $o_away_list->SelectIndex($first_real_team_index + 1);
             // if there was a home team but not an away team, make sure we don't select the home team against itself
             if ($b_got_home and $o_away_list->GetSelectedValue() == (string) $o_match->GetHomeTeamId()) {
                 $o_away_list->SelectIndex($first_real_team_index);
             }
         } else {
             if ($b_got_away) {
                 $o_away_list->SelectOption($o_match->GetAwayTeamId());
             }
             if (!$b_is_new_match) {
                 # Note which away team was previously saved, even if it's "not known" - this is for JavaScript to know it shouldn't auto-change the away team
                 $away_saved = new TextBox($this->GetNamingPrefix() . 'SavedAway', $o_match->GetAwayTeamId());
                 $away_saved->SetMode(TextBoxMode::Hidden());
                 $o_match_box->AddControl($away_saved);
                 unset($away_saved);
             }
         }
     }
     # Where?
     # If tournament match, assume same ground as tournament. Otherwise ask the user for ground.
     if ($b_is_tournament_match) {
         $ground = new TextBox($this->GetNamingPrefix() . 'Ground', $this->tournament->GetGroundId() ? $this->tournament->GetGroundId() : $o_match->GetGroundId());
         $ground->SetMode(TextBoxMode::Hidden());
         $o_match_box->AddControl($ground);
     } else {
         $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 home grounds for this season to the top of the list
         $a_home_ground_ids = array();
         foreach ($this->a_teams as $teams) {
             foreach ($teams as $o_team) {
                 $a_home_ground_ids[$o_team->GetId()] = $o_team->GetGround()->GetId();
             }
         }
         $a_home_grounds = array();
         $a_other_grounds = array();
         /* @var $o_ground Ground */
         foreach ($this->a_grounds as $o_ground) {
             if (array_search($o_ground->GetId(), $a_home_ground_ids) > -1) {
                 $a_home_grounds[] = $o_ground;
             } else {
                 $a_other_grounds[] = $o_ground;
             }
         }
         # Add home grounds
         foreach ($a_home_grounds as $o_ground) {
             $option = new XhtmlOption($o_ground->GetNameAndTown(), $o_ground->GetId());
             $option->SetGroupName('Home 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('Away grounds');
             $o_ground_list->AddControl($option);
         }
         # Select ground
         if ($o_match->GetGroundId()) {
             $o_ground_list->SelectOption($o_match->GetGroundId());
         } elseif ($this->i_match_type == MatchType::PRACTICE and isset($this->context_team)) {
             $o_ground_list->SelectOption($this->context_team->GetGround()->GetId());
         }
         $o_ground_part = new FormPart('Where?', $o_ground_list);
         $o_match_box->AddControl($o_ground_part);
         # Note which grounds belong to which teams, for use by match-fixture-edit-control.js to select a ground when the home team is changed
         # Format is 1,2;2,3;4,5
         # where ; separates each team, and for each team the first number identifies the team and the second is the ground
         $s_team_ground = '';
         foreach ($a_home_ground_ids as $i_team => $i_ground) {
             if ($s_team_ground) {
                 $s_team_ground .= ';';
             }
             $s_team_ground .= $i_team . ',' . $i_ground;
         }
         $o_hidden = new TextBox($this->GetNamingPrefix() . 'TeamGround', $s_team_ground);
         $o_hidden->SetMode(TextBoxMode::Hidden());
         $o_match_box->AddControl($o_hidden);
         unset($o_hidden);
         # Note which ground was previously saved - this is for JavaScript to know it shouldn't auto-change the ground
         if (!$b_is_new_match) {
             $o_hidden = new TextBox($this->GetNamingPrefix() . 'SavedGround', $o_match->GetGroundId());
             $o_hidden->SetMode(TextBoxMode::Hidden());
             $o_match_box->AddControl($o_hidden);
             unset($o_hidden);
         }
     }
     # Notes
     $o_notes = new TextBox($this->GetNamingPrefix() . 'Notes', $o_match->GetNotes());
     $o_notes->SetMode(TextBoxMode::MultiLine());
     $o_notes_part = new FormPart('Notes', $o_notes);
     $o_match_box->AddControl($o_notes_part);
     # Remember match type, tournament and short URL
     $o_type = new TextBox($this->GetNamingPrefix() . 'MatchType', $this->GetMatchType());
     $o_type->SetMode(TextBoxMode::Hidden());
     $o_match_box->AddControl($o_type);
     if ($b_is_tournament_match) {
         $tourn_box = new TextBox($this->GetNamingPrefix() . 'Tournament', $this->tournament->GetId());
         $tourn_box->SetMode(TextBoxMode::Hidden());
         $o_match_box->AddControl($tourn_box);
     }
     $o_short_url = new TextBox($this->GetNamingPrefix() . 'ShortUrl', $o_match->GetShortUrl());
     $o_short_url->SetMode(TextBoxMode::Hidden());
     $o_match_box->AddControl($o_short_url);
     # Note the context team - to be picked up by JavaScript to enable auto-changing of away team if
     # context team is not selected as home team
     if (isset($this->context_team)) {
         $context_team_box = new TextBox($this->GetNamingPrefix() . 'ContextTeam', $this->context_team->GetId());
         $context_team_box->SetMode(TextBoxMode::Hidden());
         $o_match_box->AddControl($context_team_box);
     }
 }
    /**
     * Helper to build and send the email when a match has been added by a public user
     *
     * @param Match $o_match
     * @param User $o_user
     * @param bool $b_is_new_match
     * @param bool $b_is_deleted_match
     * @param string $s_email
     * @param Season[] $seasons
     */
    private function SendMatchUpdatedEmail(Match $o_match, User $o_user, $b_is_new_match, $b_is_deleted_match, $s_email, $seasons = null)
    {
        # text of email
        $s_season_list = '';
        if (is_array($seasons)) {
            $i_total_seasons = count($seasons);
            for ($i = 0; $i < $i_total_seasons; $i++) {
                if ($i == 0) {
                    $s_season_list = "'" . $seasons[$i]->GetCompetitionName() . "'";
                } else {
                    if ($i == $i_total_seasons - 1) {
                        $s_season_list .= " and '" . $seasons[$i]->GetCompetitionName() . "'";
                    } else {
                        $s_season_list .= ", '" . $seasons[$i]->GetCompetitionName() . "'";
                    }
                }
            }
        }
        $s_season = $s_season_list ? " in the {$s_season_list}" : '';
        $s_why = $s_season_list ? $s_season_list : 'matches';
        $new = $b_is_new_match ? 'new ' : '';
        $verb = $b_is_new_match ? 'added' : 'updated';
        if ($b_is_deleted_match) {
            $verb = 'deleted';
        }
        $match_text = $o_match->GetMatchType() == MatchType::TOURNAMENT ? 'tournament' : 'match';
        $s_title = html_entity_decode($o_match->GetTitle());
        $s_date = ucfirst($o_match->GetStartTimeFormatted());
        $s_ground = is_object($o_match->GetGround()) ? $o_match->GetGround()->GetNameAndTown() : '';
        $s_notes = $o_match->GetNotes();
        $s_domain = $this->settings->GetDomain();
        $s_url = 'https://' . $s_domain . $o_match->GetNavigateUrl();
        $s_contact_url = 'https://' . $s_domain . $this->settings->GetFolder('Contact');
        $s_home_name = $o_match->GetMatchType() == MatchType::TOURNAMENT ? '' : html_entity_decode($o_match->GetHomeTeam()->GetName());
        $s_away_name = $o_match->GetMatchType() == MatchType::TOURNAMENT ? '' : html_entity_decode($o_match->GetAwayTeam()->GetName());
        $s_bat_first = is_null($o_match->Result()->GetHomeBattedFirst()) ? 'Not known which team batted first' : ($o_match->Result()->GetHomeBattedFirst() ? $s_home_name : $s_away_name) . ' batted first';
        $s_home_runs = is_null($o_match->Result()->GetHomeRuns()) ? '(not known)' : $o_match->Result()->GetHomeRuns();
        $s_home_wickets = is_null($o_match->Result()->GetHomeWickets()) ? '(not known)' : $o_match->Result()->GetHomeWickets();
        if ($s_home_wickets == -1) {
            $s_home_wickets = 'all out';
        } else {
            $s_home_wickets = 'for ' . $s_home_wickets . ' wickets';
        }
        $s_away_runs = is_null($o_match->Result()->GetAwayRuns()) ? '(not known)' : $o_match->Result()->GetAwayRuns();
        $s_away_wickets = is_null($o_match->Result()->GetAwayWickets()) ? '(not known)' : $o_match->Result()->GetAwayWickets();
        if ($s_away_wickets == -1) {
            $s_away_wickets = 'all out';
        } else {
            $s_away_wickets = 'for ' . $s_away_wickets . ' wickets';
        }
        $s_user = $o_user->GetName();
        $s_body = wordwrap("A {$new}{$match_text} has been {$verb} on the Stoolball England website at {$s_domain}{$s_season}.\n\n" . "The {$match_text} was {$verb} by {$s_user}.\n\n" . "The {$match_text} details are as follows:\n\n" . "    {$s_title}\n" . "    {$s_date}\n" . "    {$s_ground}");
        if ($s_notes) {
            $s_notes = "\n\n" . wordwrap($s_notes, 70);
            $s_notes = str_replace("\n", "\n    ", $s_notes);
            $s_body .= $s_notes;
        }
        if ($o_match->GetStartTime() <= gmdate('U') and !$b_is_new_match and $o_match->GetMatchType() != MatchType::TOURNAMENT) {
            $s_body .= <<<EMAILBODY


\t{$s_bat_first}
EMAILBODY;
            if ($o_match->Result()->GetHomeBattedFirst() === false) {
                $s_body .= <<<EMAILBODY

\t{$s_away_name} score: {$s_away_runs} runs {$s_away_wickets}
\t{$s_home_name} score: {$s_home_runs} runs {$s_home_wickets}
EMAILBODY;
            } else {
                $s_body .= <<<EMAILBODY

\t{$s_home_name} score: {$s_home_runs} runs {$s_home_wickets}
\t{$s_away_name} score: {$s_away_runs} runs {$s_away_wickets}
EMAILBODY;
            }
        }
        $s_body .= "\n\n";
        if (!$b_is_deleted_match) {
            $s_body .= wordwrap("You can view the {$match_text} at {$s_url}\n\n");
        }
        $s_body .= wordwrap("You have received this email because you are the administrative contact for {$s_why} on {$s_domain}.\n\n" . "We let you know when a {$match_text} is {$verb} by a member of the public, so that you can check there's nothing wrong.\n\n" . "If this email has been sent to the wrong address, or if the {$match_text} details are wrong, please let us know using the contact form at {$s_contact_url}.\n\n");
        # send email, copy to me
        require_once 'Zend/Mail.php';
        $o_email = new Zend_Mail('UTF-8');
        $o_email->addTo($s_email);
        $o_email->setFrom('*****@*****.**', 'Stoolball England alerts');
        $o_email->setSubject(ucfirst($match_text) . " {$verb}: {$s_title}, {$s_date}");
        $o_email->setBodyText($s_body);
        try {
            $o_email->send();
        } catch (Zend_Mail_Transport_Exception $e) {
            # Do nothing - failure to send this email should not be a fatal error
        }
    }
 protected function OnPreRender()
 {
     /* @var $o_home Team */
     /* @var $o_away Team */
     /* @var $o_tourney Match */
     # Date and tournament
     $o_date_para = new XhtmlElement('p');
     $o_date = new XhtmlElement('abbr', htmlentities($this->o_match->GetStartTimeFormatted(), ENT_QUOTES, "UTF-8", false));
     $o_date->SetTitle(Date::Microformat($this->o_match->GetStartTime()));
     # hCalendar
     $o_date->SetCssClass('dtstart');
     # hCalendar
     $o_date->AddAttribute("property", "schema:startDate");
     $o_date->AddAttribute("datatype", "xsd:date");
     $o_date->AddAttribute("content", Date::Microformat($this->o_match->GetStartTime()));
     $o_date_para->AddControl('When: ');
     $o_date_para->AddControl($o_date);
     # hCalendar end date
     if ($this->o_match->GetIsStartTimeKnown()) {
         $i_end_time = $this->o_match->GetStartTime() + 60 * 90;
         $o_hcal_end = new XhtmlElement('abbr', ' until around ' . htmlentities(Date::Time($i_end_time), ENT_QUOTES, "UTF-8", false));
         $o_hcal_end->SetTitle(Date::Microformat($i_end_time));
         $o_hcal_end->SetCssClass('metadata dtend');
         $o_date_para->AddControl($o_hcal_end);
     }
     # If we know the time and place, show when the sun sets
     # TODO: Assumes UK
     if ($this->o_match->GetGround() instanceof Ground and $this->o_match->GetGround()->GetAddress()->GetLatitude() and $this->o_match->GetGround()->GetAddress()->GetLongitude()) {
         $o_date_para->AddControl(' <span class="sunset">sunset ' . htmlentities(Date::Time(date_sunset($this->o_match->GetStartTime(), SUNFUNCS_RET_TIMESTAMP, $this->o_match->GetGround()->GetAddress()->GetLatitude(), $this->o_match->GetGround()->GetAddress()->GetLongitude())), ENT_QUOTES, "UTF-8", false) . '</span>');
     }
     # Display match type/season/tournament
     if ($this->o_match->GetMatchType() == MatchType::TOURNAMENT_MATCH) {
         $o_date_para->SetCssClass('description');
         # hCal
         $o_tourney = $this->o_match->GetTournament();
         if (is_object($o_tourney)) {
             $tournament_link = new XhtmlAnchor(htmlentities($o_tourney->GetTitle(), ENT_QUOTES, "UTF-8", false), $o_tourney->GetNavigateUrl());
             $tournament_link->AddAttribute("typeof", "schema:SportsEvent");
             $tournament_link->AddAttribute("about", $o_tourney->GetLinkedDataUri());
             $tournament_link->AddAttribute("rel", "schema:url");
             $tournament_link->AddAttribute("property", "schema:name");
             $tournament_container = new XhtmlElement("span", $tournament_link);
             $tournament_container->AddAttribute("rel", "schema:superEvent");
             # Check for 'the' to get the grammar right
             $s_title = strtolower($o_tourney->GetTitle());
             if (strlen($s_title) >= 4 and substr($s_title, 0, 4) == 'the ') {
                 $o_date_para->AddControl(', in ');
             } else {
                 $o_date_para->AddControl(', in the ');
             }
             $o_date_para->AddControl($tournament_container);
             $o_date_para->AddControl('.');
         } else {
             $o_date_para->AddControl(', in a tournament.');
         }
     } else {
         # hCalendar desc, built up at the same time as the date and league/tournament
         $hcal_desc = new XhtmlElement('div', null, 'description');
         $this->AddControl($hcal_desc);
         $s_detail_xhtml = ucfirst(MatchType::Text($this->o_match->GetMatchType()));
         $season_list_xhtml = null;
         if ($this->o_match->Seasons()->GetCount() == 1) {
             $season = $this->o_match->Seasons()->GetFirst();
             $season_name = new XhtmlAnchor(htmlentities($season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false), $season->GetNavigateUrl());
             $b_the = !(stristr($season->GetCompetitionName(), 'the ') === 0);
             $s_detail_xhtml .= ' in ' . ($b_the ? 'the ' : '') . $season_name->__toString() . '.';
         } elseif ($this->o_match->Seasons()->GetCount() > 1) {
             $s_detail_xhtml .= ' in the following seasons: ';
             $season_list_xhtml = new XhtmlElement('ul');
             $seasons = $this->o_match->Seasons()->GetItems();
             $total_seasons = count($seasons);
             for ($i = 0; $i < $total_seasons; $i++) {
                 $season = $seasons[$i];
                 /* @var $season Season */
                 $season_name = new XhtmlAnchor(htmlentities($season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false), $season->GetNavigateUrl());
                 $li = new XhtmlElement('li', $season_name);
                 if ($i < $total_seasons - 2) {
                     $li->AddControl(new XhtmlElement('span', ', ', 'metadata'));
                 } else {
                     if ($i < $total_seasons - 1) {
                         $li->AddControl(new XhtmlElement('span', ' and ', 'metadata'));
                     }
                 }
                 $season_list_xhtml->AddControl($li);
             }
         } else {
             $s_detail_xhtml .= '.';
         }
         $hcal_desc->AddControl(new XhtmlElement('p', $s_detail_xhtml));
         if (!is_null($season_list_xhtml)) {
             $hcal_desc->AddControl($season_list_xhtml);
         }
     }
     # Who
     $o_home = $this->o_match->GetHomeTeam();
     $o_away = $this->o_match->GetAwayTeam();
     $has_home = $o_home instanceof Team;
     $has_away = $o_away instanceof Team;
     if ($has_home or $has_away) {
         $who = new XhtmlElement("p", "Who: ");
         if ($has_home) {
             $who->AddControl($this->CreateTeamLink($o_home));
         }
         if ($has_home and $has_away) {
             $who->AddControl(" and ");
         }
         if ($has_away) {
             $who->AddControl($this->CreateTeamLink($o_away));
         }
         $this->AddControl($who);
     }
     # When
     $this->AddControl($o_date_para);
     # Ground
     $o_ground = $this->o_match->GetGround();
     if (is_object($o_ground)) {
         $o_ground_link = new XhtmlElement('a', htmlentities($o_ground->GetNameAndTown(), ENT_QUOTES, "UTF-8", false));
         $o_ground_link->AddAttribute('href', $o_ground->GetNavigateUrl());
         $o_ground_link->SetCssClass('location');
         # hCalendar
         $o_ground_link->AddAttribute("typeof", "schema:Place");
         $o_ground_link->AddAttribute("about", $o_ground->GetLinkedDataUri());
         $o_ground_link->AddAttribute("rel", "schema:url");
         $o_ground_link->AddAttribute("property", "schema:name");
         $o_ground_control = new XhtmlElement('p', 'Where: ');
         $o_ground_control->AddAttribute("rel", "schema:location");
         $o_ground_control->AddControl($o_ground_link);
         $this->AddControl($o_ground_control);
     }
     # Add result
     $o_result = $this->o_match->Result();
     $b_result_known = !$o_result->GetResultType() == MatchResult::UNKNOWN;
     $toss_known = !is_null($this->o_match->Result()->GetTossWonBy());
     $b_batting_order_known = !is_null($this->o_match->Result()->GetHomeBattedFirst());
     $has_scorecard_data = ($o_result->HomeBatting()->GetCount() or $o_result->HomeBowling()->GetCount() or $o_result->AwayBatting()->GetCount() or $o_result->AwayBowling()->GetCount() or $o_result->GetHomeRuns() or $o_result->GetHomeWickets() or $o_result->GetAwayRuns() or $o_result->GetAwayWickets());
     $has_player_of_match = $this->o_match->Result()->GetPlayerOfTheMatch() instanceof Player and $this->o_match->Result()->GetPlayerOfTheMatch()->GetName();
     $has_player_of_match_home = $this->o_match->Result()->GetPlayerOfTheMatchHome() instanceof Player and $this->o_match->Result()->GetPlayerOfTheMatchHome()->GetName();
     $has_player_of_match_away = $this->o_match->Result()->GetPlayerOfTheMatchAway() instanceof Player and $this->o_match->Result()->GetPlayerOfTheMatchAway()->GetName();
     if ($b_result_known or $b_batting_order_known or $has_scorecard_data) {
         # Put result in header, so long as we have something to put after it. Otherwise put the result after it.
         $result_header = "Result";
         if ($b_result_known and ($b_batting_order_known or $has_scorecard_data)) {
             $result_header .= ": " . $this->o_match->GetResultDescription();
         }
         $result_header = new XhtmlElement('h2', htmlentities($result_header, ENT_QUOTES, "UTF-8", false));
         if ($has_scorecard_data) {
             $result_header->AddCssClass("hasScorecard");
         }
         $this->AddControl($result_header);
     }
     if ($toss_known) {
         $toss_team = $this->o_match->Result()->GetTossWonBy() === TeamRole::Home() ? $this->o_match->GetHomeTeam() : $this->o_match->GetAwayTeam();
         if ($toss_team instanceof Team) {
             $toss_text = $toss_team->GetName() . " won the toss";
             if ($b_batting_order_known) {
                 $chose_to = ($this->o_match->Result()->GetTossWonBy() === TeamRole::Home()) == $this->o_match->Result()->GetHomeBattedFirst() ? "bat" : "bowl";
                 $toss_text .= " and chose to " . $chose_to;
             }
             $this->AddControl("<p>" . Html::Encode($toss_text) . '.</p>');
         }
     }
     # If at least one player recorded, create a container for the schema.org metadata
     if ($has_scorecard_data or $has_player_of_match or $has_player_of_match_home or $has_player_of_match_away) {
         $this->AddControl('<div rel="schema:performers">');
     }
     if ($has_scorecard_data) {
         $this->CreateScorecard($this->o_match);
     } else {
         # Got to be just result and batting order now. Only include result if batting order's not there, otherwise result will already be in header.
         if ($b_result_known and !$b_batting_order_known) {
             $this->AddControl(new XhtmlElement('p', htmlentities($this->o_match->GetResultDescription(), ENT_QUOTES, "UTF-8", false) . '.'));
         }
         if ($b_batting_order_known) {
             $this->AddControl(new XhtmlElement('p', htmlentities(($this->o_match->Result()->GetHomeBattedFirst() ? $o_home->GetName() : $o_away->GetName()) . ' batted first.'), ENT_QUOTES, "UTF-8", false));
         }
     }
     # Player of the match
     if ($has_player_of_match) {
         $player = $this->o_match->Result()->GetPlayerOfTheMatch();
         $team = $player->Team()->GetId() == $o_home->GetId() ? $o_home->GetName() : $o_away->GetName();
         $player_of_match = new XhtmlElement('p', 'Player of the match: <a property="schema:name" rel="schema:url" href="' . htmlentities($player->GetPlayerUrl(), ENT_QUOTES, "UTF-8", false) . '">' . htmlentities($player->GetName(), ENT_QUOTES, "UTF-8", false) . "</a> ({$team})");
         $player_of_match->AddAttribute("typeof", "schema:Person");
         $player_of_match->AddAttribute("about", $player->GetLinkedDataUri());
         $this->AddControl($player_of_match);
     }
     if ($has_player_of_match_home) {
         $player = $this->o_match->Result()->GetPlayerOfTheMatchHome();
         $player_of_match = new XhtmlElement('p', $o_home->GetName() . ' player of the match: <a property="schema:name" rel="schema:url" href="' . htmlentities($player->GetPlayerUrl(), ENT_QUOTES, "UTF-8", false) . '">' . htmlentities($player->GetName(), ENT_QUOTES, "UTF-8", false) . "</a>");
         $player_of_match->AddAttribute("typeof", "schema:Person");
         $player_of_match->AddAttribute("about", $player->GetLinkedDataUri());
         $this->AddControl($player_of_match);
     }
     if ($has_player_of_match_away) {
         $player = $this->o_match->Result()->GetPlayerOfTheMatchAway();
         $player_of_match = new XhtmlElement('p', $o_away->GetName() . ' player of the match: <a property="schema:name" rel="schema:url" href="' . htmlentities($player->GetPlayerUrl(), ENT_QUOTES, "UTF-8", false) . '">' . htmlentities($player->GetName(), ENT_QUOTES, "UTF-8", false) . "</a>");
         $player_of_match->AddAttribute("typeof", "schema:Person");
         $player_of_match->AddAttribute("about", $player->GetLinkedDataUri());
         $this->AddControl($player_of_match);
     }
     # End container for the schema.org metadata
     if ($has_scorecard_data or $has_player_of_match or $has_player_of_match_home or $has_player_of_match_away) {
         $this->AddControl('</div>');
     }
     # Add notes
     if ($this->o_match->GetNotes()) {
         $this->AddControl(new XhtmlElement('h2', 'Notes'));
         $s_notes = htmlentities($this->o_match->GetNotes(), ENT_QUOTES, "UTF-8", false);
         $s_notes = XhtmlMarkup::ApplyCharacterEntities($s_notes);
         require_once 'email/email-address-protector.class.php';
         $protector = new EmailAddressProtector($this->o_settings);
         $s_notes = $protector->ApplyEmailProtection($s_notes, AuthenticationManager::GetUser()->IsSignedIn());
         $s_notes = XhtmlMarkup::ApplyHeadings($s_notes);
         $s_notes = XhtmlMarkup::ApplyParagraphs($s_notes);
         $s_notes = XhtmlMarkup::ApplyLists($s_notes);
         $s_notes = XhtmlMarkup::ApplySimpleTags($s_notes);
         $s_notes = XhtmlMarkup::ApplyLinks($s_notes);
         if (strpos($s_notes, '<p>') > -1) {
             $this->AddControl($s_notes);
         } else {
             $this->AddControl(new XhtmlElement('p', $s_notes));
         }
     }
     # hCalendar metadata
     $o_hcal_para = new XhtmlElement('p');
     $o_hcal_para->SetCssClass('metadata');
     $this->AddControl($o_hcal_para);
     # hCalendar timestamp
     $o_hcal_para->AddControl('Status: At ');
     $o_hcal_stamp = new XhtmlElement('abbr', htmlentities(Date::Time(gmdate('U')), ENT_QUOTES, "UTF-8", false));
     $o_hcal_stamp->SetTitle(Date::Microformat());
     $o_hcal_stamp->SetCssClass('dtstamp');
     $o_hcal_para->AddControl($o_hcal_stamp);
     # hCalendar GUID
     $o_hcal_para->AddControl(' match ');
     $o_hcal_guid = new XhtmlElement('span', htmlentities($this->o_match->GetLinkedDataUri(), ENT_QUOTES, "UTF-8", false));
     $o_hcal_guid->SetCssClass('uid');
     $o_hcal_para->AddControl($o_hcal_guid);
     # work out hCalendar status
     $s_status = 'CONFIRMED';
     switch ($this->o_match->Result()->GetResultType()) {
         case MatchResult::CANCELLED:
         case MatchResult::POSTPONED:
         case MatchResult::AWAY_WIN_BY_FORFEIT:
         case MatchResult::HOME_WIN_BY_FORFEIT:
             $s_status = 'CANCELLED';
     }
     # hCalendar URL and status
     $o_hcal_para->AddControl(' is ');
     $o_hcal_url = new XhtmlAnchor($s_status, 'http://' . $_SERVER['HTTP_HOST'] . $this->o_match->GetNavigateUrl());
     $o_hcal_url->SetCssClass('url status');
     $o_hcal_para->AddControl($o_hcal_url);
 }