You are here

public function NewsletterSubscriberController::save in Newsletter 7

Same name and namespace in other branches
  1. 7.2 modules/subscriber/includes/newsletter_subscriber.controller.inc \NewsletterSubscriberController::save()

File

includes/newsletter.subscriber.controller.inc, line 50
Controller class definition file for newsletter_subscriber entity.

Class

NewsletterSubscriberController
Newsletter Subscriber Controller

Code

public function save($subscriber, $needs_confirm = FALSE) {
  if (isset($subscriber->uid) && is_numeric($subscriber->uid)) {

    // Make sure that user's email is same with subscriber's,
    // so we can safely assume that this subscriber is same with user.
    $user = user_load($subscriber->uid);
    $subscriber->uid = isset($user->mail) && $subscriber->email == $user->mail ? $user->uid : NULL;
  }
  field_attach_presave('newsletter_subscriber', $subscriber);

  // Make sure to invoke the presave hook.
  module_invoke_all('entity_presave', $subscriber, 'newsletter_subscriber');
  if (!isset($subscriber->nsid)) {
    if (!valid_email_address($subscriber->email)) {
      return drupal_set_message(t('%mail is not a valid e-mail.Please provide a valid e-mail', array(
        '%mail' => $subscriber->email,
      )), 'warning');
    }
    elseif (newsletter_is_subscribed($subscriber->email)) {
      return drupal_set_message(t('%mail is already subscribed', array(
        '%mail' => $subscriber->email,
      )), 'warning');
    }
    $subscriber->created = REQUEST_TIME;
    $subscriber->ip = ip_address();
    $subscriber->hash = drupal_hmac_base64(REQUEST_TIME . $subscriber->email, drupal_get_hash_salt() . ip_address());
    $subscriber->confirmation_timestamp = $needs_confirm ? 0 : REQUEST_TIME;
    drupal_write_record('newsletter_subscriber', $subscriber);
    field_attach_insert('newsletter_subscriber', $subscriber);

    // Make sure to invoke the insert hook.
    module_invoke_all('entity_insert', $subscriber, 'newsletter_subscriber');
  }
  else {
    drupal_write_record('newsletter_subscriber', $subscriber, 'nsid');
    field_attach_update('newsletter_subscriber', $subscriber);

    // Make sure to invoke the update hook.
    module_invoke_all('entity_update', $subscriber, 'newsletter_subscriber');
  }
  if ($needs_confirm && arg(0) != 'admin') {
    $sent = newsletter_create()
      ->sendBasic(1, $subscriber->email);
    $sent ? drupal_set_message(t("Thank you for your subscription. An e-mail has been sent to your address. Please click the provided link to prove you are the owner of this e-mail address and confirm subscription. If you can't find our e-mail please check your spam folder."), 'status') : drupal_set_message(t('Thank you for your subscription.Your subscription was recorded but the confirmation mail fail to sent probably due to a server failure.'), 'warning');
  }
  return $subscriber;
}