public function AddPlayer()
 {
     $this->player = $this->editor->GetDataObject();
     $this->player->Team()->SetId($_GET['team']);
     require_once "stoolball/team-manager.class.php";
     $team_manager = new TeamManager($this->GetSettings(), $this->GetDataConnection());
     $team_manager->ReadById(array($this->player->Team()->GetId()));
     $this->team = $team_manager->GetFirst();
     unset($team_manager);
     if ($this->editor->CancelClicked()) {
         $this->Redirect($this->team->GetPlayersNavigateUrl());
     }
     if ($this->IsValid()) {
         # Now check again, because it's a new request, that the user has permission
         $this->CheckForPermission($this->team);
         # Check whether a player by that name already exists
         $this->player_manager->MatchExistingPlayer($this->player);
         if ($this->player->GetId()) {
             $this->add_player_already_exists = true;
         } else {
             $this->player_manager->SavePlayer($this->player);
             unset($player_manager);
             $this->Redirect($this->team->GetPlayersNavigateUrl());
         }
     }
 }
 public function __construct(Team $team)
 {
     $this->team = $team;
     $this->searchable = new SearchItem();
     $this->searchable->SearchItemId("team" . $team->GetId());
     $this->searchable->SearchItemType("team");
     $this->searchable->Url($team->GetNavigateUrl());
     $this->searchable->Title($team->GetNameAndType());
     $this->searchable->Description($this->GetSearchDescription());
     $this->searchable->WeightOfType(1000);
     $keywords = array($team->GetName(), $team->GetGround()->GetAddress()->GetLocality(), $team->GetGround()->GetAddress()->GetTown());
     $this->searchable->Keywords(implode(" ", $keywords));
     $content = array($team->GetGround()->GetAddress()->GetAdministrativeArea(), $team->GetIntro(), $team->GetPlayingTimes(), $team->GetCost(), $team->GetContact());
     $this->searchable->FullText(implode(" ", $content));
     $this->searchable->RelatedLinksHtml('<ul>' . '<li><a href="' . $team->GetStatsNavigateUrl() . '">Statistics</a></li>' . '<li><a href="' . $team->GetPlayersNavigateUrl() . '">Players</a></li>' . '<li><a href="' . $team->GetCalendarNavigateUrl() . '">Match calendar</a></li>' . '</ul>');
 }
    function OnPageLoad()
    {
        require_once 'stoolball/statistics-calculator.class.php';
        $calculator = new StatisticsCalculator();
        $calculator->AnalyseMatchData($this->stats, $this->team);
        $title = "Team statistics for " . $this->team->GetNameAndType();
        if ($this->season) {
            $title .= " in the {$this->season} season";
        }
        echo "<h1>" . htmlentities($title, ENT_QUOTES, "UTF-8", false) . "</h1>";
        # See what stats we've got available
        $has_team_stats = $calculator->EnoughDataForStats($this->season);
        $has_most_runs = count($this->most_runs);
        $has_most_wickets = count($this->most_wickets);
        $has_catch_stats = count($this->most_catches);
        $has_run_outs = count($this->most_run_outs);
        $has_player_of_match_stats = count($this->most_player_of_match);
        $has_player_stats = ($has_most_runs or $has_most_wickets or $has_catch_stats or $has_run_outs or $has_player_of_match_stats);
        require_once 'xhtml/navigation/tabs.class.php';
        $tabs = array('Summary' => $this->team->GetNavigateUrl());
        if ($has_player_stats) {
            $tabs['Players'] = $this->team->GetPlayersNavigateUrl();
        }
        $tabs['Statistics'] = '';
        echo new Tabs($tabs);
        ?>
        <div class="box tab-box">
            <div class="dataFilter"></div>
            <div class="box-content">
        <?php 
        if (!$has_team_stats and !$has_player_stats) {
            $scope = $this->team->GetNameAndType();
            if ($this->season) {
                $scope .= " in the {$this->season} season";
            }
            $scope = htmlentities($scope, ENT_QUOTES, "UTF-8", false);
            echo "<p>There aren't any statistics for {$scope} yet.</p>" . '<p>To find out how to add them, see <a href="/play/manage/website/matches-and-results-why-you-should-add-yours/">Matches and results - why you should add yours</a>.</p>';
        } else {
            require_once 'stoolball/team-runs-table.class.php';
            require_once 'stoolball/statistics/player-statistics-table.class.php';
            echo '<div class="statsGroup">';
            if ($has_team_stats or $has_most_runs) {
                echo '<div class="statsColumns">
				<h2>Batting statistics</h2>
				<div class="statsColumn">';
                if ($has_most_runs) {
                    echo new PlayerStatisticsTable("Most runs", "Runs", $this->most_runs, false);
                    if ($has_most_runs >= 10) {
                        echo '<p class="statsViewAll"><a href="/play/statistics/most-runs' . $this->statistics_query . '">Most runs &#8211; view all and filter</a></p>';
                    }
                }
                echo "</div><div class=\"statsColumn\">";
                if ($has_team_stats) {
                    echo new TeamRunsTable($calculator->TotalMatchesWithRunData($this->season), $calculator->RunsScored($this->season), $calculator->RunsConceded($this->season), $calculator->HighestInnings($this->season), $calculator->LowestInnings($this->season), $calculator->AverageInnings($this->season));
                }
                if ($has_most_runs) {
                    echo '<p class="statsViewAll"><a href="/play/statistics/individual-scores' . $this->statistics_query . '">Individual scores</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-scores-of-100' . $this->statistics_query . '">Most scores of 100 or more</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-scores-of-50' . $this->statistics_query . '">Most scores of 50 or more</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/batting-average' . $this->statistics_query . '">Batting averages</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/batting-strike-rate' . $this->statistics_query . '">Batting strike rates</a></p>';
                }
                echo "</div></div>";
            }
            if ($has_most_wickets or $has_team_stats) {
                echo '<div class="statsColumns">
					<h2>Bowling statistics and match results</h2>
					  <div class="statsColumn">';
                if ($has_most_wickets) {
                    # Show top bowlers
                    $table = new PlayerStatisticsTable("Most wickets", "Wickets", $this->most_wickets, false);
                    $table->SetCssClass($table->GetCssClass() . " bowling");
                    echo $table;
                    if ($has_most_wickets >= 10) {
                        echo '<p class="statsViewAll"><a href="/play/statistics/most-wickets' . $this->statistics_query . '">Most wickets &#8211; view all and filter</a></p>';
                    }
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-5-wickets' . $this->statistics_query . '">Most times taking 5 wickets in an innings</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-wickets-by-bowler-and-catcher' . $this->statistics_query . '">Most wickets by a bowling and catching combination</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/bowling-performances' . $this->statistics_query . '">Bowling performances</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/bowling-average' . $this->statistics_query . '">Bowling averages</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/economy-rate' . $this->statistics_query . '">Economy rates</a></p>';
                    echo '<p class="statsViewAll"><a href="/play/statistics/bowling-strike-rate' . $this->statistics_query . '">Bowling strike rates</a></p>';
                }
                ?>
					</div><div class="statsColumn">
                        <span class="chart-js-template" id="all-results-chart"></span>
                        <span class="chart-js-template" id="home-results-chart"></span>
                        <span class="chart-js-template" id="away-results-chart"></span>
					</div></div>
                    <?php 
            }
            ?>
<span class="chart-js-template" id="opponents-chart"></span><?php 
            if ($has_catch_stats or $has_run_outs) {
                echo '<div class="statsColumns">
				<h2>Fielding statistics</h2>
				<div class="statsColumn">';
                if ($has_catch_stats) {
                    # Show top catchers
                    $table = new PlayerStatisticsTable("Most catches", "Catches", $this->most_catches, false);
                    $table->SetCssClass($table->GetCssClass() . " bowling");
                    echo $table;
                    if ($has_catch_stats >= 10) {
                        echo '<p class="statsViewAll"><a href="/play/statistics/most-catches' . $this->statistics_query . '">Most catches &#8211; view all and filter</a></p>';
                    }
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-catches-in-innings' . $this->statistics_query . '">Most catches in an innings &#8211; view all and filter</a></p>';
                }
                echo "</div><div class=\"statsColumn\">";
                if ($has_run_outs) {
                    $table = new PlayerStatisticsTable("Most run-outs", "Run-outs", $this->most_run_outs, false);
                    $table->SetCssClass($table->GetCssClass() . " bowling");
                    echo $table;
                    if ($has_run_outs >= 10) {
                        echo '<p class="statsViewAll"><a href="/play/statistics/most-run-outs' . $this->statistics_query . '">Most run-outs &#8211; view all and filter</a></p>';
                    }
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-run-outs-in-innings' . $this->statistics_query . '">Most run-outs in an innings &#8211; view all and filter</a></p>';
                }
                echo "</div></div>";
            }
            echo '<h2>All-round performance statistics</h2>';
            if ($has_player_of_match_stats) {
                echo new PlayerStatisticsTable("Most player of the match nominations", "Nominations", $this->most_player_of_match, false);
                echo '<p class="statsViewAll"><a href="/play/statistics/player-performances' . $this->statistics_query . '">Player performances &#8211; view all and filter</a></p>';
                echo '<p class="statsViewAll"><a href="/play/statistics/player-of-match' . $this->statistics_query . '">Player of the match nominations &#8211; view all and filter</a></p>';
                if ($has_player_of_match_stats >= 10) {
                    echo '<p class="statsViewAll"><a href="/play/statistics/most-player-of-match' . $this->statistics_query . '">Most player of the match nominations &#8211; view all and filter</a></p>';
                }
            } else {
                echo '<p><a href="/play/statistics/player-performances' . $this->statistics_query . '">Player performances &#8211; view all and filter</a></p>';
            }
            echo "</div>";
            # close statsGroup
            # Link to other seasons
            if (count($calculator->SeasonYears()) > 1) {
                echo new XhtmlElement('h2', 'More statistics for ' . htmlentities($this->team->GetName(), ENT_QUOTES, "UTF-8", false));
                echo '<div class="season-list"><ul>';
                if ($this->season) {
                    echo "<li><a href=\"" . htmlentities($this->team->GetStatsNavigateUrl(), ENT_QUOTES, "UTF-8", false) . "\">All seasons</a></li>";
                }
                foreach ($calculator->SeasonYears() as $season_years) {
                    $season_url = str_replace("/", "-", $season_years);
                    # Link to season if it's not the current season. Important to use !== because
                    # we're comparing numeric strings and with != 2009-10 is equal to 2009
                    if (!isset($_GET['params']) or $season_url !== $_GET["params"]) {
                        echo "<li><a href=\"" . htmlentities($this->team->GetStatsNavigateUrl() . "/" . $season_url, ENT_QUOTES, "UTF-8", false) . "\">" . htmlentities($season_years, ENT_QUOTES, "UTF-8", false) . " season</a></li>";
                    }
                }
                echo "</ul></div>";
            }
        }
        ?>
        </div>
        </div>
        <?php 
    }
    function OnPageLoad()
    {
        /* @var $team Team */
        $team = $this->team;
        # display the team
        echo '<div class="team" typeof="schema:SportsTeam" about="' . htmlentities($this->team->GetLinkedDataUri(), ENT_QUOTES, "UTF-8", false) . '">';
        echo new TeamNameControl($this->team, 'h1');
        require_once 'xhtml/navigation/tabs.class.php';
        $tabs = array('Summary' => '');
        if ($this->has_player_stats) {
            $tabs['Players'] = $this->team->GetPlayersNavigateUrl();
        }
        $tabs['Statistics'] = $this->team->GetStatsNavigateUrl();
        echo new Tabs($tabs);
        ?>
        <div class="box tab-box">
            <div class="dataFilter"></div>
            <div class="box-content">
        <?php 
        if (!$this->is_one_time_team) {
            # add club name
            if ($team->GetClub()->GetId()) {
                $o_club_para = new XhtmlElement('p');
                $o_club_para->AddControl('Part of ');
                $o_club_link = new XhtmlElement('a', htmlentities($team->GetClub()->GetName(), ENT_QUOTES, "UTF-8", false));
                $o_club_link->AddAttribute('href', $team->GetClub()->GetNavigateUrl());
                $o_club_para->AddControl($o_club_link);
                $o_club_para->AddControl('.');
                echo $o_club_para;
            }
        }
        # Add intro
        if ($team->GetIntro()) {
            $s_intro = htmlentities($team->GetIntro(), ENT_QUOTES, "UTF-8", false);
            $s_intro = XhtmlMarkup::ApplyParagraphs($s_intro);
            $s_intro = XhtmlMarkup::ApplyLists($s_intro);
            $s_intro = XhtmlMarkup::ApplySimpleXhtmlTags($s_intro, false);
            $s_intro = XhtmlMarkup::ApplyLinks($s_intro);
            if (strpos($s_intro, '<p>') > -1) {
                echo '<div property="schema:description">' . $s_intro . '</div>';
            } else {
                echo '<p property="schema:description">' . $s_intro . '</p>';
            }
        }
        ######################
        ### When and where ###
        ######################
        echo new XhtmlElement('h2', 'When and where');
        if (!$this->is_one_time_team) {
            # Add not playing, if relevant
            if (!$team->GetIsActive()) {
                echo new XhtmlElement('p', new XhtmlElement('strong', 'This team doesn\'t play any more.'));
            }
        }
        # add ground
        $ground = $team->GetGround();
        if ($ground->GetId() and $ground->GetName()) {
            echo '<p rel="schema:location">This team plays at <a typeof="schema:Place" about="' . htmlentities($ground->GetLinkedDataUri(), ENT_QUOTES, "UTF-8", false) . '" property="schema:name" rel="schema:url" href="' . htmlentities($ground->GetNavigateUrl(), ENT_QUOTES, "UTF-8", false) . '">' . htmlentities($ground->GetNameAndTown(), ENT_QUOTES, "UTF-8", false) . '</a>.</p>';
        }
        if (!$this->is_one_time_team) {
            # Add playing times
            if ($team->GetPlayingTimes()) {
                $s_times = htmlentities($team->GetPlayingTimes(), ENT_QUOTES, "UTF-8", false);
                $s_times = XhtmlMarkup::ApplyParagraphs($s_times);
                $s_times = XhtmlMarkup::ApplyLists($s_times);
                $s_times = XhtmlMarkup::ApplySimpleXhtmlTags($s_times, false);
                $s_times = XhtmlMarkup::ApplyLinks($s_times);
                echo $s_times;
            }
        }
        # Match list
        if (count($this->a_matches)) {
            if ($this->is_one_time_team) {
                $came = "came";
                if (count($this->a_matches) == 1 and $this->a_matches[0]->GetStartTime() > gmdate("U")) {
                    $came = "will come";
                }
                echo "<p>This team {$came} together once to play in a tournament.</p>";
            } else {
                echo new XhtmlElement('h2', 'Matches this season');
            }
            echo new MatchListControl($this->a_matches);
        }
        if (!$this->is_one_time_team) {
            #################
            ###  Seasons  ###
            #################
            $seasons = array();
            foreach ($team->Seasons() as $team_in_season) {
                $seasons[] = $team_in_season->GetSeason();
            }
            if (count($seasons)) {
                $season_list = new SeasonListControl($seasons);
                $season_list->SetShowCompetition(true);
                echo $season_list;
            }
        }
        #################
        ###   Cost    ###
        #################
        if ($team->GetCost()) {
            echo new XhtmlElement('h2', 'How much does it cost?');
            $cost = XhtmlMarkup::ApplyParagraphs(htmlentities($team->GetCost(), ENT_QUOTES, "UTF-8", false));
            $cost = XhtmlMarkup::ApplyLists($cost);
            $cost = XhtmlMarkup::ApplySimpleXhtmlTags($cost, false);
            $cost = XhtmlMarkup::ApplyLinks($cost);
            echo $cost;
        }
        #####################
        ### Find out more ###
        #####################
        if ($team->GetContact() or $team->GetWebsiteUrl()) {
            echo new XhtmlElement('h2', 'Contact details');
        }
        # Add contact details
        $s_contact = $team->GetContact();
        if ($s_contact) {
            # protect emails before escaping HTML, because it's trying to recognise the actual HTML tags
            require_once 'email/email-address-protector.class.php';
            $protector = new EmailAddressProtector($this->GetSettings());
            $s_contact = $protector->ApplyEmailProtection($s_contact, AuthenticationManager::GetUser()->IsSignedIn());
            $s_contact = htmlentities($s_contact, ENT_QUOTES, "UTF-8", false);
            $s_contact = XhtmlMarkup::ApplyParagraphs($s_contact);
            $s_contact = XhtmlMarkup::ApplyLists($s_contact);
            $s_contact = XhtmlMarkup::ApplySimpleXhtmlTags($s_contact, false);
            $s_contact = XhtmlMarkup::ApplyLinks($s_contact);
            echo $s_contact;
        }
        # Add website
        $s_website = $team->GetWebsiteUrl();
        if ($s_website) {
            echo new XhtmlElement('p', new XhtmlAnchor("Visit " . htmlentities($team->GetName(), ENT_QUOTES, "UTF-8", false) . "'s website", $s_website));
        }
        if ($team->GetClub() instanceof Club and $team->GetClub()->GetClubmarkAccredited()) {
            ?>
            <p><img src="/images/logos/clubmark.png" alt="Clubmark accredited" width="150" height="29" /></p>
            <p>This is a <a href="http://www.sportenglandclubmatters.com/club-mark/">Clubmark accredited</a> stoolball club.</p>
            <?php 
        }
        if (!$this->is_one_time_team) {
            # Prompt for more contact information, unless it's a repreaentative team when we don't expect it
            if (!$s_contact and !$s_website and $team->GetTeamType() !== Team::REPRESENTATIVE) {
                if ($team->GetPrivateContact()) {
                    ?>
    <p>We may be able to contact this team, but we don't have permission to publish their details. If you'd like to play for them, <a href="/contact">contact us</a> and we'll pass your details on.</p>
    				<?php 
                } else {
                    ?>
    <p>This team hasn't given us any contact details. If you'd like to play for them, try contacting their opposition teams or the secretary of their league, and ask them to pass on your details.</p>
    				<?php 
                }
                ?>
    <p>If you play for this team, please <a href="<?php 
                echo $this->GetSettings()->GetUrl('TeamAdd');
                ?>
">help us to improve this page</a>.</p>
    			<?php 
            }
        }
        $club = $this->team->GetClub();
        if ($club and ($club->GetFacebookUrl() or $club->GetTwitterAccount() or $club->GetInstagramAccount())) {
            ?>
            <div class="social screen">
            <?php 
            if ($club->GetFacebookUrl()) {
                ?>
                <a href="<?php 
                echo Html::Encode($club->GetFacebookUrl());
                ?>
" class="facebook-group"><img src="/images/play/find-us-on-facebook.png" alt="Find us on Facebook" width="137" height="22" /></a>
            <?php 
            }
            if ($club->GetTwitterAccount()) {
                ?>
                <a href="https://twitter.com/<?php 
                echo Html::Encode(substr($club->GetTwitterAccount(), 1));
                ?>
" class="twitter-follow-button">Follow <?php 
                echo Html::Encode($this->team->GetClub()->GetTwitterAccount());
                ?>
</a>
                <script src="https://platform.twitter.com/widgets.js"></script>
                <?php 
            }
            if ($club->GetInstagramAccount()) {
                ?>
                <a href="https://www.instagram.com/<?php 
                echo Html::Encode(trim($club->GetInstagramAccount(), '@'));
                ?>
/?ref=badge" class="instagram"><img src="//badges.instagram.com/static/images/ig-badge-view-24.png" alt="Instagram" /></a>
                <?php 
            }
            ?>
            </div>
            <?php 
        } else {
            $this->ShowSocial();
        }
        ?>
        </div>
        </div>
        </div>
        <?php 
        $this->AddSeparator();
        $o_panel = new TeamEditPanel($this->GetSettings(), $this->team, $this->seasons, $this->a_matches);
        $o_panel->AddCssClass("with-tabs");
        echo $o_panel;
        $this->Render();
        ### Build charts ###
        if ($this->has_facebook_page_url) {
            $this->ShowFacebookPage($club->GetFacebookUrl(), $club->GetName());
        }
        # Show top players, except for once-only teams which have a very short page for this to sit alongside
        if ($this->has_player_stats and !$this->is_one_time_team) {
            require_once 'stoolball/statistics-highlight-table.class.php';
            echo new StatisticsHighlightTable($this->best_batting, $this->most_runs, $this->best_bowling, $this->most_wickets, $this->most_catches, "All seasons");
        }
        // Show graphs of results, except for once-only teams which have a very short page for this to sit alongside
        if (!$this->is_one_time_team) {
            ?>
             <span class="chart-js-template supporting-chart" id="all-results-chart"></span>
            <?php 
        }
    }