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