Subversion Repositories VORC

Rev

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

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