Subversion Repositories CoffeeCatalog

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 4
Line 1... Line 1...
1
#!/usr/bin/perl -w
1
#!/usr/bin/perl -w
Line 2... Line 2...
2
 
2
 
3
use strict;
3
use strict;
4
use CGI qw(:standard escapeHTML);
4
use CGI qw(:standard escape escapeHTML);
5
print header (),
5
print header (),
Line 6... Line 6...
6
			start_html (-title => "Form Element Demonstration", -bgcolor => "white"); 
6
			start_html (-title => "Form Element Demonstration", -bgcolor => "white"); 
7
 
7
 
8
if (defined (param ("name"))) {
8
if (defined (param ("roaster"))) {
9
	process_form ();
9
	process_form ();
10
} else {
10
} else {
Line 11... Line 11...
11
	display_upload_form ();
11
	display_upload_form ();
Line 12... Line 12...
12
}
12
}
13
 
13
 
14
print end_html ();
14
print end_html ();
15
 
15
 
-
 
16
 
-
 
17
sub display_upload_form  {
-
 
18
	print start_multipart_form (-action => url ()),
16
 
19
		    "Roaster: ",
-
 
20
		    textfield (-name => "roaster",
-
 
21
					 -value => "",
-
 
22
					 -override => 1,
-
 
23
					 -size => 30), br (),
17
sub display_upload_form  {
24
		    "URL: ",
18
	print start_multipart_form (-action => url ()),
25
		    textfield (-name => "url",
19
		    "Image file: ", br (),
26
					 -value => "",
20
		    filefield (-name => "image", -size => 60),
27
					 -override => 1,
21
		    br (),
28
					 -size => 30), br (),
22
		    "Descriptive name for image: ", br (),
29
		    "Location (City, State): ",
-
 
30
		    textfield (-name => "location",
-
 
31
					 -value => "",
-
 
32
					 -override => 1,
23
		    textfield (-name => "name",
33
					 -size => 60),
24
					 -value => "",
34
		    br (), br (),
25
					 -override => 1,
35
		    "Logo: ", br (),
Line 26... Line 36...
26
					 -size => 60),
36
		    filefield (-name => "logo", -size => 60),
-
 
37
		    br (),
-
 
38
		    submit (-name => "choice", -value => "Submit"),
27
		    br (), br (),
39
		    end_form ();
-
 
40
}
-
 
41
 
28
		    submit (-name => "choice", -value => "Submit"),
42
sub process_form  {
29
		    end_form ();
43
  use WebDB;
30
}
44
  
31
 
45
	my $roaster = param ("roaster");
32
sub process_form  {
46
	my $url = param ("url");
33
	my $name = param ("name");    # image name
47
	my $location = param ("location");
34
	my $image = param ("image");	# image file
48
	my $logo = param ("logo");
35
	my @errors = ();
49
	my @errors = ();
-
 
50
  my $dbh;
-
 
51
  my $mime_type;
36
  my $dbh;
52
  my ($full, $thumb);
37
  my $mime_type;
53
  my $serve_url;
-
 
54
  $roaster = WebDB::trim ($roaster);	        # trim extraneous whitespace from name
-
 
55
  $url = WebDB::trim ($url);	        # trim extraneous whitespace from name
38
  my ($full, $thumb);
56
  $location = WebDB::trim ($location);	        # trim extraneous whitespace from name
39
  my $serve_url;
57
  $logo = "" unless defined ($logo); # convert undef to empty string
40
  $image = "" unless defined ($image); # convert undef to empty string
58
  	 # check for required fields
41
  $name = WebDB::trim ($name);	        # trim extraneous whitespace from name
59
  push (@errors, "Please supply a Roaster name") if $roaster eq "";
42
  	 # check for required fields
60
  push (@errors, "Please supply the Roaster's location") if $location eq "";
43
  push (@errors, "Please supply an image name") if $name eq "";
61
  push (@errors, "Please supply a URL to the Roaster") if $url eq "";
44
  push (@errors, "Please specify an image file") if $image eq "";
62
  push (@errors, "Please specify an image file") if $logo eq "";
45
  if (@errors)	 {
63
  if (@errors)	 {
46
  	print p ("The following errors occurred:");
64
  	print p ("The following errors occurred:");
47
  	print ul (li (\@errors));
65
  	print ul (li (\@errors));
48
  	print p ("Please click your Browser's Back button to\n"
66
  	print p ("Please click your Browser's Back button to\n"
49
  				   . "return to the previous page and correct the problem.");
67
  				   . "return to the previous page and correct the problem.");
50
  	return;
68
  	return;
51
  }	 # Form was okay;  get image type and contents and create new record.
69
  }	 # Form was okay;  get image type and contents and create new record.
52
  # Use REPLACE to clobber any old image with the same name.
70
  # Use REPLACE to clobber any old image with the same name.
53
  $mime_type = uploadInfo ($image)->{'Content-Type'};
71
  $mime_type = uploadInfo ($logo)->{'Content-Type'};
54
  ($full, $thumb) = read_image_file ($image);
72
  ($full, $thumb) = read_image_file ($logo);
55
  $dbh = WebDB::connect ();
73
  $dbh = WebDB::connect ();
56
  $dbh->do (
74
  $dbh->do (
57
  		   "REPLACE INTO image
75
  		   "REPLACE INTO roasters
58
		    (name,image,thumbnail,mime_type)
76
		    (roaster,url,logo,thumbnail,location,note,mime_type)
59
		    VALUES(?,?,?,?)",
77
		    VALUES(?,?,?,?,?,?,?)",
60
			   undef,
78
			   undef,
61
			   $name, $full, $thumb, $mime_type);
79
			   $roaster, $url, $full, $thumb, $location, "", $mime_type);
62
	$dbh->disconnect ();	 # Image was stored into database successfully.  Present confirmation
80
	$dbh->disconnect ();	 # Image was stored into database successfully.  Present confirmation
63
		 # page that displays both the full size and thumbnail images.
81
		 # page that displays both the full size and thumbnail images.
64
	print p ("The image upload was successful.");
82
	print p ("Roaster successfully added.");
65
	# encode the name with escape() for URL, but with escapeHTML() otherwise
83
	# encode the name with escape() for URL, but with escapeHTML() otherwise
66
	$serve_url = sprintf ("serve_image.pl?name=%s", escape ($name));
84
	$serve_url = sprintf ("serve_image.pl?name=%s", escape ($roaster));
67
	$name = escapeHTML ($name);
85
	$roaster = escapeHTML ($roaster);
68
	$mime_type = escapeHTML ($mime_type);
86
	$mime_type = escapeHTML ($mime_type);
69
	print p ("Image name: $name"),
87
	print p ("Roaster: $roaster"),
70
		    p ("MIME type: $mime_type"),
88
		    p ("MIME type: $mime_type"),
71
		    p ("Full size image:"),
89
		    p ("Full size image:"),
Line 72... Line 90...
72
		    img ({-src => $serve_url, -alt => $name}), "\n",
90
		    img ({-src => $serve_url, -alt => $roaster}), "\n",