Subversion Repositories CoffeeCatalog

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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