Subversion Repositories VORC

Rev

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

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