Rev 22 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/perluse strict;use cPanelUserConfig;use RollerCon;use HTML::Tiny;use CGI qw/param header start_html url uploadInfo/;my $h = HTML::Tiny->new( mode => 'html' );my $cookie_string = authenticate (5) || die;my ($EML, $PWD, $LVL) = split /&/, $cookie_string;my $ID = getUser ($EML);my $RCAUTH_cookie = cookie (-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");my $tmpdir = "/tmp/";print header (-cookie=>$RCAUTH_cookie),start_html (-title => "Bulk Upload vORC Data", -style => {'src' => "/style.css"} );if (defined (param ("uploadedfile"))) {process_form ();} else {display_upload_form ();}print end_html ();use CGI qw(:standard escape escapeHTML);sub display_upload_form {print $h->div ("Upload a CSV File of department shifts...");print $h->div ("These should be the columns: (dept, role, type, date, location, start_time, end_time, doubletime, note)");print start_multipart_form (-action => url ()),# "Upload File: ", br (),$h->input ({name => "uploadedfile",class => "inputfile",type => "file",id => "file",size => 60}) . $h->label ({ for=>"file", class=>"top" }, $h->span ("Choose File...")),br (), br (),submit (-name => "choice", -value => "Submit"), $h->input ({ type=>"button", value => "Cancel" , onClick=>"history.back(); return false;" }),end_form ();printJavascript ();}sub printJavascript {print<<JSCRIPT;<SCRIPT language="JavaScript"><!--var inputs = document.querySelectorAll( '.inputfile' );Array.prototype.forEach.call( inputs, function( input ){var label = input.nextElementSibling,labelVal = label.innerHTML;input.addEventListener( 'change', function( e ){var fileName = e.target.value.split( '\\\\' ).pop();if( fileName )label.querySelector( 'span' ).innerHTML = fileName;elselabel.innerHTML = labelVal;});});//--></SCRIPT>JSCRIPT}sub process_form {use WebDB;my $dbh = WebDB::connect ();my $uploadedfile = param ("uploadedfile");my @errors = ();my $mime_type;my $serve_url;logit ($ID->{RCid}, "Bulk Uploaded shifts from file $uploadedfile");push (@errors, "Please specify a file") if $uploadedfile eq "";$mime_type = uploadInfo ($uploadedfile)->{'Content-Type'};if ($mime_type ne "text/csv") {push @errors, "Expecting a CSV file, but received a '$mime_type'.";}if (@errors) {print p ("The following errors occurred:");print ul (li (\@errors));print p ("Please click your Browser's Back button to\n". "return to the previous page and correct the problem.");return;} # Form was okay;print $h->div ("Processing file...");my $fh = upload ('uploadedfile');my @columnlabels = split /,/, <$fh>;@columnlabels = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } @columnlabels;my $idtrue = 0;if ($columnlabels[0] eq "id") {$idtrue = 1;shift @columnlabels;}my @AcceptableColumns = qw(dept, role, type, date, location, start_time, end_time, doubletime, note);my $fields = join ", ", @columnlabels;my $values = join ", ", map { '?' } 0..$#columnlabels;my $sth = $dbh->prepare ("insert into shift ($fields) values ($values)");while (<$fh>) {chomp;my @R = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } split /,/;shift @R if $idtrue;push @R, "" if (scalar @R < scalar @columnlabels and eof);print "inserting: ", join (" ", @R), $h->br;$sth->execute (map { defined $_ ? $_ : undef } @R) or print $sth->errstr;}print "DONE!", $h->br, $h->br;$dbh->disconnect (); # Image was stored into database successfully. Present confirmation# Display link to main page so user can upload another imageprint $h->hr, $h->a ({ href => url }, "[Upload another file]"), $h->a ({ href => "/schedule/" }, "[Home]");}sub error {my $msg = shift;print p (escapeHTML ("Error: $msg")), end_html ();exit (0);}