#!/usr/bin/perl -w # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-03-14 15:59:32 -0400 (Tue, 14 Mar 2017) $ # $Author: hvs $ # $Rev: 49905 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/play-index/event_finder.cgi $ # Copyright 2000-2017, SPORTS REFERENCE, LLC All rights reserved. use Carp; use CGI::Fast ':standard'; use FindBin qw($Bin); 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::Players; use SRlocal::PlayIndex; use SRlocal::PlayIndex::Formatting; use SRlocal::PlayIndex::Subscribe; use SRlocal::PlayIndex::Constants; use SRlocal::PlayIndex::Statdefine; use SRlocal::PlayIndex::EventFinder; use SRlocal::Retrosheet::Plays; use SRlocal::Stats; use SRlocal::Templates; chomp($0); MAIN: { my $dbh = get_site_db_connection( $Bin, $SRlocal::PlayIndex::Constants::PI_DSN ); my $dbhSR = get_site_db_connection( $Bin, 'SR' ); my $site_params_ref = get_site_params($Bin); my $srtemplates = new SRlocal::Templates; while ( my $q = new CGI::Fast ) { ## Check user subscription my %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 $is_ajax = url_boolean( $q, 'ajax', $FALSE ); my $is_submitted = url_boolean( $q, 'request', $FALSE ); my $describe_only = url_boolean( $q, 'describe_only', $FALSE ); my $is_post = url_boolean( $q, 'post', $FALSE ); my $is_allstar = url_boolean( $q, 'as', $FALSE ); my $event = url_alphanum( $q, 'event', 23 ); my $suffix = url_regex( $q, 'suffix', qr/^_[a-z]+$/, '' ); if ( !$suffix ) { $suffix = $is_post ? '_post' : $is_allstar ? '_as' : ''; } ## Set the years for All-Star since we don't pass those. if ($is_allstar) { $year = $FIRST_YEAR_ALLSTAR; $q->append( -name => 'year', -values => $FIRST_YEAR_ALLSTAR ); $q->append( -name => 'year_to', -values => $LAST_YEAR_ALLSTAR ); } my $page_url = $SRlocal::Constants::SITE_URL . '/play-index/event_finder.cgi?'; my $title = ( !$player_id && !$is_post && !$is_allstar ? 'Team ' : '' ) . ( $is_allstar ? 'All-Star ' : '' ) . ( $is_post ? 'Postseason ' : '' ) . ( $type eq 'b' ? 'Batting' : $type eq 'p' ? 'Pitching' : 'Other' ) . ' Event Finder'; if ($player_id) { my $player_name = gtrc( $dbh, 'bio', { player_ID => $player_id }, 'name_common' ); my $player_url = generate_player_url($player_id); $title .= qq{ — $player_name}; } $year = 0 if ( $year =~ /career/ ); 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 => 'event_finder', }; if ( !$describe_only ) { $model = _forms( $dbh, $q, $srtemplates, $model, { sub_level => $sub_level, id => $player_id, year => $year, event => $event, type => $type, user => $user{user_ID}, is_post => $is_post, is_allstar => $suffix && $suffix eq '_as', } ); } if ($is_submitted) { $model = SRlocal::PlayIndex::EventFinder::run_report( $dbh, $q, $srtemplates, $model, { sub_level => $sub_level, id => $player_id, year => $year, event => $event, type => $type, user => $user{user_ID}, suffix_type => $suffix, is_post => $is_post, is_allstar => $suffix && $suffix eq '_as', division_to_print => url_integer( $q, 'divisory', 0 ), out_type => url_regex( $q, 'out_type', qr/^out[1-9]$/, '' ), } ); } ## We need to output the form here because running the report can ## add event fields to the form. $model->{form_output} = $srtemplates->process( 'Partials/Forms/Form_PI.tt2', $model->{form} ); ## Display the page and cache if necessary pi_output_page( $dbh, $q, $srtemplates, $model, $describe_only, $sub_level, $TRUE ); } # cleanup and exit. $dbhSR->disconnect; $dbh->disconnect; } sub _build_years_array { my ( $from, $to, $first_year ) = @_; $first_year = $first_year // $FIRST_YEAR_RETRO_PBP; my @years = ( $from .. $to ); my $years = [ map { { val => $_, label => $_, disabled => $_ < $first_year, default => $_ == $CURRENT_YEAR_RETRO } } @years ]; return $years; } ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _forms { my ( $dbh, $cgi, $srtemplates, $model, $ref ) = @_; my $id = $ref->{id}; my $sub_level = $ref->{sub_level}; my $year = $ref->{year}; my $event = $ref->{event}; my $type = $ref->{type}; my $is_post = $ref->{is_post}; my $is_allstar = $ref->{is_allstar}; my $hidden = $EMPTY_STR; my $years_second_set = $EMPTY_STR; my $fields = []; my $from = url_alpha( $cgi, 'from', 'button' ); my $divisory = url_integer( $cgi, 'divisory', 1 ); my $years_select = { label => 'Years', group => $TRUE, linear => $TRUE, name => 'event_year', subfields => [ { type => 'dropdown', name => 'year', no_chosen => $TRUE, }, { type => 'sr_preset_button', label => 'to', data => { preset => "{'year': 'year_to'}" }, }, { type => 'dropdown', name => 'year_to', no_chosen => $TRUE, }, ], }; push @$fields, $years_select if !$is_allstar; my $team_select = { type => 'dropdown', label => 'Team', name => 'team_id', no_chosen => $TRUE, choices => [ { val => '', label => 'All Teams' }, @SRlocal::PlayIndex::Constants::_franchises, ], }; # we have a player here if ( is_nonempty($id) ) { my $year_min = gtrc( $dbh, 'players', { player_ID => $id }, 'year_min' ); my $year_max = gtrc( $dbh, 'players', { player_ID => $id }, 'year_max' ); $years_select->{subfields}[0]{choices} = _build_years_array( $year_min, $year_max ); $years_select->{subfields}[2]{choices} = _build_years_array( $year_min, $year_max ); push @$fields, { type => 'hidden', name => 'id', val => $id }; if ($is_post) { push @$fields, { type => 'hidden', name => 'post', val => $TRUE }; push @$fields, { type => 'hidden', name => 'suffix', val => '_post' }; $years_select->{label} = 'Postseason'; } } elsif ($is_allstar) { push @$fields, { type => 'hidden', name => 'as', val => $TRUE }; push @$fields, { type => 'hidden', name => 'suffix', val => '_as' }; push @$fields, { type => 'hidden', name => 'team_id', val => '' }; push @$fields, { type => 'label', label => 'Years', val => 'All-Star Games (1933-Present)' }; } elsif ($is_post) { push @$fields, { type => 'hidden', name => 'post', val => $TRUE }; push @$fields, { type => 'hidden', name => 'suffix', val => '_post' }; $years_select->{subfields}[0]{choices} = _build_years_array( $FIRST_YEAR_POST, $LAST_YEAR_POST, $FIRST_YEAR_POST ); $years_select->{subfields}[2]{choices} = _build_years_array( $FIRST_YEAR_POST, $LAST_YEAR_POST, $FIRST_YEAR_POST ); $years_select->{label} = "Postseason ($FIRST_YEAR_POST-Present)"; push @$fields, $team_select; } else { $years_select->{subfields}[0]{choices} = _build_years_array( $FIRST_YEAR_RETRO_PBP, $LAST_YEAR_RETRO_PBP ); $years_select->{subfields}[2]{choices} = _build_years_array( $FIRST_YEAR_RETRO_PBP, $LAST_YEAR_RETRO_PBP ); push @$fields, $team_select; } push @$fields, { type => 'hidden', name => 'divisory', val => $divisory }, { type => 'hidden', name => 'from', val => $from }, { type => 'hidden', name => 'type', val => $type }, $SRlocal::PlayIndex::Constants::choose_events, $SRlocal::PlayIndex::Constants::choose_out_types; ## Build the final form details my $form = { method => 'get', action => '/play-index/event_finder.cgi', name => 'event_finder', formdata => scalar $cgi->Vars, open => $TRUE, form_main => { fields => $fields }, }; # print STDERR Dumper $fields; $model->{note} = $is_allstar ? qq{Event data is mostly complete back to $FIRST_YEAR_ALLSTAR.} : $is_post ? qq{Event data is mostly complete back to $FIRST_YEAR_POST.} : qq{Event data is complete back to 1974 and mostly complete back to 1950.}; $model->{form} = $form; return $model; }