function simplenews_user in Simplenews 6.2
Same name and namespace in other branches
- 5 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 922 - Simplenews node handling, sent email, newsletter block and general hooks
Code
function simplenews_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'register':
$options = $default_value = $hidden = array();
// Determine the newsletters to which a user can choose to subscribe.
// Determine to which other newsletter a user is automatically subscribed.
foreach (simplenews_get_newsletters(variable_get('simplenews_vid', ''), TRUE) as $newsletter) {
$subscribe_new_account = variable_get('simplenews_new_account_' . $newsletter->tid, 'none');
$opt_inout_method = variable_get('simplenews_opt_inout_' . $newsletter->tid, 'double');
if (($subscribe_new_account == 'on' || $subscribe_new_account == 'off') && ($opt_inout_method == 'single' || $opt_inout_method == 'double')) {
$options[$newsletter->tid] = check_plain($newsletter->name);
$default_value[$newsletter->tid] = $subscribe_new_account == 'on' ? $newsletter->tid : 0;
}
else {
if ($subscribe_new_account == 'silent' || $subscribe_new_account == 'on' && $opt_inout_method == 'hidden') {
$hidden[] = $newsletter->tid;
}
}
}
$form = array();
if (count($options)) {
$form['simplenews'] = array(
'#type' => 'fieldset',
'#title' => t('Newsletters'),
'#description' => t('Select the newsletter(s) to which you wish to subscribe.'),
'#weight' => 5,
);
$form['simplenews']['newsletters'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $default_value,
);
}
if (count($hidden)) {
$form['simplenews_hidden'] = array(
'#type' => 'hidden',
'#value' => implode(',', $hidden),
);
}
return $form;
break;
case 'insert':
// suppress login of different groups (e.g. logintoboggan) where mail address is not authenticated yet. Opt in/out settings are ignored in non-anonymous cases.
// The user registration form may contain (hidden) form element to
// subscribe to newsletters. Data of these elements are stored in
// the $account->data variable.
// We subscribe the user according to the (hidden) form elements.
// take over previously anonymous subscribed mail.
$query = "\n SELECT snid\n FROM {simplenews_subscriptions}\n WHERE mail = '%s'";
if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
db_query("\n UPDATE {simplenews_subscriptions}\n SET uid = %d,\n language = '%s'\n WHERE snid = %d", $edit['uid'], $edit['language'], $result->snid);
}
// Process hidden (automatic) subscriptions.
if (isset($edit['simplenews_hidden'])) {
foreach (explode(',', $edit['simplenews_hidden']) as $tid) {
simplenews_subscribe_user($account->mail, $tid, FALSE, 'automatically');
}
}
// Process subscription check boxes.
if (isset($edit['newsletters'])) {
foreach (array_keys(array_filter($edit['newsletters'])) as $tid) {
simplenews_subscribe_user($account->mail, $tid, FALSE, 'website');
$newsletters = simplenews_get_newsletters(variable_get('simplenews_vid', ''), TRUE);
drupal_set_message(t('You have been subscribed to %newsletter.', array(
'%newsletter' => $newsletters[$tid]->name,
)));
}
}
// set inactive if not created by an administrator. this needs a cleaner API.
if (!user_access('administer users')) {
db_query("\n UPDATE {simplenews_subscriptions}\n SET activated = %d\n WHERE uid = %d", 0, $account->uid);
}
break;
case 'login':
// is authenticated user: activate subscription
db_query("\n UPDATE {simplenews_subscriptions}\n SET activated = %d\n WHERE uid = %d", 1, $account->uid);
break;
case 'update':
// always keep uid, mail, language in sync
if ($category == 'account' && $edit['mail']) {
$query = "\n SELECT snid\n FROM {simplenews_subscriptions}\n WHERE uid = %d";
if ($result = db_fetch_object(db_query($query, $account->uid))) {
// user has snid. if user changes mail to previously subscribed anonymous mail, remove subscription.
db_query("\n DELETE FROM {simplenews_subscriptions}\n WHERE mail = '%s'\n AND uid = 0", $edit['mail']);
// migrate current subscription snid to new mail, language.
db_query("\n UPDATE {simplenews_subscriptions}\n SET mail = '%s',\n language = '%s'\n WHERE snid = %d", $edit['mail'], $edit['language'], $result->snid);
}
else {
// no current snid. try taking over subscription via user mail match.
$query = "\n SELECT snid\n FROM {simplenews_subscriptions}\n WHERE mail = '%s'";
if ($result = db_fetch_object(db_query($query, $edit['mail']))) {
db_query("\n UPDATE {simplenews_subscriptions}\n SET uid = %d,\n language = '%s'\n 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)) {
// TODO: since we can't detect if a user is in mail verification process, we always enable
// user subscriptions if user is active. this also matches for unverified users.
// we should depend on a contrib module to detect this state and suppress activation.
$activate = $edit['status'];
db_query("\n UPDATE {simplenews_subscriptions}\n SET activated = %d\n WHERE uid = %d", $activate, $account->uid);
}
}
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("\n SELECT s.snid\n FROM {simplenews_subscriptions} s\n 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("\n UPDATE {simplenews_subscriptions}\n SET uid = 0\n WHERE uid = %d", $account->uid);
}
break;
case 'form':
if ($category == 'newsletter') {
// This is reached only if op=categories access callback passed.
module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
module_load_include('inc', 'simplenews', 'includes/simplenews.subscription');
$form_state = array();
$form = simplenews_subscriptions_account_form($form_state, $account);
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'),
);
// Collect newsletter to which the current user is subscribed.
// 'hidden' newsletters are not listed.
foreach (simplenews_get_newsletters(variable_get('simplenews_vid', '')) 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
AND t.status = %d', $account->uid, $newsletter->tid, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED))) {
$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,
);
}
// Avoid notice.
if (count(element_children($account->content['simplenews'])) == 0) {
$account->content['simplenews']['#children'] = '';
}
}
break;
}
}