function OnPostback()
 {
     # Get the item info and store it
     $id = $this->manager->GetItemId($this->data_object);
     $this->manager->ReadById(array($id));
     $this->data_object = $this->manager->GetFirst();
     # Check whether cancel was clicked
     if (isset($_POST['cancel'])) {
         $this->Redirect($this->data_object->GetNavigateUrl());
     }
     # Check whether delete was clicked
     if (isset($_POST['delete'])) {
         # Only offer delete option if there's more than one season. Don't want to delete last season because
         # that leaves an empty competition which won't display. Instead, must delete whole competition with its one remaining season.
         # How many seasons in this competition?
         if (is_object($this->data_object) and $this->data_object->GetCompetition() instanceof Competition and $this->data_object->GetCompetition()->GetId()) {
             $this->manager->Clear();
             $this->manager->ReadByCompetitionId(array($this->data_object->GetCompetition()->GetId()));
             $this->seasons_in_competition = $this->manager->GetCount();
         }
         if ($this->seasons_in_competition > 1) {
             # Check again that the requester has permission to delete this item
             if ($this->has_permission) {
                 # Delete it
                 $this->manager->Delete(array($id));
                 # Update the competition in the search engine, because the latest season may have changed.
                 require_once 'stoolball/competition-manager.class.php';
                 require_once "search/competition-search-adapter.class.php";
                 $this->SearchIndexer()->DeleteFromIndexById("competition" . $this->data_object->GetCompetition()->GetId());
                 $competition_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
                 $competition_manager->ReadById(array($this->data_object->GetCompetition()->GetId()));
                 $competition = $competition_manager->GetFirst();
                 $adapter = new CompetitionSearchAdapter($competition);
                 $this->SearchIndexer()->Index($adapter->GetSearchableItem());
                 $this->SearchIndexer()->CommitChanges();
                 # Note success
                 $this->deleted = true;
             }
         }
     }
 }
 function OnPostback()
 {
     $this->season = $this->edit->GetDataObject();
     if (!$this->season->GetId()) {
         $existing_season_id = $this->season_manager->CheckIfSeasonExists($this->season->GetCompetition()->GetId(), $this->season->GetStartYear(), $this->season->GetEndYear());
         if ($existing_season_id) {
             require_once 'data/validation/required-field-validator.class.php';
             $season = new Season($this->GetSettings());
             $season->SetId($existing_season_id);
             $validator = new RequiredFieldValidator('This_Validator_Will_Fail', "The season you're adding already exists &#8211; <a href=\"" . $season->GetNavigateUrl() . "\">edit season</a>");
             $validator->SetValidIfNotFound(false);
             $this->edit->AddValidator($validator);
         }
     }
     # Get the competition. This is used to build the page title and, when saving, the short URL.
     # It is also re-indexed in search below.
     $this->competition_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     $this->competition_manager->ReadById(array($this->season->GetCompetition()->GetId()));
     $this->season->SetCompetition($this->competition_manager->GetFirst());
     unset($this->competition_manager);
     # save data if valid
     if ($this->IsValid()) {
         $b_saved_new = !(bool) $this->season->GetId();
         $id = $this->season_manager->SaveSeason($this->season);
         $this->season->SetId($id);
         # Add the competition to the search  engine. Re-request so we have team names as well as IDs.
         require_once "search/competition-search-adapter.class.php";
         $this->SearchIndexer()->DeleteFromIndexById("competition" . $this->season->GetCompetition()->GetId());
         $adapter = new CompetitionSearchAdapter($this->season->GetCompetition());
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         # If just saved a new season, redirect to load this page from scratch
         # (When just loading data on this page, didn't load correctly into aggregated editors)
         if ($b_saved_new) {
             $this->Redirect($this->season->GetEditSeasonUrl());
         }
         $this->Redirect($this->season->GetNavigateUrl());
     }
 }
 private function IndexCompetitions()
 {
     require_once 'stoolball/competition-manager.class.php';
     require_once 'search/competition-search-adapter.class.php';
     $this->SearchIndexer()->DeleteFromIndexByType("competition");
     $manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     $manager->ReadAll();
     $results = $manager->GetItems();
     unset($manager);
     foreach ($results as $result) {
         $adapter = new CompetitionSearchAdapter($result);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
     }
     $this->SearchIndexer()->CommitChanges();
 }
 function OnLoadPageData()
 {
     /* @var $o_competition Competition */
     # check parameter
     if (!isset($_GET['item']) or !is_numeric($_GET['item'])) {
         http_response_code(400);
         exit;
     }
     # new data managers
     $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
     # get competition
     $latest = (isset($_GET['latest']) and $_GET['latest'] == '1');
     if ($latest) {
         $o_comp_manager->ReadById(array($_GET['item']), null);
     } else {
         $o_comp_manager->ReadById(null, array($_GET['item']));
     }
     $this->competition = $o_comp_manager->GetFirst();
     $this->season = $this->competition->GetWorkingSeason();
     # must have found a competition
     if (!$this->competition instanceof Competition or !$this->season instanceof Season) {
         http_response_code(404);
         exit;
     }
     # If the competition was requested, redirect to the current season
     if ($latest) {
         http_response_code(303);
         header("Location: " . $this->season->GetNavigateUrl());
         return;
     }
     # Update search engine. Only do this for the latest season as then we have the right teams already.
     if ($this->competition->GetSearchUpdateRequired() and $latest) {
         $this->SearchIndexer()->DeleteFromIndexById("competition" . $this->competition->GetId());
         require_once "search/competition-search-adapter.class.php";
         $adapter = new CompetitionSearchAdapter($this->competition);
         $this->SearchIndexer()->Index($adapter->GetSearchableItem());
         $this->SearchIndexer()->CommitChanges();
         $o_comp_manager->SearchUpdated($this->competition->GetId());
     }
     unset($o_comp_manager);
     # get matches
     $o_match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     $o_match_manager->ReadBySeasonId(array($this->season->GetId()));
     $a_matches = $o_match_manager->GetItems();
     $this->season->SetMatches($a_matches);
     # While we're here, check if there are any outstanding notifications to be sent
     $o_match_manager->NotifyMatchModerator();
     unset($o_match_manager);
     # Get stats highlights
     require_once 'stoolball/statistics/statistics-manager.class.php';
     $statistics_manager = new StatisticsManager($this->GetSettings(), $this->GetDataConnection());
     $statistics_manager->FilterBySeason(array($this->season->GetId()));
     $statistics_manager->FilterMaxResults(1);
     $this->best_batting = $statistics_manager->ReadBestBattingPerformance();
     $this->best_bowling = $statistics_manager->ReadBestBowlingPerformance();
     $this->most_runs = $statistics_manager->ReadBestPlayerAggregate("runs_scored");
     $this->most_wickets = $statistics_manager->ReadBestPlayerAggregate("wickets");
     $this->most_catches = $statistics_manager->ReadBestPlayerAggregate("catches");
     # See what stats we've got available
     $best_batting_count = count($this->best_batting);
     $best_bowling_count = count($this->best_bowling);
     $best_batters = count($this->most_runs);
     $best_bowlers = count($this->most_wickets);
     $best_catchers = count($this->most_catches);
     $this->has_player_stats = ($best_batting_count or $best_batters or $best_bowling_count or $best_bowlers or $best_catchers);
     if (!$this->has_player_stats) {
         $player_of_match = $statistics_manager->ReadBestPlayerAggregate("player_of_match");
         $this->has_player_stats = (bool) count($player_of_match);
     }
     unset($statistics_manager);
     # Get other seasons
     $a_comp_ids = array($this->competition->GetId());
     $o_season_manager = new SeasonManager($this->GetSettings(), $this->GetDataConnection());
     $o_season_manager->ReadByCompetitionId($a_comp_ids);
     $a_other_seasons = $o_season_manager->GetItems();
     $this->competition->SetSeasons(array());
     foreach ($a_other_seasons as $season) {
         if ($season->GetId() == $this->season->GetId()) {
             $this->competition->AddSeason($this->season, true);
         } else {
             $this->competition->AddSeason($season, false);
         }
     }
     unset($o_season_manager);
 }