#!/usr/bin/perl BEGIN { use lib '/usr/home/cfg' ; require push_inc ; } use CGI qw( :standard ) ; use CGI::Carp qw(fatalsToBrowser) ; use LWP::Simple qw($ua get) ; use Date::Calc qw(:all) ; use MIME::Lite ; use Mail::Sendmail ; use Time::Piece ; use Spreadsheet::ParseXLSX ; use File::Temp qw(tempfile) ; require cfg ; print header; # CGI.pm method #------------------------------------------------------------------------------- my @ARGV = split(/\&/, $ENV{'QUERY_STRING'}); my $type = $ARGV[0] ; our ($q) = CGI -> new() ; my $iaction = $q -> param('iaction') ; my $iattachdoc = $q -> param('iattachdoc') ; # our $debug = 1 ; # our $testing = 1 ; #------------------------------------------------------------------------------------------ &today ; if ($iaction eq '') { &screen1 ; } elsif ($iaction eq 'upload') { &db_open_ro ; our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it &read_import ; &screen2 ; $db_ignore_open_close = 0 ; # do one open and one close instead of repeating it &db_close_conn ; } exit ; #------------------------------------------------------------------------------------------ sub read_read_import_lines { our $max_unmatched_id = 0 ; my $upload_filehandle = $q->upload('iattachdoc') ; } #------------------------------------------------------------------------------------------ sub read_import { $print_box_content_rows .= &common_min_forms_start('import-analytics-fixtures') ; my $upload_filehandle = $q->upload('iattachdoc') ; unless ($upload_filehandle) { $warning = "PLEASE UPLOAD AN EXCEL FILE!!!" ; &common_min_alert_type ; &screen1 ; } our %totals = () ; my $line_cnt = 0 ; my %event_cnt = () ; if ($upload_filehandle !~ /.xlsx/) { $error = "PLEASE UPLOAD AN EXCEL TYPE FILE!!!" ; &common_min_alert_type ; &screen1 ; } my $file_total_amnt = 0 ; my $file_nr_of_rows = 0 ; our %coding_type = ("s" => "Standard","p" => "Premium") ; # our %footage_type = ("s" => "SSS","r" => "Raw") ; our %footage_type = ("s" => "Pixellot","r" => "YouTube") ; our %coding_type2 = ("standard" => "s","premium" => "p") ; our %footage_type2 = ("sss" => "s","raw" => "r","pixellot" => "s","youtube" => "r") ; our %service_type2 = ("live coding" => 1,"individual 48 hrs" => 2,"post coding 24 hrs" => 3) ; our %fixtures_input_vals = () ; our %fixtures_input_vals_2 = () ; our $suffix = "fixtures" ; if ($type eq 'xlsx') { &db_min_ro('analytics_ht_colors','id,name','','','') ; foreach (keys %{$db{analytics_ht_colors}}) { $analytics_ht_colors_name_to_id{lc $db{analytics_ht_colors}{$_}{name}} = $_ ; } &db_min_ro('sport_types','id,name','','','') ; foreach (keys %{$db{sport_types}}) { $sport_type_name_to_id{lc $db{sport_types}{$_}{name}} = $_ ; } $sport_type_name_to_id{"waterpolo"} = 4 ; &db_switch_conn('sss'); &db_min_ro('teams','id,name','','','') ; foreach (keys %{$db{teams}}) { $team_name_to_id{lc $db{teams}{$_}{name}} = $_ ; } &db_switch_conn('aisa'); my %seen_col_val = () ; if ($type eq 'csv') { while ( <$upload_filehandle> ) { # foreach (@imported_file_data) { next if $_ !~ /\d+/ ; $file_nr_of_rows++ ; chomp $_ ; $_ =~ s/\R\z// ; # Removes any line-ending character (\n, \r\n, or \r) $_ =~ s/\"//g ; my @def_split = split(/\;/,$_) ; push @rows, \@def_split; } } else { my ($tmpfh, $tmpfilename) = tempfile(SUFFIX => '.xlsx', UNLINK => 1); binmode $upload_filehandle ; binmode $tmpfh ; while (my $bytesread = read($upload_filehandle, my $buffer, 8192)) { print $tmpfh $buffer ; } close $tmpfh ; my $parser = Spreadsheet::ParseXLSX->new() ; my $workbook = $parser->parse($tmpfilename) ; unless ($workbook) { $error = "Failed to parse Excel file: " . $parser->error() ; &common_min_alert_type ; &screen1 ; } my $worksheet = ($workbook->worksheets())[0] ; my ($row_min,$row_max) = $worksheet->row_range() ; my ($col_min,$col_max) = $worksheet->col_range() ; for my $row ($row_min .. $row_max) { my @def_split = (); for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); my $val = $cell ? $cell->value() : ''; push @def_split, $val; } push @rows, \@def_split if grep { /\S/ } @def_split; # skip empty } } my $row_cnt = 0 ; my $table_row_cnt = 0 ; foreach my $def_split_ref (@rows) { $row_cnt++; my @def_split = @$def_split_ref; my %seen_row = () ; for my $col_cnt (1 .. 13) { my $val = $def_split[$col_cnt - 1] ; next if !$val || $val =~ /Analytics-Fixtures/ || $val =~ /Analytics Fixtures/ || $val =~ /Fixture Date/ || $val =~ /Fixtures Date/ || $val =~ /Time From/ || $val =~ /Sport/ || $val =~ /Team/ || $val =~ /HT Colour/ || $val =~ /Away Team/ || $val =~ /Coding Type/ || $val =~ /Footage Type/ || $val =~ /Service Type/ || $val =~ /Age Group/ || $val =~ /Stream Forwarding/ || $val =~ /Stream Key/ || $val =~ /Stream URL/ || $val =~ /Stream url/ || $val =~ /Stream Url/ || $val =~ /Cant Charge Client/ ; $table_row_cnt++ unless $seen_row{$row_cnt} ; $seen_row{$row_cnt} = 1 ; if ($col_cnt == 1) { $val =~ s/\T//g ; } elsif ($col_cnt == 2) { $val = $sport_type_name_to_id{lc $val} ; } elsif ($col_cnt == 3 || $col_cnt == 4) { $val = lc $val ; } elsif ($col_cnt == 5 || $col_cnt == 7) { $fixtures_input_vals_2{$table_row_cnt}{$col_cnt} = $val if $val; $val = $team_name_to_id{lc $val} ; } elsif ($col_cnt == 6) { $val = $analytics_ht_colors_name_to_id{lc $val} ; } elsif ($col_cnt == 8) { $val = $coding_type2{lc $val} ; } elsif ($col_cnt == 9) { $val = $footage_type2{lc $val} ; } elsif ($col_cnt == 10) { $val = $service_type2{lc $val} ; } elsif ($col_cnt == 11) { $val = (lc $val eq 'yes') ? 1 : (lc $val eq 'no') ? 2 : 0 ; } elsif ($col_cnt == 12 || $col_cnt == 13) { $val =~ s/_x000D_//g ; $val = join("_new_line_character_", map {"$_"} split("\n",$val)) ; } $fixtures_input_vals{$table_row_cnt}{$col_cnt} = $val if $val; } } } our %col_cnt_field_id = () ; our %col_cnt_field_id_2 = () ; $col_cnt_field_id{1} = "datetimepickerStart_date_time_fixtures_" ; $col_cnt_field_id{2} = "selectReadonly_sport_fixtures_" ; $col_cnt_field_id{3} = "selectAge_group_fixtures_" ; $col_cnt_field_id{4} = "selectTeam_fixtures_" ; $col_cnt_field_id{5} = "input[name='home_team_fixtures_" ; $col_cnt_field_id_2{5} = "input[name='typeahead_home_team_fixtures_" ; $col_cnt_field_id{6} = "selectHt_colour_fixtures_" ; $col_cnt_field_id{7} = "input[name='away_team_fixtures_" ; $col_cnt_field_id_2{7} = "input[name='typeahead_away_team_fixtures_" ; $col_cnt_field_id{8} = "selectCoding_type_fixtures_" ; $col_cnt_field_id{9} = "selectFootage_type_fixtures_" ; $col_cnt_field_id{10} = "selectService_type_fixtures_" ; $col_cnt_field_id{11} = "selectStream_forwarding_fixtures_" ; $col_cnt_field_id{12} = "textareaStream_key_fixtures_" ; $col_cnt_field_id{13} = "textareaStream_URL_fixtures_" ; our $col_cnt_field_id_str = qq~~ ; our $col_cnt_field_id_str_2 = qq~~ ; our $col_cnt_field_id_str2 = qq~"11":"selectStream_forwarding_analytics_","12":"textareaStream_key_analytics_","13":"textareaStream_URL_analytics_"~ ; foreach (keys %col_cnt_field_id) { $col_cnt_field_id_str .= qq~"$_":"$col_cnt_field_id{$_}",~ ; } $col_cnt_field_id_str =~ s/\,+$//g ; foreach (keys %col_cnt_field_id_2) { $col_cnt_field_id_str_2 .= qq~"$_":"$col_cnt_field_id_2{$_}",~ ; } $col_cnt_field_id_str_2 =~ s/\,+$//g ; our $max_row_cnt = 0 ; our $fixtures_input_vals_str = qq~~ ; our $fixtures_input_vals_str_2 = qq~~ ; our %add_select_options = () ; foreach my $row_cnt (sort {$a <=> $b} keys %fixtures_input_vals_2) { $fixtures_input_vals_str_2 .= qq~"$row_cnt":{~ ; foreach my $col_cnt (sort {$a <=> $b} keys %{$fixtures_input_vals_2{$row_cnt}}) { $fixtures_input_vals_str_2 .= qq~"$col_cnt":"$fixtures_input_vals_2{$row_cnt}{$col_cnt}",~ ; } $fixtures_input_vals_str_2 =~ s/\,+$//g ; $fixtures_input_vals_str_2 .= qq~},~ ; } $fixtures_input_vals_str_2 =~ s/\,+$//g ; foreach my $row_cnt (sort {$a <=> $b} keys %fixtures_input_vals) { $max_row_cnt = $row_cnt if !$max_row_cnt || $row_cnt > $max_row_cnt ; $fixtures_input_vals_str .= qq~"$row_cnt":{~ ; foreach my $col_cnt (sort {$a <=> $b} keys %{$fixtures_input_vals{$row_cnt}}) { $fixtures_input_vals_str .= qq~"$col_cnt":"$fixtures_input_vals{$row_cnt}{$col_cnt}",~ ; if ($col_cnt == 2) { $add_select_options{selectReadonly_sport_fixtures}{$fixtures_input_vals{$row_cnt}{$col_cnt}} = $db{sport_types}{$fixtures_input_vals{$row_cnt}{$col_cnt}}{name} ; } elsif ($col_cnt == 5) { # $add_select_options{selectHome_team_fixtures}{$fixtures_input_vals{$row_cnt}{$col_cnt}} = $db{teams}{$fixtures_input_vals{$row_cnt}{$col_cnt}}{name} ; } elsif ($col_cnt == 7) { # $add_select_options{selectAway_team_fixtures}{$fixtures_input_vals{$row_cnt}{$col_cnt}} = $db{teams}{$fixtures_input_vals{$row_cnt}{$col_cnt}}{name} ; } elsif ($col_cnt == 11) { } } $fixtures_input_vals_str =~ s/\,+$//g ; $fixtures_input_vals_str .= qq~},~ ; } $fixtures_input_vals_str =~ s/\,+$//g ; $fixtures_input_vals_str =~ s/\R\z// ; # Removes any line-ending character (\n, \r\n, or \r) # print "\n fixtures_input_vals_str : $fixtures_input_vals_str" ; return unless $max_row_cnt ; } #------------------------------------------------------------------------------------------ sub screen1 { &common_min_dialog ; $print_box_content_rows .= qq~