2009-09-09 00:26:12 +00:00
< ?
function db_update_118_post ()
{
global $config ;
$available = " ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789 " ;
$availlen = strlen ( $available ) - 1 ;
$userfields = array ( " salutation " , " firstname " , " lastname " , " email " , " phonehome " , " phonework " , " phonecell " , " fax " );
//grab all the contacts from awards_contacts
2024-12-12 21:24:45 +00:00
$q = $pdo -> prepare ( " SELECT * FROM award_contacts " );
$q -> execute ();
while ( $r = $q -> fetch ( PDO :: FETCH_OBJ )) {
2009-09-09 00:26:12 +00:00
//if its older than the current year, then set them to complete/active because if they were in the
//system then, then they must have beenc omplete and active
//set anyone for the current fair year to complete=no, active = yes, because its not too late to get them
//to login and make sure that all the info is complete
if ( $r -> year < $config [ 'FAIRYEAR' ]) {
$complete = " yes " ;
$active = " yes " ;
}
else {
$complete = " no " ;
$active = " yes " ;
}
//see if a user exists with this email
2024-12-12 21:24:45 +00:00
$uq = $pdo -> prepare ( " SELECT * FROM users WHERE (username=' " . $r -> email . " ' OR email=' " . $r -> email . " ') ORDER BY year DESC LIMIT 1 " ); // AND year='$r->year'");
$uq -> execute ();
if ( $r -> email && $ur = $uq -> fetch ( PDO :: FETCH_OBJ )) {
2009-09-09 00:26:12 +00:00
$user_id = $ur -> id ;
2009-10-20 19:40:55 +00:00
echo " Using existing users.id= $user_id for award_contacts.id= $r->id because email address ( $r->email ) matches \n " ;
2009-09-09 00:26:12 +00:00
//update any info we have thats missing
$sqlset = " " ;
foreach ( $userfields AS $f ) {
//if its NOT in their USER record, but it IS in their AWARD_CONTACTS record, then bring it over, else, assume the users record has priority
if ( ! $ur -> $f && $r -> $f ) {
2024-12-12 21:24:45 +00:00
$sqlset .= " ` $f `=' " . $r -> $f . " ', " ;
2009-09-09 00:26:12 +00:00
}
}
$sql = " UPDATE users SET $sqlset `types`=' { $ur -> types } ,sponsor' WHERE id=' $user_id ' " ;
2024-12-12 21:24:45 +00:00
$stmt = $pdo -> prepare ( $sql );
$stmt -> execute ();
echo $pdo -> errorInfo ();
2009-09-09 00:26:12 +00:00
echo " Updated user record \n " ;
}
else {
//we need a username, if htere's no email, then we need to gerneate one to use.
if ( $r -> email ) {
$username = $r -> email ;
}
else {
$username = " " ;
for ( $x = 0 ; $x < 16 ; $x ++ )
$username .= $available { rand ( 0 , $availlen )};
}
//and create them a password
$password = " " ;
for ( $x = 0 ; $x < 8 ; $x ++ )
$password .= $available { rand ( 0 , $availlen )};
//set passwordset to 0000-00-00 to force it to expire on next login
$sql = " INSERT INTO users (`types`,`username`,`created`,`password`,`passwordset`,` " . implode ( " `,` " , $userfields ) . " `,`year`) VALUES ( " ;
2024-12-12 21:24:45 +00:00
$sql .= " 'sponsor',' " . $username . " ',NOW(),' $password ','0000-00-00' " ;
2009-09-09 00:26:12 +00:00
foreach ( $userfields AS $f ) {
2024-12-12 21:24:45 +00:00
$sql .= " ,' " . $r -> $f . " ' " ;
2009-09-09 00:26:12 +00:00
}
2024-12-12 21:24:45 +00:00
$sql .= " ,' " . $r -> year . " ') " ;
$stmt = $pdo -> prepare ( $sql );
$stmt -> execute ();
echo $pdo -> errorInfo ();
2009-09-09 00:26:12 +00:00
2024-12-12 21:24:45 +00:00
$user_id = $pdo -> lastInsertId ();
2009-09-09 00:26:12 +00:00
//and link it to themselves as a starting record
2024-12-12 21:24:45 +00:00
$stmt = $pdo -> prepare ( " UPDATE users SET uid=' $user_id ' WHERE id=' $user_id ' " );
$stmt -> execute ();
2009-09-09 00:26:12 +00:00
echo " Creating new users.id= $user_id for award_contacts.id= $r->id\n " ;
}
echo " Linking $user_id to users_sponsor record \n " ;
2024-12-12 21:24:45 +00:00
$stmt = $pdo -> prepare ( " INSERT INTO users_sponsor (`users_id`,`sponsors_id`,`sponsor_complete`,`sponsor_active`,`primary`,`position`,`notes`) VALUES (
2009-09-09 00:26:12 +00:00
'".$user_id."' ,
'".$r->award_sponsors_id."' ,
'$complete' ,
'$active' ,
2024-12-12 21:24:45 +00:00
'".$r->primary."' ,
'".$r->position."' ,
'".$r->notes."' ) " );
$stmt -> execute ();
echo $pdo -> errorInfo ();
2009-09-09 00:26:12 +00:00
}
}
?>