function simplenews_user in Simplenews 5
Same name and namespace in other branches
- 6.2 simplenews.module \simplenews_user()
- 6 simplenews.module \simplenews_user()
Implementation of hook_user().
Checks whether an email address is subscribed to the newsletter when a new user signs up. If so, changes uid from 0 to the new uid in simplenews_subscriptions so that the user's subscription status is known when he logs in.
File
- ./
simplenews.module, line 534
Code
function simplenews_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
if ($edit['mail']) {
$query = "SELECT snid FROM {simplenews_subscriptions} WHERE mail = '%s'";
if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
db_query("UPDATE {simplenews_subscriptions} SET uid = %d WHERE snid = %d", $edit['uid'], $result->snid);
}
}
break;
case 'update':
if ($category == 'account' && $edit['mail']) {
$query = "SELECT snid FROM {simplenews_subscriptions} WHERE uid = %d";
if ($result = db_fetch_object(db_query($query, $account->uid))) {
db_query("DELETE FROM {simplenews_subscriptions} WHERE mail = '%s' AND uid = %d", $edit['mail'], 0);
db_query("UPDATE {simplenews_subscriptions} SET mail = '%s' WHERE snid = %d", $edit['mail'], $result->snid);
}
else {
$query = "SELECT snid FROM {simplenews_subscriptions} WHERE mail = '%s'";
if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
db_query("UPDATE {simplenews_subscriptions} SET uid = %d WHERE snid = %d", $account->uid, $result->snid);
}
}
}
// Activate/deactivate subscription when account is blocked/unblocked
if ($category == 'account' && isset($edit['status'])) {
if (variable_get('simplenews_sync_account', FALSE)) {
db_query("UPDATE {simplenews_subscriptions} SET a_status = %d WHERE uid = %d", $edit['status'], $account->uid);
}
}
if ($category == 'newsletter' && user_access('subscribe to newsletters')) {
foreach ($edit['newsletters'] as $tid => $checked) {
if ($checked) {
simplenews_subscribe_user($account->mail, $tid, FALSE);
}
else {
simplenews_unsubscribe_user($account->mail, $tid, FALSE);
}
}
}
break;
case 'delete':
if (variable_get('simplenews_sync_account', FALSE)) {
// Delete subscription and all newsletter subscriptions.
// We get the snid straight from the database because user_load()
// can not be used in this stage.
$snid = db_result(db_query("SELECT s.snid FROM {simplenews_subscriptions} s WHERE s.uid = %d", $account->uid));
db_query('DELETE FROM {simplenews_snid_tid} WHERE snid = %d', $snid);
db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $snid);
}
else {
// Only remove uid from subscription data when account is removed
db_query("UPDATE {simplenews_subscriptions} SET uid = 0 WHERE uid = %d", $account->uid);
}
break;
case 'form':
if ($category == 'newsletter' && user_access('subscribe to newsletters')) {
$subscription = simplenews_get_user_subscription($account->mail);
$form = _simplenews_subscription_manager_form($subscription);
$form['subscriptions']['#title'] = t('Current newsletter subscriptions');
unset($form['update'], $form['subscriptions']['mail']);
return $form;
}
break;
case 'categories':
if (user_access('subscribe to newsletters')) {
$output[] = array(
'name' => 'newsletter',
'title' => t('My newsletters'),
'weight' => 10,
);
}
return $output;
case 'view':
global $user;
if ($user->uid == $account->uid || user_access('administer users')) {
$items = array();
$tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
foreach ($tree as $newsletter) {
if (db_num_rows(db_query('SELECT s.uid FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.uid = %d AND t.tid = %d', $account->uid, $newsletter->tid))) {
$subscriptions[] = l($newsletter->name, 'taxonomy/term/' . $newsletter->tid);
}
}
if ($subscriptions) {
$subscriptions = implode(', ', $subscriptions);
}
else {
$subscriptions = t('Currently no subscriptions');
}
// When a user has no permission to subscribe and is not subscribed
// we do not display the 'no subscriptions' message.
if (user_access('subscribe to newsletters') || $subscriptions != t('Currently no subscriptions')) {
$items[] = array(
'class' => 'item',
'title' => t('Current subscriptions'),
'value' => $subscriptions,
);
}
if (user_access('subscribe to newsletters')) {
$items[] = array(
'class' => 'item',
'title' => t('Manage subscriptions'),
'value' => l(t('My newsletters'), 'user/' . $account->uid . '/edit/newsletter'),
);
}
return array(
t('Newsletters') => $items,
);
}
break;
}
}