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;
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 )],
31
	time        => [qw(Time          20    text        default )],
32
	start_time  => [qw(Start         25    text         )],
33
	end_time    => [qw(End           30    text         )],
34
	mod_time    => [qw(ModTime       35    number         )],
35
	volhours    => [qw(VolHours      40    number         )],
36
	role        => [qw(Role          45    select      default )],
37
	type        => [qw(Type          50    select      default )],
38
	location    => [qw(Location      55    select      default )],
39
	note        => [qw(Notes         60    text        default )],
40
	RCid        => [qw(RCID          65    text         )],
41
	derby_name  => [qw(Assignee      70    select      default   )],
42
);
43
my $stylesheet = "/style.css";
8 - 44
my $homeURL = '/schedule/';
7 - 45
my @pagelimitoptions = ("All", 5, 10, 25);
46
 
47
my @whereClause;
48
if ($LVL < 5) {
49
  my $string = "dept in (".join ",", map { '"'.$_.'"' } grep { $ORCUSER->{department}->{$_} >= 3 } keys %{$ORCUSER->{department}};
50
  $string .= ")";
51
}
52
push @whereClause, "dept != 'PER'";
53
 
54
# If we need to modify line item values, create a subroutine named "modify_$columnname"
55
#    It will receive a hashref to the object lineitem
56
 
57
sub modify_id {
58
  my $hr = shift;
59
  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;";
60
  my $extrawarning = $hr->{RCid} ? "\\nWARNING! It appears someone is signed up for it." : "";
61
  return join "&nbsp;", #$hr->{id},
62
         $h->a ({ href=>"manage_shift.pl?id=$hr->{id}&choice=Update", onClick=>$clicky }, "[Edit]"),
63
         $h->a ({ href=>"manage_shift.pl?id=$hr->{id}&choice=Copy" }, "[Copy]"),
64
         $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]")
65
  ;
66
};
67
 
68
my $DEPTS = getDepartments;
69
sub modify_dept {
70
  my $hr = shift;
71
  $hr->{dept} = $DEPTS->{$hr->{dept}};
72
}
73
 
74
sub filter_dept {
75
  my $colName = shift;
76
	my $filter = shift;
77
 
78
	if (defined $filter)	{
79
		if ($filter eq "-blank-") {
80
			return "($colName = '' or isNull($colName) = 1)";
81
		}
82
		return "$colName = \"$filter\"";
83
	}	else {
84
		my $thing = "filter-${colName}";
85
    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};
86
		my $Options = "<OPTION></OPTION>".$categories;
87
 
88
		$Options =~ s/>($FORM{$thing})/ selected>$1/;
89
		return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
90
	}
91
}
92
 
93
use DateTime;
94
use DateTime::Duration;
95
sub modify_derby_name {
96
  my $t = shift;
97
 
98
 	my ($yyyy, $mm, $dd) = split /\-/, $t->{date};
99
	my $cutoff = DateTime->new(
100
        year => $yyyy,
101
        month => $mm,
102
        day => $dd,
103
        hour => 5,
104
        minute => 0,
105
        second => 0,
106
        time_zone => 'America/Los_Angeles'
107
  );
108
 
109
 	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))) {
110
 		# DROP
111
 		$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>";
112
 		if ($user->{department}->{$t->{dept}} >= 2 or $LVL > 4) {
113
 		  # NO SHOW
114
 		  $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>";
115
 		}
116
 	} elsif (!$t->{derby_name}) {
117
 		if (signUpEligible ($ORCUSER, $t, "vol") and $now < $cutoff) {
118
 			# SIGN UP
119
 			$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>";
120
 		}
121
 		if ($user->{department}->{$t->{dept}} >= 2 or $LVL > 4) {
122
 			# ADD USER
123
 			$t->{derby_name} ? $t->{derby_name} .= " | " : {};
124
 			$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>";
125
 		}
126
 	}
127
 	return $t->{derby_name};
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{$_};
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{sortby} = $displayFields[1];
194
	  $FORM{autoload} = 1;             # And turn aut0load on by default.
195
	}
196
}
197
 
198
# let's just make sure the columns are in the right order (and there aren't any missing)
199
@displayFields = sort byfield uniq @displayFields, @staticFields;
200
 
201
# If the field isn't in the displayFields list,	then add it to the hideFields list
202
@hideFields = grep { notInArray ($_, \@displayFields) } @allFields;
203
 
204
# Process any filters provided in the form to pass to the database
205
push @whereClause, map { filter ($_, $FILTER->{$_}) } grep { defined $FILTER->{$_} } @displayFields;
206
 
207
							#  Given the fields to display and the where conditions,
208
							#	  "getData" will return a reference to an array of
209
							#	  hash references of the results.
210
my ($data, $datacount) = getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit);
211
my @ProductList = @{ $data };
212
 
213
#my @ProductList = @{ getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit) };
214
my $x = scalar @ProductList; # How many results were returned?
215
 
216
# If the user is trying to download the Excel file, send it to them and then exit out.
217
if ($FORM{excel}) {
218
  exportExcel (\@ProductList, "RC_Officiating_Shifts");
219
  exit;
220
}
221
 
222
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.";
223
 
224
# Set some cookie stuff...
225
my $path = `dirname $ENV{REQUEST_URI}`; chomp $path; $path .= '/' unless $path eq "/";
226
my $queryCookie = cookie(-NAME=>$prefscookie,
227
			-VALUE=>"$QUERY_STRING",
228
			-PATH=>"$path",
229
			-EXPIRES=>'+365d');
230
 
231
# Print the header
232
print header (-cookie=> [ $queryCookie, $RCAUTH_cookie ] );
233
 
234
# 	print "<!-- FORM \n\n";				# Debug code to dump the FORM to a html comment
235
#	print "I'm catching updates!!!\n\n";
236
#	foreach $key (sort (keys %FORM))		#	Must be done after the header is written!
237
# 		{ print "\t$key:  $FORM{$key}\n"; }
238
# 	print "--> \n\n";
239
#
240
#
241
# 	print "<!-- ENV \n\n";				# Debug code to dump the ENV to a html comment
242
# 	foreach $key (sort (keys %ENV))			#	Must be done after the header is written!
243
# 		{ print "\t$key:  $ENV{$key}\n"; }
244
# 	print "--> \n\n";
245
#
246
# 	print "\n\n\n\n<!-- $QUERY_STRING --> \n\n\n\n";
247
 
248
 
249
#------------------
250
 
251
# Toggle the autoload fields within the table elements
252
our ($onClick, $onChange);   # (also used in scanFunctions)
253
my ($radiobutton, $refreshbutton, $sortby);
254
if ($FORM{autoload}) {
255
	$onClick = "onClick='submit();'";
256
	$onChange = "onChange='page.value = 1; submit();'";
257
  $radiobutton = $h->div ({ class=>'autoload' },
258
    ["Autoload Changes: ",
259
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();', checked=>[] }), "On ",
260
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();' }), "Off ",
261
    ]);
262
  $sortby = $h->select ({name=>"sortby", onChange=>'submit();' }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } grep { $_ ne "id" } @displayFields ]);
263
} else {
264
  $onClick = "";
265
	$onChange = "onChange='page.value = 1;'";
266
  $radiobutton = $h->div ({ class=>'autoload' },
267
    ["Autoload Changes: ",
268
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();' }), "On ",
269
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();', checked=>[] }), "Off ",
270
    ]);
271
  $refreshbutton = $h->input ({ type=>"button", value=>"Apply Changes", onClick=>"submit(); return false;" });
272
  $sortby = $h->select ({name=>"sortby" }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
273
}
274
 
275
 
276
 
277
 
278
print start_html (-title => $pageTitle, -style => {'src' => $stylesheet} );
279
 
280
print $h->open ('form', { action=>url, method=>'POST', name=>'Req' });
281
print $h->input ({ type=>"hidden", name=>"excel", value=>0 });
282
print $h->div ({ class => "accent pageheader" }, [
283
  $h->h1 ($pageTitle),
284
  $h->div ({ class=>"sp0" }, [
285
    $h->div ({ class=>"spLeft" }, [
286
      $radiobutton
287
    ]),
288
    $h->div ({ class=>"spRight" }, [
289
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
290
      $refreshbutton
291
    ]),
292
  ]),
293
]);
294
 
295
# Print the Hidden fields' check boxes (if there are any)
296
 
297
my $c = 1;
298
my @hiddencheckboxes;
299
my @hiddenrows;
300
foreach my $field (sort { $NAME{$a} cmp $NAME{$b}; } @hideFields) {
301
  if ($FORM{autoload}) {
302
    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} ]);
303
  } else {
304
    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} ]);
305
  }
306
  if ($c++ % 4 == 0) {
307
    push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]);
308
    @hiddencheckboxes = [];
309
  }
310
}
311
push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]) unless --$c % 4 == 0;
312
 
313
 
314
if (scalar @hideFields) {
315
  my @topleft;
316
  push @topleft, $h->div ({ class=>"nowrap" }, "Hidden Columns:");
317
  push @topleft, $h->div ({ class=>'rTable' }, [ @hiddenrows ]);
318
 
319
  print $h->div ({ class=>"sp0" }, [
320
    $h->div ({ class=>"spLeft"  }, [ @topleft ]),
321
    $h->div ({ class=>"spRight" }, [
322
      $signedOnAs
323
    ])
324
  ]);
325
}
326
 
327
# Print the main table...............................................
328
 
329
print $h->open ('div', { class=>'rTable' });
330
 
331
my @tmptitlerow;
332
foreach my $f (@displayFields)	{  # Print the Column headings
333
  if (inArray ($f, \@staticFields)) {
334
    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'" }) ]);
335
  } else {
336
    if ($FORM{autoload}) {
337
      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} ]);
338
    } else {
339
      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} ]);
340
    }
341
  }
342
}
343
 
344
# Print the filter boxes...
345
print $h->div ({ class=>'rTableHeading' }, [ @tmptitlerow ], [ map { $h->div ({ class=>'rTableCell filters' }, filter ($_)) } @displayFields ], $h->div ({ class=>"rTableCell" }));
346
 
347
# Print the things
348
foreach my $t (@ProductList)	{
349
  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 ]);
350
}
351
 
352
print $h->close ('div');
353
 
354
# close things out................................................
355
 
356
my $pages = $pagelimit eq "All" ? 1 : int( $datacount / $pagelimit + 0.99 );
357
if ($curpage > $pages) { $curpage = $pages; }
358
 
359
my @pagerange;
360
if ($pages <= 5 ) {
361
  @pagerange = 1 .. $pages;
362
} else {
363
  if ($curpage <= 3) {
364
    @pagerange = (1, 2, 3, 4, ">>");
365
  } elsif ($curpage >= $pages - 2) {
366
    @pagerange = ("<<", $pages-3, $pages-2, $pages-1, $pages);
367
  } else {
368
    @pagerange = ("<<", $curpage-1, $curpage, $curpage+1, ">>");
369
  }
370
}
371
 
372
print $h->br; # print $h->br;
373
print $h->div ({ class=>"sp0" }, [
374
    $h->div ({ class=>"spLeft" }, [
375
      $h->div ({ class=>"footer" }, [
376
        "To bookmark, save, or send this exact view, use the ",
377
        $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
378
        $h->br,
379
        "If this page is displaying oddly, ", $h->a ({ href=>url ()."?ignoreCookie=1" }, "[Reset Your View]"),
380
        $h->br,
381
        $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.]"),
382
        $h->br,
383
        "This page was displayed on ", currentTime (),
384
        $h->br,
385
        "Please direct questions, problems, and concerns to noone\@gmail.com"
386
      ])
387
    ]),
388
    $h->div ({ class=>"spRight" }, [
389
      $h->h5 ([
390
               "$x of $datacount Record". ($x == 1 ? "" : "s") ." Displayed", $h->br,
391
               "Sorted by ", $sortby, $h->br,
392
               "Displaying ", $h->select ({ name=>"limit", onChange=>"page.value = 1; submit();" }, [ map { $pagelimit == $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @pagelimitoptions ]), " Per Page", $h->br,
393
               ( $pages > 1 ? ( join " ", map { $_ == $curpage ? "<B>$_</b>" :
394
                                                $_ eq "<<"     ? $h->a ({ onClick=>qq{Req.page.value=1; Req.submit();} }, "$_") :
395
                                                $_ eq ">>"     ? $h->a ({ onClick=>qq{Req.page.value=$pages; Req.submit();} }, "$_") :
396
                                                                 $h->a ({ onClick=>qq{Req.page.value=$_; Req.submit();} }, "[$_]") } @pagerange ) : "" ), $h->br,
397
               $h->input ({ type=>"hidden", name=>"page", value=>$curpage })
398
      ])
399
    ]),
400
]);
401
 
402
#print $h->br; # print $h->br;
403
#print $h->h5 ("$x Record(s) Displayed");
404
#print $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
#  "This page was displayed on $now",
409
#  $h->br,
410
#  "Please direct questions, problems, and concerns to noone\@gmail.com"
411
#]);
412
 
413
 
414
print $h->close('form');
415
print $h->close('html');