| 4 |
- |
1 |
#!/usr/bin/perl -w
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use HTML::Tiny;
|
|
|
5 |
use CGI qw(:standard escape escapeHTML);
|
|
|
6 |
# use CGI qw/param cookie header start_html url/;
|
|
|
7 |
|
|
|
8 |
my $h = HTML::Tiny->new( mode => 'html' );
|
|
|
9 |
|
|
|
10 |
my $pageTitle = "CC Manage Roaster";
|
|
|
11 |
my $homeURL = "/";
|
|
|
12 |
|
|
|
13 |
print header (),
|
|
|
14 |
start_html (-title => "Add / Update Roaster", -style => {'src' => "style.css"} );
|
|
|
15 |
|
|
|
16 |
print $h->div ({ class => "accent pageheader" }, [
|
|
|
17 |
$h->h1 ($pageTitle),
|
|
|
18 |
$h->div ({ class=>"sp0" }, [
|
|
|
19 |
$h->div ({ class=>"spLeft" }, [
|
|
|
20 |
]),
|
|
|
21 |
$h->div ({ class=>"spRight" }, [
|
|
|
22 |
$h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
|
|
|
23 |
]),
|
|
|
24 |
]),
|
|
|
25 |
]);
|
|
|
26 |
|
|
|
27 |
my $choice = defined param ("choice") ? param ("choice") : "";
|
|
|
28 |
my $actionbutton;
|
|
|
29 |
if ($choice eq "Save") {
|
|
|
30 |
process_form ();
|
|
|
31 |
} elsif (defined (param ("roaster"))) {
|
|
|
32 |
my $roaster = param ("roaster");
|
|
|
33 |
display_form ($roaster, $choice);
|
|
|
34 |
} else {
|
|
|
35 |
display_form ();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
print $h->close ("html");
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
sub display_form {
|
|
|
42 |
my $R = shift;
|
|
|
43 |
my $view = shift;
|
|
|
44 |
my $hiddenR = "";
|
|
|
45 |
|
|
|
46 |
my ($U, $L, $I, $logo);
|
|
|
47 |
if ($R) {
|
|
|
48 |
# we're updating an existing roaster. Get the current values out of the DB...
|
|
|
49 |
use WebDB;
|
|
|
50 |
my $dbh = WebDB::connect ();
|
|
|
51 |
($U, $L) = $dbh->selectrow_array (
|
|
|
52 |
"SELECT url, location, note FROM roasters WHERE roaster = ?",
|
|
|
53 |
undef, $R);
|
|
|
54 |
$dbh->disconnect ();
|
|
|
55 |
my $i = "serve_image.pl" . sprintf ("?name=%s", escape ($R));
|
|
|
56 |
$I = $h->img ({ src => "$i;thumbnail=1", alt => $R });
|
|
|
57 |
|
|
|
58 |
# did we find a record?
|
|
|
59 |
error ("Cannot find Roaster named $R") unless defined ($L);
|
|
|
60 |
|
|
|
61 |
if ($view eq "Update") {
|
|
|
62 |
print $h->p ("Updating Roaster $R...");
|
|
|
63 |
$R = $h->input ({
|
|
|
64 |
type => "Text",
|
|
|
65 |
name => "roaster",
|
|
|
66 |
value => "$R",
|
|
|
67 |
override => 1,
|
|
|
68 |
size => 30
|
|
|
69 |
});
|
|
|
70 |
$U = $h->input ({
|
|
|
71 |
type => "Text",
|
|
|
72 |
name => "url",
|
|
|
73 |
value => "$U",
|
|
|
74 |
override => 1,
|
|
|
75 |
size => 30
|
|
|
76 |
});
|
|
|
77 |
$L = $h->input ({
|
|
|
78 |
type => "Text",
|
|
|
79 |
name => "location",
|
|
|
80 |
value => "$L",
|
|
|
81 |
override => 1,
|
|
|
82 |
size => 30
|
|
|
83 |
});
|
|
|
84 |
$logo = $I.' '.$h->input ({
|
|
|
85 |
class => "inputfile",
|
|
|
86 |
type => "file",
|
|
|
87 |
name => "logo",
|
|
|
88 |
id => "file",
|
|
|
89 |
size => 60
|
|
|
90 |
}) . $h->label ({ for=>"file", class=>"top" }, $h->span ("Choose File..."));
|
|
|
91 |
$actionbutton = $h->input ({ type=>"submit", value => "Save", name=>"choice" });
|
|
|
92 |
$actionbutton .= $h->input ({ type=>"submit", value => "Cancel", name=>"choice" });
|
|
|
93 |
} else {
|
|
|
94 |
print $h->p ("Viewing Roaster $R...");
|
|
|
95 |
$logo = $I;
|
|
|
96 |
$hiddenR = $h->input ({ type=>"hidden", name=>"roaster", value=> "$R" });
|
|
|
97 |
$actionbutton = $h->input ({ type=>"submit", value => "Update", name=>"choice" });
|
|
|
98 |
}
|
|
|
99 |
} else {
|
|
|
100 |
print $h->p ("Adding a new Roaster...");
|
|
|
101 |
|
|
|
102 |
print ;
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
$R = $h->input ({
|
|
|
107 |
type => "Text",
|
|
|
108 |
name => "roaster",
|
|
|
109 |
value => "",
|
|
|
110 |
override => 1,
|
|
|
111 |
size => 30
|
|
|
112 |
});
|
|
|
113 |
$U = $h->input ({
|
|
|
114 |
type => "Text",
|
|
|
115 |
name => "url",
|
|
|
116 |
value => "",
|
|
|
117 |
override => 1,
|
|
|
118 |
size => 30
|
|
|
119 |
});
|
|
|
120 |
$L = $h->input ({
|
|
|
121 |
type => "Text",
|
|
|
122 |
name => "location",
|
|
|
123 |
value => "",
|
|
|
124 |
override => 1,
|
|
|
125 |
size => 30
|
|
|
126 |
});
|
|
|
127 |
$logo = $h->input ({
|
|
|
128 |
class => "inputfile",
|
|
|
129 |
type => "file",
|
|
|
130 |
name => "logo",
|
|
|
131 |
id => "file",
|
|
|
132 |
size => 60
|
|
|
133 |
}) . $h->label ({ for=>"file", class=>"top" }, $h->span ("Choose File..."));
|
|
|
134 |
$actionbutton = $h->input ({ type=>"submit", value => "Save", name=>"choice" });
|
|
|
135 |
# $actionbutton .= $h->input ({ type=>"submit", value => "Cancel", name=>"choice" }); # FIX THIS <---
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
print $h->open ("form", { action => url (), method=>"POST", enctype=>"multipart/form-data" });
|
|
|
140 |
print $h->div ({ class=>"rTable" }, [
|
|
|
141 |
$h->div ({ class=>"rTableRow" }, [
|
|
|
142 |
$h->div ({ class=>"rTableCell" }, "Roaster: "),
|
|
|
143 |
$h->div ({ class=>"rTableCell" }, [ $hiddenR, $R ])
|
|
|
144 |
]),
|
|
|
145 |
$h->div ({ class=>"rTableRow" }, [
|
|
|
146 |
$h->div ({ class=>"rTableCell" }, "URL: "),
|
|
|
147 |
$h->div ({ class=>"rTableCell" }, $U)
|
|
|
148 |
]),
|
|
|
149 |
$h->div ({ class=>"rTableRow" }, [
|
|
|
150 |
$h->div ({ class=>"rTableCell" }, "Location (City, State): "),
|
|
|
151 |
$h->div ({ class=>"rTableCell" }, $L)
|
|
|
152 |
]),
|
|
|
153 |
$h->div ({ class=>"rTableRow" }, [
|
|
|
154 |
$h->div ({ class=>"rTableCell top" }, "Logo:"),
|
|
|
155 |
$h->div ({ class=>"rTableCell" }, $logo)
|
|
|
156 |
]),
|
|
|
157 |
]);
|
|
|
158 |
|
|
|
159 |
# print $I;
|
|
|
160 |
# print $h->input ({ type=>"hidden", name => "choice", value => "Save"});
|
|
|
161 |
print $actionbutton;
|
|
|
162 |
print $h->close ("form");
|
|
|
163 |
|
|
|
164 |
printJavascript ();
|
|
|
165 |
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
sub process_form {
|
|
|
169 |
use WebDB;
|
|
|
170 |
|
|
|
171 |
my $roaster = param ("roaster");
|
|
|
172 |
my $url = param ("url");
|
|
|
173 |
my $location = param ("location");
|
|
|
174 |
my $logo = param ("logo");
|
|
|
175 |
my @errors = ();
|
|
|
176 |
my $dbh;
|
|
|
177 |
my $mime_type;
|
|
|
178 |
my ($full, $thumb);
|
|
|
179 |
my $serve_url;
|
|
|
180 |
$roaster = WebDB::trim ($roaster); # trim extraneous whitespace from name
|
|
|
181 |
$url = WebDB::trim ($url); # trim extraneous whitespace from url
|
|
|
182 |
$location = WebDB::trim ($location); # trim extraneous whitespace from location
|
|
|
183 |
$logo = "" unless defined ($logo); # convert undef to empty string
|
|
|
184 |
# check for required fields
|
|
|
185 |
push (@errors, "Please supply a Roaster name") if $roaster eq "";
|
|
|
186 |
push (@errors, "Please supply the Roaster's location") if $location eq "";
|
|
|
187 |
push (@errors, "Please supply a URL to the Roaster") if $url eq "";
|
|
|
188 |
# push (@errors, "Please specify an image file") if $logo eq "";
|
|
|
189 |
if (@errors) {
|
|
|
190 |
print $h->div ({ class=>"error"}, [
|
|
|
191 |
$h->p ("The following errors occurred:"),
|
|
|
192 |
$h->ul ($h->li (@errors)),
|
|
|
193 |
$h->p ("Please click your Browser's Back button to\n"
|
|
|
194 |
. "return to the previous page and correct the problem.")
|
|
|
195 |
]);
|
|
|
196 |
return;
|
|
|
197 |
} # Form was okay; get image type and contents and create new record.
|
|
|
198 |
# Use REPLACE to clobber any old image with the same name.
|
|
|
199 |
$dbh = WebDB::connect ();
|
|
|
200 |
if ($logo) {
|
|
|
201 |
$mime_type = uploadInfo ($logo)->{'Content-Type'};
|
|
|
202 |
($full, $thumb) = read_image_file ($logo);
|
|
|
203 |
$dbh->do (
|
|
|
204 |
"REPLACE INTO roasters
|
|
|
205 |
(roaster,url,logo,thumbnail,location,note,mime_type)
|
|
|
206 |
VALUES(?,?,?,?,?,?,?)",
|
|
|
207 |
undef,
|
|
|
208 |
$roaster, $url, $full, $thumb, $location, "", $mime_type);
|
|
|
209 |
} else {
|
|
|
210 |
$dbh->do (
|
|
|
211 |
"INSERT INTO roasters
|
|
|
212 |
(roaster,url,location,note)
|
|
|
213 |
VALUES(?,?,?,?)
|
|
|
214 |
ON DUPLICATE KEY UPDATE url = ?, location = ?",
|
|
|
215 |
undef,
|
|
|
216 |
$roaster, $url, $location, "", $url, $location);
|
|
|
217 |
}
|
|
|
218 |
$dbh->disconnect (); # Image was stored into database successfully. Present confirmation
|
|
|
219 |
# page that displays both the full size and thumbnail images.
|
|
|
220 |
print $h->p ({ class=>"success" }, "Roaster successfully saved.");
|
|
|
221 |
|
|
|
222 |
display_form ($roaster);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
use Image::Magick;
|
|
|
226 |
sub read_image_file {
|
|
|
227 |
my $fh = shift; # filename/file handle
|
|
|
228 |
my $img = new Image::Magick;
|
|
|
229 |
my ($full, $thumb);
|
|
|
230 |
my $err;
|
|
|
231 |
# read full-size image directly from upload file
|
|
|
232 |
(read ($fh, $full, (stat ($fh))[7]) == (stat ($fh))[7])
|
|
|
233 |
or error ("Can't read image file: $!");
|
|
|
234 |
# produce thumbnail from full-size image
|
|
|
235 |
$err = $img->BlobToImage ($full);
|
|
|
236 |
error ("Can't convert image data: $err") if $err;
|
|
|
237 |
$err = $img->Scale (geometry => "64x64");
|
|
|
238 |
error ("Can't scale image file: $err") if $err;
|
|
|
239 |
$thumb = $img->ImageToBlob ();
|
|
|
240 |
return ($full, $thumb);
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
sub error {
|
|
|
244 |
my $msg = shift;
|
|
|
245 |
# print $h->p ({ class=>"error" }, $h->url_encode ("Error: $msg"));
|
|
|
246 |
print $h->p ({ class=>"error" }, "Error: $msg");
|
|
|
247 |
print $h->close("html");
|
|
|
248 |
exit (0);
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
sub printJavascript {
|
|
|
252 |
print<<JSCRIPT;
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
<SCRIPT language="JavaScript">
|
|
|
256 |
<!--
|
|
|
257 |
|
|
|
258 |
var inputs = document.querySelectorAll( '.inputfile' );
|
|
|
259 |
Array.prototype.forEach.call( inputs, function( input )
|
|
|
260 |
{
|
|
|
261 |
var label = input.nextElementSibling,
|
|
|
262 |
labelVal = label.innerHTML;
|
|
|
263 |
|
|
|
264 |
input.addEventListener( 'change', function( e )
|
|
|
265 |
{
|
|
|
266 |
var fileName = e.target.value.split( '\\\\' ).pop();
|
|
|
267 |
console.log("setting filename");
|
|
|
268 |
if( fileName )
|
|
|
269 |
label.querySelector( 'span' ).innerHTML = fileName;
|
|
|
270 |
else
|
|
|
271 |
label.innerHTML = labelVal;
|
|
|
272 |
});
|
|
|
273 |
});
|
|
|
274 |
|
|
|
275 |
//-->
|
|
|
276 |
</SCRIPT>
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
JSCRIPT
|
|
|
280 |
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
|