Subversion Repositories VORC

Rev

Rev 137 | Rev 196 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
32 - 1
#!/usr/bin/perl
2
 
56 bgadell 3
# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)
4
my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";
5
close STDERR;
6
open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";
7
#warn "Redirecting errors to ${error_log_path}vorc_error.log";
8
 
32 - 9
#if ($ENV{SHELL}) { die "This script shouldn't be executed from the command line!\n"; }
10
 
11
#use strict;
12
use cPanelUserConfig;
13
use CGI qw/param cookie header start_html url/;
14
use HTML::Tiny;
15
use tableViewer;
16
use RollerCon;
17
our $h = HTML::Tiny->new( mode => 'html' );
18
 
19
my $cookie_string = authenticate (3) || die;
20
our ($EML, $PWD, $LVL) = split /&/, $cookie_string;
21
my $user = getUser ($EML);
22
$user->{department} = convertDepartments ($user->{department});
56 bgadell 23
my $username = $h->a ({ href=>"/schedule/view_user.pl?submit=View&RCid=$user->{RCid}" }, $user->{derby_name});
32 - 24
my $RCid = $user->{RCid};
25
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
137 - 26
my $YEAR = 1900 + (localtime)[5];
32 - 27
 
28
 
29
my $pageTitle = "Volunteer Hours Summary";
30
my $prefscookie = "volhours";
31
our $DBTABLE = 'v_volhours';
32
my %COLUMNS = (
33
# colname   =>  [qw(DisplayName       N    type     status)],   status ->  static | default | <blank>
34
	derby_name  => [qw(DerbyName    25    select      static )],
76 bgadell 35
	full_name   => [qw(FullName    27     select       )],
32 - 36
	dept        => [qw(Department   30    select      static )],
76 bgadell 37
	hours       => [qw(Hours         40    number      static )]
32 - 38
);
39
my $stylesheet = "/style.css";
40
my $homeURL = '/schedule/';
41
my @pagelimitoptions = ("All", 5, 10, 25);
42
 
43
# Set any custom "where" DB filters here...
44
my @whereClause;
45
if ($LVL < 5) {
46
  my $string = "dept in (".join ",", map { '"'.$_.'"' } grep { $user->{department}->{$_} >= 3 } keys %{$user->{department}};
47
  $string .= ")";
48
  push @whereClause, $string;
49
}
50
push @whereClause, "dept != 'PER'";
51
 
52
# If we need to modify line item values, create a subroutine named "modify_$columnname"
53
#    It will receive a hashref to the object lineitem
54
 
55
sub modify_derby_name {
56
  my $li = shift;
56 bgadell 57
  return $h->a ({ href=>"view_user.pl?RCid=".getRCid ($li->{derby_name}) }, $li->{derby_name});
32 - 58
}
59
 
60
 
61
my $DEPTS = getDepartments ();
62
sub modify_dept {
63
  my $hr = shift;
64
  return $DEPTS->{$hr->{dept}};
65
}
66
 
67
sub filter_dept {
68
  my $colName = shift;
69
	my $filter = shift;
70
 
71
	if (defined $filter)	{
72
		if ($filter eq "-blank-") {
73
			return "($colName = '' or isNull($colName) = 1)";
74
		}
75
		return "$colName = \"$filter\"";
76
	}	else {
77
		my $thing = "filter-${colName}";
78
    my $categories = join "", map { $FORM{$thing} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $DEPTS->{$_}) : $h->option ({ value=>$_ }, $DEPTS->{$_}) } grep { $LVL > 4 or $user->{department}->{$_} >= 3 } sort keys %{$DEPTS};
79
		my $Options = "<OPTION></OPTION>".$categories;
80
 
81
		$Options =~ s/>($FORM{$thing})/ selected>$1/;
82
		return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
83
	}
84
}
85
 
86
 
87
 
88
# Ideally, nothing below this comment needs to change
89
#-------------------------------------------------------------------------------
90
 
91
 
92
our %NAME              = map  { $_ => $COLUMNS{$_}->[0] } keys %COLUMNS;
93
our %colOrderHash      = map  { $_ => $COLUMNS{$_}->[1] } keys %COLUMNS;
94
our %colFilterTypeHash = map  { $_ => $COLUMNS{$_}->[2] } keys %COLUMNS;
95
our @staticFields      = sort byfield grep { $COLUMNS{$_}->[3] eq 'static' } keys %COLUMNS;
96
our @defaultFields     = sort byfield grep { defined $COLUMNS{$_}->[3] } keys %COLUMNS;
97
#our @defaultFields     = grep { $COLUMNS{$_}->[3] eq 'default' or inArray ($_, \@staticFields) } keys %COLUMNS;
98
 
99
our @allFields = sort byfield keys %NAME;
100
our @displayFields = ();
101
our @hideFields = ();
102
my $QUERY_STRING;
103
 
104
my $pagelimit = param ("limit") // $pagelimitoptions[$#pagelimitoptions];
105
my $curpage = param ("page") // 1;
106
 
107
our %FORM;
108
my $FILTER;
109
foreach (param()) {
110
 	if (/^year$/) { #
111
		$YEAR = param($_);
112
		next;
113
	}
114
 
115
	$FORM{$_} = param($_);				# Retrieve all of the FORM data submitted
116
 
117
	if ((/^filter/) and ($FORM{$_} ne '')) {	# Build a set of filters to apply
118
		my ($filter,$field) = split /-/, $_;
56 bgadell 119
		$FILTER->{$field} = $FORM{$_} unless notInArray ($field, \@allFields);
32 - 120
	}	elsif ($FORM{$_} eq "true")			# Compile list of fields to display
121
		{ push @displayFields, $_; }
122
}
123
 
137 - 124
# Set any custom "where" DB filters here...
125
push @whereClause, "year = $YEAR";
32 - 126
 
127
if (exists $FORM{autoload})	{			# If the FORM was submitted (i.e. the page is being redisplayed),
128
							                    #  	build the data for the cookie that remembers the page setup
129
	my $disFields = join ":", @displayFields;
130
	my $fils = join ":", map { "$_=$FILTER->{$_}" } keys %{$FILTER};
131
 
132
	$QUERY_STRING = $disFields.'&'.$fils.'&'.$FORM{sortby}.'&'.$FORM{autoload};
133
}
134
 
135
 
136
if (!(exists $FORM{autoload}))	{			# No FORM was submitted...
137
	if (my $prefs = cookie ($prefscookie) and !defined param ("ignoreCookie"))	{ # Check for cookies from previous visits.
138
		my ($disF, $filts, $sb, $al) = split /&/,$prefs;
139
		@displayFields = split /:/,$disF;
140
 
141
		foreach my $pair (split /:/, $filts)	{
142
			my ($key, $value) = split /=/, $pair;
143
			$FORM{"filter-$key"} = $value;
144
			$FILTER->{$key} = $value;
145
		}
146
 
147
		$FORM{sortby} = $sb;
148
		$FORM{autoload} = $al;
149
		$QUERY_STRING = $prefs;
150
	}	else {
151
	  @displayFields = @defaultFields; # Otherwise suppply a default list of columns.
152
	  $FORM{autoload} = 1;             # And turn aut0load on by default.
153
	}
154
}
155
 
156
# let's just make sure the columns are in the right order (and there aren't any missing)
56 bgadell 157
@displayFields = grep { inArray($_, \@allFields) } sort byfield uniq @displayFields, @staticFields;
32 - 158
 
159
# If the field isn't in the displayFields list,	then add it to the hideFields list
160
@hideFields = grep { notInArray ($_, \@displayFields) } @allFields;
161
 
162
# Process any filters provided in the form to pass to the database
163
push @whereClause, map { filter ($_, $FILTER->{$_}) } grep { defined $FILTER->{$_} } @displayFields;
164
#push @whereClause, "year(date) = '$YEAR'";
165
 
166
 
167
							#  Given the fields to display and the where conditions,
168
							#	  "getData" will return a reference to an array of
169
							#	  hash references of the results.
170
my ($data, $datacount) = getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit);
171
my @ProductList = @{ $data };
172
 
173
#my @ProductList = @{ getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit) };
174
my $x = scalar @ProductList; # How many results were returned?
175
 
176
# If the user is trying to download the Excel file, send it to them and then exit out.
177
if ($FORM{excel}) {
178
  exportExcel (\@ProductList, "RC_Officiating_Shifts");
179
  exit;
180
}
181
 
138 - 182
my $signedOnAs = $username ? "Welcome, $username. ".$h->a ({ href=>"index.pl?LOGOUT" }, "[Log Out]") : "You are not signed in.";
32 - 183
 
184
# Set some cookie stuff...
65 bgadell 185
my $path = `dirname $ENV{SCRIPT_NAME}`; chomp $path; $path .= '/' unless $path eq "/";
32 - 186
my $queryCookie = cookie(-NAME=>$prefscookie,
187
			-VALUE=>"$QUERY_STRING",
188
			-PATH=>"$path",
189
			-EXPIRES=>'+365d');
190
 
191
# Print the header
192
print header (-cookie=> [ $queryCookie, $RCAUTH_cookie ] );
193
 
194
# 	print "<!-- FORM \n\n";				# Debug code to dump the FORM to a html comment
195
#	print "I'm catching updates!!!\n\n";
196
#	foreach $key (sort (keys %FORM))		#	Must be done after the header is written!
197
# 		{ print "\t$key:  $FORM{$key}\n"; }
198
# 	print "--> \n\n";
199
#
200
#
201
# 	print "<!-- ENV \n\n";				# Debug code to dump the ENV to a html comment
202
# 	foreach $key (sort (keys %ENV))			#	Must be done after the header is written!
203
# 		{ print "\t$key:  $ENV{$key}\n"; }
204
# 	print "--> \n\n";
205
#
206
# 	print "\n\n\n\n<!-- $QUERY_STRING --> \n\n\n\n";
207
 
208
 
209
#------------------
210
 
211
# Toggle the autoload fields within the table elements
212
our ($onClick, $onChange);   # (also used in scanFunctions)
213
my ($radiobutton, $refreshbutton, $sortby);
214
if ($FORM{autoload}) {
215
	$onClick = "onClick='submit();'";
216
	$onChange = "onChange='page.value = 1; submit();'";
217
  $radiobutton = $h->div ({ class=>'autoload' },
218
    ["Autoload Changes: ",
219
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();', checked=>[] }), "On ",
220
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();' }), "Off ",
221
    ]);
56 bgadell 222
  $refreshbutton = "";
32 - 223
  $sortby = $h->select ({name=>"sortby", onChange=>'submit();' }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
224
} else {
225
  $onClick = "";
226
	$onChange = "onChange='page.value = 1;'";
227
  $radiobutton = $h->div ({ class=>'autoload' },
228
    ["Autoload Changes: ",
229
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();' }), "On ",
230
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();', checked=>[] }), "Off ",
231
    ]);
232
  $refreshbutton = $h->input ({ type=>"button", value=>"Refresh", onClick=>"submit(); return false;" });
233
  $sortby = $h->select ({name=>"sortby" }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
234
}
235
 
236
 
237
 
238
 
239
print start_html (-title => $pageTitle, -style => {'src' => $stylesheet} );
240
 
241
print $h->open ('form', { action=>url, method=>'POST', name=>'Req' });
242
print $h->input ({ type=>"hidden", name=>"excel", value=>0 });
243
print $h->div ({ class => "accent pageheader" }, [
244
  $h->h1 ($pageTitle),
245
  $h->div ({ class=>"sp0" }, [
246
    $h->div ({ class=>"spLeft" }, [
247
      $radiobutton
248
    ]),
249
    $h->div ({ class=>"spRight" }, [
250
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
251
      $refreshbutton
252
    ]),
253
  ]),
254
]);
255
 
256
# Print the Hidden fields' check boxes (if there are any)
257
 
258
my $c = 1;
259
my @hiddencheckboxes;
260
my @hiddenrows;
261
foreach my $field (sort { $NAME{$a} cmp $NAME{$b}; } @hideFields) {
262
  if ($FORM{autoload}) {
263
    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} ]);
264
  } else {
265
    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} ]);
266
  }
267
  if ($c++ % 4 == 0) {
268
    push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]);
269
    @hiddencheckboxes = [];
270
  }
271
}
272
push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]) unless --$c % 4 == 0;
273
 
137 - 274
my @yearoptions;
275
foreach (@{&getYears()}) {
276
	push @yearoptions, $YEAR eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_);
277
}
32 - 278
 
279
if (scalar @hideFields) {
280
  my @topleft;
281
  push @topleft, $h->div ({ class=>"nowrap" }, "Hidden Columns:");
282
  push @topleft, $h->div ({ class=>'rTable' }, [ @hiddenrows ]);
283
 
284
  print $h->div ({ class=>"sp0" }, [
285
    $h->div ({ class=>"spLeft"  }, [ @topleft ]),
286
    $h->div ({ class=>"spRight" }, [
287
      $signedOnAs
288
    ])
289
  ]);
290
}
291
 
292
# Print the main table...............................................
293
 
294
print $h->open ('div', { class=>'rTable' });
295
 
296
my @tmptitlerow;
297
foreach my $f (@displayFields)	{  # Print the Column headings
298
  if (inArray ($f, \@staticFields)) {
299
    push @tmptitlerow, $h->div ({ class=>'rTableHead' }, [ $h->input ({ type=>"hidden", name=>$f, value=>"true" }), $NAME{$f} ]);
300
  } else {
301
    if ($FORM{autoload}) {
302
      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} ]);
303
    } else {
304
      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} ]);
305
    }
306
  }
307
}
308
 
309
# Print the filter boxes...
310
print $h->div ({ class=>'rTableHeading' }, [ @tmptitlerow ], [ map { $h->div ({ class=>'rTableCell filters' }, filter ($_)) } @displayFields ], $h->div ({ class=>"rTableCell" }));
311
 
312
# Print the things
313
foreach my $t (@ProductList)	{
314
  no strict;
56 bgadell 315
  print $h->div ({ class=>'rTableRow shaded' }, [ map { $h->div ({ class=>'rTableCell' }, exists &{"modify_".$_} ? &{"modify_".$_} ($t) : $t->{$_} ? $t->{$_} : "") } @displayFields ]);
32 - 316
}
317
 
318
 
319
 
320
 
321
print $h->close ('div');
322
 
323
# close things out................................................
324
 
325
my $pages = $pagelimit eq "All" ? 1 : int( $datacount / $pagelimit + 0.99 );
326
if ($curpage > $pages) { $curpage = $pages; }
327
 
328
my @pagerange;
329
if ($pages <= 5 ) {
330
  @pagerange = 1 .. $pages;
331
} else {
332
  if ($curpage <= 3) {
333
    @pagerange = (1, 2, 3, 4, ">>");
334
  } elsif ($curpage >= $pages - 2) {
335
    @pagerange = ("<<", $pages-3, $pages-2, $pages-1, $pages);
336
  } else {
337
    @pagerange = ("<<", $curpage-1, $curpage, $curpage+1, ">>");
338
  }
339
}
340
 
341
print $h->br; # print $h->br;
342
print $h->div ({ class=>"sp0" }, [
343
    $h->div ({ class=>"spLeft" }, [
344
      $h->div ({ class=>"footer" }, [
345
        "To bookmark, save, or send this exact view, use the ",
346
        $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
347
        $h->br,
348
        "If this page is displaying oddly, ", $h->a ({ href=>url ()."?ignoreCookie=1" }, "[Reset Your View]"),
349
        $h->br,
350
        $h->a ({ href=>"", target=>"_new", onClick=>"window.document.Req.excel.value=1; window.document.Req.submit(); window.document.Req.excel.value=0; return false;" }, "[Export Displayed Data as an Excel Document.]"),
351
        $h->br,
352
        "This page was displayed on ", currentTime (),
353
        $h->br,
137 - 354
        "Please direct questions, problems, and concerns to $SYSTEM_EMAIL",
355
        $h->br,
356
        "Displaying: ", $h->select ({ name=>"year", onchange=>"Req.submit();" }, [ @yearoptions ])
32 - 357
      ])
358
    ]),
359
    $h->div ({ class=>"spRight" }, [
360
      $h->h5 ([
361
               "$x of $datacount Record". ($x == 1 ? "" : "s") ." Displayed", $h->br,
362
               "Sorted by ", $sortby, $h->br,
363
               "Displaying ", $h->select ({ name=>"limit", onChange=>"page.value = 1; submit();" }, [ map { $pagelimit == $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @pagelimitoptions ]), " Per Page", $h->br,
364
               ( $pages > 1 ? ( join " ", map { $_ == $curpage ? "<B>$_</b>" :
365
                                                $_ eq "<<"     ? $h->a ({ onClick=>qq{Req.page.value=1; Req.submit();} }, "$_") :
366
                                                $_ eq ">>"     ? $h->a ({ onClick=>qq{Req.page.value=$pages; Req.submit();} }, "$_") :
367
                                                                 $h->a ({ onClick=>qq{Req.page.value=$_; Req.submit();} }, "[$_]") } @pagerange ) : "" ), $h->br,
368
               $h->input ({ type=>"hidden", name=>"page", value=>$curpage })
369
      ])
370
    ]),
371
]);
372
 
373
#print $h->br; # print $h->br;
374
#print $h->h5 ("$x Record(s) Displayed");
375
#print $h->div ({ class=>"footer" }, [
376
#  "To bookmark, save, or send this exact view, use the ",
377
#  $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
378
#  $h->br,
379
#  "This page was displayed on $now",
380
#  $h->br,
381
#  "Please direct questions, problems, and concerns to noone\@gmail.com"
382
#]);
383
 
384
 
385
print $h->close('form');
386
print $h->close('html');