Subversion Repositories VORC

Rev

Rev 280 | 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
 
280 - 216
  if ($LVL >= 3) {
217
    @optionList = grep { !/^PER$/ } sort keys %{ $DepartmentNames };
218
  } else {
297 - 219
    @optionList = grep { $user->{department}->{$_} >= 1 } keys %{ $user->{department} };
280 - 220
  }
192 - 221
 
222
  return $h->select ({ name=>"dept" },
223
    [ map { $selection eq $_ ?
224
              $h->option ({ value=>$_, selected=>[] }, $DepartmentNames->{$_}) :
225
              $h->option ({ value=>$_ }, $DepartmentNames->{$_})
226
          } "", @optionList ]);
227
};
228
 
229
sub select_type {
230
  my $value = shift // "";
231
 
232
  return $h->select ({ name=>"type" },
233
    [ map { $value eq $_ ?
234
              $h->option ({ value=>$_, selected=>[] }, $_) :
235
              $h->option ({ value=>$_ }, $_)
236
          } "", qw(open lead manager selected)]);
237
};
238
 
239
sub display_form  {
240
  my $R = shift;
241
  my $view = shift // "";
242
  my $actionbutton;
243
 
244
  #$view = "View" unless $LVL >= RollerCon::ADMIN;
245
 
246
  if ($view eq "POSTSAVE" and $R->{$primary} eq "NEW") {
247
      print &formField ("Cancel", "Back", "POSTSAVE");
248
      return;
249
  }
250
 
251
  if ($R) {
252
    # we're dealing with an existing thing.  Get the current values out of the DB...
253
    my $dbh = WebDB::connect ();
254
 
255
    @F{@DBFields} = $dbh->selectrow_array (
256
                     "SELECT ". join (", ", @DBFields) ." FROM $DBTable WHERE $primary = ?",
257
                      undef, $R->{$primary});
258
    $dbh->disconnect ();
259
 
260
    # did we find a record?
261
    error ("Cannot find a database entry for '$R->{$primary}'") unless defined $F{$DBFields[0]};
262
 
263
    # If the DB returns a null value, HTML::Tiny doesn't like it, so make sure nulls are converted to empty strings.
264
    map { $F{$_} = "" unless $F{$_} } @DBFields;
265
 
266
    ## Check to make sure the user can actually see the shift
267
    if ($F{assignee_id} != $user->{RCid} and
268
          $user->{department}->{$F{dept}} < RollerCon::MANAGER and
269
          $user->{department}->{VCI} < RollerCon::MANAGER and
270
          $LVL < RollerCon::ADMIN) {
271
      error ("This isn't your request, and you're not a Manager in the $DepartmentNames->{$F{dept}} department!");
272
    }
273
 
274
    if ($view eq "Update") {
275
      # We'd like to update that thing, give the user a form...
276
      print $h->p ("Updating Hours Request: $R->{$primary}...");
277
 
278
      foreach (@DBFields) {
279
        $F{$_} = formField ($_, $F{$_});
280
      }
281
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
282
 
283
      $actionbutton = formField ("choice", "Save");
284
      $actionbutton .= formField ("Cancel");
285
    } else {
286
      # We're just looking at it...
287
      print $h->p ("Viewing Hours Request: $R->{$primary}...");
288
      $F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });
289
 
290
      if ($F{assignee_id} == $user->{RCid} and $F{type} eq "request") {
291
        # Users can update or delete their own requests.
292
        $actionbutton = formField ("choice", "Update");
293
        $actionbutton .= formField ("choice", "Delete");
294
      }
295
      if ($user->{department}->{$F{dept}} >= RollerCon::MANAGER or
296
            $user->{department}->{VCI} >= RollerCon::MANAGER or
297
            $LVL >= RollerCon::ADMIN) {
298
        # Department (and VCI) Managers + SysAdmins have more capabilities
299
        $actionbutton = formField ("choice", "Update");
300
        $actionbutton .= formField ("choice", "Pend") unless $F{type} eq "request";
301
        $actionbutton .= formField ("choice", "Approve") unless $F{type} eq "approved";
302
        $actionbutton .= formField ("choice", "Deny") unless $F{type} eq "denied";
303
        $actionbutton .= formField ("choice", "Delete");
304
      }
305
      if ($view eq "POSTSAVE" or $choice eq "View") {
306
        $actionbutton .= formField ("Cancel", "Back", "POSTSAVE");
307
      } else {
308
        $actionbutton .= formField ("Cancel", "Back");
309
      }
310
 
311
      $F{assignee_id} = getUserDerbyName ($F{assignee_id});
312
      $F{dept} = $DepartmentNames->{$F{dept}};
313
      $F{doubletime} = $F{doubletime} ? "TRUE" : "FALSE";
314
    }
315
  } else {
316
    # Display the blank form for a new submission...
317
    print $h->p ("Request missing volunteer hours...");
318
 
319
    if (!scalar grep { $user->{department}->{$_} >= 1 } keys %{ $user->{department} }) {
320
      error ("You need to be a volunteer in at least one department to request missing volunteer hours.");
321
    }
322
 
323
    foreach (@DBFields) {
324
      $F{$_} = formField ($_) unless inArray($_, \@MGRFields);
325
    }
326
    $F{$DBFields[0]} = "NEW".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });
327
 
328
    # Users shouldn't see some of the fields that are reserved for Manager updates.
329
    foreach (@MGRFields) { delete $FIELDS{$_}; }
330
 
331
    $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.");
332
    $actionbutton .= formField ("choice", "Save");
333
    $actionbutton .= formField ("Cancel");
334
  }
335
 
336
 
337
  print $h->open ("form", { action => url (), name=>"Req", method=>"POST" });
338
  print $h->div ({ class=>"sp0" },
339
    $h->div ({ class=>"rTable" }, [ map ({
340
      $h->div ({ class=>"rTableRow" }, [
341
        $h->div ({ class=>"rTableCell right top" }, "$fieldDisplayName{$_}: "),
342
        $h->div ({ class=>"rTableCell" }, $F{$_})
343
      ])
344
     } sort fieldOrder keys %FIELDS),
345
   ])
346
  );
347
 
348
  print $actionbutton;
349
  print $h->close ("form");
350
}
351
 
352
sub process_form  {
353
  my %FORM;
354
  foreach (keys %FIELDS) {
355
    if ($fieldType{$_} =~ /^text/) {
356
      $FORM{$_} = WebDB::trim param ($_) // "";
357
    } else {
358
      $FORM{$_} = param ($_) // "";
359
    }
360
  }
361
 
362
  if ($FORM{id} ne "NEW" and
363
        $FORM{assignee_id} != $user->{RCid} and
364
        $user->{department}->{$FORM{dept}} < RollerCon::MANAGER and
365
        $user->{department}->{VCI} < RollerCon::MANAGER and
366
        $LVL < RollerCon::ADMIN) {
367
    error ("This isn't your request, and you're not a Manager in the $DepartmentNames->{$FORM{dept}} department!");
368
  }
369
 
370
  my @errors = ();
371
  # check for required fields
372
  foreach (@requiredFields) {
373
    push @errors, "$fieldDisplayName{$_} is missing." if $FORM{$_} eq "";
374
  }
375
 
376
  if (@errors)   {
377
    print $h->div ({ class=>"error" }, [
378
      $h->p ("The following errors occurred:"),
379
      $h->ul ($h->li (@errors)),
380
      # TO-DO: Give the users a back button?
381
      $h->p ("Please click your Browser's Back button to\n"
382
           . "return to the previous page and correct the problem.")
383
    ]);
384
    return;
385
  }  # Form was okay.
386
 
387
  $FORM{id} = saveForm (\%FORM);
388
 
389
  print $h->p ({ class=>"success" }, "Hours request successfully saved.");
390
 
391
  display_form ({ $primary=>$FORM{id} }, "POSTSAVE");
392
}
393
 
394
sub error {
395
  my $msg = shift;
396
  print $h->p ({ class=>"error" }, "Error: $msg");
397
  print $h->close("html");
398
  exit (0);
399
}
400
 
401
sub formField {
402
  my $name  = shift;
403
  my $value = shift // '';
404
  my $context = shift // '';
405
  my $type = $fieldType{$name} // "button";
406
 
407
  if ($type eq "button") {
408
    if ($name eq "Cancel") {
409
      if ($context eq "POSTSAVE") {
410
        return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"window.location.href = \"shifts.pl\"; return false;" });
411
      } else {
412
        return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"history.back(); return false;" });
413
      }
414
    } else {
415
      if ($value eq "Delete") {
416
        return $h->input ({ type=>"submit", value => $value, name=>$name, onClick=>"return confirm('Are you sure?\\nYou can NOT undelete!')" })
417
      }
418
      return $h->input ({ type=>"submit", value => $value, name=>$name })
419
    }
420
 
421
  } elsif ($type eq "textarea") {
422
    return $h->tag ("textarea", {
423
      name => $name,
424
      override => 1,
425
      cols => 30,
426
      rows => 4
427
    }, $value);
428
 
429
  } elsif ($type eq "select") {
430
    no strict;
431
    return &{"select_".$name} ($value);
432
  } elsif ($type eq "auto") {
433
    return $name eq "assignee_id" ? getUserDerbyName ($value) : $value;
434
  } elsif ($type eq "time") {
435
    return $h->input ({
436
      name => $name,
437
      type => $type,
438
      value => $value,
439
      step => 900,
440
      required => [],
441
      override => 1,
442
      size => 30
443
    });
444
  } elsif ($type eq "number") {
445
    return $h->input ({ name=>$name, type=>"number", value=>$value, step=>.25 });
446
  } elsif ($type eq "switch") {
447
    if ($value) {
448
      return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1, checked=>[] }), $h->span ({ class=>"slider round" })]);
449
    } else {
450
      return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1 }), $h->span ({ class=>"slider round" })]);
451
    }
452
# $F->{department}->{$_} = $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>"DEPT-$_", value=>0, checked=>[] }), $h->span ({ class=>"slider round" })]);
453
  } else {
454
    use tableViewer;
455
    if (inArray ($name, \@requiredFields)) {
456
      return $h->input ({
457
        name => $name,
458
        type => $type,
459
        value => $value,
460
        required => [],
461
        override => 1,
462
        size => 30
463
      });
464
    } else {
465
      return $h->input ({
466
        name => $name,
467
        type => $type,
468
        value => $value,
469
        override => 1,
470
        size => 30
471
      });
472
    }
473
  }
474
}
475