Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/perl -wuse strict;use HTML::Tiny;use CGI qw(:standard escape escapeHTML);# use CGI qw/param cookie header start_html url/;my $h = HTML::Tiny->new( mode => 'html' );my $pageTitle = "CC Manage Roaster";my $homeURL = "/";print header (),start_html (-title => "Add / Update Roaster", -style => {'src' => "style.css"} );print $h->div ({ class => "accent pageheader" }, [$h->h1 ($pageTitle),$h->div ({ class=>"sp0" }, [$h->div ({ class=>"spLeft" }, []),$h->div ({ class=>"spRight" }, [$h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),]),]),]);my $choice = defined param ("choice") ? param ("choice") : "";my $actionbutton;if ($choice eq "Save") {process_form ();} elsif (defined (param ("roaster"))) {my $roaster = param ("roaster");display_form ($roaster, $choice);} else {display_form ();}print $h->close ("html");sub display_form {my $R = shift;my $view = shift;my $hiddenR = "";my ($U, $L, $I, $logo);if ($R) {# we're updating an existing roaster. Get the current values out of the DB...use WebDB;my $dbh = WebDB::connect ();($U, $L) = $dbh->selectrow_array ("SELECT url, location, note FROM roasters WHERE roaster = ?",undef, $R);$dbh->disconnect ();my $i = "serve_image.pl" . sprintf ("?name=%s", escape ($R));$I = $h->img ({ src => "$i;thumbnail=1", alt => $R });# did we find a record?error ("Cannot find Roaster named $R") unless defined ($L);if ($view eq "Update") {print $h->p ("Updating Roaster $R...");$R = $h->input ({type => "Text",name => "roaster",value => "$R",override => 1,size => 30});$U = $h->input ({type => "Text",name => "url",value => "$U",override => 1,size => 30});$L = $h->input ({type => "Text",name => "location",value => "$L",override => 1,size => 30});$logo = $I.' '.$h->input ({class => "inputfile",type => "file",name => "logo",id => "file",size => 60}) . $h->label ({ for=>"file", class=>"top" }, $h->span ("Choose File..."));$actionbutton = $h->input ({ type=>"submit", value => "Save", name=>"choice" });$actionbutton .= $h->input ({ type=>"submit", value => "Cancel", name=>"choice" });} else {print $h->p ("Viewing Roaster $R...");$logo = $I;$hiddenR = $h->input ({ type=>"hidden", name=>"roaster", value=> "$R" });$actionbutton = $h->input ({ type=>"submit", value => "Update", name=>"choice" });}} else {print $h->p ("Adding a new Roaster...");print ;$R = $h->input ({type => "Text",name => "roaster",value => "",override => 1,size => 30});$U = $h->input ({type => "Text",name => "url",value => "",override => 1,size => 30});$L = $h->input ({type => "Text",name => "location",value => "",override => 1,size => 30});$logo = $h->input ({class => "inputfile",type => "file",name => "logo",id => "file",size => 60}) . $h->label ({ for=>"file", class=>"top" }, $h->span ("Choose File..."));$actionbutton = $h->input ({ type=>"submit", value => "Save", name=>"choice" });# $actionbutton .= $h->input ({ type=>"submit", value => "Cancel", name=>"choice" }); # FIX THIS <---}print $h->open ("form", { action => url (), method=>"POST", enctype=>"multipart/form-data" });print $h->div ({ class=>"rTable" }, [$h->div ({ class=>"rTableRow" }, [$h->div ({ class=>"rTableCell" }, "Roaster: "),$h->div ({ class=>"rTableCell" }, [ $hiddenR, $R ])]),$h->div ({ class=>"rTableRow" }, [$h->div ({ class=>"rTableCell" }, "URL: "),$h->div ({ class=>"rTableCell" }, $U)]),$h->div ({ class=>"rTableRow" }, [$h->div ({ class=>"rTableCell" }, "Location (City, State): "),$h->div ({ class=>"rTableCell" }, $L)]),$h->div ({ class=>"rTableRow" }, [$h->div ({ class=>"rTableCell top" }, "Logo:"),$h->div ({ class=>"rTableCell" }, $logo)]),]);# print $I;# print $h->input ({ type=>"hidden", name => "choice", value => "Save"});print $actionbutton;print $h->close ("form");printJavascript ();}sub process_form {use WebDB;my $roaster = param ("roaster");my $url = param ("url");my $location = param ("location");my $logo = param ("logo");my @errors = ();my $dbh;my $mime_type;my ($full, $thumb);my $serve_url;$roaster = WebDB::trim ($roaster); # trim extraneous whitespace from name$url = WebDB::trim ($url); # trim extraneous whitespace from url$location = WebDB::trim ($location); # trim extraneous whitespace from location$logo = "" unless defined ($logo); # convert undef to empty string# check for required fieldspush (@errors, "Please supply a Roaster name") if $roaster eq "";push (@errors, "Please supply the Roaster's location") if $location eq "";push (@errors, "Please supply a URL to the Roaster") if $url eq "";# push (@errors, "Please specify an image file") if $logo eq "";if (@errors) {print $h->div ({ class=>"error"}, [$h->p ("The following errors occurred:"),$h->ul ($h->li (@errors)),$h->p ("Please click your Browser's Back button to\n". "return to the previous page and correct the problem.")]);return;} # Form was okay; get image type and contents and create new record.# Use REPLACE to clobber any old image with the same name.$dbh = WebDB::connect ();if ($logo) {$mime_type = uploadInfo ($logo)->{'Content-Type'};($full, $thumb) = read_image_file ($logo);$dbh->do ("REPLACE INTO roasters(roaster,url,logo,thumbnail,location,note,mime_type)VALUES(?,?,?,?,?,?,?)",undef,$roaster, $url, $full, $thumb, $location, "", $mime_type);} else {$dbh->do ("INSERT INTO roasters(roaster,url,location,note)VALUES(?,?,?,?)ON DUPLICATE KEY UPDATE url = ?, location = ?",undef,$roaster, $url, $location, "", $url, $location);}$dbh->disconnect (); # Image was stored into database successfully. Present confirmation# page that displays both the full size and thumbnail images.print $h->p ({ class=>"success" }, "Roaster successfully saved.");display_form ($roaster);}use Image::Magick;sub read_image_file {my $fh = shift; # filename/file handlemy $img = new Image::Magick;my ($full, $thumb);my $err;# read full-size image directly from upload file(read ($fh, $full, (stat ($fh))[7]) == (stat ($fh))[7])or error ("Can't read image file: $!");# produce thumbnail from full-size image$err = $img->BlobToImage ($full);error ("Can't convert image data: $err") if $err;$err = $img->Scale (geometry => "64x64");error ("Can't scale image file: $err") if $err;$thumb = $img->ImageToBlob ();return ($full, $thumb);}sub error {my $msg = shift;# print $h->p ({ class=>"error" }, $h->url_encode ("Error: $msg"));print $h->p ({ class=>"error" }, "Error: $msg");print $h->close("html");exit (0);}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();console.log("setting filename");if( fileName )label.querySelector( 'span' ).innerHTML = fileName;elselabel.innerHTML = labelVal;});});//--></SCRIPT>JSCRIPT}