Subversion Repositories ORC

Rev

Rev 22 | Details | Compare with Previous | Last modification | View Log | RSS feed

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