#!/usr/bin/perl # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-02-28 17:18:00 -0500 (Tue, 28 Feb 2017) $ # $Author: hvs $ # $Rev: 49486 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/register/leader.cgi $ # Copyright 2000-2017, SPORTS REFERENCE, LLC All rights reserved. ################################################################### # Usage : leader.cgi # Purpose : returns a leaderboard for a minor league season # Returns : a-z full webpage # Parameters : id (the sabr leagueID) # Throws : none # See Also : # Comments : ################################################################### use Carp; use CGI; use FindBin qw($Bin); use Modern::Perl '2010'; use lib "$Bin/../lib"; use SR::Cache; use SR::Defaults; use SR::Statline; use SRlocal::Constants; use SRlocal::DB; use SRlocal::Defaults; use SRlocal::Formatting; use SRlocal::Players; use SRlocal::Register; use SRlocal::Register::Batting; use SRlocal::Register::Fetch; use SRlocal::Register::Fielding; use SRlocal::Register::Pitching; use SRlocal::Register::Managers; use SRlocal::Register::Model; use SRlocal::Stats; use SRlocal::Templates; chomp($0); MAIN: { # Get the db handle and a reference to a hash of site params our $dbh = get_site_db_connection($Bin); our $site_params_ref = get_site_params($Bin); our $q = new CGI; our $srtemplates = new SRlocal::Templates; my $id = url_chadwick_id( $q, 'id' ); my $year_id = url_integer( $q, 'year' ); my $code = url_alpha( $q, 'code' ); my $class = url_classification( $q, 'class' ); my $group = url_alpha( $q, 'group' ); my $type = url_regex( $q, 'type', qr/^(bat|pitch)$/, 'bat' ); my $sort_by = is_nonempty_print( $q->param('sort_by') ); my $show_all = is_nonempty_print( $q->param('show_all') ); my $type_title = $type eq 'bat' ? 'Batting' : 'Pitching'; my $class_name = $SRlocal::Constants::MILB_CLASSIFICATIONS{$class}{name} if ( is_nonempty($class) ); my $memd_key = $ENV{'REQUEST_URI'} // ''; $memd_key .= $site_params_ref->{site_url} . ':'; my $page_data = cache_get( 'register:leader', $memd_key ); # check to see if we got a result. if ( _nemp($page_data) ) { print $q->header( -type => 'text/html', -charset => 'utf-8' ); print $page_data; $dbh->disconnect; exit; } my $model; my $template = 'Pages/General.tt2'; # print out league leader table. if ($id) { ## Get the details for this league my $data = register_fetch_league( $dbh, $id ); $year_id = $data->{year}; $template = 'Pages/League.tt2'; my $league_name = $group && $group eq 'NLB' ? 'NLB' : $data->{name_full}; # Set up the "You Are Here" line. my @yah = ( qq{Register}, qq{Leagues}, qq{$year_id $league_name}, qq{$type_title Leaders} ); $model = { header => { page_url => qq{$SITE_URL/register/leader.cgi?id=$id&type=$type}, index_url => qq{/register/league.cgi?id=$id}, page_title => qq{$year_id $league_name $type_title Leaders}, index_title => qq{$year_id $league_name}, page_description => 'Create leaderboards for Negro, Japanese, and independent leagues and all of the minors for any statistic', you_are_here => generate_you_are_here( \@yah ), }, key_league => $id, year_id => $year_id, data => $data, }; $model->{summary} = register_build_league_header_model( $dbh, $srtemplates, $model, $type ); $model->{inner_nav} = build_register_inner_nav( $dbh, $type eq 'bat' ? 'batting_leaders' : 'pitching_leaders', $model ); if ( $type eq 'bat' ) { $model = register_get_league_players_batting_from_id( $dbh, $srtemplates, $model, $id, $year_id, $sort_by, $show_all ); } else { $model = register_get_league_players_pitching_from_id( $dbh, $srtemplates, $model, $id, $year_id, $sort_by, $show_all ); } } elsif ($year_id) { $template = 'Pages/PlayIndex.tt2'; # validate the year_id passed in. if ( $year_id !~ /^[0-9]{4}$/ ) { print $q->redirect( -uri => $SRlocal::Constants::SITE_URL . '/register/', -status => '301 Moved Permanently' ); exit; } # Set up the "You Are Here" line. my @yah = ( qq{Register}, qq{Leagues}, qq{$year_id Season Summary}, qq{$type_title Leaders} ); $model = { header => { page_url => qq{$SITE_URL/register/leader.cgi?type=$type&year=$year_id}, index_url => qq{/register/league.cgi?year=$year_id}, page_title => qq{$year_id Register $type_title Leaders}, index_title => qq{$year_id Register League Encyclopedia}, page_description => 'Create leaderboards for Negro, Japanese, and independent leagues and all of the minors for any statistic', you_are_here => generate_you_are_here( \@yah ), }, year_id => $year_id, form_id => 'leaders', }; $model->{inner_nav} = build_register_inner_nav( $dbh, $type eq 'bat' ? 'batting_leaders' : 'pitching_leaders', $model ); if ( $type eq 'bat' ) { $model = register_get_batting_leaders_year( $dbh, $srtemplates, $model, $year_id, $sort_by, { show_fas => $q->param('show_fas') // 0, group => $group } ); } else { $model = register_get_pitching_leaders_year( $dbh, $srtemplates, $model, $year_id, $sort_by, { show_fas => $q->param('show_fas') // 0, group => $group } ); } } elsif ($code) { # No id #_register_get_individual_league_index( $dbh, $fh, $code, $class, # $page_title ); } else { ## Nothing specified so redirect to the current year leaders print $q->redirect( -uri => $SRlocal::Constants::SITE_URL . '/register/leader.cgi?type=bat&year=' . $CURRENT_YEAR ); dbh->disconnect; exit; } push @{ $model->{results_list} }, register_data_note( srtemplates => $srtemplates ); ## Generate the page from a template $page_data = $srtemplates->process( $template, $model ); ## Show the page to the user. print $q->header( -type => 'text/html', -charset => 'utf-8' ); print $page_data; # stuff the page into our cache. cache_set( 'register:leader', $memd_key, $page_data ); $dbh->disconnect; }