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 – <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()); } }
/** * @return int * @param Competition $o_competition * @desc Save the supplied Competition to the database, and return the id */ function SaveCompetition($o_competition) { # Set up short URL manager require_once 'http/short-url-manager.class.php'; $o_url_manager = new ShortUrlManager($this->GetSettings(), $this->GetDataConnection()); $new_short_url = $o_url_manager->EnsureShortUrl($o_competition); # build query $category_id = is_null($o_competition->GetCategory()) ? null : $o_competition->GetCategory()->GetId(); $allowed_html = array('p', 'br', 'strong', 'em', 'a[href]', 'ul', 'ol', 'li'); # if no id, it's a new Competition; otherwise update the Competition $is_new = !$o_competition->GetId(); if ($is_new) { $s_sql = 'INSERT INTO ' . $this->GetSettings()->GetTable('Competition') . ' SET ' . "competition_name = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetName()) . ", " . "category_id = " . Sql::ProtectNumeric($category_id, true, false) . ', ' . "intro = " . $this->SqlHtmlString($o_competition->GetIntro(), $allowed_html) . ", " . "contact = " . $this->SqlHtmlString($o_competition->GetContact(), $allowed_html) . ", " . "notification_email = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetNotificationEmail()) . ", " . "website = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetWebsiteUrl()) . ", " . 'active = ' . Sql::ProtectBool($o_competition->GetIsActive()) . ', ' . 'player_type_id = ' . Sql::ProtectNumeric($o_competition->GetPlayerType()) . ", " . 'players_per_team = ' . Sql::ProtectNumeric($o_competition->GetMaximumPlayersPerTeam()) . ", " . 'overs = ' . Sql::ProtectNumeric($o_competition->GetOvers()) . ", " . "short_url = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetShortUrl()) . ", " . "update_search = 1, " . 'date_added = ' . gmdate('U') . ', ' . 'date_changed = ' . gmdate('U'); # run query $o_result = $this->GetDataConnection()->query($s_sql); # get autonumber $o_competition->SetId($this->GetDataConnection()->insertID()); # create a default season require_once 'stoolball/season-manager.class.php'; $o_season = new Season($this->GetSettings()); $o_season->SetCompetition($o_competition); $o_season->SetStartYear(gmdate('Y', gmdate('U'))); $o_season->SetEndYear(gmdate('Y', gmdate('U'))); $o_season->SetIsLatest(true); $o_season_mgr = new SeasonManager($this->GetSettings(), $this->GetDataConnection()); $o_season_mgr->SaveSeason($o_season); unset($o_season_mgr); } else { $s_sql = 'UPDATE ' . $this->GetSettings()->GetTable('Competition') . ' SET ' . "competition_name = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetName()) . ", " . "category_id = " . Sql::ProtectNumeric($category_id, true, false) . ', ' . "intro = " . $this->SqlHtmlString($o_competition->GetIntro(), $allowed_html) . ", " . "contact = " . $this->SqlHtmlString($o_competition->GetContact(), $allowed_html) . ", " . "notification_email = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetNotificationEmail()) . ", " . "website = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetWebsiteUrl()) . ", " . 'active = ' . Sql::ProtectBool($o_competition->GetIsActive()) . ', ' . 'player_type_id = ' . Sql::ProtectNumeric($o_competition->GetPlayerType()) . ", " . 'players_per_team = ' . Sql::ProtectNumeric($o_competition->GetMaximumPlayersPerTeam()) . ", " . 'overs = ' . Sql::ProtectNumeric($o_competition->GetOvers()) . ", " . "short_url = " . Sql::ProtectString($this->GetDataConnection(), $o_competition->GetShortUrl()) . ", " . "update_search = 1, " . 'date_changed = ' . gmdate('U') . ' ' . 'WHERE competition_id = ' . Sql::ProtectNumeric($o_competition->GetId()); # run query $this->GetDataConnection()->query($s_sql); } # Request search update for related objects which mention the competition $seasons = array(); $sql = "SELECT season_id FROM nsa_season WHERE competition_id = " . SQL::ProtectNumeric($o_competition->GetId(), false); $result = $this->GetDataConnection()->query($sql); while ($row = $result->fetch()) { $seasons[] = $row->season_id; } $result->closeCursor(); $seasons = implode(", ", $seasons); $sql = "UPDATE nsa_team SET update_search = 1 WHERE team_id IN \n ( \n SELECT team_id FROM nsa_team_season WHERE season_id IN ({$seasons})\n )"; $this->GetDataConnection()->query($sql); $sql = "UPDATE nsa_match SET update_search = 1 WHERE match_id IN \n ( \n SELECT match_id FROM nsa_season_match WHERE season_id IN ({$seasons})\n )"; $this->GetDataConnection()->query($sql); # Regenerate short URLs if (is_object($new_short_url)) { $new_short_url->SetParameterValuesFromObject($o_competition); $o_url_manager->Save($new_short_url); # season URLs are generated from the competition, so regenerate those too if (!$is_new) { $o_season_mgr = new SeasonManager($this->GetSettings(), $this->GetDataConnection()); $o_season_mgr->ReadByCompetitionId(array($o_competition->GetId())); $seasons = $o_season_mgr->GetItems(); unset($o_season_mgr); foreach ($seasons as $season) { /* @var $season Season */ $new_short_url = $o_url_manager->EnsureShortUrl($season, true); if (is_object($new_short_url)) { $s_sql = "UPDATE " . $this->GetSettings()->GetTable('Season') . " SET short_url = " . Sql::ProtectString($this->GetDataConnection(), $new_short_url->GetShortUrl()) . " WHERE season_id = " . Sql::ProtectNumeric($season->GetId()); $this->GetDataConnection()->query($s_sql); $new_short_url->SetParameterValuesFromObject($season); $o_url_manager->Save($new_short_url); } } } } unset($o_url_manager); return $o_competition->GetId(); }