/**
  * Adds season summary and competition data to an existing season
  *
  * @param Season $season
  * @param object $row
  */
 private function BuildSeason(Season $season, $row)
 {
     if (isset($row->season_id)) {
         $season->SetId($row->season_id);
         $season->SetName($row->season_name);
         if (isset($row->start_year)) {
             $season->SetStartYear($row->start_year);
         }
         if (isset($row->end_year)) {
             $season->SetEndYear($row->end_year);
         }
         if (isset($row->is_latest)) {
             $season->SetIsLatest($row->is_latest);
         }
         if (isset($row->intro)) {
             $season->SetIntro($row->intro);
         }
         if (isset($row->results)) {
             $season->SetResults($row->results);
         }
         if (isset($row->show_table)) {
             $season->SetShowTable($row->show_table);
         }
         if (isset($row->show_runs_scored)) {
             $season->SetShowTableRunsScored($row->show_runs_scored);
         }
         if (isset($row->show_runs_conceded)) {
             $season->SetShowTableRunsConceded($row->show_runs_conceded);
         }
         if (isset($row->short_url)) {
             $season->SetShortUrl($row->short_url);
         }
         $comp = new Competition($this->GetSettings());
         if (isset($row->competition_id)) {
             $comp->SetId($row->competition_id);
         }
         if (isset($row->competition_name)) {
             $comp->SetName($row->competition_name);
         }
         if (isset($row->competition_short_url)) {
             $comp->SetShortUrl($row->competition_short_url);
         }
         if (isset($row->notification_email)) {
             $comp->SetNotificationEmail($row->notification_email);
         }
         if (isset($row->player_type_id)) {
             $comp->SetPlayerType($row->player_type_id);
         }
         $season->SetCompetition($comp);
     }
 }
 /**
  * Populates the collection of business objects from raw data
  *
  * @return bool
  * @param MySqlRawData $o_result
  */
 protected function BuildItems(MySqlRawData $o_result)
 {
     $this->Clear();
     /* @var $o_team Team */
     # use CollectionBuilder to handle duplicates
     $o_team_builder = new CollectionBuilder();
     $o_season_builder = new CollectionBuilder();
     $o_team = null;
     while ($row = $o_result->fetch()) {
         # check whether this is a new team
         if (!$o_team_builder->IsDone($row->team_id)) {
             # store any exisiting team
             if ($o_team != null) {
                 $this->Add($o_team);
                 $o_season_builder->Reset();
             }
             # create the new team
             $o_team = new Team($this->o_settings);
             $o_team->SetId($row->team_id);
             $o_team->SetName($row->team_name);
             if (isset($row->website)) {
                 $o_team->SetWebsiteUrl($row->website);
             }
             if (isset($row->active)) {
                 $o_team->SetIsActive($row->active);
             }
             if (isset($row->team_type)) {
                 $o_team->SetTeamType($row->team_type);
             }
             if (isset($row->intro)) {
                 $o_team->SetIntro($row->intro);
             }
             if (isset($row->playing_times)) {
                 $o_team->SetPlayingTimes($row->playing_times);
             }
             if (isset($row->cost)) {
                 $o_team->SetCost($row->cost);
             }
             if (isset($row->contact)) {
                 $o_team->SetContact($row->contact);
             }
             if (isset($row->contact_nsa)) {
                 $o_team->SetPrivateContact($row->contact_nsa);
             }
             if (isset($row->short_url)) {
                 $o_team->SetShortUrl($row->short_url);
             }
             if (isset($row->player_type_id)) {
                 $o_team->SetPlayerType($row->player_type_id);
             }
             $o_team->SetSchoolYears(array(1 => isset($row->year1) and $row->year1, 2 => isset($row->year2) and $row->year2, 3 => isset($row->year3) and $row->year3, 4 => isset($row->year4) and $row->year4, 5 => isset($row->year5) and $row->year5, 6 => isset($row->year6) and $row->year6, 7 => isset($row->year7) and $row->year7, 8 => isset($row->year8) and $row->year8, 9 => isset($row->year9) and $row->year9, 10 => isset($row->year10) and $row->year10, 11 => isset($row->year11) and $row->year11, 12 => isset($row->year12) and $row->year12));
             if (isset($row->update_search) and $row->update_search == 1) {
                 $o_team->SetSearchUpdateRequired();
             }
             if (isset($row->date_changed)) {
                 $o_team->SetLastAudit(new AuditData($row->modified_by_id, $row->known_as, $row->date_changed));
             }
             $club = new Club($this->GetSettings());
             if (isset($row->club_id)) {
                 $club->SetId($row->club_id);
                 if (isset($row->club_name)) {
                     $club->SetName($row->club_name);
                 }
                 if (isset($row->twitter)) {
                     $club->SetTwitterAccount($row->twitter);
                 }
                 if (isset($row->facebook)) {
                     $club->SetFacebookUrl($row->facebook);
                 }
                 if (isset($row->instagram)) {
                     $club->SetInstagramAccount($row->instagram);
                 }
                 if (isset($row->clubmark)) {
                     $club->SetClubmarkAccredited($row->clubmark);
                 }
                 if (isset($row->club_short_url)) {
                     $club->SetShortUrl($row->club_short_url);
                 }
             }
             # If the website is actually a Facebook page, move it, overriding the club Facebook page is necessary
             if (strpos($o_team->GetWebsiteUrl(), 'facebook.com/') !== false) {
                 $club->SetFacebookUrl($o_team->GetWebsiteUrl());
                 $o_team->SetWebsiteUrl('');
             }
             $o_team->SetClub($club);
             if (isset($row->ground_id) and $row->ground_id) {
                 $o_ground = new Ground($this->o_settings);
                 $o_ground->SetId($row->ground_id);
                 $address = $o_ground->GetAddress();
                 if (isset($row->saon)) {
                     $address->SetSaon($row->saon);
                 }
                 if (isset($row->town)) {
                     if (isset($row->paon)) {
                         $address->SetPaon($row->paon);
                     }
                     if (isset($row->street_descriptor)) {
                         $address->SetStreetDescriptor($row->street_descriptor);
                     }
                     if (isset($row->locality)) {
                         $address->SetLocality($row->locality);
                     }
                     $address->SetTown($row->town);
                     if (isset($row->administrative_area)) {
                         $address->SetAdministrativeArea($row->administrative_area);
                     }
                     if (isset($row->postcode)) {
                         $address->SetPostcode($row->postcode);
                     }
                     if (isset($row->latitude)) {
                         $address->SetGeoLocation($row->latitude, $row->longitude, null);
                     }
                     $o_ground->SetAddress($address);
                 }
                 if (isset($row->ground_short_url)) {
                     $o_ground->SetShortUrl($row->ground_short_url);
                 }
                 $o_team->SetGround($o_ground);
             }
         }
         # Competition/Season a cause of multiple rows
         if (isset($row->season_id) and !$o_season_builder->IsDone($row->season_id) and isset($row->competition_id)) {
             $o_season = new Season($this->o_settings);
             $o_season->SetId($row->season_id);
             $o_season->SetName($row->season_name);
             $o_season->SetIsLatest($row->is_latest);
             $o_season->SetStartYear($row->start_year);
             $o_season->SetEndYear($row->end_year);
             if (isset($row->season_short_url)) {
                 $o_season->SetShortUrl($row->season_short_url);
             }
             $o_competition = new Competition($this->o_settings);
             $o_competition->SetId($row->competition_id);
             $o_competition->SetName($row->competition_name);
             $o_season->SetCompetition($o_competition);
             $o_team->Seasons()->Add(new TeamInSeason(null, $o_season, isset($row->withdrawn_league) ? $row->withdrawn_league : null));
             unset($o_season);
             unset($o_competition);
         }
     }
     # store final team
     if ($o_team != null) {
         $this->Add($o_team);
     }
     return true;
 }
 /**
  * @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();
 }