Subversion Repositories CoffeeCatalog

Rev

Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed

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