Subversion Repositories VORC

Rev

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