Subversion Repositories PEEPS

Rev

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

#!/usr/bin/perl

use strict;
use PEEPS;
use WebDB;

my $dbh = WebDB::connect ();  
my $DEBUG = 1;

my $inputfile = shift // "";

if ($inputfile) {
        process_file ($inputfile);
} else {
        error ("No input file specified.");
}

$dbh->disconnect ();



sub process_file  {
        my $uploadedfile = shift or error ("No file passed to 'process_file()'");
        my @errors = ();
  
  logit (0, "Bulk Uploaded PEEPS User Affiliations from file $uploadedfile");
  
  print "Processing file [$uploadedfile]...\n" if $DEBUG;
  open(INPUT, $uploadedfile) or error ("Could not open file [$uploadedfile]!");
  
  my @columnlabels = split /,/, <INPUT>;
  @columnlabels = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } @columnlabels;
  error ("File not read!") unless scalar @columnlabels;
#  pop @columnlabels;
    
#  my @AcceptableColumns = qw(event_name order_date ticket_type id first_name last_name email derby_name);
#  @columnlabels = @AcceptableColumns;

  my $fields = join ", ", @columnlabels;
  my $values = join ", ", map { '?' } 0..$#columnlabels;
  my $sth = $dbh->prepare ("replace into role ($fields) values ($values)");
  
  while (<INPUT>) {
    chomp;
    
    my @R = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } split /,/;
    
    if (scalar @R != scalar @columnlabels) {
      # there's an extra field, likely from commas in the member_org_name...
      my $extra = scalar @R - scalar @columnlabels;
#      print "fixing a derby name with $extra comma(s)...: @R\n";
      $R[2] = join ", ", @R[2..2+$extra];
      splice @R, 3, $extra;
      $R[2] =~ s/"//g;
#      print "now it's @R\n";
#      exit;
    }
    
    for my $i (0..$#R) {
      $R[$i] = undef unless $R[$i];
    }
    
    ($R[2]) = $dbh->selectrow_array ("select id from organization where league_name = ?", undef, $R[2]);
    next unless $R[2];
    
    foreach my $role (split /\|/, $R[1]) {
      print "inserting: ", join (" ", $R[0], $role, $R[2]), "\n" if $DEBUG;
      $sth->execute ( $R[0], $role, $R[2] ) or error($sth->errstr);
    }
  }
  print "DONE!\n\n" if $DEBUG;
}


sub error {
        my $msg = shift;
        logit (0, "Problem with Bulk MVP Pass upload: ".$msg);
        print "Error: $msg\n";
        exit (1);
}