You are here

class NewsletterNewsletterController in Newsletter 7

Newsletter Newsletter Controller

Hierarchy

Expanded class hierarchy of NewsletterNewsletterController

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

File

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

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

  /**
   *
   */
  public function create($title, $ntid, $custom = FALSE) {
    $newsletter = new stdClass();
    $newsletter->title = $title;
    $newsletter->ntid = $ntid;
    $newsletter->custom = $custom ? 1 : 0;
    $newsletter->send_id = 0;
    $this
      ->saveNew($newsletter);
    return $newsletter;
  }

  /**
   * Saves a new custom newsletter to database.
   *
   * @param $newsletter
   *   A full newsletter object.
   */
  protected function saveNew($newsletter) {
    $max_send_id = db_query('SELECT MAX(send_id) as send_id
      FROM {newsletter_newsletter}
      WHERE title = :title AND ntid = :ntid', array(
      ':title' => $newsletter->title,
      ':ntid' => $newsletter->ntid,
    ))
      ->fetchField();
    if ($max_send_id != '') {
      $newsletter->send_id = ++$max_send_id;
    }
    drupal_write_record('newsletter_newsletter', $newsletter);
    module_invoke_all('entity_insert', $newsletter, 'newsletter_newsletter');
  }

  /**
   * Saves a newsletter to database.
   *
   * @param $newsletter
   *   A full newsletter object.
   */
  public function save($newsletter) {
    if (empty($newsletter->nnid)) {
      return isset($newsletter->title) && isset($newsletter->ntid) ? $this
        ->saveNew($newsletter) : FALSE;
    }
    drupal_write_record('newsletter_newsletter', $newsletter, 'nnid');
    field_attach_update('newsletter_newsletter', $newsletter);
    module_invoke_all('entity_update', $newsletter, 'newsletter_newsletter');
  }

  /**
   *
   */
  public function saveDraft($subscribers, $draft) {
    $drafts = variable_get('newsletter_custom_drafts', array());
    $drafts[$draft->nnid] = array(
      'subscribers' => $subscribers,
      'ntid' => $draft->ntid,
    );
    variable_set('newsletter_custom_drafts', $drafts);
  }
  public function delete($nnid) {
    $newsletter = newsletter_newsletter_load($nnid);
    $deleted = db_delete('newsletter_newsletter')
      ->condition('nnid', $nnid)
      ->execute();
    module_invoke_all('entity_delete', $newsletter, 'newsletter_newsletter');

    // It's necessary to clear the caches when an entity is deleted.
    cache_clear_all();
    $this
      ->resetCache();
    return $deleted ? TRUE : FALSE;
  }
  public function deleteDraft($nnid) {
    $drafts = variable_get('newsletter_custom_drafts', array());
    if (!empty($drafts[$nnid])) {
      unset($drafts[$nnid]);
      variable_set('newsletter_custom_drafts', $drafts);

      // Also delete from database.
      return $this
        ->delete($nnid);
    }
  }

}

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
NewsletterNewsletterController::create public function
NewsletterNewsletterController::delete public function
NewsletterNewsletterController::deleteDraft public function
NewsletterNewsletterController::save public function Saves a newsletter to database.
NewsletterNewsletterController::saveDraft public function
NewsletterNewsletterController::saveNew protected function Saves a new custom newsletter to database.
NewsletterNewsletterController::__construct public function Constructor: sets basic variables. Overrides DrupalDefaultEntityController::__construct