Subversion Repositories VORC

Rev

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