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 Users 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 coverage ($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 derby_name...
      my $extra = scalar @R - scalar @columnlabels;
#      print "fixing a derby name with $extra comma(s)...: @R\n";
      $R[5] = join ", ", @R[5..5+$extra];
      splice @R, 6, $extra;
      $R[5] =~ s/"//g;
#      print "now it's @R\n";
#      exit;
    }
    
    # The last value (birthdate) should be a valid date format.
    #    $R[$#R] = ($R[$#R] =~ /^\d{4}-\d{2}-\d{2}$/) ? $R[$#R] : undef;
    
    for my $i (0..$#R) {
      $R[$i] = undef unless $R[$i];
    }
    
    print "inserting: ", join (" ", @R), "\n" if $DEBUG;
    $sth->execute ( @R ) 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);
}