Subversion Repositories VORC

Rev

Rev 121 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
105 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::SYSADMIN) || 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 = 1900 + (localtime)[5];
26
 
27
 
28
my $pageTitle = "Class Survey Questions";
29
my $prefscookie = "class_survey_questions";
30
our $DBTABLE = 'survey_question';
31
my %COLUMNS = (
32
# colname   =>  [qw(DisplayName       N    type     status)],   status ->  static | default | <blank>
33
	qid         => [qw(QID           5    number      static )],
34
	qorder      => [qw(Order        10    number      default )],
35
	question    => [qw(Question     20    text        default )],
36
	type        => [qw(Type         30    select      default )],
37
	required    => [qw(Req          40    boolean      default )],
38
	private     => [qw(Private      45    boolean      default )],
39
	added       => [qw(Timestamp    50    date        default )],
40
);
41
my $stylesheet = "/style.css";
42
my $homeURL = '/schedule/';
43
my @pagelimitoptions = ("All", 5, 10, 25);
44
 
45
# Set any custom "where" DB filters here...
46
my @whereClause;
47
 
48
# If we need to modify line item values, create a subroutine named "modify_$columnname"
49
#    It will receive a hashref to the object lineitem
50
 
51
sub modify_qid {
52
  my $hr = shift;
53
  my $clicky = $hr->{qorder} ? "event.stopPropagation(); if (confirm('WARNING!\\nYou are modifying an active question.')==true) {return true;} else {return false;}" : "return true;";
54
  my $extrawarning = $hr->{count} ? "\\nWARNING! It appears someone is signed up for it." : "";
55
  return join "&nbsp;", #$hr->{id},
56
         $h->a ({ href=>"view_class_survey_question.pl?qid=$hr->{qid}&choice=Update", onClick=>$clicky }, "[Edit]"),
57
         $h->a ({ href=>"view_class_survey_question.pl?qid=$hr->{qid}&choice=Copy" }, "[Copy]"),
58
         $h->a ({ href=>"view_class_survey_question.pl?qid=$hr->{qid}&choice=Delete", onClick=>"event.stopPropagation(); if (confirm('Are you sure you want to DELETE this question?$extrawarning')==true) {return true;} else {return false;}" }, "[Delete]")
59
  ;
60
};
61
 
62
 
63
sub modify_required {
64
  my $thing = shift;
65
  return $thing->{required} ? "True" : "False";
66
}
67
 
68
sub modify_private {
69
  my $thing = shift;
70
  return $thing->{private} ? "True" : "False";
71
}
72
 
73
 
74
# Ideally, nothing below this comment needs to change
75
#-------------------------------------------------------------------------------
76
 
77
 
78
our %NAME              = map  { $_ => $COLUMNS{$_}->[0] } keys %COLUMNS;
79
our %colOrderHash      = map  { $_ => $COLUMNS{$_}->[1] } keys %COLUMNS;
80
our %colFilterTypeHash = map  { $_ => $COLUMNS{$_}->[2] } keys %COLUMNS;
81
our @staticFields      = sort byfield grep { $COLUMNS{$_}->[3] eq 'static' } keys %COLUMNS;
82
our @defaultFields     = sort byfield grep { defined $COLUMNS{$_}->[3] } keys %COLUMNS;
83
#our @defaultFields     = grep { $COLUMNS{$_}->[3] eq 'default' or inArray ($_, \@staticFields) } keys %COLUMNS;
84
 
85
our @allFields = sort byfield keys %NAME;
86
our @displayFields = ();
87
our @hideFields = ();
88
my $QUERY_STRING;
89
 
90
my $pagelimit = param ("limit") // $pagelimitoptions[$#pagelimitoptions];
91
my $curpage = param ("page") // 1;
92
 
93
our %FORM;
94
my $FILTER;
95
foreach (param()) {
96
 	if (/^year$/) { #
97
		$YEAR = param($_);
98
		next;
99
	}
100
 
101
	$FORM{$_} = param($_);				# Retrieve all of the FORM data submitted
102
 
103
	if ((/^filter/) and ($FORM{$_} ne '')) {	# Build a set of filters to apply
104
		my ($filter,$field) = split /-/, $_;
105
		$FILTER->{$field} = $FORM{$_} unless notInArray ($field, \@allFields);
106
	}	elsif ($FORM{$_} eq "true")			# Compile list of fields to display
107
		{ push @displayFields, $_; }
108
}
109
 
110
 
111
if (exists $FORM{autoload})	{			# If the FORM was submitted (i.e. the page is being redisplayed),
112
							                    #  	build the data for the cookie that remembers the page setup
113
	my $disFields = join ":", @displayFields;
114
	my $fils = join ":", map { "$_=$FILTER->{$_}" } keys %{$FILTER};
115
 
116
	$QUERY_STRING = $disFields.'&'.$fils.'&'.$FORM{sortby}.'&'.$FORM{autoload};
117
}
118
 
119
 
120
if (!(exists $FORM{autoload}))	{			# No FORM was submitted...
121
	if (my $prefs = cookie ($prefscookie) and !defined param ("ignoreCookie"))	{ # Check for cookies from previous visits.
122
		my ($disF, $filts, $sb, $al) = split /&/,$prefs;
123
		@displayFields = split /:/,$disF;
124
 
125
		foreach my $pair (split /:/, $filts)	{
126
			my ($key, $value) = split /=/, $pair;
127
			$FORM{"filter-$key"} = $value;
128
			$FILTER->{$key} = $value;
129
		}
130
 
131
		$FORM{sortby} = $sb;
132
		$FORM{autoload} = $al;
133
		$QUERY_STRING = $prefs;
134
	}	else {
135
	  @displayFields = @defaultFields; # Otherwise suppply a default list of columns.
136
	  $FORM{autoload} = 1;             # And turn aut0load on by default.
137
	}
138
}
139
 
140
# let's just make sure the columns are in the right order (and there aren't any missing)
141
@displayFields = grep { inArray($_, \@allFields) } sort byfield uniq @displayFields, @staticFields;
142
 
143
# If the field isn't in the displayFields list,	then add it to the hideFields list
144
@hideFields = grep { notInArray ($_, \@displayFields) } @allFields;
145
 
146
# Process any filters provided in the form to pass to the database
147
push @whereClause, map { filter ($_, $FILTER->{$_}) } grep { defined $FILTER->{$_} } @displayFields;
148
#push @whereClause, "year(date) = '$YEAR'";
149
 
150
 
151
							#  Given the fields to display and the where conditions,
152
							#	  "getData" will return a reference to an array of
153
							#	  hash references of the results.
154
my ($data, $datacount) = getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit);
155
my @ProductList = @{ $data };
156
 
157
#my @ProductList = @{ getData (\@displayFields, \@whereClause, $DBTABLE, $FORM{sortby}, $curpage, $pagelimit) };
158
my $x = scalar @ProductList; # How many results were returned?
159
 
160
# If the user is trying to download the Excel file, send it to them and then exit out.
161
if ($FORM{excel}) {
162
  exportExcel (\@ProductList, "RC_Officiating_Shifts");
163
  exit;
164
}
165
 
138 - 166
my $signedOnAs = $username ? "Welcome, $username. ".$h->a ({ href=>"index.pl?LOGOUT" }, "[Log Out]") : "You are not signed in.";
105 bgadell 167
 
168
# Set some cookie stuff...
169
my $path = `dirname $ENV{SCRIPT_NAME}`; chomp $path; $path .= '/' unless $path eq "/";
170
my $queryCookie = cookie(-NAME=>$prefscookie,
171
			-VALUE=>"$QUERY_STRING",
172
			-PATH=>"$path",
173
			-EXPIRES=>'+365d');
174
 
175
# Print the header
176
print header (-cookie=> [ $queryCookie, $RCAUTH_cookie ] );
177
 
178
# 	print "<!-- FORM \n\n";				# Debug code to dump the FORM to a html comment
179
#	print "I'm catching updates!!!\n\n";
180
#	foreach $key (sort (keys %FORM))		#	Must be done after the header is written!
181
# 		{ print "\t$key:  $FORM{$key}\n"; }
182
# 	print "--> \n\n";
183
#
184
#
185
# 	print "<!-- ENV \n\n";				# Debug code to dump the ENV to a html comment
186
# 	foreach $key (sort (keys %ENV))			#	Must be done after the header is written!
187
# 		{ print "\t$key:  $ENV{$key}\n"; }
188
# 	print "--> \n\n";
189
#
190
# 	print "\n\n\n\n<!-- $QUERY_STRING --> \n\n\n\n";
191
 
192
 
193
#------------------
194
 
195
# Toggle the autoload fields within the table elements
196
our ($onClick, $onChange);   # (also used in scanFunctions)
197
my ($radiobutton, $refreshbutton, $sortby);
198
if ($FORM{autoload}) {
199
	$onClick = "onClick='submit();'";
200
	$onChange = "onChange='page.value = 1; submit();'";
201
  $radiobutton = $h->div ({ class=>'autoload' },
202
    ["Autoload Changes: ",
203
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();', checked=>[] }), "On ",
204
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();' }), "Off ",
205
    ]);
206
  $refreshbutton = "";
207
  $sortby = $h->select ({name=>"sortby", onChange=>'submit();' }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
208
} else {
209
  $onClick = "";
210
	$onChange = "onChange='page.value = 1;'";
211
  $radiobutton = $h->div ({ class=>'autoload' },
212
    ["Autoload Changes: ",
213
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>1, onClick=>'submit();' }), "On ",
214
    $h->input ({ type=>"radio", name=>'autoload', class=>'accent', value=>0, onClick=>'submit();', checked=>[] }), "Off ",
215
    ]);
216
  $refreshbutton = $h->input ({ type=>"button", value=>"Refresh", onClick=>"submit(); return false;" });
217
  $sortby = $h->select ({name=>"sortby" }, [ map { $FORM{sortby} eq $_ ? $h->option ({ value=>$_, selected=>[] }, $NAME{$_}) : $h->option ({ value=>$_ }, $NAME{$_}) } @displayFields ]);
218
}
219
 
220
 
221
 
222
 
223
print start_html (-title => $pageTitle, -style => {'src' => $stylesheet} );
224
 
225
print $h->open ('form', { action=>url, method=>'POST', name=>'Req' });
226
print $h->input ({ type=>"hidden", name=>"excel", value=>0 });
227
print $h->div ({ class => "accent pageheader" }, [
228
  $h->h1 ($pageTitle),
229
  $h->div ({ class=>"sp0" }, [
230
    $h->div ({ class=>"spLeft" }, [
231
      $radiobutton
232
    ]),
233
    $h->div ({ class=>"spRight" }, [
234
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
235
      $refreshbutton
236
    ]),
237
  ]),
238
]);
239
 
240
# Print the Hidden fields' check boxes (if there are any)
241
 
242
my $c = 1;
243
my @hiddencheckboxes;
244
my @hiddenrows;
245
foreach my $field (sort { $NAME{$a} cmp $NAME{$b}; } @hideFields) {
246
  if ($FORM{autoload}) {
247
    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} ]);
248
  } else {
249
    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} ]);
250
  }
251
  if ($c++ % 4 == 0) {
252
    push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]);
253
    @hiddencheckboxes = [];
254
  }
255
}
256
push @hiddenrows, $h->div ({ class=>'rTableRow' }, [ @hiddencheckboxes ]) unless --$c % 4 == 0;
257
 
258
 
259
if (scalar @hideFields) {
260
  my @topleft;
261
  push @topleft, $h->div ({ class=>"nowrap" }, "Hidden Columns:");
262
  push @topleft, $h->div ({ class=>'rTable' }, [ @hiddenrows ]);
263
 
264
  print $h->div ({ class=>"sp0" }, [
265
    $h->div ({ class=>"spLeft"  }, [ @topleft ]),
266
    $h->div ({ class=>"spRight" }, [
267
      $signedOnAs
268
    ])
269
  ]);
270
}
271
 
272
# Print the main table...............................................
273
 
274
print $h->open ('div', { class=>'rTable' });
275
 
276
my @tmptitlerow;
277
foreach my $f (@displayFields)	{  # Print the Column headings
278
  if (inArray ($f, \@staticFields)) {
279
    push @tmptitlerow, $h->div ({ class=>'rTableHead' }, [ $h->input ({ type=>"hidden", name=>$f, value=>"true" }), $NAME{$f}, '&nbsp;', $h->input ({ type=>"button", value=>"Add", onClick=>"event.stopPropagation(); window.location.href='view_class_survey_question.pl'" }) ]);
280
#    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}, $LVL >= RollerCon::MANAGER ? "&nbsp;".$h->input ({ type=>"button", value=>"Add", onClick=>"event.stopPropagation(); window.location.href='view_shift.pl'" }) : "" ]);
281
  } else {
282
    if ($FORM{autoload}) {
283
      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} ]);
284
    } else {
285
      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} ]);
286
    }
287
  }
288
}
289
 
290
# Print the filter boxes...
291
print $h->div ({ class=>'rTableHeading' }, [ @tmptitlerow ], [ map { $h->div ({ class=>'rTableCell filters' }, filter ($_)) } @displayFields ], $h->div ({ class=>"rTableCell" }));
292
 
293
# Print the things
294
foreach my $t (@ProductList)	{
295
  no strict;
296
  print $h->div ({ class=>'rTableRow shaded', onclick=>"location.href='view_class_survey_question.pl?qid=$t->{qid}&choice=View'" }, [ map { $h->div ({ class=>'rTableCell' }, exists &{"modify_".$_} ? &{"modify_".$_} ($t) : $t->{$_} ? $t->{$_} : "") } @displayFields ]);
297
#  print $h->div ({ class=>'rTableRow shaded' }, [ map { $h->div ({ class=>'rTableCell' }, exists &{"modify_".$_} ? &{"modify_".$_} ($t) : $t->{$_} ? $t->{$_} : "") } @displayFields ]);
298
}
299
 
300
 
301
 
302
 
303
print $h->close ('div');
304
 
305
# close things out................................................
306
 
307
my $pages = $pagelimit eq "All" ? 1 : int( $datacount / $pagelimit + 0.99 );
308
if ($curpage > $pages) { $curpage = $pages; }
309
 
310
my @pagerange;
311
if ($pages <= 5 ) {
312
  @pagerange = 1 .. $pages;
313
} else {
314
  if ($curpage <= 3) {
315
    @pagerange = (1, 2, 3, 4, ">>");
316
  } elsif ($curpage >= $pages - 2) {
317
    @pagerange = ("<<", $pages-3, $pages-2, $pages-1, $pages);
318
  } else {
319
    @pagerange = ("<<", $curpage-1, $curpage, $curpage+1, ">>");
320
  }
321
}
322
 
323
print $h->br; # print $h->br;
324
print $h->div ({ class=>"sp0" }, [
325
    $h->div ({ class=>"spLeft" }, [
326
      $h->div ({ class=>"footer" }, [
327
        "To bookmark, save, or send this exact view, use the ",
328
        $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
329
        $h->br,
330
        "If this page is displaying oddly, ", $h->a ({ href=>url ()."?ignoreCookie=1" }, "[Reset Your View]"),
331
        $h->br,
332
        $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.]"),
333
        $h->br,
334
        "This page was displayed on ", currentTime (),
335
        $h->br,
336
        "Please direct questions, problems, and concerns to $SYSTEM_EMAIL"
337
      ])
338
    ]),
339
    $h->div ({ class=>"spRight" }, [
340
      $h->h5 ([
341
               "$x of $datacount Record". ($x == 1 ? "" : "s") ." Displayed", $h->br,
342
               "Sorted by ", $sortby, $h->br,
343
               "Displaying ", $h->select ({ name=>"limit", onChange=>"page.value = 1; submit();" }, [ map { $pagelimit == $_ ? $h->option ({ selected=>[] }, $_) : $h->option ($_) } @pagelimitoptions ]), " Per Page", $h->br,
344
               ( $pages > 1 ? ( join " ", map { $_ == $curpage ? "<B>$_</b>" :
345
                                                $_ eq "<<"     ? $h->a ({ onClick=>qq{Req.page.value=1; Req.submit();} }, "$_") :
346
                                                $_ eq ">>"     ? $h->a ({ onClick=>qq{Req.page.value=$pages; Req.submit();} }, "$_") :
347
                                                                 $h->a ({ onClick=>qq{Req.page.value=$_; Req.submit();} }, "[$_]") } @pagerange ) : "" ), $h->br,
348
               $h->input ({ type=>"hidden", name=>"page", value=>$curpage })
349
      ])
350
    ]),
351
]);
352
 
353
#print $h->br; # print $h->br;
354
#print $h->h5 ("$x Record(s) Displayed");
355
#print $h->div ({ class=>"footer" }, [
356
#  "To bookmark, save, or send this exact view, use the ",
357
#  $h->a ({ href=>'', onClick=>"window.document.Req.method = 'GET'; Req.submit(); return false;" }, "[Full URL]"),
358
#  $h->br,
359
#  "This page was displayed on $now",
360
#  $h->br,
361
#  "Please direct questions, problems, and concerns to noone\@gmail.com"
362
#]);
363
 
364
 
365
print $h->close('form');
366
print $h->close('html');