Subversion Repositories CoffeeCatalog

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9 - 1
#!/usr/bin/perl
2
 
3
#if ($ENV{SHELL}) { die "This script shouldn't be executed from the command line!\n"; }
4
 
5
use CGI qw/param cookie header start_html url/;
6
use HTML::Tiny;
7
use scanFunctions;
8
our $h = HTML::Tiny->new( mode => 'html' );
9
 
10
my $pageTitle = "CC Coffees";
11
my $prefscookie = "CCCoffeesPrefs";
12
our $DBTABLE = 'v_coffees';
13
my %COLUMNS = (
14
	coffee      =>  [qw(Coffee               05   text   static)],
15
	roaster     =>  [qw(Roaster              10   text   static)],
16
	region      =>  [qw(Region               15   text   default)],
17
	source      =>  [qw(Source               20   select)],
18
	url         =>  [qw(URL                  25   text   default)],
19
	rating      =>  [qw(Rating               30   number)],
20
	location    =>  [qw(Location             35   text)],
21
	note        =>  [qw(Notes                40   text)],
22
	date_added  =>  [qw(Date Added      45   text)]
23
  );
24
my $stylesheet = "style.css";
25
my $homeURL = '/';
26
my @pagelimitoptions = (5, 10, 25, "All");
27
 
28
 
29
# If we need to modify line item values, create a subroutine named "modify_$columnname"
30
#    It will recieve a hashref to the object lineitem
31
 
32
sub modify_coffee {
33
  # Turn it into a link to the manage_roaster.pl page
34
  my $lineItem = shift;
35
  my $rster = $h->url_encode ($lineItem->{'roaster'});
36
  my $cfee  = $h->url_encode ($lineItem->{'coffee'});
37
  return $h->a ({ href=>"/manage_coffee.pl?coffee=$cfee&roaster=$rster" }, $lineItem->{'coffee'});
38
}
39
 
40
sub modify_roaster {
41
  # Turn it into a link to the manage_roaster.pl page
42
  my $lineItem = shift;
43
  my $rster = $h->url_encode ($lineItem->{'roaster'});
44
  return $h->a ({ href=>"/manage_roaster.pl?roaster=$rster" }, $lineItem->{'roaster'});
45
}
46
 
47
sub modify_url {
48
  # Make the URL an actual link
49
  my $lineItem = shift;
50
  return $h->a ({ href=>$lineItem->{'url'}, target=>"_blank" }, $lineItem->{'url'});
51
}
52
 
53
my $addNewButton = $h->div ({ style=>"float:right;" }, $h->input ({ type=>"button", value=>"Add New", style=>"margin:0;", onClick=>"window.location.href='manage_coffee.pl'" }));
54
 
55
 
56
 
57
 
58
# Ideally, nothing below this comment needs to change
59
#-------------------------------------------------------------------------------
60
 
61
 
62
our %NAME              = map  { $_ => $COLUMNS{$_}->[0] } keys %COLUMNS;
63
our %colOrderHash      = map  { $_ => $COLUMNS{$_}->[1] } keys %COLUMNS;
64
our %colFilterTypeHash = map  { $_ => $COLUMNS{$_}->[2] } keys %COLUMNS;
65
our @staticFields      = sort byfield grep { $COLUMNS{$_}->[3] eq 'static' } keys %COLUMNS;
66
our @defaultFields     = grep { defined $COLUMNS{$_}->[3] } keys %COLUMNS;
67
#our @defaultFields     = grep { $COLUMNS{$_}->[3] eq 'default' or inArray ($_, \@staticFields) } keys %COLUMNS;
68
 
69
our @allFields = sort byfield keys %NAME;
70
our @displayFields = ();
71
our @hideFields = ();
72
my $QUERY_STRING;
73
 
74
my $pagelimit = param ("limit") // $pagelimitoptions[$#pagelimitoptions];
75
my $curpage = param (page) // 1;
76
 
77
our %FORM;
78
foreach (param()) {
79
	$FORM{$_} = param($_);				# Retrieve all of the FORM data submitted
80
 
81
	if ((/^filter/) and ($FORM{$_} ne '')) {	# Build a set of filters to apply
82
		my ($filter,$field) = split /-/, $_;
83
		$FILTER->{$field} = $FORM{$_};
84
	}	elsif ($FORM{$_} eq "true")			# Compile list of fields to display
85
		{ push @displayFields, $_; }
86
}
87
 
88
 
89
if (exists $FORM{autoload})	{			# If the FORM was submitted (i.e. the page is being redisplayed),
90
							                    #  	build the data for the cookie that remembers the page setup
91
	my $disFields = join ":", @displayFields;
92
	my $fils = join ":", map { "$_=$FILTER->{$_}" } keys %{$FILTER};
93
 
94
	$QUERY_STRING = $disFields.'&'.$fils.'&'.$FORM{sortby}.'&'.$FORM{autoload};
95
}
96
 
97
 
98
if (!(exists $FORM{autoload}))	{			# No FORM was submitted...
99
	if (my $prefs = cookie ($prefscookie) and !defined param ("ignoreCookie"))	{ # Check for cookies from previous visits.
100
		my ($disF, $filts, $sb, $al) = split /&/,$prefs;
101
		@displayFields = split /:/,$disF;
102
 
103
		foreach $pair (split /:/, $filts)	{
104
			my ($key, $value) = split /=/, $pair;
105
			$FORM{"filter-$key"} = $value;
106
			$FILTER->{$key} = $value;
107
		}
108
 
109
		$FORM{sortby} = $sb;
110
		$FORM{autoload} = $al;
111
		$QUERY_STRING = $prefs;
112
	}	else { @displayFields = @defaultFields; } # Otherwise suppply a default list of columns.
113
}
114
 
115
# let's just make sure the columns are in the right order (and there aren't any missing)
116
@displayFields = sort byfield uniq @displayFields, @staticFields;
117
 
118
# If the field isn't in the displayFields list,	then add it to the hideFields list
119
@hideFields = grep { notInArray ($_, \@displayFields) } @allFields;
120
 
121
# Process any filters provided in the form to pass to the database
122
my @whereClause = map { filter ($_, $FILTER->{$_}) } grep { defined $FILTER->{$_} } @displayFields;
123
 
124
							#  Given the fields to display and the where conditions,
125
							#	  "getData" will return a reference to an array of
126
							#	  hash references of the results.
127
my ($data, $datacount) = getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit);
128
my @ProductList = @{ $data };
129
 
130
#my @ProductList = @{ getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit) };
131
my $x = scalar @ProductList; # How many results were returned?
132
 
133
# Set some cookie stuff...
134
my $path = `dirname $ENV{REQUEST_URI}`; chomp $path; $path .= '/' unless $path eq "/";
135
my $queryCookie = cookie(-NAME=>$prefscookie,
136
			-VALUE=>"$QUERY_STRING",
137
			-PATH=>"$path",
138
			-EXPIRES=>'+365d');
139
 
140
print header (-cookie=> [ $queryCookie ] );
141
 
142
# 	print "<!-- FORM \n\n";				# Debug code to dump the FORM to a html comment
143
#	print "I'm catching updates!!!\n\n";
144
#	foreach $key (sort (keys %FORM))		#	Must be done after the header is written!
145
# 		{ print "\t$key:  $FORM{$key}\n"; }
146
# 	print "--> \n\n";
147
#
148
#
149
# 	print "<!-- ENV \n\n";				# Debug code to dump the ENV to a html comment
150
# 	foreach $key (sort (keys %ENV))			#	Must be done after the header is written!
151
# 		{ print "\t$key:  $ENV{$key}\n"; }
152
# 	print "--> \n\n";
153
#
154
# 	print "\n\n\n\n<!-- $QUERY_STRING --> \n\n\n\n";
155
 
156
 
157
#------------------
158
 
159
# Toggle the autoload fields within the table elements
160
our ($onClick, $onChange);   # (also used in scanFunctions)
161
my ($radiobutton, $refreshbutton, $sortby);
162
if ($FORM{autoload}) {
163
	$onClick = "onClick='submit();'";
164
	$onChange = "onChange='submit();'";
165
  $radiobutton = $h->div ({ class=>'autoload' },
166
    ["Autoload Changes: ",
167
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>1, onClick=>'submit();', checked=>[] }), "On ",
168
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>0, onClick=>'submit();' }), "Off ",
169
    ]);
170
  $sortby = $h->select ({name=>sortby, onChange=>'submit();' }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
171
} else {
172
  $onClick = "";
173
	$onChange = "";
174
  $radiobutton = $h->div ({ class=>'autoload' },
175
    ["Autoload Changes: ",
176
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>1, onClick=>'submit();' }), "On ",
177
    $h->input ({ type=>radio, name=>'autoload', class=>'accent', value=>0, onClick=>'submit();', checked=>[] }), "Off ",
178
    ]);
179
  $refreshbutton = $h->input ({ type=>"button", value=>"Apply Changes", onClick=>"submit(); return false;" });
180
  $sortby = $h->select ({name=>sortby }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
181
}
182
 
183
 
184
 
185
 
186
print start_html (-title => $pageTitle, -style => {'src' => $stylesheet} );
187
 
188
# Print the header
189
 
190
print $h->open ('form', { action=>url, method=>'POST', name=>'Req' });
191
print $h->div ({ class => "accent pageheader" }, [
192
  $h->h1 ($pageTitle),
193
  $h->div ({ class=>"sp0" }, [
194
    $h->div ({ class=>"spLeft" }, [
195
      $radiobutton
196
    ]),
197
    $h->div ({ class=>"spRight" }, [
198
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
199
      $refreshbutton
200
    ]),
201
  ]),
202
]);
203
 
204
# Print the Hidden fields' check boxes (if there are any)
205
 
206
my $c = 1;
207
my @hiddencheckboxes;
208
my @hiddenrows;
209
foreach $field (sort { $NAME{$a} cmp $NAME{$b}; } @hideFields) {
210
  if ($FORM{autoload}) {
211
    push @hiddencheckboxes, $h->div ({ class=>'rTableCell quarters nowrap', onClick=>"Req.$field.click();" }, [ $h->input ({ type=>'checkbox', class=>'accent', name=>$field, value=>'true', onClick=>"event.stopPropagation(); submit();" }), $NAME{$field} ]);
212
  } else {
213
    push @hiddencheckboxes, $h->div ({ class=>'rTableCell quarters nowrap', onClick=>"Req.$field.checked=!Req.$field.checked;" }, [ $h->input ({ type=>'checkbox', class=>'accent', name=>$field, value=>'true', onClick=>"event.stopPropagation();" }), $NAME{$field} ]);
214
  }
215
  if ($c++ % 4 == 0) {
216
    push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]);
217
    @hiddencheckboxes = [];
218
  }
219
}
220
push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]) unless --$c % 4 == 0;
221
 
222
 
223
if (scalar @hideFields) {
224
  my @topleft;
225
  push @topleft, $h->div ({ class=>"nowrap" }, "Hidden Columns:");
226
  push @topleft, $h->div ({ class=>'rTable' }, [ @hiddenrows ]);
227
 
228
  print $h->div ({ class=>"sp0" }, [
229
    $h->div ({ class=>"spLeft"  }, [ @topleft ]),
230
    $h->div ({ class=>"spRight" }, [
231
    ])
232
  ]);
233
}
234
 
235
# Print the main table...............................................
236
 
237
print $h->open ('div', { class=>'rTable' });
238
 
239
my @tmptitlerow;
240
foreach $f (@displayFields)	{  # Print the Column headings
241
  if (inArray ($f, \@staticFields)) {
242
    push @tmptitlerow, $h->div ({ class=>'rTableHead' }, [ $h->input ({ type=>"hidden", name=>$f, value=>"true" }), $NAME{$f}, $addNewButton ]);
243
    $addNewButton = "";
244
  } else {
245
    if ($FORM{autoload}) {
246
      push @tmptitlerow, $h->div ({ class=>'rTableHead', onClick=>"Req.$f.click();" }, [ $h->input ({ type=>"checkbox", class=>"accent", name=>$f, value=>"true", checked=>[], onClick=>'event.stopPropagation(); submit();' }), $NAME{$f} ]);
247
    } else {
248
      push @tmptitlerow, $h->div ({ class=>'rTableHead', onClick=>"Req.$f.checked=!Req.$f.checked;" }, [ $h->input ({ type=>"checkbox", class=>"accent", name=>$f, value=>"true", checked=>[], onClick=>"event.stopPropagation();" }), $NAME{$f} ]);
249
    }
250
  }
251
}
252
 
253
my @tmpfilters;
254
foreach $f (@displayFields) {  # and now the filter boxes
255
  push @tmpfilters, $h->div ({ class=>'rTableCell filters' }, filter ($f) )
256
}
257
 
258
print $h->div ({ class=>'rTableHeading' }, [ @tmptitlerow ], [ @tmpfilters ], $h->div ({ class=>"rTableCell" }));
259
 
260
foreach $t (@ProductList)	{		# Unt now we print the things!
261
  my @tmprow;
262
	foreach $f (@displayFields)
263
		{
264
		  # Look to see if there's a function named modify_<fieldname>() defined for this field
265
		  if (exists &{"modify_".$f}) {
266
		    $t->{$f} = &{"modify_".$f} ($t);
267
		  }
268
 
269
		  push @tmprow, $h->div ({ class=>'rTableCell' }, $t->{$f});
270
		}
271
  print $h->div ({ class=>'rTableRow shaded' }, [ @tmprow ]);
272
}
273
 
274
print $h->close ('div');
275
 
276
# close things out................................................
277
 
278
my $pages = $pagelimit eq "All" ? 1 : int( $datacount / $pagelimit + 0.99 );
279
if ($curpage > $pages) { $curpage = $pages; }
280
 
281
 
282
print $h->br; # print $h->br;
283
print $h->div ({ class=>"sp0" }, [
284
    $h->div ({ class=>"spLeft" }, [
285
      $h->div ({ class=>"footer" }, [
286
        "To bookmark, save, or send this exact view, use the ",
287
        $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
288
        $h->br,
289
        "If this page is displaying oddly, ",
290
        $h->a ({ href=>url ()."?ignoreCookie=1" }, "[Reset Your View]"),
291
        $h->br,
292
        "This page was displayed on $now",
293
        $h->br,
294
        "Please direct questions, problems, and concerns to noone\@gmail.com"
295
      ])
296
    ]),
297
    $h->div ({ class=>"spRight" }, [
298
      $h->h5 ([
299
               "$x of $datacount Record". ($x == 1 ? "" : "s") ." Displayed", $h->br,
300
               "Sorted by ", $sortby, $h->br,
301
               "Displaying ", $h->select ({ name=>"limit", onChange=>"page.value = 1; submit();" }, [ map { $pagelimit == $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @pagelimitoptions ]),
302
               ( $pages > 1 ? " Per Page: ". ( join " ", map { $_ == $curpage ? "<B>$_</b>" : $h->a ({ onClick=>qq{Req.page.value=$_; Req.submit();} }, "[$_]") } (1 .. $pages) ) : "" ), $h->br,
303
               $h->input ({ type=>"hidden", name=>"page", value=>$curpage })
304
      ])
305
    ]),
306
]);
307
 
308
#print $h->br; # print $h->br;
309
#print $h->h5 ("$x Record(s) Displayed");
310
#print $h->div ({ class=>"footer" }, [
311
#  "To bookmark, save, or send this exact view, use the ",
312
#  $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
313
#  $h->br,
314
#  "This page was displayed on $now",
315
#  $h->br,
316
#  "Please direct questions, problems, and concerns to noone\@gmail.com"
317
#]);
318
 
319
 
320
print $h->close('form');
321
print $h->close('html');