Subversion Repositories VORC

Rev

Rev 58 | Rev 119 | 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";

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

my $cookie_string = authenticate(3) || die;
my ($EML, $PWD, $LVL) = split /&/, $cookie_string;
my $user = getUser($EML);
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");

print header (-cookie=>$RCAUTH_cookie);

#foreach (sort keys %ENV) {
#       print "$_: $ENV{$_}\n<br>";
#}
my $change = param ('change');
my $RCid   = param ('RCid') // $user->{RCid};
my $MVPid     = param ('MVPid');

my $targetUser = getUser $RCid;

print start_html (-title => "vORC Update MVP Ticket Match", -style => {'src' => "/style.css"}), $h->open ("body");

if ($change eq "lookup") {
    
  print $h->form ({ action=>url }, [
    $h->input ({ type=>"hidden", name=>"change", value=>"add" }),
    $h->input ({ type=>"hidden", name=>"RCid", value=>$RCid }),
    $h->p ("Match ticket to User ($RCid):"),
    $h->input ({ type=>"text", name=>"MVPid", id=>"pickticket" }),
    $h->input ({ type=>"submit", value=>"Save", onClick=>"if (document.getElementById('pickticket').value === '') { return false; }" }),
    $h->button ({ onClick=>"window.close();" }, "Cancel")
  ]);

} else {
  print $h->p ("Making an MVP Ticket change...");

  my $target = $targetUser->{derby_name};
  print $h->p ("So, <b>$user->{derby_name}</b>, you'd like to <b>$change</b> an MVP Ticket for <b>$target</b>: <b>$RCid</b>...");

  my $change_err = changeTicket($change, $RCid, $MVPid);
  my $closer;
  if ($change_err) {
    print $change_err;
    if ($change_err =~ /^WARNING/) {
          print $h->form ({ action=>url }, [
            $h->input ({ type=>"hidden", name=>"change", value=>"override" }),
                $h->input ({ type=>"hidden", name=>"MVPid", value=>$MVPid }),
                $h->input ({ type=>"hidden", name=>"RCid", value=>$RCid }),
                $h->input ({ type=>"submit", value=>"OVERRIDE", onClick=>"if (confirm('Are you sure you want to override the warning?')==true) { return true } else { return false }" }),
          ]);
    }
    print $h->br, $h->button ({ onClick=>"window.close();" }, "Close");
  } else {
    print "<br>This window will close automatically in 3 seconds.";
    $closer = 'setTimeout(() => { window.close(); }, 3000);';
  }

  print<<tail;
  <SCRIPT language="JavaScript">
  <!--
                function reloadParent() {
        window.opener.location.reload();
        $closer
                }
                
                reloadParent();
  //-->
  </SCRIPT>
tail
}
print $h->close ("body"), $h->close ("html");

sub changeTicket {
  my $change = shift // "";
  my $rcid   = shift // "";
  my $mvpid  = shift // "";
  return "ERROR: Missing Details to make the change" unless $change and $rcid and $mvpid;
  
  my $dbh = getRCDBH;
  my $sth;
  
  if ($change eq "Delete" or $change eq "override") {
    if ($change ne "override") {
      my ($badidea) = $dbh->selectrow_array ("select id from v_ticket where RCid = ? and email = vorc_email and full_name = vorc_full_name", undef, $rcid);
      return "WARNING: This user's email and full name match the MVP ticket. (It will likely re-match.)" if $badidea;
    }
    
    $sth = $dbh->prepare ("delete from RCid_ticket_link where RCid = ? and MVPid = ?");
    
  } else {
    my ($taken) = $dbh->selectrow_array ("select RCid from RCid_ticket_link where MVPid = ? or RCid = ?", undef, $mvpid, $rcid);
    return "ERROR ($taken): Either that user or that ticket is already matched." if $taken;

    my ($exists) = $dbh->selectrow_array ("select id from ticket where id = ?", undef, $mvpid);
    return "ERROR: That MVP Ticket doesn't seem to exist ($mvpid)." unless $exists;
    
    $sth = $dbh->prepare ("insert into RCid_ticket_link (RCid, MVPid) values (?, ?)");

  }
  
  if ($sth->execute ($rcid, $mvpid)) {
    logit ($user->{RCid}, "Updated MVP Ticket: $rcid $change $mvpid");
    logit ($rcid, "MVP Ticket Change: $user->{derby_name} $change");
    print "SUCCESS!";
    ## Send the user an email...
    sendEmail ($targetUser) if $change eq "add";
    return;
  } else {
    return "ERROR: Something failed. Maybe this will help:". $sth->errstr();
  }  
}


sub sendEmail {
        use RCMailer;
        my $data = shift;

        my $email = $data->{email};
        my $subject = "RollerCon MVP Ticket Added to Account";
        my $body = $h->p (
    "Greetings,",
    "Your account to sign up for MVP Classes at RollerCon with the following information:",

    $h->ul ([
      $h->li ("Derby Name: $data->{derby_name}", "Real Name:    $data->{real_name}", "Email Address: $data->{email}")
      ]),

    "Has been activated!",

    "You are now able to log in to view and sign up for classes (after June 16th)!  YAAAAAYYYY!!!!!  (Imagine Kermit the Frog doing muppet hands here.)",

    $h->a ({ href=>"https://volunteers.rollercon.com/schedule/classes.pl" }, "Take me to the classes!"),

    "Please note that you are limited to signing up for a limited number of classes.",

    "If you've already signed up for your limit of classes, but another class REALLY strikes your fancy, try dropping one of your current classes.  That should allow you to pick up a different one.",

    "The number of classes you're allowed to sign up for starts at two, but may increase as we get closer to RollerCon.",

    "If you're new to using vORC, you may want to read this:",

    $h->a ({ href=>"https://rollercon.com/about/mvp-class-reg/how-to-vorc/" }, "How to VORC"),

    "-RollerCon Management",
    "(this is an automated message, please don't reply)"
  );

        # send the message
        EmailUser($email, $subject, $body);
}