Subversion Repositories VORC

Rev

Rev 168 | 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";

use strict;
use cPanelUserConfig;
use WebDB;
use HTML::Tiny;
use RollerCon;
use CGI qw/param header start_html url/;
my $h = HTML::Tiny->new( mode => 'html' );

my %F;

my $cookie_string = authenticate (RollerCon::ADMIN) || die;
our ($EML, $PWD, $LVL) = split /&/, $cookie_string;
my $user = getUser ($EML);
$user->{department} = convertDepartments $user->{department};
my $DepartmentNames = getDepartments ();
my $username = $user->{derby_name};
my $RCid = $user->{RCid};
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
my $YEAR = 1900 + (localtime)[5];


my $pageTitle = "Email Users";
my $homeURL = "/schedule/";

sub emailUsers {
  error ("ERROR: Only SysAdmins can change games.") unless $LVL >= RollerCon::ADMIN;
  
  my ($type, $ID, $subject, $body) = @_;
  
        my @emails;
  my $dbh = WebDB::connect ();  
        if ($type eq "class") {
    push @emails, map { @{$_} } @{ $dbh->selectall_arrayref ("select email from v_class_signup_new join official on v_class_signup_new.RCid = official.RCid where id = ?", undef, $ID) };
        } elsif ($type eq "game") {
          push @emails, map { @{$_} } @{ $dbh->selectall_arrayref ("select email from v_shift_officiating where id = ? and isnull(email) = 0 union select email from v_shift_announcer where id = ? and isnull(email) = 0", undef, $ID, $ID) };
        }
        $dbh->disconnect ();
        
        use RCMailer;
        foreach (@emails) {
          EmailUser ($_, $subject, $body);
          sleep (1);
        }
  print "SENT!";
        return;
}


print header (),
                        start_html (-title => $pageTitle, -style => {'src' => "/style.css"} ); 

print $h->div ({ class => "accent pageheader" }, [
  $h->h1 ($pageTitle),
  $h->div ({ class=>"sp0" }, [
    $h->div ({ class=>"spLeft" }, [
    ]),
    $h->div ({ class=>"spRight" }, [
      $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
    ]),
  ]),
]);

my $SEND = "";
if (param ("choice") eq "Send Email") {
  $SEND = 1;
}

print $h->open ("form", { action => url (), name=>"Req", method=>"POST" });

my $type    = chooseType ();
my $ID      = chooseID   ($type);
printDetails ($type, $ID) unless (!$type or !$ID);
my $subject = setSubject ();
my $body    = setBody ();
my $choice = param ("choice") // "";

print $h->br, $h->br;
if ($SEND) {
  emailUsers ($type, $ID, $subject, $body);
} else {
  print $h->input ({ type=>"button", value => "Cancel" , onClick=>"history.back(); return false;" });
  print " ";
  print $h->input ({ type=>"submit", name=>"choice", value => "Send Email" });
}

print $h->close ("form");
print $h->close ("html");

sub chooseType {
  my $type = param ("type") // "";
  $type = "" unless ($type eq "class" or $type eq "game");
    
  for ("class", "game") {
    my %PROPS = (
      type     => "radio",
      id       => $_,
      name     => "type",
      value    => $_,
      onChange => "Req.ID.value=''; submit();"
    );
    $PROPS{checked} = [] if $type eq $_;
    $PROPS{disabled} = [] if $SEND;
    
    print $h->input (\%PROPS);    
    print $h->label ({for=>$_}, ucfirst $_);
    print $h->br;
  }
  print $h->br;
  
  return $type;
}

sub chooseID {
  my $type = shift // "";
  my $ID = param ("ID") // "";
  $ID = "" unless $ID =~ /^\d+$/;
  
  if (!$type) {
    print $h->div ("Please select either Class or Game.");
    print $h->input ({type=>"hidden", name=>"ID"});
    print $h->br;
    return "";
  } else {
    my $table = $type eq "class" ? "v_class_new" : "v_games";
    my $field = $type eq "class" ? "name" : "teams";
    my $dbh = WebDB::connect ();
    #push @queued_users, map { @{$_} } @{ $qdbh->selectall_arrayref ("select queueid from queue where timestampdiff(minute, last_seen, now()) < 7 and (timestamp <> last_seen or timestampdiff(second, last_seen, now()) <= 60) order by timestamp") };
    my %things = map { $_->[0] => $_->[1] } @{ $dbh->selectall_arrayref ("select distinct id, $field from $table where year(date) = year(now())") };
        $dbh->disconnect ();
        if ($SEND) {
          print $h->div([ucfirst $type.": ", $things{$ID}]);
        } else {
      print $h->div([ucfirst $type.": ", $h->select ({ name=>"ID", onChange=>"submit();" }, [ $h->option (),
        map { $ID eq $_ ? $h->option ({ value=>$_, selected=>[] }, $things{$_}) : $h->option ({ value=>$_ }, $things{$_}) } sort { $things{$a} cmp $things{$b} } keys %things
      ])]);
    }
  }
  print $h->br;
  return $ID;
}

sub printDetails {
  my $type = shift // "";
  my $ID   = shift // "";
  
  error ("No Class or Game Selected") unless $type and $ID;
  
  my $dbh = WebDB::connect ();
  
  my $user_count;
  if ($type eq "class") {
    print map { $_.$h->br } $dbh->selectrow_array ("select id, name, coach, date, dayofweek, time, location from v_class_new where id = ?", undef, $ID);
    ($user_count) = $dbh->selectrow_array ("select count(email) from v_class_signup_new join official on v_class_signup_new.RCid = official.RCid where id = ?", undef, $ID);
    print $user_count." Users".$h->br;
  } elsif ($type eq "game") {
    print map { $_.$h->br } $dbh->selectrow_array ("select id, teams, date, dayofweek, time, track, gtype from v_games where id = ?", undef, $ID);
    ($user_count) = $dbh->selectrow_array ("select count(*) from (select email from v_shift_officiating where id = ? and isnull(email) = 0 union select email from v_shift_announcer where id = ? and isnull(email) = 0) temptable", undef, $ID, $ID);
    print $user_count." Users".$h->br;
  } else {
    error ("Unknown Type: ".$type);
  }
  
        $dbh->disconnect ();
  print $h->br;
}

sub setSubject {
  my $subject = param ("subject") // "";
  
  if ($SEND) {
    print "Subject: ", $h->input ({ type=>"text", name=>"subject", value=>"$subject", size=>30, readonly=>[] });
  } else {
    print "Subject: ", $h->input ({ type=>"text", name=>"subject", value=>"$subject", size=>30 });
  }
  print $h->br;
  
  print $h->br;
  return $subject;
}

sub setBody {
  my $body = param ("body") // "";
  
  if ($SEND) {
    print $h->tag ("textarea", { name=>"body", value=>"$body", cols => 45, rows => 10, readonly=>[] }, $body);
  } else {
    print $h->tag ("textarea", { name=>"body", value=>"$body", cols => 45, rows => 10 }, $body);
  }
  
  return $body;
}


sub error {
        my $msg = shift;
        print $h->p ({ class=>"error" }, "Error: $msg");
  print $h->close("html");
        exit (0);
}