Subversion Repositories VORC

Rev

Rev 196 | Rev 229 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/perl

# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)
my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";
close STDERR;
open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";
#warn "Redirecting errors to ${error_log_path}vorc_error.log";

#if ($ENV{SHELL}) { die "This script shouldn't be executed from the command line!\n"; }

use strict;
use cPanelUserConfig;
use CGI qw/param cookie header start_html url/;
use HTML::Tiny;
use tableViewer;
use RollerCon;
use DateTime;
use DateTime::Duration;
our $h = HTML::Tiny->new( mode => 'html' );

my $cookie_string = authenticate (RollerCon::USER) || die;
our ($EML, $PWD, $LVL) = split /&/, $cookie_string;
my $RCAUTH_cookie = CGI::Cookie->new (-name=>'RCAUTH', -value=>"$cookie_string", -expires=>"+30m");
my $now = DateTime->now (time_zone => 'America/Los_Angeles');

my $pageTitle = "Shift Management";
our $DBTABLE = 'v_shift';
my %COLUMNS = (
# colname   =>  [qw(DisplayName       N    type     status)],   status ->  static | default | <blank>
  id          => [qw(ID             5    number         )],
  dept        => [qw(Department    10    select       )],
  date        => [qw(Date          15    date        default )],
  dayofweek   => [qw(Day           17    select      )],
  time        => [qw(Time          20    text        default )],
  start_time  => [qw(Start         25    text         )],
  end_time    => [qw(End           30    text         )],
  mod_time    => [qw(ModTime       35    number         )],
  doubletime  => [qw(DoubleTime    37    boolean         )],
  volhours    => [qw(VolHours      40    number         )],
  role        => [qw(Role          45    text      default )],
  type        => [qw(Type          50    select      default )],
  location    => [qw(Location      55    select      default )],
  note        => [qw(Notes         60    text        default )],
  RCid        => [qw(RCID          65    text         )],
  derby_name  => [qw(Assignee      70    select      default   )],
);

my @whereClause;
# Users should only see shifts within their own departments (except VCI sees all)
if ($LVL < 5 and $ORCUSER->{VCI} < 2) {
  my $string = "dept in (".join ",", map { '"'.$_.'"' } grep { $ORCUSER->{department}->{$_} >= 1 } keys %{$ORCUSER->{department}};
  $string .= ")";
  push @whereClause, $string;
}
push @whereClause, "dept != 'PER'";

# If we need to modify line item values, create a subroutine named "modify_$columnname"
#    It will receive a hashref to the object lineitem

sub modify_doubletime {
  my $thing = shift;
  return $thing->{doubletime} ? "True" : "False";
}

sub modify_id {
  my $hr = shift;
  return $hr->{id} unless $LVL >= RollerCon::ADMIN;
  if ($hr->{dept} eq "COA" and $hr->{location} =~ /MVP/) {
    return $h->a ({ href=>"view_class.pl?id=".getClassID ($hr->{id})."&choice=Update" }, "[Edit Class]");
  } else {
    my $clicky = $hr->{RCid} ? "event.stopPropagation(); if (confirm('WARNING!\\nYou are modifying a shift that someone has signed up for.')==true) {return true;} else {return false;}" : "return true;";
    my $extrawarning = $hr->{RCid} ? "\\nWARNING! It appears someone is signed up for it." : "";
    return join "&nbsp;", #$hr->{id},
           $h->a ({ href=>"view_shift.pl?id=$hr->{id}&choice=Update", onClick=>$clicky }, "[Edit]"),
           $h->a ({ href=>"view_shift.pl?id=$hr->{id}&choice=Copy" }, "[Copy]"),
           $h->a ({ href=>"view_shift.pl?id=$hr->{id}&choice=Delete", onClick=>"event.stopPropagation(); if (confirm('Are you sure you want to DELETE this shift?$extrawarning')==true) {return true;} else {return false;}" }, "[Delete]")
    ;
  }
};

my $DEPTS = getDepartments;
sub modify_dept {
  my $hr = shift;
  $hr->{orig_dept} = $hr->{dept};
  $hr->{dept} = $DEPTS->{$hr->{dept}};
}

sub filter_dept {
  my $colName = shift;
  my $filter = shift;
  
  if (defined $filter)  {
    if ($filter eq "-blank-") {
      return "($colName = '' or isNull($colName) = 1)";
    }
    return "$colName = \"$filter\"";
  } else {
    my $tmpFormValue = param ("filter-${colName}");
    my $categories = join "", map { $tmpFormValue eq $_ ? $h->option ({ value=>$_, selected=>[] }, $DEPTS->{$_}) : $h->option ({ value=>$_ }, $DEPTS->{$_}) } grep { $LVL > 4 or exists $ORCUSER->{department}->{$_} } grep { !/^PER$/ } sort keys %{$DEPTS};
    my $Options = "<OPTION></OPTION>".$categories;
    
    $Options =~ s/>($tmpFormValue)/ selected>$1/;
    
    my $autoload = param ("autoload") // 1;
    my $onChange = $autoload ? "onChange='page.value = 1; submit();'" : "";
    return "<SELECT name=filter-${colName} $onChange>$Options</SELECT>";
  }
}

sub modify_derby_name {
  my $t = shift;
  my $dept = $t->{orig_dept} // $t->{dept};
  
  if ($t->{derby_name}) {
    if ($ORCUSER->{department}->{$dept} >= RollerCon::LEAD or $LVL >= RollerCon::ADMIN) {
      $t->{derby_name} = $h->a ({ href=>"/schedule/view_user.pl?RCid=$t->{RCid}" }, $t->{derby_name});
    } else {
      $t->{derby_name} = "FILLED" unless getUser($t->{RCid})->{showme};
      return $t->{derby_name};
    }
  }
  
  if ($t->{type} eq "request" or $t->{type} eq "approved" or $t->{type} eq "denied") {
    # Don't let anyone change the assignee for an Hours Request.
    return $t->{derby_name};
  }
  
  if ($dept eq "COA" and $t->{location} =~ /MVP/) {
    return $LVL >= RollerCon::ADMIN ? $t->{derby_name} . " | " . $h->a ({ href=>"view_class.pl?id=".getClassID ($t->{id})."&choice=Update" }, "[Edit Class]") : $t->{derby_name};
  }
  
  my ($yyyy, $mm, $dd) = split /\-/, $t->{date};
  my $cutoff = DateTime->new(
        year => $yyyy,
        month => $mm,
        day => $dd,
        hour => 5,
        minute => 0,
        second => 0,
        time_zone => 'America/Los_Angeles'
  );
  
  if (($t->{RCid} == $ORCUSER->{RCid} and $t->{type} ne "selected" and $now < $cutoff) or ($t->{derby_name} and ($ORCUSER->{department}->{$dept} >= 2 or $LVL >= 5))) {
    # DROP
    $t->{derby_name} = "$t->{derby_name} <A HREF='#' onClick=\"event.stopPropagation(); if (confirm('Really? You want to drop this person from the shift?')==true) { window.open('make_shift_change.pl?change=del&RCid=$t->{RCid}&id=$t->{id}','Confirm Shift Change','resizable,height=260,width=370'); return false; }\">[DROP]</a>";
    if ($ORCUSER->{department}->{$dept} >= 2 or $LVL > 4) {
      # NO SHOW
      $t->{derby_name} .= " | <A HREF='#' onClick=\"event.stopPropagation(); if (confirm('Really? They were a no show?')==true) { window.open('make_shift_change.pl?noshow=true&change=del&RCid=$t->{RCid}&id=$t->{id}','Confirm Shift Change','resizable,height=260,width=370'); return false; }\">[NO SHOW]</a>";
    }
  } elsif (!$t->{derby_name}) {
    if (findConflict ($ORCUSER->{RCid}, $t->{id})) {
      $t->{derby_name} .= "*schedule conflict*";
    } elsif ($dept eq "EMT" and !$ORCUSER->{emt_verified}) {
      $t->{derby_name} .= "*needs verification*";
    } elsif (signUpEligible ($ORCUSER, $t, "vol") and $now < $cutoff) {
      # SIGN UP
      $t->{derby_name} = "<A HREF='#' onClick=\"event.stopPropagation(); window.open('make_shift_change.pl?change=add&RCid=$ORCUSER->{RCid}&id=$t->{id}','Confirm Shift Change','resizable,height=260,width=370'); return false;\">[SIGN UP]</a>";
    }
    if ($ORCUSER->{department}->{$dept} >= 2 or $LVL > 4) {
      # ADD USER
      $t->{derby_name} ? $t->{derby_name} .= " | " : {};
      $t->{derby_name} .= "<A HREF='#' onClick=\"event.stopPropagation(); window.open('make_shift_change.pl?change=lookup&RCid=$ORCUSER->{RCid}&id=$t->{id}','Confirm Shift Change','resizable,height=260,width=370'); return false;\">[ADD USER]</a>";
    }
  }
  return $t->{derby_name};
}

sub modify_time {
  my $t = shift;
  return convertTime $t->{time};
}

sub modify_start_time {
  my $t = shift;
  return convertTime $t->{start_time};
}

sub modify_end_time {
  my $t = shift;
  return convertTime $t->{end_time};
}

sub modify_volhours {
  my $t = shift;
  if ($t->{type} eq "request") {
    return "<i>TBD</i>";
  } else {
    return $t->{volhours};
  }
}

# If we need to modify how a filter works, create a subroutine named "filter_$columnname"
#    It will receive two fields, the field name and the current filter value (if any)

# Uncomment and update if we want to enable clicking on a row to open a new page.
#
sub addRowClick {
  my $t = shift;
  
  if ($t->{type} eq "request" or $t->{type} eq "approved" or $t->{type} eq "denied") {
    return "location.href='missing_hours.pl?id=$t->{id}'";
  } else {
    return "location.href='view_shift.pl?id=$t->{id}'";
  }
}

# Call the function to print the table view page (with available options)
printTablePage ({ Title   => $pageTitle,
                  Table   => $DBTABLE,
                  Columns => \%COLUMNS,
                  Where   => join (" and ", @whereClause),
                  RCAuth  => $RCAUTH_cookie,
                  DisplayYearSelect => 1,
                  ShowMyShifts  => 1,
                  HighlightShifts => 1,
                  PersonalTimeButton => 1,
                  HeaderButton => { field  => "id",
                                    button => $h->input ({ type=>"button", value=>"Add", onClick=>"event.stopPropagation(); window.location.href='view_shift.pl'" }) }
                 });