#!/usr/bin/perl

use DBI;

sub getDBHandle() {
    my($dbh,$dbhost,$database,$port,$user,$password);
    #### Default connection string variables
    $database = $_[0];
    $user = "hbcuconnect";
    $password = "KungfuKa4";
    $dbhost = "newdb.hbcuconnect.com";
    $port = "3306"; #### Be default port is 3306 if left blank
    
    if ($database) {
 	$dbh = DBI->connect("DBI:mysql:database=$database;host=$dbhost;port=$port", $user, $password);
    }

    return($dbh);
}

sub is_participant {
  my $sth = $dbh->prepare(q(
SELECT registry_id
  FROM sweepstakes
 WHERE registry_id = ?
));

  $sth->execute( $_[0] );

  my $row = $sth->fetchrow_arrayref();

  $sth->finish();

  if ( $row ) {
    return 1;
  } else {
    return 0;
  }
}

sub update_last_visit {
  my $dbh = shift;
  my $sth = $dbh->prepare(q(
UPDATE employer_data
   SET last_visit = curdate()
 WHERE email = ?
));

  $sth->execute( $_[0] );

  $sth->finish();
}

#############################################################################
sub track_user() {
    #### Call this function when you want to see what a "logged in" user is doing
    my ($dbh,$user_id) = @_;
    my ($sth);
    my $ip_address = $ENV{'REMOTE_ADDR'};
    my $active_url = $ENV{'SCRIPT_NAME'};
    my $browser = $ENV{'HTTP_USER_AGENT'};

    $sql = "INSERT INTO user_tracking SET user_id=?, ip_address=?, active_url=?, browser=?";
    $sth = $dbh->prepare($sql);
    $sth->execute($user_id,$ip_address,$active_url,$browser) || print $q->header() . $sth->errstr();
    $sth->finish();
}

################################################################################
sub get_organization_logo {
  my ($dbh, $org) = @_;

  my $sth = $dbh->prepare(q(
SELECT logo
  FROM organizations
 WHERE organization = ?
));

  $sth->execute( $org );

  my $logo = $sth->fetchrow_hashref;

  $sth->finish;

  return $logo->{logo};
}

1;
