#!/usr/bin/perl -w # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-03-07 16:34:28 -0500 (Tue, 07 Mar 2017) $ # $Author: hvs $ # $Rev: 49720 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/play-index/game_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 qw($Bin); use JSON; use Modern::Perl '2010'; use lib "$Bin/../lib"; use SR::Auth::Users; use SR::Cache; use SR::Defaults; use SR::Logging; 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::GameFinder; use SRlocal::Retrosheet::Plays; use SRlocal::Stats; use SRlocal::Templates; chomp($0); #### [] Starting: "$Bin/$0 " . join(' ',@ARGV) MAIN: { # make connections 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 ) { ################################################################## # Get our data out and set up our request. ################################################################## 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|player)$/, 'player' ); 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 $suffix = url_regex( $q, 'suffix', qr/^_[a-z]+$/, '' ); our $title = ( $class eq 'team' ? 'Team ' : $EMPTY_STR ) . ( $type eq 'b' ? 'Batting' : $type eq 'p' ? 'Pitching' : 'Other' ) . ' Game Finder'; our $page_url = $SRlocal::Constants::SITE_URL . '/play-index/game_finder.cgi?'; if ($player_id) { my $player_name = gtrc( $dbh, 'bio', { player_ID => $player_id }, 'name_common' ); $title .= $SPACE . '—' . $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 => 'game_finder', description => ' ', }; if ( !$describe_only ) { ## Get the model for generating the PI form my $form_settings = _get_gl_form( $dbh, $q, $srtemplates, $model, $class, $type, $player_id, $suffix ); ## Generate the PI form $model->{form_output} = $srtemplates->process( 'Partials/Forms/Form_PI.tt2', $form_settings ); ## Add a note about data availablity $model->{note} = $SRlocal::PlayIndex::Constants::DATA_SEASONS; } # if this is a form submission, ajax or not, get the results. if ($is_submitted) { $model = SRlocal::PlayIndex::GameFinder::run_report( $dbh, $srtemplates, $model, { dbh => $dbh, sub_level => $sub_level, user => $user{user_ID}, id => $player_id, type => $type, is_ajax => $is_ajax, q_param => $q, describe_only => $describe_only, suffix_type => $suffix, } ); } ## 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; } =head2 _get_gl_form Parameters : $class => 'team' 'player' $type => 'b' 'p' Description: Builds the form for the different game finders =cut sub _get_gl_form { my ( $dbh, $cgi, $srtemplates, $model, $class, $type, $player_id, $suffix ) = @_; my $report = $class eq 'team' ? 't' . $type : $type; my $match = _nempp( $cgi->param('match'), 'basic' ); my $type_name = $type eq 'b' ? 'batting' : 'pitching'; my $type_name_2 = $type eq 'b' ? 'batter' : 'pitcher'; ## Build the report type selector my $form_top_options = { fields => [ { name => 'match', type => 'radio', val => 'basic', choices => [] } ] }; ## Add all of the report types to the form foreach my $option ( @{ $PI_GAME_FINDER_DETAILS{$report}{types} } ) { my $choice = { label => $PI_GAME_FINDER_TYPES{$class}{$option} . ' (' . $PI_GAME_FINDER_DETAILS{$report}{match}{$option} . ')', val => $option, sr_preset => { hide => ( $option eq 'basic' ? '.sort_by_matching_games' : '.orderby' ), show => ( $option eq 'basic' ? '.orderby' : '.sort_by_matching_games' ), } }; push @{ $form_top_options->{fields}[0]{choices} }, $choice; } ## Check to see if we should display the weather box. my $using_weather = !_emp( $cgi->param('use_temperature') ) || !_emp( $cgi->param('use_wind_speed') ) || !_emp( $cgi->param('use_wind_direction') ) || !_emp( $cgi->param('use_precipitation') ) || !_emp( $cgi->param('use_sky') ); my $weather_box = $SRlocal::PlayIndex::Constants::WEATHER_BOX; $weather_box->{var}{toggleopen} = $using_weather; ## Build the main part of the PI form my $form_main = { fields => [ @{ $PI_GAME_FINDER_DETAILS{$report}{additional_fields} }, $weather_box, { type => 'hidden', name => 'as', val => ( $class eq 'team' ? 'team_' . $type_name : 'result_' . $type_name_2 ) }, { type => 'hidden', name => 'class', val => $class }, { type => 'hidden', name => 'offset', val => 0 }, { type => 'hidden', name => 'type', val => $type }, ], }; ## Update default selections if ($suffix) { if ( $suffix eq '_post' ) { ## Make a copy since we are changing it. my $game_type_select = $form_main->{fields}[$SRlocal::PlayIndex::GameFinder::ADDITIONAL_FIELD_GAME_TYPE_SELECT]; $game_type_select = clone($game_type_select); $form_main->{fields}[$SRlocal::PlayIndex::GameFinder::ADDITIONAL_FIELD_GAME_TYPE_SELECT] = $game_type_select; ## Make postseason select box visible $game_type_select->{subfields}[1]{toggleopen} = $TRUE; $game_type_select->{subfields}[2]{toggleopen} = $TRUE; } ## Make a copy since we are changing it. my $game_year_select = $form_main->{fields}[$SRlocal::PlayIndex::GameFinder::ADDITIONAL_FIELD_GAME_YEAR_SELECT]; $game_year_select = clone($game_year_select); $form_main->{fields}[$SRlocal::PlayIndex::GameFinder::ADDITIONAL_FIELD_GAME_YEAR_SELECT] = $game_year_select; ## Change the year selections $game_year_select->{subfields}[0]{choices} = $suffix eq '_as' ? [@SRlocal::PlayIndex::Constants::ALLSTAR_YEARS] : $suffix eq '_post' ? [@SRlocal::PlayIndex::Constants::POST_YEARS] : []; $game_year_select->{subfields}[2]{choices} = $suffix eq '_as' ? [@SRlocal::PlayIndex::Constants::ALLSTAR_YEARS] : $suffix eq '_post' ? [@SRlocal::PlayIndex::Constants::POST_YEARS] : []; } ## Any dynamic widgets need to be executed. my $i = 0; for my $field ( @{ $form_main->{fields} } ) { if ( ref $field eq 'CODE' ) { $form_main->{fields}[$i] = &$field($cgi); } $i++; } push @{ $form_main->{fields} }, { type => 'hidden', name => 'id', val => $player_id } if $player_id; ## Build the filter criteria control my $form_criteria = SRlocal::PlayIndex::Constants::get_form_additional_criteria( 'game_finder', $report, $class, $cgi ); ## Build the sort control my $form_sort = SRlocal::PlayIndex::Constants::get_orderby_box( $PI_GAME_FINDER_DETAILS{$report}{order_by}, $PI_GAME_FINDER_DETAILS{$report}{order_by_default}, undef, { sort_by_matching_games => $match ne 'basic' } ); ## Build the final form details my $form_settings = { method => 'get', action => '/play-index/game_finder.cgi', name => 'game_finder', script => $SRlocal::PlayIndex::Constants::GAME_YEAR_SELECT_JS . $SRlocal::PlayIndex::Constants::CHOOSE_TEAM_BY_NICKNAME_SELECT_JS, formdata => scalar $cgi->Vars, form_sort => $form_sort, form_top_options => $form_top_options, form_main => $form_main, form_criteria => $form_criteria, }; return $form_settings; }