#!/usr/bin/perl -w # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-03-24 00:40:35 -0400 (Fri, 24 Mar 2017) $ # $Author: sean $ # $Rev: 50251 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/play-index/season_finder.cgi $ # Copyright 2000-2017, SPORTS REFERENCE, LLC All rights reserved. use Carp; use CGI::Fast ':standard'; use Clone 'clone'; use Data::Dumper; use FindBin '$Bin'; use JSON; use Modern::Perl '2010'; use lib "$Bin/../lib"; use SR::Auth::Users; use SR::Cache; use SR::Defaults; use SR::Statline; use SRlocal::Constants; use SRlocal::DB; use SRlocal::Defaults; use SRlocal::Formatting; use SRlocal::PlayIndex; use SRlocal::PlayIndex::Formatting; use SRlocal::PlayIndex::Subscribe; use SRlocal::PlayIndex::Constants; use SRlocal::PlayIndex::Statdefine; use SRlocal::PlayIndex::SeasonFinder; use SRlocal::Retrosheet::Plays; use SRlocal::Stats; use SRlocal::Templates; chomp($0); MAIN: { ## Setup our $dbh = get_site_db_connection( $Bin, $SRlocal::PlayIndex::Constants::PI_DSN ); our $dbhSR = get_site_db_connection( $Bin, 'SR' ); our $site_params_ref = get_site_params($Bin); our $srtemplates = new SRlocal::Templates; while ( my $q = new CGI::Fast ) { ## Subscription Check our %user; if ( $ENV{'HTTP_COOKIE'} ) { %user = %{ SR::Auth::Users::get_user($q) }; } my $sub_level = SRlocal::PlayIndex::Subscribe::subscriber_level( $dbhSR, $user{user_ID}, 'pi_subscribe' ); ################################################################## ## Check if this result is already cached. If so, just return. my $was_cached = pi_check_cache_and_output( $dbh, $q, $sub_level ); next if $was_cached; ## Query Parameters my $player_id = url_player_id( $q, 'id', '' ); my $year = url_alphanum( $q, 'year', 0 ); my $type = url_regex( $q, 'type', qr/^(b|p)$/, 'b' ); my $class = url_regex( $q, 'class', qr/^team$/, '' ); my $is_ajax = url_regex( $q, 'ajax', qr/^1$/, $FALSE ); my $is_submitted = url_regex( $q, 'request', qr/^1$/, $FALSE ); my $describe_only = url_regex( $q, 'describe_only', qr/^1$/, $FALSE ); my $sum = url_regex( $q, 'sum', qr/^(0|1|smatchCar|smatchTeam|smatchTeamCareer|smatchYear)$/, 0 ); $year = 0 if ( $year =~ /career/ ); ## Page Setup our $title = ( $type eq 'b' ? 'Batting' : $type eq 'p' ? 'Pitching' : 'Other' ) . ' Season Finder'; our $page_url = $SRlocal::Constants::SITE_URL . '/play-index/season_finder.cgi?'; if ($player_id) { my $player_name = gtrc( $dbh, 'bio', { player_ID => $player_id }, 'name_common' ); $title .= $SPACE . '&mdash' . $SPACE . $player_name; } my @yah = ( 'Play Index', qq{$title} ); my $model = { header => { page_title => $title, page_url => $page_url, you_are_here => generate_you_are_here( \@yah ), qi_section => 'play_index', page_description => 'Find single-season or multi-year totals to create batting leaderboards by team, position, age and more', }, form_id => 'season_finder', description => ' ', form_params => { sum => $sum, }, }; if ( !$describe_only ) { $model = _forms( $dbh, $srtemplates, $model, { sub_level => $sub_level, user => $user{user_ID}, id => $player_id, year => $year, type => $type, class => $class, q_param => $q, } ); } if ($is_submitted) { $model = SRlocal::PlayIndex::SeasonFinder::run_report( $dbh, $srtemplates, $model, { sub_level => $sub_level, user => $user{user_ID}, id => $player_id, year => $year, type => $type, is_ajax => $is_ajax, q_param => $q, describe_only => $describe_only, } ); } ## Display the page and cache if necessary pi_output_page( $dbh, $q, $srtemplates, $model, $describe_only, $sub_level, $FALSE ); } ## cleanup and exit. $dbhSR->disconnect; $dbh->disconnect; } =head2 _forms Parameters : dbh, srtemplates, model, ref Returns : ref to model Description : builds the form for the PI ref: { sub_level => from SRlocal::PlayIndex::Subscribe::subscriber_level user => from SR::Auth::Users::get_user id => player_id year => 2015, type => 'p' or 'b', class => 'team' or undef, q_param => cgi query, } =cut sub _forms { my ( $dbh, $srtemplates, $model, $ref ) = @_; my $id = $ref->{id}; my $sub_level = $ref->{sub_level}; my $year = $ref->{year}; my $type = $ref->{type}; my $class = $ref->{class}; my $q_param = $ref->{q_param}; my $form_settings; if ( $type eq 'b' ) { $form_settings = _get_player_bat_sl_form( $dbh, $srtemplates, $model, { id => $id, year => $year, q_param => $q_param, user => $ref->{user}, } ); } elsif ( $type eq 'p' ) { $form_settings = _get_player_pitch_sl_form( $dbh, $srtemplates, $model, { dbh => $dbh, year => $year, q_param => $q_param, id => $id, user => $ref->{user}, } ); } # Print out divs that allow us to view the information. $model->{form_output} = $srtemplates->process( 'Partials/Forms/Form_PI.tt2', $form_settings ); $model->{note} = $SRlocal::PlayIndex::Constants::DATA_SEASONS; return $model; } ################################################################################ # Print out the form for the batters. ################################################################################ ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _get_player_bat_sl_form { my ( $dbh, $srtemplates, $model, $ref ) = @_; my $id = $ref->{id}; my $year = is_empty_zero( $ref->{year} ); my $q_param = $ref->{q_param}; my $sum = $model->{form_params}{sum}; my $year_min = is_empty($id) ? 1901 : gtrc( $dbh, 'players', { player_ID => $id }, 'year_min' ); my $year_max = is_empty($id) ? $STATS_YEAR_MAX : gtrc( $dbh, 'players', { player_ID => $id }, 'year_max' ); my $years_widget = SRlocal::PlayIndex::Formatting::get_seasons_years_select( $year_min, $year_max ); my $hidden = $EMPTY_STR; $hidden = { type => 'hidden', name => 'id', val => $id } if ( is_nonempty($id) ); my $form_sort = SRlocal::PlayIndex::Constants::get_orderby_box( $SRlocal::PlayIndex::Constants::ORDERBY_BATTING_SEASON, 'HR', $SRlocal::PlayIndex::Constants::RESULTS_LAYOUT_BOX_BSL ); my $form_criteria = SRlocal::PlayIndex::Constants::get_form_additional_criteria( 'season_finder', 'b', 'player', $q_param ); my $saved_query_select = SRlocal::PlayIndex::get_saved_query_select( $dbh, $ref->{user} ); my $form_top_options = { fields => [ { name => 'sum', type => 'radio', val => 0, choices => [ { label => 'Find Single Seasons Totals' . ' (the most home runs in a season in 1960-1969 was 61 by Maris in 1961)', val => 0, default => $TRUE, sr_preset => { hide => '.playing_time_career', show => '.playing_time_season,#order_by_list' }, }, { label => 'Find Totals for Combined Seasons or Careers' . ' (the most home runs in 1960-1969 was 393 by Killebrew)', val => 1, sr_preset => { hide => '.playing_time_season', show => '.playing_time_career,#order_by_list' }, }, { label => 'Find Players with Seasons Matching Criteria' . ' (Babe Ruth had 11 forty home run seasons)', val => 'smatchCar', sr_preset => { hide => '.playing_time_career,#order_by_list', show => '.playing_time_season' }, }, { label => 'Find Teams with Players Matching Criteria (season)' . ' (the 2005 Rangers had seven players with 20+ home runs)', val => 'smatchTeam', sr_preset => { hide => '.playing_time_career,#order_by_list', show => '.playing_time_season' }, }, { label => 'Find Teams with Players Matching Criteria (career)' . ' (the Detroit Tigers have two players with 3000+ career hits)', val => 'smatchTeamCareer', sr_preset => { hide => '.playing_time_season,#order_by_list', show => '.playing_time_career' }, }, { label => 'Find Seasons with Players Matching Criteria' . ' (1999 had 103 players with 20+ home runs)', val => 'smatchYear', sr_preset => { hide => '.playing_time_career,#order_by_list', show => '.playing_time_season' }, }, ] } ] }; my $playing_time_season_box = clone($SRlocal::PlayIndex::Constants::PLAYING_TIME_SEASON_BOX); my $playing_time_career_box = clone($SRlocal::PlayIndex::Constants::PLAYING_TIME_CAREER_BOX); $playing_time_season_box->{toggleopen} = $sum eq '0' || $sum eq 'smatchCar' || $sum eq 'smatchTeam' || $sum eq 'smatchYear'; $playing_time_career_box->{toggleopen} = !$playing_time_season_box->{toggleopen}; my $form_main = { fields => [ { type => 'hidden', name => 'as', val => 'result_batter' }, { type => 'hidden', name => 'offset', val => 0 }, { type => 'hidden', name => 'type', val => 'b' }, $years_widget, $SRlocal::PlayIndex::Constants::SEASON_SELECT_BOXES, $SRlocal::PlayIndex::Constants::SEASON_AGE_SELECT_BOXES, $SRlocal::PlayIndex::Constants::ROOKIE_SEASON_RADIO, $SRlocal::PlayIndex::Constants::LEAGUE_THEN_TEAM_SELECT_BOXES, $SRlocal::PlayIndex::Constants::ACTIVE_PLAYER_BOX, $SRlocal::PlayIndex::Constants::HALLOFFAME_PLAYER_BOX, $SRlocal::PlayIndex::Constants::ALLSTAR_PLAYER_BOX, $SRlocal::PlayIndex::Constants::BATTING_HAND_BOX, $SRlocal::PlayIndex::Constants::THROWING_HAND_BOX, $SRlocal::PlayIndex::Constants::DEFENSIVE_POSITION_BOX_SEASON, $playing_time_season_box, $playing_time_career_box, $saved_query_select, ], }; push @{ $form_main->{fields} }, $hidden if ($hidden); my $form_settings = { method => 'get', action => '/play-index/season_finder.cgi', name => 'season_finder', formdata => scalar $q_param->Vars, form_sort => $form_sort, form_top_options => $form_top_options, form_main => $form_main, form_criteria => $form_criteria, }; return $form_settings; } ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _get_player_pitch_sl_form { my ( $dbh, $srtemplates, $model, $ref ) = @_; my $id = $ref->{id}; my $year = is_empty_zero( $ref->{year} ); my $q_param = $ref->{q_param}; my $year_min = is_empty($id) ? 1901 : gtrc( $dbh, 'players', { player_ID => $id }, 'year_min' ); my $year_max = is_empty($id) ? $STATS_YEAR_MAX : gtrc( $dbh, 'players', { player_ID => $id }, 'year_max' ); my $years_widget = SRlocal::PlayIndex::Formatting::get_seasons_years_select( $year_min, $year_max ); my $hidden = $EMPTY_STR; $hidden .= qq{\n} if ( is_nonempty($id) ); my $form_sort = SRlocal::PlayIndex::Constants::get_orderby_box( $SRlocal::PlayIndex::Constants::ORDERBY_PITCHING_SEASON, 'SO', $SRlocal::PlayIndex::Constants::RESULTS_LAYOUT_BOX_PSL ); my $form_criteria = SRlocal::PlayIndex::Constants::get_form_additional_criteria( 'season_finder', 'p', 'player', $q_param ); my $saved_query_select = SRlocal::PlayIndex::get_saved_query_select( $dbh, $ref->{user} ); my $form_top_options = { fields => [ { name => 'sum', type => 'radio', val => 0, choices => [ { label => 'Find Single Seasons Totals' . ' (the most strikeouts in a season in 1960-1969 was 382 by Koufax in 1965)', val => 0, default => $TRUE, sr_preset => { hide => '.playing_time_career', show => '.playing_time_season,#order_by_list' }, }, { label => 'Find Totals for Combined Seasons or Careers' . ' (the most strikeouts in 1960-1969 was 2,071 by Gibson)', val => 1, sr_preset => { hide => '.playing_time_season', show => '.playing_time_career,#order_by_list' }, }, { label => 'Find Players with Seasons Matching Criteria' . ' (Nolan Ryan had 15 200+ strikeout seasons)', val => 'smatchCar', sr_preset => { hide => '.playing_time_career,#order_by_list', show => '.playing_time_season' }, }, { label => 'Find Teams with Players Matching Criteria (season)' . ' (the 1969 Astros had three pitchers with 200+ strikeouts)', val => 'smatchTeam', sr_preset => { hide => '.playing_time_career,#order_by_list', show => '.playing_time_season' }, }, { label => 'Find Teams with Players Matching Criteria (career)' . ' (the Yankees have 9 pitchers with 150+ career wins)', val => 'smatchTeamCareer', sr_preset => { hide => '.playing_time_season,#order_by_list', show => '.playing_time_career' }, }, { label => 'Find Seasons with Players Matching Criteria' . ' (1969 had 15 players with 200+ strikeouts)', val => 'smatchYear', sr_preset => { hide => '.playing_time_career,#order_by_list', show => '.playing_time_season' }, }, ] } ] }; my $form_main = { fields => [ { type => 'hidden', name => 'as', val => 'result_pitcher' }, { type => 'hidden', name => 'offset', val => 0 }, { type => 'hidden', name => 'type', val => 'p' }, $years_widget, $SRlocal::PlayIndex::Constants::SEASON_SELECT_BOXES, $SRlocal::PlayIndex::Constants::SEASON_AGE_SELECT_BOXES, $SRlocal::PlayIndex::Constants::ROOKIE_SEASON_RADIO, $SRlocal::PlayIndex::Constants::LEAGUE_THEN_TEAM_SELECT_BOXES, $SRlocal::PlayIndex::Constants::ACTIVE_PLAYER_BOX, $SRlocal::PlayIndex::Constants::HALLOFFAME_PLAYER_BOX, $SRlocal::PlayIndex::Constants::ALLSTAR_PLAYER_BOX, $SRlocal::PlayIndex::Constants::THROWING_HAND_BOX, $SRlocal::PlayIndex::Constants::PITCHER_SEASON_ROLE_BOX, $SRlocal::PlayIndex::Constants::PITCHING_TIME_SEASON_BOX, $SRlocal::PlayIndex::Constants::PITCHING_TIME_CAREER_BOX, $saved_query_select, ], }; push @{ $form_main->{fields} }, $hidden if ($hidden); my $form_settings = { method => 'get', action => '/play-index/season_finder.cgi', name => 'season_finder', formdata => scalar $q_param->Vars, form_sort => $form_sort, form_top_options => $form_top_options, form_main => $form_main, form_criteria => $form_criteria, }; return $form_settings; }