/**
  * Builds a match object containing the result information posted by the control
  *
  */
 public function BuildPostedDataObject()
 {
     $match = new Match($this->GetSettings());
     $match->SetMatchType(MatchType::TOURNAMENT);
     # Get match id
     $s_key = $this->GetNamingPrefix() . 'item';
     if (isset($_POST[$s_key])) {
         $s_id = $_POST[$s_key];
         if (strlen($s_id)) {
             $match->SetId($s_id);
         }
     }
     # Get the title
     $s_key = $this->GetNamingPrefix() . 'Title';
     if (isset($_POST[$s_key])) {
         $match->SetTitle(strip_tags($_POST[$s_key]));
     }
     # Get the qualification type
     $s_key = $this->GetNamingPrefix() . 'Qualify';
     if (isset($_POST[$s_key])) {
         $match->SetQualificationType($_POST[$s_key]);
     }
     # Get the player type
     $s_key = $this->GetNamingPrefix() . 'PlayerType';
     if (isset($_POST[$s_key])) {
         $match->SetPlayerType($_POST[$s_key]);
     }
     $s_key = $this->GetNamingPrefix() . "Players";
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $match->SetMaximumPlayersPerTeam($_POST[$s_key]);
     }
     # Get the number of overs
     $s_key = $this->GetNamingPrefix() . "Overs";
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $match->SetOvers($_POST[$s_key]);
     }
     # Get the short URL
     $s_key = $this->GetNamingPrefix() . 'ShortUrl';
     if (isset($_POST[$s_key])) {
         $match->SetShortUrl($_POST[$s_key]);
     }
     # Get the start date
     $s_key = $this->GetNamingPrefix() . 'Start';
     $match->SetStartTime(DateControl::GetPostedTimestampUtc($s_key));
     $match->SetIsStartTimeKnown(DateControl::GetIsTimePosted($s_key));
     # Get the initial team
     $team = new Team($this->GetSettings());
     $s_key = $this->GetNamingPrefix() . 'ContextTeam';
     if (isset($_POST[$s_key]) and strlen($_POST[$s_key])) {
         $team->SetId($_POST[$s_key]);
         $match->AddAwayTeam($team);
     }
     # 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]);
         $match->SetGround($o_ground);
     }
     # Get the notes
     $s_key = $this->GetNamingPrefix() . 'Notes';
     if (isset($_POST[$s_key])) {
         $match->SetNotes($_POST[$s_key]);
     }
     $this->SetDataObject($match);
 }
 /**
  * Helper to build ground for a match from raw data
  *
  * @param Match $o_match
  * @param DataRow $o_row
  */
 private function BuildGround(Match $o_match, $o_row)
 {
     if (isset($o_row->ground_id)) {
         $o_ground = new Ground($this->GetSettings());
         $o_ground->SetId($o_row->ground_id);
         if (isset($o_row->ground_short_url)) {
             $o_ground->SetShortUrl($o_row->ground_short_url);
         }
         if (isset($o_row->town)) {
             $o_addr = $o_ground->GetAddress();
             $o_addr->SetSaon($o_row->saon);
             $o_addr->SetPaon($o_row->paon);
             if (isset($o_row->street_descriptor)) {
                 $o_addr->SetStreetDescriptor($o_row->street_descriptor);
             }
             if (isset($o_row->locality)) {
                 $o_addr->SetLocality($o_row->locality);
             }
             $o_addr->SetTown($o_row->town);
             if (isset($o_row->postcode)) {
                 $o_addr->SetPostcode($o_row->postcode);
             }
         }
         if (isset($o_row->latitude)) {
             $o_addr->SetGeoLocation($o_row->latitude, $o_row->longitude, $o_row->geo_precision);
         }
         $o_match->SetGround($o_ground);
         unset($o_ground);
     }
 }
 /**
  * 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);
 }
 public function OnPostback()
 {
     # If there's no id, ensure no match object is created. Page will then display a "match not found" message.
     # There's a separate page for adding matches, even for admins.
     if (!$this->editor->GetDataObjectId()) {
         return;
     }
     # Get the submitted match
     $this->match = $this->editor->GetDataObject();
     $check_match = $this->match;
     # Because this is a new request, if the user isn't admin we need to reverify whether this is the match owner
     # before letting anything happen. Can't trust that info from a postback so MUST go to the database to check it.
     if (!$this->b_user_is_match_admin) {
         $this->match_manager->ReadByMatchId(array($this->editor->GetDataObjectId()));
         $check_match = $this->match_manager->GetFirst();
         $this->b_user_is_match_owner = ($check_match instanceof Match and AuthenticationManager::GetUser()->GetId() == $check_match->GetAddedBy()->GetId());
         if ($this->b_user_is_match_owner) {
             # Set the owner of the match. This means the edit control knows who the owner is and therefore
             # whether to display the fixture editor on an invalid postback
             $this->match->SetAddedBy(AuthenticationManager::GetUser());
         } else {
             # If user is neither admin nor owner, they won't have the team info. Get it from the $check_match so
             # that the match title can be updated correctly with a changed result.
             $this->match->SetHomeTeam($check_match->GetHomeTeam());
             $this->match->SetAwayTeam($check_match->GetAwayTeam());
         }
     }
     # Don't wan't to edit tournaments on this page, so even as admin make sure we're not trying to save one.
     # If user's not admin, can't change the match type, so find out what that is from the db too. For admin,
     # $check_match is the submitted one as the match type might've been changed.
     $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         # If so, get the match's short URL and redirect
         $this->match_manager->ExpandMatchUrl($this->match);
         $this->Redirect($this->match->GetNavigateUrl());
     }
     # save data if valid
     if ($this->IsValid() and !$this->b_is_tournament) {
         # Check whether the user has permission to update the fixture as well as the result
         if ($this->b_user_is_match_admin or $this->b_user_is_match_owner) {
             # Get the ground name from the database. This is used when compiling an email about the updated match result.
             if ($this->match->GetGround() instanceof Ground and $this->match->GetGround()->GetId() and !$this->match->GetGround()->GetName()) {
                 require_once 'stoolball/ground-manager.class.php';
                 $ground_manager = new GroundManager($this->GetSettings(), $this->GetDataConnection());
                 $ground_manager->ReadById(array($this->match->GetGround()->GetId()));
                 if ($ground_manager->GetCount()) {
                     $this->match->SetGround($ground_manager->GetFirst());
                 }
                 unset($ground_manager);
             }
             $this->match_manager->SaveFixture($this->match);
             if ($this->b_user_is_match_admin) {
                 $this->match_manager->SaveSeasons($this->match, false);
             }
             $this->editor->SetNavigateUrl($this->match->GetEditNavigateUrl());
             # because edit URL may have changed
         }
         # Save the result
         $this->match_manager->SaveIfPlayed($this->match);
         $this->match_manager->SaveWhoWonTheToss($this->match);
         $this->match_manager->SaveWhoBattedFirst($this->match);
         # If match didn't happen or the teams aren't known yet, save and finish, otherwise go to next page
         $result = $this->match->Result()->GetResultType();
         if ($result == MatchResult::HOME_WIN_BY_FORFEIT or $result == MatchResult::AWAY_WIN_BY_FORFEIT or $result == MatchResult::CANCELLED or $result == MatchResult::POSTPONED or $check_match->GetStartTime() > gmdate('U') or $this->b_user_is_match_admin and (!$this->match->GetHomeTeamId() or !$this->match->GetAwayTeamId())) {
             # Match may have been updated so, first, send an email
             $this->match_manager->NotifyMatchModerator($this->match->GetId());
             http_response_code(303);
             $this->Redirect($this->match->GetNavigateUrl());
         } else {
             http_response_code(303);
             $this->Redirect($this->match->EditScorecardUrl());
         }
     }
 }
 /**
  * Populates the collection of business objects from raw data
  *
  * @return bool
  * @param MySqlRawData $o_result
  */
 protected function BuildItems(MySqlRawData $o_result)
 {
     $this->Clear();
     $o_match_builder = new CollectionBuilder();
     $o_team_builder = new CollectionBuilder();
     while ($o_row = $o_result->fetch()) {
         if (!$o_match_builder->IsDone($o_row->match_id)) {
             if (isset($o_match)) {
                 $this->Add($o_match);
                 $o_team_builder->Reset();
             }
             # create new
             $o_match = new Match($this->GetSettings());
             $o_match->SetId($o_row->match_id);
             if (isset($o_row->start_time)) {
                 $o_match->SetStartTime($o_row->start_time);
             }
             if (isset($o_row->home_runs)) {
                 $o_match->Result()->SetHomeRuns($o_row->home_runs);
             }
             if (isset($o_row->away_runs)) {
                 $o_match->Result()->SetAwayRuns($o_row->away_runs);
             }
             if (isset($o_row->match_result_id)) {
                 $o_match->Result()->SetResultType($o_row->match_result_id);
             }
             if (isset($o_row->home_team_id) and !is_null($o_row->home_team_id)) {
                 $o_home = new Team($this->o_settings);
                 $o_home->SetId($o_row->home_team_id);
                 if (isset($o_row->home_team_name)) {
                     $o_home->SetName($o_row->home_team_name);
                 }
                 if (isset($o_row->home_short_url)) {
                     $o_home->SetShortUrl($o_row->home_short_url);
                 }
                 $o_match->SetHomeTeam($o_home);
                 unset($o_home);
             }
             if (isset($o_row->ground_id)) {
                 $o_ground = new Ground($this->GetSettings());
                 $o_ground->SetId($o_row->ground_id);
                 $o_match->SetGround($o_ground);
                 unset($o_ground);
             }
         }
         # Add away teams
         if (isset($o_row->away_team_id) && !$o_team_builder->IsDone($o_row->away_team_id)) {
             $o_away = new Team($this->o_settings);
             $o_away->SetId($o_row->away_team_id);
             if (isset($o_row->away_team_name)) {
                 $o_away->SetName($o_row->away_team_name);
             }
             if (isset($o_row->away_short_url)) {
                 $o_away->SetShortUrl($o_row->away_short_url);
             }
             $o_match->AddAwayTeam($o_away);
             unset($o_away);
         }
     }
     # Add final match
     if (isset($o_match)) {
         $this->Add($o_match);
     }
     return true;
 }