Subversion Repositories ORC

Rev

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