function OnPostback()
 {
     # Get the match info and store it
     $i_id = $this->manager->GetItemId($this->match);
     $this->manager->ReadByMatchId(array($i_id));
     $this->match = $this->manager->GetFirst();
     if (!$this->match instanceof Match) {
         # This can be the case if the back button is used to go back to the "match has been deleted" page.
         $this->b_deleted = true;
         return;
     }
     # Check whether cancel was clicked
     if (isset($_POST['cancel'])) {
         $this->Redirect($this->match->GetNavigateUrl());
     }
     # Check whether delete was clicked
     if (isset($_POST['delete'])) {
         # Check again that the requester has permission to delete this match
         $has_permission = (AuthenticationManager::GetUser()->Permissions()->HasPermission(PermissionType::MANAGE_MATCHES) or AuthenticationManager::GetUser()->GetId() == $this->match->GetAddedBy()->GetId());
         if ($has_permission) {
             # Delete the match
             $this->manager->DeleteMatch(array($i_id));
             # Remove match, and all dependent tournament matches, from search results
             foreach ($this->match->GetMatchesInTournament() as $tournament_match) {
                 $this->SearchIndexer()->DeleteFromIndexById("match" . $tournament_match->GetId());
             }
             $this->SearchIndexer()->DeleteFromIndexById("match" . $this->match->GetId());
             $this->SearchIndexer()->CommitChanges();
             require_once 'stoolball/data-change-notifier.class.php';
             $notifier = new DataChangeNotifier($this->GetSettings());
             $notifier->MatchUpdated($this->match, AuthenticationManager::GetUser(), false, true);
             # Update 'next 5 matches'
             $this->manager->ReadNext();
             $this->a_next_matches = $this->manager->GetItems();
             # Note success
             $this->b_deleted = true;
         }
     }
 }
 /**
  * Notifies the moderator(s) for a match that it has been added or updated. If no arguments, send all outstanding notifications.
  * @param int $match_id
  * @return void
  */
 public function NotifyMatchModerator($match_id = null)
 {
     # Check in the queue whether to actually send the email, because even if "Save" is clicked on a match it may not have
     # been updated if the data didn't change. If the data changed there should be an entry in the queue.
     require_once 'data/queue-action.enum.php';
     $queue = $this->GetSettings()->GetTable('Queue');
     $user = $this->GetSettings()->GetTable('User');
     $this->Lock($queue);
     $sql = "SELECT DISTINCT {$queue}.data, {$queue}.action, {$user}.user_id, {$user}.known_as, {$user}.email\r\n\t\tFROM {$queue} LEFT JOIN {$user} ON {$queue}.user_id = {$user}.user_id\r\n\t\tWHERE action IN (" . QueueAction::MATCH_ADDED . "," . QueueAction::MATCH_UPDATED . ") ";
     $match_ids = array();
     if (is_null($match_id)) {
         # Get any notifications from over an hour ago. That should only be ones where the user clicked away from an add/update wizard before the last page.
         # Want to avoid logging an update on p1 of a wizard and sending an email before they finish p4 and submit.
         $sql .= "AND {$queue}.date_added < " . (gmdate('U') - 60 * 60);
     } else {
         # Check in the queue for the specific match
         $sql .= "AND {$queue}.data " . Sql::ProtectNumeric($match_id, false, true);
     }
     $result = $this->GetDataConnection()->query($sql);
     while ($row = $result->fetch()) {
         $user_who_updated_match = new User($row->user_id, $row->known_as);
         $user_who_updated_match->SetEmail($row->email);
         $match_ids[(int) $row->data] = array($user_who_updated_match, (int) $row->action == QueueAction::MATCH_ADDED);
     }
     $result->closeCursor();
     unset($result);
     if (count($match_ids)) {
         # Now that we know what notification to send, remove those match(es) from the queue
         $sql = "DELETE FROM {$queue} WHERE action IN (" . QueueAction::MATCH_ADDED . "," . QueueAction::MATCH_UPDATED . ") AND data IN (" . join(', ', array_keys($match_ids)) . ") ";
         $this->GetDataConnection()->query($sql);
     }
     $this->Unlock();
     # Get info on the matches needed to send the email
     if (count($match_ids)) {
         $s_match = $this->GetSettings()->GetTable('Match');
         $s_season = $this->GetSettings()->GetTable('Season');
         $s_season_match = $this->GetSettings()->GetTable('SeasonMatch');
         $s_comp = $this->GetSettings()->GetTable('Competition');
         $s_ground = $this->GetSettings()->GetTable('Ground');
         $s_mt = $this->GetSettings()->GetTable('MatchTeam');
         $sql = "SELECT {$s_match}.match_id, {$s_match}.match_title, {$s_match}.start_time, {$s_match}.start_time_known, {$s_match}.match_type, {$s_match}.short_url,\r\n\t\t\t{$s_match}.home_bat_first, {$s_match}.match_notes, {$s_match}.home_runs, {$s_match}.home_wickets, {$s_match}.away_runs, {$s_match}.away_wickets,\r\n\t\t\t{$s_season}.season_id, {$s_season}.season_name, {$s_season}.start_year, {$s_season}.end_year, " . $s_comp . '.competition_id, ' . $s_comp . '.competition_name, ' . $s_comp . '.notification_email, ' . "{$s_ground}.ground_id, {$s_ground}.saon, {$s_ground}.paon, {$s_ground}.town,\r\n\t\t\thome_team.team_id AS home_team_id, home_team.team_name AS home_team_name,\r\n\t\t\taway_team.team_id AS away_team_id, away_team.team_name AS away_team_name\r\n\t\t\tFROM ((((((({$s_match} LEFT OUTER JOIN {$s_season_match} ON {$s_match}.match_id = {$s_season_match}.match_id) " . 'LEFT OUTER JOIN ' . $s_season . ' ON ' . $s_season_match . '.season_id = ' . $s_season . '.season_id) ' . 'LEFT OUTER JOIN ' . $s_comp . ' ON ' . $s_season . '.competition_id = ' . $s_comp . '.competition_id) ' . 'LEFT OUTER JOIN ' . $s_ground . ' ON ' . $s_match . '.ground_id = ' . $s_ground . '.ground_id) ' . 'LEFT OUTER JOIN ' . $s_mt . ' AS home_link ON ' . $s_match . '.match_id = home_link.match_id AND home_link.team_role = ' . TeamRole::Home() . ') ' . 'LEFT OUTER JOIN nsa_team AS home_team ON home_link.team_id = home_team.team_id AND home_link.team_role = ' . TeamRole::Home() . ")\r\n\t\t\tLEFT OUTER JOIN {$s_mt} AS away_link ON {$s_match}.match_id = away_link.match_id AND away_link.team_role = " . TeamRole::Away() . ') ' . 'LEFT OUTER JOIN nsa_team AS away_team ON away_link.team_id = away_team.team_id AND away_link.team_role = ' . TeamRole::Away() . "\r\n\t\t\tWHERE {$s_match}.match_id IN (" . join(', ', array_keys($match_ids)) . ") ";
         # run query
         $result = $this->GetDataConnection()->query($sql);
         $this->BuildItems($result);
         $result->closeCursor();
         unset($result);
         # send email
         require_once 'data-change-notifier.class.php';
         $o_notify = new DataChangeNotifier($this->GetSettings());
         foreach ($this->GetItems() as $match) {
             $o_notify->MatchUpdated($match, $match_ids[$match->GetId()][0], $match_ids[$match->GetId()][1]);
         }
     }
 }