You are here

function simplenews_user_insert in Simplenews 7

Same name and namespace in other branches
  1. 8.2 simplenews.module \simplenews_user_insert()
  2. 8 simplenews.module \simplenews_user_insert()
  3. 7.2 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 873
Simplenews node handling, sent email, newsletter block and general hooks

Code

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

  // 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'])) {
    $nl_tids = array_keys(array_filter($edit['newsletters']));
    if (!empty($nl_tids)) {
      $newsletters = simplenews_categories_load_multiple($nl_tids, array(
        'show_all' => TRUE,
      ));
      foreach ($newsletters as $newsletter) {
        simplenews_subscribe_user($account->mail, $newsletter->tid, FALSE, 'website', $edit['language']);
        drupal_set_message(t('You have been subscribed to %newsletter.', array(
          '%newsletter' => _simplenews_newsletter_name($newsletter),
        )));
      }
    }
  }

  // Process hidden (automatic) subscriptions.
  if (isset($edit['simplenews_hidden'])) {
    foreach (explode(',', $edit['simplenews_hidden']) as $tid) {
      simplenews_subscribe_user($account->mail, $tid, 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();
  }
}