Subversion Repositories VORC

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
222 - 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
use strict;
10
use cPanelUserConfig;
11
use WebDB;
12
use HTML::Tiny;
13
use RollerCon;
14
use CGI qw/param header start_html url/;
15
my $h = HTML::Tiny->new( mode => 'html' );
16
 
17
my %F;
18
 
19
my $cookie_string = authenticate (RollerCon::USER) || die;
20
our ($EML, $PWD, $LVL) = split /&/, $cookie_string;
21
my $user = getUser ($EML);
22
$user->{department} = convertDepartments $user->{department};
23
my $username = $user->{derby_name};
24
my $RCid = $user->{RCid};
25
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
26
my $YEAR = 1900 + (localtime)[5];
27
 
28
my $pageTitle = "Change Coach Bio";
29
my $homeURL = "/schedule/";
30
my $DBTable = "coach_bio";
31
my %FIELDS = (
32
	RCid         => [qw(RCid          15    auto      required )],
33
	bio          => [qw(Bio           20    textarea  required )],
34
);
35
 
36
my %fieldDisplayName = map  { $_ => $FIELDS{$_}->[0]   } keys %FIELDS;
37
my %fieldType        = map  { $_ => $FIELDS{$_}->[2]   } keys %FIELDS;
38
my @requiredFields   = sort fieldOrder grep { defined $FIELDS{$_}->[3] } keys %FIELDS;
39
my @DBFields   = sort fieldOrder grep { $fieldType{$_} =~ /^(text|select|number|switch|date|time|auto)/ } keys %FIELDS;
40
my @ROFields   = sort fieldOrder grep { $fieldType{$_} =~ /^(readonly)/ } keys %FIELDS;
41
my $primary = $DBFields[0];
42
 
43
print header (-cookie=>$RCAUTH_cookie),
44
			start_html (-title => $pageTitle, -style => {'src' => "/style.css"} );
45
 
46
print $h->div ({ class => "accent pageheader" }, [
47
  $h->h1 ($pageTitle),
48
  $h->div ({ class=>"sp0" }, [
49
    $h->div ({ class=>"spLeft" }, [ ]),
50
    $h->div ({ class=>"spRight" }, [
51
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
52
    ]),
53
  ]),
54
]);
55
 
56
my %GETFORM = map { split /=/ } split /&/, $ENV{QUERY_STRING};
57
my $choice = param ("choice") // $GETFORM{choice} // "";
58
my $thing  = param ($primary) // $GETFORM{$primary} // "";
59
 
60
if ($choice eq "Save") {
61
	process_form ();
62
} elsif ($thing) {
63
  error ("Unexpected RCid [$thing]") unless $thing =~ /^\d+$/;
64
 
65
  if ($choice eq "Delete") {
66
    delete_item ({ $primary => $thing });
67
  } else {
68
	  display_form ({ $primary => $thing }, $choice);
69
	}
70
} else {
71
  error ("RCid expected. You can't insert a new Coach Bio from here.");
72
	display_form (); # blank form
73
}
74
 
75
print $h->close ("html");
76
 
77
 
78
sub saveForm {
79
  my $FTS = shift;
80
 
81
  my $dbh = WebDB::connect ();
82
#  if ($FTS->{$DBFields[0]} eq "NEW") {
83
 
84
  $dbh->do ("replace into $DBTable (RCid, bio) values (?, ?)", undef, $FTS->{$primary}, $FTS->{bio});
85
  logit ($RCid, "$username edited the Coach Bio for RCid: $FTS->{$primary}");
86
 
87
	$dbh->disconnect ();	 # stored into database successfully.
88
	return $FTS->{$primary};
89
}
90
 
91
sub delete_item {
92
  my $X = shift;
93
  my $dbh = WebDB::connect ();
94
 
95
  $dbh->do ("delete from $DBTable where $primary = ?", undef, $X->{$primary});
96
 
97
  $dbh->disconnect ();
98
  logit ($RCid, "$username deleted Coach Bio ($X->{$primary})");
99
  print "Coach Bio Deleted: $X->{$primary}", $h->br;
100
  print &formField ("Cancel", "Back", "POSTSAVE");
101
}
102
 
103
 
104
sub display_form  {
105
  my $R = shift;
106
  my $view = shift // "";
107
	my $actionbutton;
108
 
109
  if ($view eq "POSTSAVE" and $R->{$primary} eq "NEW") {
110
      print &formField ("Cancel", "Back", "POSTSAVE");
111
      return;
112
  }
113
 
114
  if ($R) {
115
    # we're dealing with an existing thing.  Check to make sure they're actually a coach...
116
    my $coach = getUser ($R->{$primary});
117
    error ("User with $primary [$R->{$primary}] not found.") unless $coach;
118
    error ("You need to either by an Admin or the Coach you're trying to view to see this.") unless $ORCUSER->{RCid} eq $coach->{RCid} or $ORCUSER->{access} >= RollerCon::SYSADMIN;
119
    $coach->{department} = convertDepartments ($coach->{department});
120
    error ("User with $primary [$R->{$primary}] doesn't seem to be a Coach") unless $coach->{department}->{COA} >= RollerCon::USER;
121
 
122
    # Get the current values out of the DB...
123
    my $dbh = WebDB::connect ();
124
	  @F{@DBFields} = $dbh->selectrow_array (
125
                     "SELECT ". join (", ", @DBFields) ." FROM $DBTable WHERE $primary = ?",
126
                      undef, $R->{$primary});
127
	  $dbh->disconnect ();
128
    $F{RCid} = $R->{$primary};
129
 
130
    # If the DB returns a null value, HTML::Tiny doesn't like it, so make sure nulls are converted to empty strings.
131
    map { $F{$_} = "" unless $F{$_} } @DBFields;
132
 
133
    if ($view eq "Update") {
134
      # We'd like to update that thing, give the user a form...
135
      print $h->p ("Updating Coach Bio: ".getUser ($R->{$primary})->{derby_name});
136
 
137
      foreach (@DBFields) {
138
        $F{$_} = formField ($_, $F{$_});
139
      }
140
 
141
      $actionbutton = formField ("choice", "Save");
142
      $actionbutton .= formField ("Cancel");
143
    } else {
144
      # We're just looking at it...
145
      print $h->p ("Viewing Coach Bio: ".getUser ($R->{$primary})->{derby_name});
146
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
147
 
148
      # Put the time fields into the user's preference
149
      map { $F{$_} = convertTime $F{$_} } grep { $fieldType{$_} eq "time" } keys %FIELDS;
150
 
151
      $F{bio} =~ s/\n/<br>/g;
152
 
153
      $actionbutton = formField ("choice", "Update");
154
      if ($view eq "POSTSAVE" or $choice eq "View") {
155
        $actionbutton .= formField ("Cancel", "Back", "POSTSAVE");
156
      } else {
157
        $actionbutton .= formField ("Cancel", "Back");
158
      }
159
    }
160
  }
161
 
162
	print $h->open ("form", { action => url (), name=>"Req", method=>"POST" });
163
	print $h->div ({ class=>"sp0" },
164
	  $h->div ({ class=>"rTable" }, [ map ({
165
      $h->div ({ class=>"rTableRow" }, [
166
        $h->div ({ class=>"rTableCell right top", style=>"font-size:unset;" }, "$fieldDisplayName{$_}: "),
167
        $h->div ({ class=>"rTableCell", style=>"font-size:unset;" }, $F{$_})
168
      ])
169
       } sort fieldOrder keys %FIELDS),
170
   ])
171
  );
172
 
173
  print $actionbutton;
174
  print $h->close ("form");
175
 
176
}
177
 
178
sub process_form  {
179
  my %FORM;
180
  foreach (keys %FIELDS) {
181
  	if ($fieldType{$_} =~ /^text/ and $_ ne "title") {
182
  		$FORM{$_} = WebDB::trim param ($_) // "";
183
  	  $FORM{$_} =~ s/</&lt;/g;
184
      $FORM{$_} =~ s/>/&gt;/g;
185
  	} else {
186
	  	$FORM{$_} = param ($_) // "";
187
  	}
188
  }
189
 
190
  	 # check for required fields
191
	my @errors = ();
192
	foreach (@requiredFields) {
193
		push @errors, "$fieldDisplayName{$_} is missing." if $FORM{$_} eq "" and $FIELDS{$_}->[3] ne "static";
194
	}
195
 
196
	push @errors, "RCid is Missing or Incorrect." unless $FORM{$primary} =~ /^\d+$/;
197
 
198
  if (@errors)	 {
199
    print $h->div ({ class=>"error" }, [
200
  	  $h->p ("The following errors occurred:"),
201
  	  $h->ul ($h->li (@errors)),
202
  	  $h->p ("Please click your Browser's Back button to\n"
203
  	  	   . "return to the previous page and correct the problem.")
204
  	]);
205
  	return;
206
  }	 # Form was okay.
207
 
208
  $FORM{$primary} = saveForm (\%FORM);
209
 
210
	print $h->p ({ class=>"success" }, "Coach Bio successfully saved.");
211
 
212
  display_form ({ $primary=>$FORM{$primary} }, "POSTSAVE");
213
}
214
 
215
sub error {
216
	my $msg = shift;
217
	print $h->p ({ class=>"error" }, "Error: $msg");
218
  print $h->close("html");
219
	exit (0);
220
}
221
 
222
sub fieldOrder {
223
	$FIELDS{$a}->[1] <=> $FIELDS{$b}->[1];
224
}
225
 
226
sub formField {
227
	my $name  = shift;
228
	my $value = shift // '';
229
	my $context = shift // '';
230
	my $type = $fieldType{$name} // "button";
231
 
232
  if ($type eq "button") {
233
		if ($name eq "Cancel") {
234
		  if ($context eq "POSTSAVE") {
235
		    return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"window.location.href = \"users.pl\"; return false;" });
236
		  } else {
237
		    return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"history.back(); return false;" });
238
		  }
239
		} else {
240
			return $h->input ({ type=>"submit", value => $value, name=>$name })
241
		}
242
 
243
	} elsif ($type eq "textarea") {
244
	  return $h->tag ("textarea", {
245
	    name => $name,
246
	    override => 1,
247
			cols => 70,
248
			rows => 15
249
	  }, $value);
250
 
251
  } elsif ($type eq "select") {
252
    no strict;
253
    return &{"select_".$name} ($value);
254
	}	elsif ($type eq "auto") {
255
	  return $value.$h->input ({ type=>"hidden", name=>$name, value=>$value });
256
  }	elsif ($type eq "time") {
257
	  return $h->input ({
258
	    name => $name,
259
	    type => $type,
260
	    value => $value,
261
	    step => 900,
262
	    required => [],
263
	    override => 1,
264
	    size => 30
265
	  });
266
  }	elsif ($type eq "number") {
267
    return $h->input ({ name=>$name, type=>"number", value=>$value, step=>1 });
268
  }	elsif ($type eq "switch") {
269
    if ($value) {
270
      return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1, checked=>[] }), $h->span ({ class=>"slider round" })]);
271
    } else {
272
      return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1 }), $h->span ({ class=>"slider round" })]);
273
    }
274
	}	else {
275
	  use tableViewer;
276
	  if (inArray ($name, \@requiredFields)) {
277
  	  return $h->input ({
278
  	    name => $name,
279
  	    type => $type,
280
  	    value => $value,
281
  	    required => [],
282
  	    override => 1,
283
  	    size => 30
284
  	  });
285
	  } else {
286
  	  return $h->input ({
287
  	    name => $name,
288
  	    type => $type,
289
  	    value => $value,
290
  	    override => 1,
291
  	    size => 30
292
  	  });
293
  	}
294
	}
295
}
296