Subversion Repositories ORC

Rev

Rev 8 | Go to most recent revision | Details | 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 strict;
6
use CGI qw/param cookie header start_html url/;
7
use HTML::Tiny;
8
use tableViewer;
9
use RollerCon;
10
our $h = HTML::Tiny->new( mode => 'html' );
11
use DateTime;
12
use DateTime::Duration;
13
my $now = DateTime->now;
14
 
15
my $cookie_string = authenticate (1) || die;
16
# Add checking to make sure they're in this department
17
our ($EML, $PWD, $LVL) = split /&/, $cookie_string;
18
my $user = getUser ($EML);
19
$user->{department} = convertDepartments $user->{department};
20
 
21
if ($user->{department}->{"ANN"} < 1 or $LVL < 4) {
22
	print header(-cookie=>$RCAUTH_cookie);
23
	printRCHeader("Unauthorized Page");
24
	print $h->div ({ class=>"error" }, "You're not an Announcer");
25
	print $h->div ("Your user account is not registered as an Announcer, so you can't see these shifts.  It's possible that your access is still being reviewed.  Please be patient.");
26
	print $h->a ({ href=>"/" }, "[Go Home]");
27
	print $h->close ("html");
28
	exit;
29
}
30
 
31
my $username = $h->a ({ href=>"/schedule/manage_user.pl?submit=View&RCid=$user->{RCid}" }, $user->{derby_name});
32
my $RCid = $user->{RCid};
33
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
34
#my $YEAR = 1900 + (localtime)[5]; #which year of data to display, default to current
35
my $YEAR = "2019";
36
my @addToWhereClause = ("year(date) = '$YEAR'", "type = 'announcer'");
37
 
38
#$LVL = 1;
39
 
40
my $pageTitle = "Announcer Shifts";
41
my $prefscookie = "ashiftscookie";
42
our $DBTABLE = 'v_shift_announcer';
43
my %COLUMNS = (
44
# colname   =>  [qw(DisplayName       N    type     status)],   status ->  static | default | <blank>
45
	id						 => [qw(ID           5    number            )],
46
	date					 => [qw(Date        10    date      default )],
47
	dayofweek			 => [qw(Day         15    select            )],
48
	time					 => [qw(Time        20    text      default )],
49
	volhours       => [qw(VolHours    23    number            )],
50
	track					 => [qw(Track       25    select    default )],
51
	teams					 => [qw(Teams       30    text      default )],
52
	level					 => [qw(Level       35    select    default )],
53
	restrictions   => [qw(Rs          40    select            )],
54
	gtype					 => [qw(Type        45    select    default )],
55
	notes  				 => [qw(Notes       50    text              )],
56
	role					 => [qw(Role        55    select            )],
57
	tla						 => [qw(TLA         60    select    default )],
58
	name					 => [qw(Position    65    text              )],
59
	type					 => [qw(Class       70    select    default )],
60
	RCid					 => [qw(RCid        75    number            )],
61
	derby_name     => [qw(Assignee    80    select    default )]
62
);
63
 
64
if ($user->{department}->{"ANN"} > 1) {
65
# Add:
66
#		real_name			 RealName
67
#		email					 Email
68
  $COLUMNS{'real_name'} = [qw(RealName    85    text)];
69
  $COLUMNS{'email'}     = [qw(Email       90    text)];
70
}
71
 
72
my $stylesheet = "/style.css";
73
my $homeURL = '/';
74
my @pagelimitoptions = ("All", 5, 10, 25);
75
 
76
# If we need to modify line item values, create a subroutine named "modify_$columnname"
77
#    It will receive a hashref to the object lineitem (RETURN the modified field at the end of the function!)
78
 
79
sub modify_derby_name {
80
  my $t = shift;
81
 
82
  if ($user->{department}->{"ANN"} < 2 and $t->{derby_name} and $t->{RCid} != $RCid and $LVL < 5) {
83
    $t->{derby_name} = "FILLED";
84
  }
85
 
86
 	my ($yyyy, $mm, $dd) = split /\-/, $t->{date};
87
	my $cutoff = DateTime->new(
88
        year => $yyyy,
89
        month => $mm,
90
        day => $dd,
91
        hour => 5,
92
        minute => 0,
93
        second => 0,
94
        time_zone => 'America/Los_Angeles'
95
  );
96
 
97
 	if (($t->{RCid} == $RCid and $t->{gtype} !~ /^selected/ and $now < $cutoff) or ($t->{derby_name} and ($user->{department}->{"ANN"} >= 2 or $LVL >= 5))) {
98
 		# DROP
99
 		$t->{derby_name} = "$t->{derby_name} <A HREF='#' onClick=\"window.open('make_shift_change.pl?change=del&RCid=$t->{RCid}&id=$t->{id}&role=$t->{role}','Confirm Shift Change','resizable,height=260,width=370'); return false;\">[DROP]</a>";
100
 		if ($user->{department}->{"ANN"} >= 2 or $LVL > 4) {
101
 		  # NO SHOW
102
 		  $t->{derby_name} .= " | <A HREF='#' onClick=\"if (confirm('Really? They were a no show?')==true) { window.open('make_shift_change.pl?noshow=true&change=del&RCid=$t->{RCid}&id=$t->{id}&role=$t->{role}','Confirm Shift Change','resizable,height=260,width=370'); return false; }\">[NO SHOW]</a>";
103
 		}
104
 	} elsif (!$t->{derby_name}) {
105
 		if (signUpEligible ($ORCUSER, $t) and $now < $cutoff) {
106
 			# SIGN UP
107
 			$t->{derby_name} = "<A HREF='#' onClick=\"window.open('make_shift_change.pl?change=add&RCid=$RCid&id=$t->{id}&role=$t->{role}','Confirm Shift Change','resizable,height=260,width=370'); return false;\">[SIGN UP]</a>";
108
 		}
109
 		if ($user->{department}->{"ANN"} >= 2 or $LVL > 4) {
110
 			# ADD USER
111
 			$t->{derby_name} ? $t->{derby_name} .= " | " : {};
112
 			$t->{derby_name} .= "<A HREF='#' onClick=\"window.open('make_shift_change.pl?change=lookup&RCid=$RCid&id=$t->{id}&role=$t->{role}','Confirm Shift Change','resizable,height=260,width=370'); return false;\">[ADD USER]</a>";
113
 		}
114
 	}
115
 	return $t->{derby_name};
116
}
117
 
118
 
119
 
120
 
121
 
122
# Ideally, nothing below this comment needs to change
123
#-------------------------------------------------------------------------------
124
 
125
 
126
our %NAME              = map  { $_ => $COLUMNS{$_}->[0] } keys %COLUMNS;
127
our %colOrderHash      = map  { $_ => $COLUMNS{$_}->[1] } keys %COLUMNS;
128
our %colFilterTypeHash = map  { $_ => $COLUMNS{$_}->[2] } keys %COLUMNS;
129
our @staticFields      = sort byfield grep { $COLUMNS{$_}->[3] eq 'static' } keys %COLUMNS;
130
our @defaultFields     = sort byfield grep { defined $COLUMNS{$_}->[3] } keys %COLUMNS;
131
#our @defaultFields     = grep { $COLUMNS{$_}->[3] eq 'default' or inArray ($_, \@staticFields) } keys %COLUMNS;
132
 
133
our @allFields = sort byfield keys %NAME;
134
our @displayFields = ();
135
our @hideFields = ();
136
my $QUERY_STRING;
137
 
138
my $pagelimit = param ("limit") // $pagelimitoptions[$#pagelimitoptions];
139
my $curpage = param ("page") // 1;
140
 
141
our %FORM;
142
my $FILTER;
143
foreach (param()) {
144
	if (/^year$/) { #
145
		$YEAR = param($_);
146
		next;
147
	}
148
 
149
	$FORM{$_} = param($_);				# Retrieve all of the FORM data submitted
150
 
151
	if ((/^filter/) and ($FORM{$_} ne '')) {	# Build a set of filters to apply
152
		my ($filter,$field) = split /-/, $_;
153
		$FILTER->{$field} = $FORM{$_};
154
	}	elsif ($FORM{$_} eq "true")	{		# Compile list of fields to display
155
	  if ($_ ne "shiftinclude") {
156
		  push @displayFields, $_;
157
		}
158
  }
159
}
160
 
161
 
162
if (exists $FORM{autoload})	{			# If the FORM was submitted (i.e. the page is being redisplayed),
163
							                    #  	build the data for the cookie that remembers the page setup
164
	my $disFields = join ":", @displayFields;
165
	my $fils = join ":", map { "$_=$FILTER->{$_}" } keys %{$FILTER};
166
 
167
	$QUERY_STRING = $disFields.'&'.$fils.'&'.$FORM{sortby}.'&'.$FORM{autoload}.'&'.$FORM{shiftinclude};
168
}
169
 
170
 
171
if (!(exists $FORM{autoload}))	{			# No FORM was submitted...
172
	if (my $prefs = cookie ($prefscookie) and !defined param ("ignoreCookie"))	{ # Check for cookies from previous visits.
173
		my ($disF, $filts, $sb, $al, $si) = split /&/, $prefs;
174
		@displayFields = split /:/,$disF;
175
 
176
		foreach my $pair (split /:/, $filts)	{
177
			my ($key, $value) = split /=/, $pair;
178
			$FORM{"filter-$key"} = $value;
179
			$FILTER->{$key} = $value;
180
		}
181
 
182
		$FORM{sortby} = $sb;
183
		$FORM{autoload} = $al;
184
		$FORM{shiftinclude} = $si;
185
		$QUERY_STRING = $prefs;
186
	}	else {
187
	  @displayFields = @defaultFields; # Otherwise suppply a default list of columns.
188
	  $FORM{autoload} = 1;             # And turn aut0load on by default.
189
	}
190
}
191
 
192
# let's just make sure the columns are in the right order (and there aren't any missing)
193
@displayFields = sort byfield uniq @displayFields, @staticFields;
194
 
195
# If the field isn't in the displayFields list,	then add it to the hideFields list
196
@hideFields = grep { notInArray ($_, \@displayFields) } @allFields;
197
 
198
# Process any filters provided in the form to pass to the database
199
my @whereClause = map { filter ($_, $FILTER->{$_}) } grep { defined $FILTER->{$_} } @displayFields;
200
push @whereClause, @addToWhereClause;
201
 
202
							#  Given the fields to display and the where conditions,
203
							#	  "getData" will return a reference to an array of
204
							#	  hash references of the results.
205
my ($data, $datacount) = getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit);
206
my @ProductList = @{ $data };
207
 
208
#my @ProductList = @{ getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit) };
209
my $x = scalar @ProductList; # How many results were returned?
210
 
211
# If the user is trying to download the Excel file, send it to them and then exit out.
212
if ($FORM{excel}) {
213
  exportExcel (\@ProductList, "RC_Officiating_Shifts");
214
  exit;
215
}
216
 
217
my @shifts;
218
if ($FORM{shiftinclude} eq "true") {
219
  my @SIWhere = ("year(date) = '$YEAR'");
220
  push @SIWhere, "RCid = $ORCUSER->{RCid}";
221
  my ($d, $c) = getData (\@displayFields, \@SIWhere, $DBTABLE, $FORM{sortby});
222
  @shifts = @{ $d };
223
}
224
 
225
 
226
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.";
227
 
228
# Set some cookie stuff...
229
my $path = `dirname $ENV{REQUEST_URI}`; chomp $path; $path .= '/' unless $path eq "/";
230
my $queryCookie = cookie(-NAME=>$prefscookie,
231
			-VALUE=>"$QUERY_STRING",
232
			-PATH=>"$path",
233
			-EXPIRES=>'+365d');
234
 
235
# Print the header
236
print header (-cookie=> [ $queryCookie, $RCAUTH_cookie ] );
237
 
238
# 	print "<!-- FORM \n\n";				# Debug code to dump the FORM to a html comment
239
#	print "I'm catching updates!!!\n\n";
240
#	foreach $key (sort (keys %FORM))		#	Must be done after the header is written!
241
# 		{ print "\t$key:  $FORM{$key}\n"; }
242
# 	print "--> \n\n";
243
#
244
#
245
# 	print "<!-- ENV \n\n";				# Debug code to dump the ENV to a html comment
246
# 	foreach $key (sort (keys %ENV))			#	Must be done after the header is written!
247
# 		{ print "\t$key:  $ENV{$key}\n"; }
248
# 	print "--> \n\n";
249
#
250
# 	print "\n\n\n\n<!-- $QUERY_STRING --> \n\n\n\n";
251
 
252
 
253
#------------------
254
 
255
# Toggle the autoload fields within the table elements
256
our ($onClick, $onChange);   # (also used in scanFunctions)
257
my ($radiobutton, $refreshbutton, $sortby);
258
if ($FORM{autoload}) {
259
	$onClick = "onClick='submit();'";
260
	$onChange = "onChange='page.value = 1; submit();'";
261
  $radiobutton = $h->div ({ class=>'autoload' },
262
    ["Autoload Changes: ",
263
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();', checked=>[] }), "On ",
264
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();' }), "Off ",
265
    ]);
266
  $sortby = $h->select ({name=>"sortby", onChange=>'submit();' }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
267
} else {
268
  $onClick = "";
269
	$onChange = "onChange='page.value = 1;'";
270
  $radiobutton = $h->div ({ class=>'autoload' },
271
    ["Autoload Changes: ",
272
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();' }), "On ",
273
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();', checked=>[] }), "Off ",
274
    ]);
275
  $refreshbutton = $h->input ({ type=>"button", value=>"Apply Changes", onClick=>"submit(); return false;" });
276
  $sortby = $h->select ({ name=>"sortby" }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
277
}
278
 
279
my $SIChecked;
280
if ($FORM{shiftinclude}) {
281
  $SIChecked = $h->input ({ type=>"checkbox", name=>"shiftinclude", value=>"true", checked=>[], onClick=>'submit();' });
282
} else {
283
  $SIChecked = $h->input ({ type=>"checkbox", name=>"shiftinclude", value=>"true", onClick=>'submit();' });
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
my @yearoptions;
324
foreach (@{&getYears()}) {
325
	push @yearoptions, $YEAR eq $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_);
326
}
327
 
328
if (scalar @hideFields) {
329
  my @topleft;
330
  push @topleft, $h->div ({ class=>"nowrap" }, "Hidden Columns:");
331
  push @topleft, $h->div ({ class=>'rTable' }, [ @hiddenrows ]);
332
 
333
  print $h->div ({ class=>"sp0" }, [
334
    $h->div ({ class=>"spLeft"  }, [ @topleft ]),
335
    $h->div ({ class=>"spRight" }, [
336
      $signedOnAs, $h->br,
337
      "Show my shifts: ", $SIChecked
338
    ])
339
  ]);
340
}
341
 
342
# Print the main table...............................................
343
 
344
print $h->open ('div', { class=>'rTable' });
345
 
346
my @tmptitlerow;
347
foreach my $f (@displayFields)	{  # Print the Column headings
348
  if (inArray ($f, \@staticFields)) {
349
    push @tmptitlerow, $h->div ({ class=>'rTableHead' }, [ $h->input ({ type=>"hidden", name=>$f, value=>"true" }), $NAME{$f} ]);
350
  } else {
351
    if ($FORM{autoload}) {
352
      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} ]);
353
    } else {
354
      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} ]);
355
    }
356
  }
357
}
358
 
359
# Print the filter boxes...
360
print $h->div ({ class=>'rTableHeading' }, [ @tmptitlerow ], [ map { $h->div ({ class=>'rTableCell filters' }, filter ($_)) } @displayFields ], $h->div ({ class=>"rTableCell" }));
361
 
362
 
363
if ($FORM{shiftinclude}) {  # Include all of the user's shifts at the top
364
  foreach my $t (@shifts)	{
365
	  print $h->div ({ class=>'rTableRow highlighted' }, [ map { $h->div ({ class=>'rTableCell' }, exists &{"modify_".$_} ? &{"modify_".$_} ($t) : $t->{$_}) } @displayFields ]);
366
  }
367
  print $h->hr ({ width=>"500%" });
368
}
369
 
370
 
371
# Print the things
372
foreach my $t (@ProductList)	{
373
  if ($t->{RCid} eq $ORCUSER->{RCid}) {
374
	  print $h->div ({ class=>'rTableRow highlighted' }, [ map { $h->div ({ class=>'rTableCell' }, exists &{"modify_".$_} ? &{"modify_".$_} ($t) : $t->{$_}) } @displayFields ]);
375
  } else {
376
	  print $h->div ({ class=>'rTableRow shaded' }, [ map { $h->div ({ class=>'rTableCell' }, exists &{"modify_".$_} ? &{"modify_".$_} ($t) : $t->{$_}) } @displayFields ]);
377
  }
378
}
379
 
380
 
381
print $h->close ('div');
382
 
383
# close things out................................................
384
 
385
my $pages = $pagelimit eq "All" ? 1 : int( $datacount / $pagelimit + 0.99 );
386
if ($curpage > $pages) { $curpage = $pages; }
387
 
388
my @pagerange;
389
if ($pages <= 5 ) {
390
  @pagerange = 1 .. $pages;
391
} else {
392
  if ($curpage <= 3) {
393
    @pagerange = (1, 2, 3, 4, ">>");
394
  } elsif ($curpage >= $pages - 2) {
395
    @pagerange = ("<<", $pages-3, $pages-2, $pages-1, $pages);
396
  } else {
397
    @pagerange = ("<<", $curpage-1, $curpage, $curpage+1, ">>");
398
  }
399
}
400
 
401
print $h->br; # print $h->br;
402
print $h->div ({ class=>"sp0" }, [
403
    $h->div ({ class=>"spLeft" }, [
404
      $h->div ({ class=>"footer" }, [
405
        "To bookmark, save, or send this exact view, use the ",
406
        $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
407
        $h->br,
408
        "If this page is displaying oddly, ", $h->a ({ href=>url ()."?ignoreCookie=1" }, "[Reset Your View]"),
409
        $h->br,
410
        $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.]"),
411
        $h->br,
412
        "This page was displayed on ", currentTime (),
413
        $h->br,
414
        "Please direct questions, problems, and concerns to noone\@gmail.com",
415
        $h->br,
416
        "Displaying: ", $h->select ({ name=>"year", onchange=>"Req.submit();" }, [ @yearoptions ])
417
      ])
418
    ]),
419
    $h->div ({ class=>"spRight" }, [
420
      $h->h5 ([
421
               "$x of $datacount Record". ($x == 1 ? "" : "s") ." Displayed", $h->br,
422
               "Sorted by ", $sortby, $h->br,
423
               "Displaying ", $h->select ({ name=>"limit", onChange=>"page.value = 1; submit();" }, [ map { $pagelimit == $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @pagelimitoptions ]), " Per Page", $h->br,
424
               ( $pages > 1 ? ( join " ", map { $_ == $curpage ? "<B>$_</b>" :
425
                                                $_ eq "<<"     ? $h->a ({ onClick=>qq{Req.page.value=1; Req.submit();} }, "$_") :
426
                                                $_ eq ">>"     ? $h->a ({ onClick=>qq{Req.page.value=$pages; Req.submit();} }, "$_") :
427
                                                                 $h->a ({ onClick=>qq{Req.page.value=$_; Req.submit();} }, "[$_]") } @pagerange ) : "" ), $h->br,
428
               $h->input ({ type=>"hidden", name=>"page", value=>$curpage })
429
      ])
430
    ]),
431
]);
432
 
433
#print $h->br; # print $h->br;
434
#print $h->h5 ("$x Record(s) Displayed");
435
#print $h->div ({ class=>"footer" }, [
436
#  "To bookmark, save, or send this exact view, use the ",
437
#  $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
438
#  $h->br,
439
#  "This page was displayed on $now",
440
#  $h->br,
441
#  "Please direct questions, problems, and concerns to noone\@gmail.com"
442
#]);
443
 
444
 
445
print $h->close('form');
446
print $h->close('html');