/**
  * Sends a notification that a match has been modified by a public user
  *
  * @param Match $o_match
  * @param User $o_user
  * @param bool $b_is_new_match
  * @param bool $b_is_deleted_match
  */
 public function MatchUpdated(Match $o_match, User $o_user, $b_is_new_match = false, $b_is_deleted_match = false)
 {
     # Don't send email if match added by a trusted administrator
     if ($o_user->Permissions()->HasPermission(PermissionType::MANAGE_MATCHES)) {
         return;
     }
     # Match may have been added to multiple seasons (though this is theoretical because at the time
     # of writing only an admin can add to multiple seasons... and they don't generate emails.)
     # Nevertheless the object model is geared to that possibility and this way the code makes sense
     # of the object model.
     if ($o_match->Seasons()->GetCount()) {
         $emails_to_send = array();
         foreach ($o_match->Seasons() as $season) {
             /* @var $season Season */
             ### Find the email address to notify for this season. ###
             # If the competition has a manager, use their email address...
             $email = '';
             if (!is_null($season->GetCompetition()) and $season->GetCompetition()->GetNotificationEmail()) {
                 # ...but don't email the competition manager if they're adding/updating the match!
                 if ($season->GetCompetition()->GetNotificationEmail() == $o_user->GetEmail()) {
                     continue;
                 }
                 $email = $season->GetCompetition()->GetNotificationEmail();
             } else {
                 # If there's no competition manager, send to backup email address
                 $email = $this->settings->GetMatchUpdatesEmail();
             }
             # Add the current season to the list of seasons to notify that email address about
             if ($email) {
                 if (!isset($emails_to_send[$email])) {
                     $emails_to_send[$email] = array();
                 }
                 $emails_to_send[$email][] = $season;
             }
         }
         # And now send an email to each address, listing the change for all the seasons they represent
         foreach ($emails_to_send as $email => $seasons) {
             $this->SendMatchUpdatedEmail($o_match, $o_user, $b_is_new_match, $b_is_deleted_match, $email, $seasons);
         }
     } else {
         # If there's no season there's no competition manager, send to backup email address
         $this->SendMatchUpdatedEmail($o_match, $o_user, $b_is_new_match, $b_is_deleted_match, $this->settings->GetMatchUpdatesEmail());
     }
 }
 function OnLoadPageData()
 {
     /* @var $match_manager MatchManager */
     /* @var $editor TournamentEditControl */
     # get id of Match
     $i_id = $this->match_manager->GetItemId();
     if (!$i_id) {
         return;
     }
     # no need to read if redisplaying invalid form
     if ($this->IsValid()) {
         $this->match_manager->ReadByMatchId(array($i_id));
         $this->tournament = $this->match_manager->GetFirst();
         if ($this->tournament instanceof Match) {
             $this->b_user_is_match_owner = AuthenticationManager::GetUser()->GetId() == $this->tournament->GetAddedBy()->GetId();
             $this->b_is_tournament = $this->tournament->GetMatchType() == MatchType::TOURNAMENT;
         } else {
             return;
         }
     }
     if ($this->b_user_is_match_admin or $this->b_user_is_match_owner) {
         # get all grounds
         require_once 'stoolball/ground-manager.class.php';
         $o_ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
         $o_ground_manager->ReadAll();
         $this->editor->Grounds()->SetItems($o_ground_manager->GetItems());
         unset($o_ground_manager);
         # get teams in seasons, in order to promote home grounds of teams
         $season_ids = array();
         foreach ($this->tournament->Seasons() as $season) {
             $season_ids[] = $season->GetId();
         }
         require_once 'stoolball/team-manager.class.php';
         $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
         if (count($season_ids)) {
             $team_manager->ReadBySeasonId($season_ids);
             $this->editor->ProbableTeams()->SetItems($team_manager->GetItems());
         }
         unset($team_manager);
     }
 }
 /**
  * Creates the controls when the editor is in its season view
  *
  */
 private function CreateSeasonControls(Match $match, XhtmlElement $match_box)
 {
     /* @var $season Season */
     $css_class = 'TournamentEdit checkBoxList';
     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);
     $heading = 'Select seasons';
     if ($this->show_step_number) {
         $heading .= ' – step 3 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"));
     # Preserve match title, because we need it to send the email when the seasons are saved
     $title = new TextBox($this->GetNamingPrefix() . 'Title', $match->GetTitle(), $this->IsValidSubmit());
     $title->SetMode(TextBoxMode::Hidden());
     $match_box->AddControl($title);
     # If the list of seasons includes ones in which the only match type is tournament, then
     # they're annual tournaments like Expo and Seaford. Although those tournaments can be listed
     # in other seasons (they're of interest to the league teams), we don't want other tournaments
     # listed on pages which are supposed to be just about those annual tournaments. So exclude them
     # from the collection of possible seasons. Next bit of code will add them back in for any
     # tournaments which actually are meant to be in those seasons.
     $seasons = $this->seasons->GetItems();
     $len = count($seasons);
     for ($i = 0; $i < $len; $i++) {
         # Make sure only seasons which contain tournaments are listed. Necessary to include all match types
         # in the Seasons() collection from the database so that we can test what other match types seasons support.
         if (!$seasons[$i]->MatchTypes()->Contains(MatchType::TOURNAMENT)) {
             unset($seasons[$i]);
             continue;
         }
         if ($seasons[$i]->MatchTypes()->GetCount() == 1 and $seasons[$i]->MatchTypes()->GetFirst() == MatchType::TOURNAMENT) {
             unset($seasons[$i]);
         }
     }
     $this->seasons->SetItems($seasons);
     # If the list of possible seasons doesn't include the one(s) the match is already in,
     # or the ones the context team plays in, add those to the list of possibles
     $a_season_ids = array();
     foreach ($this->seasons as $season) {
         $a_season_ids[] = $season->GetId();
     }
     foreach ($match->Seasons() as $season) {
         if (!in_array($season->GetId(), $a_season_ids, true)) {
             $this->seasons->Insert($season);
             $a_season_ids[] = $season->GetId();
         }
     }
     if (isset($this->context_team)) {
         $match_year = Date::Year($match->GetStartTime());
         foreach ($this->context_team->Seasons() as $team_in_season) {
             /* @var $team_in_season TeamInSeason */
             if (!in_array($team_in_season->GetSeasonId(), $a_season_ids, true) and ($team_in_season->GetSeason()->GetStartYear() == $match_year or $team_in_season->GetSeason()->GetEndYear() == $match_year)) {
                 $this->seasons->Insert($team_in_season->GetSeason());
                 $a_season_ids[] = $team_in_season->GetSeasonId();
             }
         }
     }
     require_once 'xhtml/forms/checkbox.class.php';
     $seasons_list = '';
     if ($this->seasons->GetCount()) {
         # Sort the seasons by name, because they've been messed up by the code above
         $this->seasons->SortByProperty('GetCompetitionName');
         $match_box->AddControl(new XhtmlElement('p', 'Tick all the places we should list your tournament:'));
         $match_box->AddControl('<div class="radioButtonList">');
         foreach ($this->seasons as $season) {
             # Select season if it's one of the seasons the match is already in
             $b_season_selected = false;
             foreach ($match->Seasons() as $match_season) {
                 $b_season_selected = ($b_season_selected or $match_season->GetId() == $season->GetId());
             }
             /* @var $season Season */
             $box = new CheckBox($this->GetNamingPrefix() . 'Season' . $season->GetId(), $season->GetCompetitionName(), $season->GetId(), $b_season_selected, $this->IsValidSubmit());
             $seasons_list .= $season->GetId() . ';';
             $match_box->AddControl($box);
         }
         $match_box->AddControl('</div>');
         # Remember all the season ids to make it much easier to find the data on postback
         $seasons = new TextBox($this->GetNamingPrefix() . 'Seasons', $seasons_list, $this->IsValidSubmit());
         $seasons->SetMode(TextBoxMode::Hidden());
         $match_box->AddControl($seasons);
     } else {
         $match_month = 'in ' . Date::MonthAndYear($match->GetStartTime());
         $type = strtolower(PlayerType::Text($match->GetPlayerType()));
         $match_box->AddControl(new XhtmlElement('p', "Unfortunately we don't have details of any {$type} competitions {$match_month} to list your tournament in."));
         $match_box->AddControl(new XhtmlElement('p', 'Please click \'Save tournament\' to continue.'));
     }
 }
 /**
  * Saves the seasons a match is in
  *
  * @param Match $match
  * @param bool $b_is_new_match
  */
 public function SaveSeasons(Match $match, $b_is_new_match)
 {
     $s_match = $this->GetSettings()->GetTable('Match');
     $s_season_match = $this->GetSettings()->GetTable('SeasonMatch');
     $season_table = $this->GetSettings()->GetTable('Season');
     $comp_table = $this->GetSettings()->GetTable('Competition');
     # Get ids of the tournament matches as well as this match, because they must necessarily be in the same season as their tournament
     # so we'll update the tournament matches whenever we update the season
     $a_matches_in_seasons = array();
     # Check GetId() rather than $b_is_new_match because match is being added as a multi-part process. Even though it's
     # new, by this point in the process it has an id that we need to use.
     if ($match->GetId()) {
         $a_matches_in_seasons[] = $match->GetId();
     }
     if (!$b_is_new_match and $match->GetMatchType() == MatchType::TOURNAMENT) {
         $s_sql = "SELECT match_id FROM {$s_match} WHERE tournament_match_id = " . Sql::ProtectNumeric($match->GetId());
         $o_result = $this->GetDataConnection()->query($s_sql);
         while ($row = $o_result->fetch()) {
             $a_matches_in_seasons[] = $row->match_id;
         }
         $o_result->closeCursor();
     }
     # All changes to master data from here are logged, because this method can be called from the public interface
     # Clear out seasons for this match and its tournament matches, ready to re-insert
     if (count($a_matches_in_seasons)) {
         $sql = "DELETE FROM {$s_season_match} WHERE match_id IN (" . join(', ', $a_matches_in_seasons) . ')';
         $this->LoggedQuery($sql);
     }
     # Add seasons again with new data
     foreach ($match->Seasons() as $season) {
         /* @var $season Season */
         foreach ($a_matches_in_seasons as $i_match_id) {
             $sql = "INSERT INTO {$s_season_match} (season_id, match_id) VALUES (" . Sql::ProtectNumeric($season->GetId()) . ', ' . Sql::ProtectNumeric($i_match_id) . ') ';
             $this->LoggedQuery($sql);
         }
         # If participation in this match implies the team is part of the whole season (ie not practice or tournament or friendly),
         # make sure the team is in the season
         if ($match->GetMatchType() == MatchType::CUP or $match->GetMatchType() == MatchType::LEAGUE) {
             require_once 'stoolball/season-manager.class.php';
             $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
             if ($match->GetHomeTeamId()) {
                 $season_manager->EnsureTeamIsInSeason($match->GetHomeTeamId(), $season->GetId());
             }
             $a_away = $match->GetAwayTeams();
             if (is_array($a_away) and count($a_away)) {
                 foreach ($a_away as $o_away_team) {
                     if (is_null($o_away_team)) {
                         continue;
                     }
                     $season_manager->EnsureTeamIsInSeason($o_away_team->GetId(), $season->GetId());
                 }
             }
             unset($season_manager);
         }
     }
     # The number of players in the match is derived from the competitions it's in. It's never entered directly even
     # by admins or displayed. Done to save extra queries when rendering scorecard editing interfaces. If people want to
     # display the number of players per match they can enter the results with the players' names.
     # Get the max number of players who may play based on the competitions the match is in, so long as this isn't a tournament or friendly.
     # For a tournament we'll ask the user instead, so just ignore this code and keep the existing value. Even in a season different
     # tournaments may have different rules, so really can't automate. Friendlies are just as flexible so, again, can't predict.
     if ($match->GetId() and $match->GetMatchType() != MatchType::TOURNAMENT and $match->GetMatchType() != MatchType::TOURNAMENT_MATCH and $match->GetMatchType() != MatchType::FRIENDLY) {
         $season_ids = array();
         foreach ($match->Seasons() as $season) {
             $season_ids[] = $season->GetId();
         }
         if (count($season_ids)) {
             $s_sql = "SELECT MAX({$comp_table}.players_per_team) AS players_per_team, MAX({$comp_table}.overs) AS overs\r\n\t\t\t\tFROM {$season_table} INNER JOIN {$comp_table} ON {$season_table}.competition_id = {$comp_table}.competition_id\r\n\t\t\t\tWHERE {$season_table}.season_id IN (" . implode(',', $season_ids) . ")";
             $result = $this->GetDataConnection()->query($s_sql);
             if (!$this->GetDataConnection()->isError()) {
                 $row = $result->fetch();
                 $match->SetMaximumPlayersPerTeam($row->players_per_team);
                 $match->SetOvers($row->overs);
             }
             $result->closeCursor();
         }
         # Update the match. Using the GetMaximumPlayersPerTeam property because it will give us the existing value
         # (possibly the default value if the code above didn't run because there were no seasons).
         $sql = "UPDATE {$s_match} SET\r\n\t\t\t\t\t\tplayers_per_team = " . Sql::ProtectNumeric($match->GetMaximumPlayersPerTeam()) . ",\r\n\t\t\t\t\t\tovers = " . Sql::ProtectNumeric($match->GetOvers()) . "\r\n\t\t\t\t\t\tWHERE match_id = " . Sql::ProtectNumeric($match->GetId());
         $this->LoggedQuery($sql);
     }
     # This season is mentioned in search results for a match, so request an update,
     # and note for auditing that the match has been changed
     $sql = "UPDATE nsa_match SET \r\n\t    update_search = 1, \r\n\t    date_changed = " . gmdate("U") . ", \r\n        modified_by_id = " . Sql::ProtectNumeric(AuthenticationManager::GetUser()->GetId()) . "\r\n\t    WHERE match_id = " . Sql::ProtectNumeric($match->GetId(), false);
     $this->LoggedQuery($sql);
     # Match data has changed so notify moderator
     $this->QueueForNotification($match->GetId(), $b_is_new_match);
 }
 function OnLoadPageData()
 {
     /* @var $match_manager MatchManager */
     /* @var $editor MatchEditControl */
     # get id of Match
     $i_id = $this->match_manager->GetItemId();
     if ($i_id) {
         # Get details of match but, if invalid, don't replace submitted details with saved ones
         if ($this->IsValid()) {
             $this->match_manager->ReadByMatchId(array($i_id));
             $this->match_manager->ExpandMatchScorecards();
             $this->match = $this->match_manager->GetFirst();
             if ($this->match instanceof Match) {
                 $this->b_user_is_match_owner = AuthenticationManager::GetUser()->GetId() == $this->match->GetAddedBy()->GetId();
                 $this->b_is_tournament = $this->match->GetMatchType() == MatchType::TOURNAMENT;
             }
         }
         unset($this->match_manager);
         # get all competitions if user has permission to change the season
         if ($this->b_user_is_match_admin) {
             require_once 'stoolball/competition-manager.class.php';
             $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
             $o_comp_manager->ReadAllSummaries();
             $this->editor->SetSeasons(CompetitionManager::GetSeasonsFromCompetitions($o_comp_manager->GetItems()));
             unset($o_comp_manager);
         }
         if ($this->b_user_is_match_admin or $this->b_user_is_match_owner) {
             # get all teams
             $season_ids = array();
             if ($this->match instanceof Match) {
                 foreach ($this->match->Seasons() as $season) {
                     $season_ids[] = $season->GetId();
                 }
             }
             require_once 'stoolball/team-manager.class.php';
             $o_team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
             if ($this->match instanceof Match and $this->match->GetMatchType() == MatchType::TOURNAMENT_MATCH) {
                 $o_team_manager->FilterByTournament(array($this->match->GetTournament()->GetId()));
                 $o_team_manager->FilterByTeamType(array());
                 # override default to allow all team types
                 $o_team_manager->ReadTeamSummaries();
             } else {
                 if ($this->b_user_is_match_admin or !count($season_ids) or $this->match->GetMatchType() == MatchType::FRIENDLY) {
                     $o_team_manager->ReadById();
                     # we need full data on the teams to get the seasons they are playing in;
                 } else {
                     # If the user can't change the season, why let them select a team that's not in the season?
                     $o_team_manager->ReadBySeasonId($season_ids);
                 }
             }
             $this->editor->SetTeams(array($o_team_manager->GetItems()));
             unset($o_team_manager);
             # get all grounds
             require_once 'stoolball/ground-manager.class.php';
             $o_ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
             $o_ground_manager->ReadAll();
             $this->editor->SetGrounds($o_ground_manager->GetItems());
             unset($o_ground_manager);
         }
     }
     # Tournament or match not found is page not found
     if (!$this->match instanceof Match or $this->b_is_tournament) {
         http_response_code(404);
         $this->page_not_found = true;
     }
 }
 function OnLoadPageData()
 {
     /* @var $o_last_match Match */
     /* @var $season Season */
     /* @var $team Team */
     # First best guess at where user came from is the page field posted back,
     # second best is tournaments page. Either can be tampered with, so there will be
     # a check later before they're used for redirection. If there's a context
     # season or team its URL will be read from the db and overwrite this later.
     if (isset($_POST['page'])) {
         $this->destination_url_if_cancelled = $_POST['page'];
     } else {
         $this->destination_url_if_cancelled = "/tournaments";
     }
     # new data manager
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     $match_manager->FilterByMatchType(array(MatchType::TOURNAMENT));
     # Check whether cancel was clicked
     if ($this->IsPostback() and $this->editor->CancelClicked()) {
         # new tournament, nothing saved yet, so just send user back where they came from
         $this->Cancel();
     }
     # Save match
     if ($this->IsPostback() and $this->IsValid()) {
         # Get posted match
         $this->tournament = $this->editor->GetDataObject();
         # Save match
         $match_manager->SaveFixture($this->tournament);
         if (count($this->tournament->GetAwayTeams())) {
             $match_manager->SaveTeams($this->tournament);
         }
         if ($this->season instanceof Season) {
             $this->tournament->Seasons()->Add($this->season);
             $match_manager->SaveSeasons($this->tournament, true);
         }
         http_response_code(303);
         $this->Redirect($this->tournament->AddTournamentTeamsUrl());
     }
     if (isset($this->season)) {
         $season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
         $season_manager->ReadById(array($this->season->GetId()));
         $this->season = $season_manager->GetFirst();
         $this->editor->SetContextSeason($this->season);
         $this->destination_url_if_cancelled = $this->season->GetNavigateUrl();
         unset($season_manager);
         # If we're adding a match to a season, get last game in season for its date
         $match_manager->ReadLastInSeason($this->season->GetId());
     }
     if (isset($this->team)) {
         # Get more information about the team itself
         require_once 'stoolball/team-manager.class.php';
         $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
         $team_manager->ReadById(array($this->team->GetId()));
         $this->team = $team_manager->GetFirst();
         $this->editor->SetContextTeam($this->team);
         $this->destination_url_if_cancelled = $this->team->GetNavigateUrl();
         # Get the last game already scheduled for the team to use its date
         $match_manager->ReadLastForTeam($this->team->GetId());
         # Read teams played in the last year in order to get their grounds, which will be put at the top of the select list
         $team_manager->ReadRecentOpponents(array($this->team->GetId()), 12);
         $this->editor->ProbableTeams()->SetItems($team_manager->GetItems());
         unset($team_manager);
     }
     # Use the date of the most recent tournament if it was this year, otherwise just use the default of today
     $o_last_match = $match_manager->GetFirst();
     if (is_object($o_last_match) and gmdate('Y', $o_last_match->GetStartTime()) == gmdate('Y')) {
         if ($o_last_match->GetIsStartTimeKnown()) {
             # If the last match has a time, use it
             $this->editor->SetDefaultTime($o_last_match->GetStartTime());
         } else {
             # If the last match has no time, use 11am BST
             $this->editor->SetDefaultTime(gmmktime(10, 0, 0, gmdate('m', $o_last_match->GetStartTime()), gmdate('d', $o_last_match->GetStartTime()), gmdate('Y', $o_last_match->GetStartTime())));
         }
     }
     unset($match_manager);
     # Get grounds
     $o_ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
     $o_ground_manager->ReadAll();
     $a_grounds = $o_ground_manager->GetItems();
     $this->editor->Grounds()->SetItems($a_grounds);
     unset($o_ground_manager);
 }
 /**
  * Builds a match object containing the result information posted by the control
  *
  */
 public function BuildPostedDataObject()
 {
     $o_match = new Match($this->GetSettings());
     # Get match id
     $s_key = $this->GetNamingPrefix() . 'item';
     if (isset($_POST[$s_key])) {
         $s_id = $_POST[$s_key];
         if (strlen($s_id)) {
             $o_match->SetId($s_id);
         }
     }
     # Get the short URL
     $s_key = $this->GetNamingPrefix() . 'ShortUrl';
     if (isset($_POST[$s_key])) {
         $o_match->SetShortUrl($_POST[$s_key]);
     }
     # Get the start date
     $s_key = $this->GetNamingPrefix() . 'Start';
     $o_match->SetStartTime(DateControl::GetPostedTimestampUtc($s_key));
     $o_match->SetIsStartTimeKnown(DateControl::GetIsTimePosted($s_key));
     # Get the home team
     # Test for (int)$_POST[$s_key] deliberately excludes "Not known" value, which is 0
     $o_home = new Team($this->GetSettings());
     $s_key = $this->GetNamingPrefix() . 'Home';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and (int) $_POST[$s_key]) {
         $o_home->SetId($_POST[$s_key]);
         $o_match->SetHomeTeam($o_home);
     }
     # Get the away team
     # Test for (int)$_POST[$s_key] deliberately excludes "Not known" value, which is 0
     $o_away = new Team($this->GetSettings());
     $s_key = $this->GetNamingPrefix() . 'Away';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key]) and (int) $_POST[$s_key]) {
         $o_away->SetId($_POST[$s_key]);
         $o_match->SetAwayTeam($o_away);
     }
     # Get the ground
     $s_key = $this->GetNamingPrefix() . 'Ground';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $o_ground = new Ground($this->GetSettings());
         $o_ground->SetId($_POST[$s_key]);
         $o_match->SetGround($o_ground);
     }
     # Get the notes
     $s_key = $this->GetNamingPrefix() . 'Notes';
     if (isset($_POST[$s_key])) {
         $o_match->SetNotes($_POST[$s_key]);
     }
     # Get the match type
     $s_key = $this->GetNamingPrefix() . 'MatchType';
     if (isset($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
         $o_match->SetMatchType($_POST[$s_key]);
     }
     # Get the tournament
     if ($o_match->GetMatchType() == MatchType::TOURNAMENT_MATCH) {
         $s_key = $this->GetNamingPrefix() . 'Tournament';
         if (isset($_POST[$s_key]) and is_numeric($_POST[$s_key])) {
             $tournament = new Match($this->GetSettings());
             $tournament->SetMatchType(MatchType::TOURNAMENT);
             $tournament->SetId($_POST[$s_key]);
             $o_match->SetTournament($tournament);
         }
     }
     # Get the season
     $s_key = $this->GetNamingPrefix() . 'Season';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $o_season = new Season($this->GetSettings());
         $o_season->SetId($_POST[$s_key]);
         $o_match->Seasons()->Add($o_season);
     }
     $this->SetDataObject($o_match);
 }
    function OnPageLoad()
    {
        if (!$this->match instanceof Match) {
            echo new XhtmlElement('h1', ucfirst($this->match_or_tournament) . ' already deleted');
            echo new XhtmlElement('p', "The " . $this->match_or_tournament . " you're trying to delete does not exist or has already been deleted.");
            return;
        }
        echo new XhtmlElement('h1', 'Delete ' . $this->match_or_tournament . ': <cite>' . htmlentities($this->match->GetTitle(), ENT_QUOTES, "UTF-8", false) . '</cite>');
        if ($this->b_deleted) {
            ?>
			<p>The <?php 
            echo $this->match_or_tournament;
            ?>
 has been deleted.</p>
			<?php 
            if ($this->match->GetTournament() instanceof Match) {
                echo '<p><a href="' . htmlentities($this->match->GetTournament()->GetNavigateUrl(), ENT_QUOTES, "UTF-8", false) . '">Go to ' . htmlentities($this->match->GetTournament()->GetTitle(), ENT_QUOTES, "UTF-8", false) . '</a></p>';
            } else {
                if ($this->match->Seasons()->GetCount()) {
                    foreach ($this->match->Seasons() as $season) {
                        echo '<p><a href="' . htmlentities($season->GetNavigateUrl(), ENT_QUOTES, "UTF-8", false) . '">Go to ' . htmlentities($season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false) . '</a></p>';
                    }
                } else {
                    echo '<p><a href="/matches/">View all matches</a></p>';
                }
            }
        } else {
            $has_permission = (AuthenticationManager::GetUser()->Permissions()->HasPermission(PermissionType::MANAGE_MATCHES) or AuthenticationManager::GetUser()->GetId() == $this->match->GetAddedBy()->GetId());
            if ($has_permission) {
                $s_detail = 'This is a ' . MatchType::Text($this->match->GetMatchType());
                $s_detail .= $this->match->GetIsStartTimeKnown() ? ' starting at ' : ' on ';
                $s_detail .= $this->match->GetStartTimeFormatted() . '. ';
                $s_context = '';
                if ($this->match->GetTournament() instanceof Match) {
                    $s_context = "It's in the " . $this->match->GetTournament()->GetTitle();
                }
                if ($this->match->Seasons()->GetCount()) {
                    $season = $this->match->Seasons()->GetFirst();
                    $b_the = !(stristr($season->GetCompetitionName(), 'the ') === 0);
                    $s_context .= $s_context ? ', in ' : 'It\'s in ';
                    $s_context .= $b_the ? 'the ' : '';
                    if ($this->match->Seasons()->GetCount() == 1) {
                        $s_context .= $season->GetCompetitionName() . '.';
                    } else {
                        $s_context .= 'following seasons: ';
                    }
                }
                $s_detail .= $s_context;
                echo new XhtmlElement('p', htmlentities($s_detail, ENT_QUOTES, "UTF-8", false));
                if ($this->match->Seasons()->GetCount() > 1) {
                    $seasons = new XhtmlElement('ul');
                    foreach ($this->match->Seasons() as $season) {
                        $seasons->AddControl(new XhtmlElement('li', htmlentities($season->GetCompetitionName(), ENT_QUOTES, "UTF-8", false)));
                    }
                    echo $seasons;
                }
                if ($this->match->GetMatchType() == MatchType::TOURNAMENT) {
                    ?>
					<p>Deleting a tournament cannot be undone.</p>
					<?php 
                } else {
                    ?>
					<p>Deleting a match cannot be undone. The match will be removed from all league tables and statistics.</p>
					<?php 
                }
                ?>
				<p>Are you sure you want to delete this <?php 
                echo $this->match_or_tournament;
                ?>
?</p>
				<form action="<?php 
                echo htmlentities($this->match->GetDeleteNavigateUrl(), ENT_QUOTES, "UTF-8", false);
                ?>
" method="post" class="deleteButtons">
				<div>
				<input type="submit" value="Delete <?php 
                echo $this->match_or_tournament;
                ?>
" name="delete" />
				<input type="submit" value="Cancel" name="cancel" />
				</div>
				</form>
				<?php 
                $this->AddSeparator();
                require_once 'stoolball/user-edit-panel.class.php';
                $panel = new UserEditPanel($this->GetSettings(), 'this ' . $this->match_or_tournament);
                $panel->AddLink('view this ' . $this->match_or_tournament, $this->match->GetNavigateUrl());
                $panel->AddLink('edit this ' . $this->match_or_tournament, $this->match->GetEditNavigateUrl());
                echo $panel;
            } else {
                ?>
				<p>Sorry, you can't delete a <?php 
                echo $this->match_or_tournament;
                ?>
 unless you added it.</p>
				<p><a href="<?php 
                echo htmlentities($this->match->GetNavigateUrl(), ENT_QUOTES, "UTF-8", false);
                ?>
">Go back to <?php 
                echo $this->match_or_tournament;
                ?>
</a></p>
				<?php 
            }
        }
    }
 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);
 }