You are here

class NewsletterSubscriberController in Newsletter 7

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

Newsletter Subscriber Controller

Hierarchy

Expanded class hierarchy of NewsletterSubscriberController

1 string reference to 'NewsletterSubscriberController'
newsletter_entity_info in ./newsletter.module
Implements hook_entity_info().

File

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

View source
class NewsletterSubscriberController extends DrupalDefaultEntityController {
  public function __construct($entityType) {
    parent::__construct('newsletter_subscriber');
  }

  // The Drupal core default controller does not provide a create() method, so
  // we add one. This is a useful place to assign defaults to the properties.
  public function create() {
    $subscriber = new stdClass();
    $subscriber->email = '';
    $subscriber->uid = NULL;
    $subscriber->firstname = NULL;
    $subscriber->lastname = NULL;
    $subscriber->gender = NULL;
    $subscriber->receive_format = NULL;
    $subscriber->ip = NULL;
    $subscriber->confirmed = 0;
    return $subscriber;
  }

  /*
   * Saves a new newsletter subscriber in database.
   *
   * @param $subscriber
   *   An object conatining the following:
   *    email - Required.
   *    uid - Optional.
   *    firstname - Optional.
   *    lastname - Optional.
   *    gender - Optional.
   *    ip - Optional.
   *    confirmed - Required.
   * @param $needs_confirm
   *   Whether a email asking for subscription confirmation should be sent.
   * @return
   *   The full, saved subscriber object.
   */
  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;
  }
  public function delete($nsid) {
    $subscriber = newsletter_subscriber_load($nsid);
    $deleted = db_delete('newsletter_subscriber')
      ->condition('nsid', $nsid)
      ->execute();
    module_invoke_all('entity_delete', $subscriber, 'newsletter_subscriber');
    field_attach_delete('newsletter_subscriber', $subscriber);
    cache_clear_all();
    $this
      ->resetCache();
    return $deleted ? TRUE : FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::buildQuery protected function Builds the query to load the entity. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::load public function Implements DrupalEntityControllerInterface::load(). Overrides DrupalEntityControllerInterface::load
DrupalDefaultEntityController::resetCache public function Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalEntityControllerInterface::resetCache
NewsletterSubscriberController::create public function
NewsletterSubscriberController::delete public function
NewsletterSubscriberController::save public function
NewsletterSubscriberController::__construct public function Constructor: sets basic variables. Overrides DrupalDefaultEntityController::__construct