science-ation/db/db.update.76.php

88 lines
2.2 KiB
PHP

<?
function db_update_76_pre()
{
/*
* Find all users that exist multiple times and merge them, fixing the
* types link. Right now this can only happen with committee members
* and volunteers
*/
$q = $pdo->prepare('SELECT DISTINCT username FROM users WHERE 1');
$q->execute();
while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
$user = $r['username'];
if ($user == '')
continue;
$qq = $pdo->prepare("SELECT * FROM users WHERE username=?");
$qq->execute([$user]);
if ($qq->rowCount() <= 1)
continue;
/* Fix $user */
/* Load all their data */
while ($rr = $qq->fetch(PDO::FETCH_ASSOC)) {
$types = explode(',', $rr['types']);
foreach ($types as $t) {
$u[$t] = $rr;
}
}
/* Make sure we have what we think we have */
$cid = intval($u['committee']['id']);
$vid = intval($u['volunteer']['id']);
if ($cid == 0 || $vid == 0) {
echo "\n\n\nDATABASE ERROR: User $user exists multiple
times, but I was unable to fix it. Please visit
www.sfiab.ca and send us an email so we can help sort
out your database. It is likely that user $user will
experience problems logging in
";
continue;
}
/* Copy everything into the committee entry */
$fields = array('firstname', 'lastname', 'username', 'password',
'email',
'phonehome', 'phonework', 'phonecell', 'fax', 'organization',
'address', 'address2', 'city', 'province', 'postalcode');
$query = "`types`='committee,volunteer'";
foreach ($fields as $f) {
if ($u['committee'][$f] == '' && $u['volunteer'][$f] != '') {
$v = $u['volunteer'][$f];
$query .= ",`$f`='$v'";
}
}
$query = "UPDATE users SET ? WHERE id=?";
echo "$query\n";
$stmt = $pdo->prepare($query);
$stmt->execute([$query,$cid]);
/* Now fix the volunteers links */
$query = "UPDATE volunteer_positions_signup SET users_id=? WHERE users_id=?";
echo "$query\n";
$stmt = $pdo->prepare($query);
$stmt->execute([$cid,$vid]);
/*
* The user_volunteer table is empty, we should just delete it,
* no need to update it
*/
/* Delete the old user */
$query = "DELETE FROM users WHERE id=?";
echo "$query\n";
$stmt = $pdo->prepare($query);
$stmt->execute([$vid]);
}
}
?>