Subversion Repositories CoffeeCatalog

Rev

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

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