Subversion Repositories VORC

Rev

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