Subversion Repositories VORC

Rev

Rev 192 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
192 - 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;
226 - 14
use CGI qw/param header start_html url url_param/;
192 - 15
my $h = HTML::Tiny->new( mode => 'html' );
16
 
17
my %F;
18
 
19
my $cookie_string = authenticate (RollerCon::USER) || 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 = "Missing Hours";
31
my $homeURL = "/schedule/";
32
my $DBTable = "shift";
33
my %FIELDS = (
34
  id          => [qw(ShiftID        5    auto      static )],
35
  dept        => [qw(Department    10    select      required )],
36
  role        => [qw(Role          15    text      required )],
37
  type        => [qw(Type          20    select      manager )],
38
  date        => [qw(Date          25    date        required )],
39
  location    => [qw(Location      30    text      required )],
40
  start_time  => [qw(Start         35    time        required )],
41
  end_time    => [qw(End           40    time        required )],
42
  doubletime  => [qw(DoubleHours   42    switch      manager  )],
43
  assignee_id => [qw(Assignee      50    auto        manager )],
44
  mod_time    => [qw(ModTime       45    number      manager   )],
45
  note        => [qw(Notes         55    textarea       )],
46
);
47
 
48
 
49
my %fieldDisplayName = map  { $_ => $FIELDS{$_}->[0]   } keys %FIELDS;
50
my %fieldType        = map  { $_ => $FIELDS{$_}->[2]   } keys %FIELDS;
51
my @requiredFields   = sort fieldOrder grep { defined $FIELDS{$_}->[3] and $FIELDS{$_}->[3] ne "manager" } keys %FIELDS;
52
my @DBFields   = sort fieldOrder grep { $fieldType{$_} =~ /^(text|select|number|switch|date|time|auto)/ } keys %FIELDS;
53
my @ROFields   = sort fieldOrder grep { $fieldType{$_} =~ /^(readonly)/ } keys %FIELDS;
54
my @MGRFields   = sort fieldOrder grep { $FIELDS{$_}->[3] eq "manager" } keys %FIELDS;
55
my $primary = $DBFields[0];
56
 
57
print header (-cookie=>$RCAUTH_cookie),
58
      start_html (-title => $pageTitle, -style => {'src' => "/style.css"} );
59
 
60
print $h->div ({ class => "accent pageheader" }, [
61
  $h->h1 ($pageTitle),
62
  $h->div ({ class=>"sp0" }, [
63
    $h->div ({ class=>"spLeft" }, [
64
    ]),
65
    $h->div ({ class=>"spRight" }, [
66
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
67
    ]),
68
  ]),
69
]);
70
 
71
my $choice = param ("choice") // "";
72
if ($choice eq "Save") {
73
  process_form ();
226 - 74
} elsif (defined (param ($primary) || url_param ($primary))) {
75
  my $thing = param ($primary); $thing //= url_param ($primary);
192 - 76
  if ($choice eq "Delete") {
77
    delete_item ({ $primary => $thing });
78
  } elsif ($choice eq "Approve") {
79
    decide_hours ({ $primary => $thing }, "approved");
80
  } elsif ($choice eq "Deny") {
81
    decide_hours ({ $primary => $thing }, "denied");
82
  } elsif ($choice eq "Pend") {
83
    decide_hours ({ $primary => $thing }, "request");
84
  } else {
85
    display_form ({ $primary => $thing }, $choice);
86
  }
87
} else {
88
  display_form (); # blank form
89
}
90
 
91
print $h->close ("html");
92
 
93
 
94
sub fieldOrder {
95
  $FIELDS{$a}->[1] <=> $FIELDS{$b}->[1];
96
}
97
 
98
sub saveForm {
99
  my $FTS = shift;
100
 
101
  my $dbh = WebDB::connect ();
102
  if ($FTS->{$DBFields[0]} eq "NEW") {
103
    # Check to make sure the user is requesting hours from one of their volunteer departments.
104
    error ("You need to be a volunteer in the Department that you're requesting hours from.") unless $user->{department}->{$FTS->{dept}} > 0;
105
 
106
    # Check to see if their request overlaps with any existing shifts?
107
    my $conflicts = findConflict ($user->{RCid}, [$FTS->{date}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{id}], "personal");
108
    if ($conflicts) {
109
      error ("You already have a shift that conflicts with that time [$conflicts].");
110
    }
111
 
112
    $dbh->do (
113
      "INSERT INTO $DBTable
114
      (dept,role,type,date,location,start_time,end_time,doubletime,note,assignee_id)
115
      VALUES(?,?,?,?,?,?,?,?,?,?)",
116
      undef,
117
      $FTS->{dept}, $FTS->{role}, "request", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, 0, $FTS->{note}, $user->{RCid}
118
    );
119
    ($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 = ?", undef, $FTS->{dept}, $FTS->{role}, "request", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note});
120
    logit ($RCid, "$username requested missing hours ($FTS->{id}, $FTS->{dept}, $FTS->{role}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time})");
121
  } else {
122
    ($FTS->{assignee_id}) = $dbh->selectrow_array ("select assignee_id from $DBTable where $primary = ?", undef, $FTS->{$primary});
123
    if ($FTS->{assignee_id} != $user->{RCid} and
124
        $user->{department}->{$FTS->{dept}} < RollerCon::MANAGER and
125
        $user->{department}->{VCI} < RollerCon::MANAGER and
126
        $LVL < RollerCon::ADMIN) {
127
      error ("This isn't your request, and you're not a Manager in the $DepartmentNames->{$FTS->{dept}} department!");
128
    }
129
 
130
    if ($FTS->{assignee_id} == $user->{RCid} and
131
        $user->{department}->{$FTS->{dept}} < RollerCon::MANAGER and
132
        $user->{department}->{VCI} < RollerCon::MANAGER and
133
        $LVL < RollerCon::ADMIN) {
134
 
135
      $dbh->do (
136
        "UPDATE $DBTable
137
        SET dept=?, role=?, date=?, location=?, start_time=?, end_time=?, note=?
138
        WHERE id = ?",
139
        undef,
140
        $FTS->{dept}, $FTS->{role}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{note}, $FTS->{id}
141
      );
142
    } else {
143
      $FTS->{type} = "request" unless $FTS->{type};
144
      if ($FTS->{mod_time}) {
145
        $dbh->do (
146
          "UPDATE $DBTable
147
          SET dept=?, role=?, type=?, date=?, location=?, start_time=?, end_time=?, mod_time=?, doubletime=?, note=?
148
          WHERE id = ?",
149
          undef,
150
          $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{mod_time}, $FTS->{doubletime}, $FTS->{note}, $FTS->{id}
151
        );
152
      } else {
153
        $dbh->do (
154
          "UPDATE $DBTable
155
          SET dept=?, role=?, type=?, date=?, location=?, start_time=?, end_time=?, mod_time=null, doubletime=?, note=?
156
          WHERE id = ?",
157
          undef,
158
          $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{doubletime}, $FTS->{note}, $FTS->{id}
159
        );
160
      }
161
    }
162
    logit ($RCid, "$username updated hours request ($FTS->{id}, $FTS->{dept}, $FTS->{role}, $FTS->{type}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time})");
163
  }
164
 
165
  $dbh->disconnect ();   # stored into database successfully.
166
  return $FTS->{id};
167
}
168
 
169
sub delete_item {
170
  my $X = shift;
171
  my $dbh = WebDB::connect ();
172
 
173
  my ($assignee, $hours_dept) = $dbh->selectrow_array ("select assignee_id, dept from $DBTable where $primary = ?", undef, $X->{$primary});
174
  if ($assignee != $user->{RCid} and
175
      $user->{department}->{$hours_dept} < RollerCon::MANAGER and
176
      $user->{department}->{VCI} < RollerCon::MANAGER and
177
      $LVL < RollerCon::ADMIN) {
178
    logit ($RCid, "$username tried to delete hours request ($X->{$primary})");
179
    error ("ERROR: You don't have permission to delete this hours request!");
180
  }
181
  $dbh->do ("delete from $DBTable where $primary = ?", undef, $X->{$primary});
182
  $dbh->disconnect ();
183
  logit ($RCid, "$username deleted hours request ($X->{$primary})");
184
  print "Hours Request Deleted: $X->{$primary}", $h->br;
185
  print &formField ("Cancel", "Back", "POSTSAVE");
186
}
187
 
188
sub decide_hours {
189
  my $X = shift;
190
  my $decision = shift;
191
  my $dbh = WebDB::connect ();
192
 
193
  my ($hours_dept) = $dbh-> selectrow_array ("select dept from $DBTable where id = ?", undef, $X->{$primary});
194
  error ("Hours Request not found [$X->{$primary}]") unless $hours_dept;
195
 
196
  if ($user->{department}->{$hours_dept} < RollerCon::MANAGER and
197
      $user->{department}->{VCI} < RollerCon::MANAGER and
198
      $LVL < RollerCon::ADMIN) {
199
    error ("You're not a Manager in the $DepartmentNames->{$hours_dept} department!");
200
  }
201
 
202
  $dbh->do ("update $DBTable set type = ? where $primary = ?", undef, $decision, $X->{$primary});
203
  $dbh->disconnect ();
204
  logit ($RCid, "$username $decision hours request ($X->{$primary})");
205
  $decision = "pended" if $decision eq "request";
206
  print "Hours ".ucfirst $decision.": $X->{$primary}", $h->br;
207
#  print &formField ("Cancel", "Back", "POSTSAVE");
208
  display_form ({ $primary => $X->{$primary} }, "View");
209
 
210
}
211
 
212
sub select_dept {
213
  my $selection = shift;
214
  my @optionList;
215
 
216
  @optionList = grep { $user->{department}->{$_} >= 1 } keys %{ $user->{department} };
217
 
218
  return $h->select ({ name=>"dept" },
219
    [ map { $selection eq $_ ?
220
              $h->option ({ value=>$_, selected=>[] }, $DepartmentNames->{$_}) :
221
              $h->option ({ value=>$_ }, $DepartmentNames->{$_})
222
          } "", @optionList ]);
223
};
224
 
225
sub select_type {
226
  my $value = shift // "";
227
 
228
  return $h->select ({ name=>"type" },
229
    [ map { $value eq $_ ?
230
              $h->option ({ value=>$_, selected=>[] }, $_) :
231
              $h->option ({ value=>$_ }, $_)
232
          } "", qw(open lead manager selected)]);
233
};
234
 
235
sub display_form  {
236
  my $R = shift;
237
  my $view = shift // "";
238
  my $actionbutton;
239
 
240
  #$view = "View" unless $LVL >= RollerCon::ADMIN;
241
 
242
  if ($view eq "POSTSAVE" and $R->{$primary} eq "NEW") {
243
      print &formField ("Cancel", "Back", "POSTSAVE");
244
      return;
245
  }
246
 
247
  if ($R) {
248
    # we're dealing with an existing thing.  Get the current values out of the DB...
249
    my $dbh = WebDB::connect ();
250
 
251
    @F{@DBFields} = $dbh->selectrow_array (
252
                     "SELECT ". join (", ", @DBFields) ." FROM $DBTable WHERE $primary = ?",
253
                      undef, $R->{$primary});
254
    $dbh->disconnect ();
255
 
256
    # did we find a record?
257
    error ("Cannot find a database entry for '$R->{$primary}'") unless defined $F{$DBFields[0]};
258
 
259
    # If the DB returns a null value, HTML::Tiny doesn't like it, so make sure nulls are converted to empty strings.
260
    map { $F{$_} = "" unless $F{$_} } @DBFields;
261
 
262
    ## Check to make sure the user can actually see the shift
263
    if ($F{assignee_id} != $user->{RCid} and
264
          $user->{department}->{$F{dept}} < RollerCon::MANAGER and
265
          $user->{department}->{VCI} < RollerCon::MANAGER and
266
          $LVL < RollerCon::ADMIN) {
267
      error ("This isn't your request, and you're not a Manager in the $DepartmentNames->{$F{dept}} department!");
268
    }
269
 
270
    if ($view eq "Update") {
271
      # We'd like to update that thing, give the user a form...
272
      print $h->p ("Updating Hours Request: $R->{$primary}...");
273
 
274
      foreach (@DBFields) {
275
        $F{$_} = formField ($_, $F{$_});
276
      }
277
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
278
 
279
      $actionbutton = formField ("choice", "Save");
280
      $actionbutton .= formField ("Cancel");
281
    } else {
282
      # We're just looking at it...
283
      print $h->p ("Viewing Hours Request: $R->{$primary}...");
284
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
285
 
286
      if ($F{assignee_id} == $user->{RCid} and $F{type} eq "request") {
287
        # Users can update or delete their own requests.
288
        $actionbutton = formField ("choice", "Update");
289
        $actionbutton .= formField ("choice", "Delete");
290
      }
291
      if ($user->{department}->{$F{dept}} >= RollerCon::MANAGER or
292
            $user->{department}->{VCI} >= RollerCon::MANAGER or
293
            $LVL >= RollerCon::ADMIN) {
294
        # Department (and VCI) Managers + SysAdmins have more capabilities
295
        $actionbutton = formField ("choice", "Update");
296
        $actionbutton .= formField ("choice", "Pend") unless $F{type} eq "request";
297
        $actionbutton .= formField ("choice", "Approve") unless $F{type} eq "approved";
298
        $actionbutton .= formField ("choice", "Deny") unless $F{type} eq "denied";
299
        $actionbutton .= formField ("choice", "Delete");
300
      }
301
      if ($view eq "POSTSAVE" or $choice eq "View") {
302
        $actionbutton .= formField ("Cancel", "Back", "POSTSAVE");
303
      } else {
304
        $actionbutton .= formField ("Cancel", "Back");
305
      }
306
 
307
      $F{assignee_id} = getUserDerbyName ($F{assignee_id});
308
      $F{dept} = $DepartmentNames->{$F{dept}};
309
      $F{doubletime} = $F{doubletime} ? "TRUE" : "FALSE";
310
    }
311
  } else {
312
    # Display the blank form for a new submission...
313
    print $h->p ("Request missing volunteer hours...");
314
 
315
    if (!scalar grep { $user->{department}->{$_} >= 1 } keys %{ $user->{department} }) {
316
      error ("You need to be a volunteer in at least one department to request missing volunteer hours.");
317
    }
318
 
319
    foreach (@DBFields) {
320
      $F{$_} = formField ($_) unless inArray($_, \@MGRFields);
321
    }
322
    $F{$DBFields[0]} = "NEW".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });
323
 
324
    # Users shouldn't see some of the fields that are reserved for Manager updates.
325
    foreach (@MGRFields) { delete $FIELDS{$_}; }
326
 
327
    $actionbutton = $h->p ({ class=>"hint" }, "Providing extra details of what you were doing or which lead / manager you".$h->br."worked with in the Notes field will help reviewers approve your submission.");
328
    $actionbutton .= formField ("choice", "Save");
329
    $actionbutton .= formField ("Cancel");
330
  }
331
 
332
 
333
  print $h->open ("form", { action => url (), name=>"Req", method=>"POST" });
334
  print $h->div ({ class=>"sp0" },
335
    $h->div ({ class=>"rTable" }, [ map ({
336
      $h->div ({ class=>"rTableRow" }, [
337
        $h->div ({ class=>"rTableCell right top" }, "$fieldDisplayName{$_}: "),
338
        $h->div ({ class=>"rTableCell" }, $F{$_})
339
      ])
340
     } sort fieldOrder keys %FIELDS),
341
   ])
342
  );
343
 
344
  print $actionbutton;
345
  print $h->close ("form");
346
}
347
 
348
sub process_form  {
349
  my %FORM;
350
  foreach (keys %FIELDS) {
351
    if ($fieldType{$_} =~ /^text/) {
352
      $FORM{$_} = WebDB::trim param ($_) // "";
353
    } else {
354
      $FORM{$_} = param ($_) // "";
355
    }
356
  }
357
 
358
  if ($FORM{id} ne "NEW" and
359
        $FORM{assignee_id} != $user->{RCid} and
360
        $user->{department}->{$FORM{dept}} < RollerCon::MANAGER and
361
        $user->{department}->{VCI} < RollerCon::MANAGER and
362
        $LVL < RollerCon::ADMIN) {
363
    error ("This isn't your request, and you're not a Manager in the $DepartmentNames->{$FORM{dept}} department!");
364
  }
365
 
366
  my @errors = ();
367
  # check for required fields
368
  foreach (@requiredFields) {
369
    push @errors, "$fieldDisplayName{$_} is missing." if $FORM{$_} eq "";
370
  }
371
 
372
  if (@errors)   {
373
    print $h->div ({ class=>"error" }, [
374
      $h->p ("The following errors occurred:"),
375
      $h->ul ($h->li (@errors)),
376
      # TO-DO: Give the users a back button?
377
      $h->p ("Please click your Browser's Back button to\n"
378
           . "return to the previous page and correct the problem.")
379
    ]);
380
    return;
381
  }  # Form was okay.
382
 
383
  $FORM{id} = saveForm (\%FORM);
384
 
385
  print $h->p ({ class=>"success" }, "Hours request successfully saved.");
386
 
387
  display_form ({ $primary=>$FORM{id} }, "POSTSAVE");
388
}
389
 
390
sub error {
391
  my $msg = shift;
392
  print $h->p ({ class=>"error" }, "Error: $msg");
393
  print $h->close("html");
394
  exit (0);
395
}
396
 
397
sub formField {
398
  my $name  = shift;
399
  my $value = shift // '';
400
  my $context = shift // '';
401
  my $type = $fieldType{$name} // "button";
402
 
403
  if ($type eq "button") {
404
    if ($name eq "Cancel") {
405
      if ($context eq "POSTSAVE") {
406
        return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"window.location.href = \"shifts.pl\"; return false;" });
407
      } else {
408
        return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"history.back(); return false;" });
409
      }
410
    } else {
411
      if ($value eq "Delete") {
412
        return $h->input ({ type=>"submit", value => $value, name=>$name, onClick=>"return confirm('Are you sure?\\nYou can NOT undelete!')" })
413
      }
414
      return $h->input ({ type=>"submit", value => $value, name=>$name })
415
    }
416
 
417
  } elsif ($type eq "textarea") {
418
    return $h->tag ("textarea", {
419
      name => $name,
420
      override => 1,
421
      cols => 30,
422
      rows => 4
423
    }, $value);
424
 
425
  } elsif ($type eq "select") {
426
    no strict;
427
    return &{"select_".$name} ($value);
428
  } elsif ($type eq "auto") {
429
    return $name eq "assignee_id" ? getUserDerbyName ($value) : $value;
430
  } elsif ($type eq "time") {
431
    return $h->input ({
432
      name => $name,
433
      type => $type,
434
      value => $value,
435
      step => 900,
436
      required => [],
437
      override => 1,
438
      size => 30
439
    });
440
  } elsif ($type eq "number") {
441
    return $h->input ({ name=>$name, type=>"number", value=>$value, step=>.25 });
442
  } elsif ($type eq "switch") {
443
    if ($value) {
444
      return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1, checked=>[] }), $h->span ({ class=>"slider round" })]);
445
    } else {
446
      return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1 }), $h->span ({ class=>"slider round" })]);
447
    }
448
# $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
449
  } else {
450
    use tableViewer;
451
    if (inArray ($name, \@requiredFields)) {
452
      return $h->input ({
453
        name => $name,
454
        type => $type,
455
        value => $value,
456
        required => [],
457
        override => 1,
458
        size => 30
459
      });
460
    } else {
461
      return $h->input ({
462
        name => $name,
463
        type => $type,
464
        value => $value,
465
        override => 1,
466
        size => 30
467
      });
468
    }
469
  }
470
}
471