#!/usr/bin/perl -w # AUTHORS # Sean Forman # Hans Van Slooten # $Date: 2017-03-06 12:12:11 -0500 (Mon, 06 Mar 2017) $ # $Author: hvs $ # $Rev: 49675 $ # $HeadURL: http://svn.sports-reference.com/svn/br_repos/br/trunk/play-index/inning_summary.cgi $ # Copyright 2000-2017, SPORTS REFERENCE, LLC All rights reserved. use Carp; use CGI; use FindBin qw($Bin); use Modern::Perl '2010'; use lib "$Bin/../lib"; use SR::Auth::Users; use SR::Defaults; use SR::Statline; use SRlocal::Constants; use SRlocal::DB; use SRlocal::Defaults; use SRlocal::Formatting; use SRlocal::Stats; use SRlocal::PlayIndex; use SRlocal::PlayIndex::Formatting; use SRlocal::PlayIndex::Subscribe; use SRlocal::PlayIndex::Constants; use SRlocal::PlayIndex::Statdefine; use SRlocal::Templates; chomp($0); MAIN: { my $q = new CGI; 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; ################################################################## # 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' ); my $year = url_alphanum( $q, 'year', 0 ); my $team = url_regex( $q, 'team_id', qr/^[A-Z]{2,3}$/, 'ANY' ); my $is_submitted = url_regex( $q, 'request', qr/^1$/, $FALSE ); my $describe_only = url_regex( $q, 'describe_only', qr/^1$/, $FALSE ); my $team_name = gtrc( $dbh, 'majors_team', { year_ID => $year, franch_ID => $team }, 'name' ); my $title = 'Scoring and Leads Summary'; if ( $team_name && $year ) { $title = "$year $team_name $title"; } elsif ( $team && $team ne 'ANY' && $year ) { $title = "$year $team $title"; } elsif ( $team && $team eq 'ANY' && $year ) { $title = "$year MLB $title"; } else { $title = "Teams $title"; } my $page_url = $SRlocal::Constants::SITE_URL . '/play-index/inning_summary.cgi?'; my @yah = ( 'Play Index', qq{$title} ); my $model = { header => { you_are_here => generate_you_are_here( \@yah ), page_title => $title, page_url => $page_url, page_description => 'Find single-season or multi-year totals to create batting leaderboards by team, position, age and more', qi_section => 'play_index', }, form_id => 'inning_summary', }; my $form_settings = _forms( $dbh, $q, $model, $srtemplates, { sub_level => $sub_level, team => $team, year => $year, } ); ## 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 . ' By inning scoring data is complete back to 1940.'; # if this is a form submission, ajax or not, get the results. if ($year) { _run_report( $dbh, $q, $model, $srtemplates, { sub_level => $sub_level, team => $team, year => $year, } ); } ## Generate the page my $page_data; if ( !$describe_only ) { $page_data = $srtemplates->process( 'Pages/PlayIndex.tt2', $model ); } else { $page_data = encode_json( $model->{description_array} ); } ## Output the file to the user print $q->header( -type => ( !$describe_only ? 'text/html' : 'text/json' ), -charset => 'utf-8', -SR_SRC => 'build' ); print $page_data; # cleanup and exit. $dbhSR->disconnect; $dbh->disconnect; } ################################################################### # Usage : zzzz # Purpose : zzzz # Returns : zzzz # Parameters : zzzz # Throws : zzzz # See Also : zzzz # Comments : zzzz ################################################################### sub _forms { my ( $dbh, $cgi, $model, $srtemplates, $ref ) = @_; my $sub_level = $ref->{sub_level}; my $year = $ref->{year}; my $team = $ref->{team}; # Preselect the team currently chosen. my $team_choose = ''; $team_choose =~ s/ selected//i; $team_choose =~ s/(