function simplenews_user in Simplenews 6
Same name and namespace in other branches
- 5 simplenews.module \simplenews_user()
- 6.2 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 710 - Simplnews node handling, sent email, newsletter block and general hooks
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, language = '%s' WHERE snid = %d", $edit['uid'], $edit['language'], $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', language = '%s' WHERE snid = %d", $edit['mail'], $edit['language'], $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, language = '%s' WHERE snid = %d", $account->uid, $account->language, $result->snid);
}
}
}
// Activate/deactivate subscription when account is blocked/unblocked
if ($category == 'account' && isset($edit['status'])) {
if (variable_get('simplenews_sync_account', TRUE)) {
db_query("UPDATE {simplenews_subscriptions} SET activated = %d WHERE uid = %d", $edit['status'], $account->uid);
}
}
if ($category == 'newsletter' && (user_access('subscribe to newsletters', $account) || user_access('administer users'))) {
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', TRUE)) {
// Delete subscription and all newsletter subscriptions when account is removed.
// We don't use simplenews_get_subscription() here because the user is already
// deleted from the {user} table.
$snid = db_result(db_query("SELECT s.snid FROM {simplenews_subscriptions} s WHERE s.mail = '%s'", $account->mail));
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') {
$subscription = simplenews_get_subscription($account);
$form = _simplenews_subscription_manager_form($subscription);
$form['subscriptions']['#title'] = t('Current newsletter subscriptions');
unset($form['update'], $form['subscriptions']['mail']);
return $form;
}
break;
case 'categories':
// Newsletter tab with custom permission check.
$output[] = array(
'name' => 'newsletter',
'title' => t('My newsletters'),
'weight' => 10,
'access callback' => 'simplenews_subscription_edit_access',
);
return $output;
break;
case 'view':
global $user;
if ($user->uid == $account->uid || user_access('administer users')) {
$account->content['simplenews'] = array(
'#type' => 'user_profile_category',
'#title' => t('Newsletters'),
);
$tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
foreach ($tree as $newsletter) {
if (db_result(db_query('SELECT COUNT(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 (isset($subscriptions)) {
$subscriptions = implode(', ', $subscriptions);
}
else {
$subscriptions = t('None');
}
// 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', $account) || $subscriptions != t('None')) {
$account->content['simplenews']['subscriptions'] = array(
'#type' => 'user_profile_item',
'#title' => t('Current subscriptions'),
'#value' => $subscriptions,
);
}
if (user_access('subscribe to newsletters', $account)) {
$account->content['simplenews']['my_newsletters'] = array(
'#type' => 'user_profile_item',
'#value' => t('Manage <a href="!url">my subscriptions</a>', array(
'!url' => url('user/' . $account->uid . '/edit/newsletter'),
)),
'#weight' => -1,
);
}
}
break;
}
}