Subversion Repositories VORC

Rev

Rev 128 | Rev 136 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 - 1
#!/usr/bin/perl
2
 
56 bgadell 3
# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)
4
my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";
5
close STDERR;
6
open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";
7
#warn "Redirecting errors to ${error_log_path}vorc_error.log";
8
 
16 - 9
use strict;
10
use cPanelUserConfig;
11
use RollerCon;
12
use HTML::Tiny;
13
use CGI qw/param header start_html url uploadInfo/;
14
my $h = HTML::Tiny->new( mode => 'html' );
15
 
16
my $cookie_string = authenticate (5) || die;
17
my ($EML, $PWD, $LVL) = split /&/, $cookie_string;
18
my $ID = getUser ($EML);
19
my $RCAUTH_cookie = cookie (-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
20
 
21
my $tmpdir = "/tmp/";
22
 
23
 
24
print header (-cookie=>$RCAUTH_cookie),
25
			start_html (-title => "Bulk Upload vORC Data", -style => {'src' => "/style.css"} );
26
 
27
if (defined (param ("uploadedfile"))) {
28
	process_form ();
29
} else {
30
	display_upload_form ();
31
}
32
 
33
print end_html ();
34
 
35
use CGI qw(:standard escape escapeHTML);
36
 
37
sub display_upload_form  {
38
  print $h->div ("Upload a CSV File of department shifts...");
128 - 39
  print $h->div ("These should be the columns: (dept, role, type, date, location, start_time, end_time, doubletime, note, assignee_id)");
16 - 40
	print start_multipart_form (-action => url ()),
41
#		    "Upload File: ", br (),
42
      $h->input ({
43
        name => "uploadedfile",
44
        class => "inputfile",
45
        type => "file",
46
        id   => "file",
47
        size => 60
48
      }) . $h->label ({ for=>"file", class=>"top" }, $h->span ("Choose File...")),
49
        br (), br (),
50
		    submit (-name => "choice", -value => "Submit"), $h->input ({ type=>"button", value => "Cancel" , onClick=>"history.back(); return false;" }),
51
		    end_form ();
52
		    printJavascript ();
53
}
54
 
55
sub printJavascript {
56
    print<<JSCRIPT;
57
 
58
<SCRIPT language="JavaScript">
59
<!--
60
 
61
var inputs = document.querySelectorAll( '.inputfile' );
62
Array.prototype.forEach.call( inputs, function( input )
63
{
64
	var label	 = input.nextElementSibling,
65
		labelVal = label.innerHTML;
66
 
67
	input.addEventListener( 'change', function( e )
68
	{
69
		var fileName = e.target.value.split( '\\\\' ).pop();
70
		if( fileName )
71
			label.querySelector( 'span' ).innerHTML = fileName;
72
		else
73
			label.innerHTML = labelVal;
74
	});
75
});
76
 
77
//-->
78
</SCRIPT>
79
 
80
 
81
JSCRIPT
82
}
83
 
84
sub process_form  {
85
  use WebDB;
86
  my $dbh = WebDB::connect ();
87
 
88
	my $uploadedfile = param ("uploadedfile");
89
	my @errors = ();
90
  my $mime_type;
91
  my $serve_url;
92
 
93
  logit ($ID->{RCid}, "Bulk Uploaded shifts from file $uploadedfile");
94
 
95
  push (@errors, "Please specify a file") if $uploadedfile eq "";
96
  $mime_type = uploadInfo ($uploadedfile)->{'Content-Type'};
97
  if ($mime_type ne "text/csv") {
98
    push @errors, "Expecting a CSV file, but received a '$mime_type'.";
99
  }
100
  if (@errors)	 {
101
  	print p ("The following errors occurred:");
102
  	print ul (li (\@errors));
103
  	print p ("Please click your Browser's Back button to\n"
104
  				   . "return to the previous page and correct the problem.");
105
  	return;
106
  }	 # Form was okay;
107
 
108
  print $h->div ("Processing file...");
109
  my $fh = upload ('uploadedfile');
110
 
111
  my @columnlabels = split /,/, <$fh>;
112
  @columnlabels = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } @columnlabels;
113
 
114
  my $idtrue = 0;
115
  if ($columnlabels[0] eq "id") {
116
    $idtrue = 1;
117
    shift @columnlabels;
118
  }
119
 
128 - 120
  my @AcceptableColumns = qw(dept, role, type, date, location, start_time, end_time, doubletime, note, assignee_id);
16 - 121
 
122
 
123
  my $fields = join ", ", @columnlabels;
124
  my $values = join ", ", map { '?' } 0..$#columnlabels;
125
  my $sth = $dbh->prepare ("insert into shift ($fields) values ($values)");
126
 
127
  while (<$fh>) {
128
    chomp;
129
    my @R = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } split /,/;
130
    shift @R if $idtrue;
113 - 131
    $R[2] = lc $R[2] if $columnlabels[2] eq "type";
29 - 132
    push @R, "" if (scalar @R < scalar @columnlabels and eof);
16 - 133
    print "inserting: ", join (" ", @R), $h->br;
133 - 134
    $sth->execute (map { $_ eq '' ? undef : $_ } map { defined $_ ? $_ : undef } @R) or print $sth->errstr;
16 - 135
  }
136
  print "DONE!", $h->br, $h->br;
128 - 137
 
16 - 138
	$dbh->disconnect ();	 # Image was stored into database successfully.  Present confirmation
139
 
140
	# Display link to main page so user can upload another image
141
	print $h->hr, $h->a ({ href => url }, "[Upload another file]"), $h->a ({ href => "/schedule/" }, "[Home]");
142
}
143
 
144
 
145
sub error {
146
	my $msg = shift;
147
	print p (escapeHTML ("Error: $msg")), end_html ();
148
	exit (0);
149
}