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