Subversion Repositories VORC

Rev

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