Subversion Repositories VORC

Rev

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