You are here

function simplenews_user_insert in Simplenews 7.2

Same name and namespace in other branches
  1. 8.2 simplenews.module \simplenews_user_insert()
  2. 8 simplenews.module \simplenews_user_insert()
  3. 7 simplenews.module \simplenews_user_insert()
  4. 3.x simplenews.module \simplenews_user_insert()

Implements hook_user_insert().

Update uid and preferred language when the new account was already subscribed.

File

./simplenews.module, line 1055
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_user_insert(&$edit, $account, $newsletter) {

  // Use the email address to check if new account is already subscribed.
  $subscriber = simplenews_subscriber_load_by_mail($edit['mail']);

  // If the user is subscribed, we update the subscriber with uid and language.
  if ($subscriber) {
    $subscriber->uid = $edit['uid'];
    $subscriber->language = $edit['language'];
    $subscriber->activated = 1;
    simplenews_subscriber_save($subscriber);
  }

  // Process subscription check boxes.
  if (isset($edit['newsletters'])) {
    $newsletter_ids = array_keys(array_filter($edit['newsletters']));
    if (!empty($newsletter_ids)) {
      $newsletters = simplenews_newsletter_load_multiple($newsletter_ids);
      foreach ($newsletters as $newsletter) {
        simplenews_subscribe($account->mail, $newsletter->newsletter_id, FALSE, 'website', $edit['language']);
        drupal_set_message(t('You have been subscribed to %newsletter.', array(
          '%newsletter' => $newsletter->name,
        )));
      }
    }
  }

  // Process hidden (automatic) subscriptions.
  if (isset($edit['simplenews_hidden'])) {
    foreach (explode(',', $edit['simplenews_hidden']) as $newsletter_id) {
      simplenews_subscribe($account->mail, $newsletter_id, FALSE, 'automatically', $edit['language']);
    }
    unset($edit['simplenews_hidden']);
  }

  // set inactive if not created by an administrator.
  // @todo This needs a cleaner API.
  if (!user_access('administer users')) {

    // this user will be activated on first login (see simplenews_user_login)
    $query = db_update('simplenews_subscriber')
      ->fields(array(
      'activated' => SIMPLENEWS_SUBSCRIPTION_INACTIVE,
    ))
      ->condition('uid', $account->uid)
      ->execute();
  }
}