You are here

abstract class Notifications_Scheduler_Event in Notifications 7

Notifications Schedule Event class

Unlike regular events, these are full auto-contained events that know which message to produce and how to send it. Thus, these events will be queued in regular Drupal queue.

These events should allow batched execution too. TBD.

Hierarchy

Expanded class hierarchy of Notifications_Scheduler_Event

File

notifications_scheduler/notifications_scheduler.inc, line 16
Drupal Notifications Framework - Default class file

View source
abstract class Notifications_Scheduler_Event extends Notifications_Event {

  // Default system queue name and item if came from queue
  public $queue = 'notifications_event';
  protected $queue_item;

  // Subscriptions counter, this starts with 1 (si the event record is not deleted)
  // And with every process is set to the max sid processed
  public $counter = 1;

  // Subscriptions to be processed on every batch, indexed by sid
  protected $subscriptions;

  // Store action parameters
  protected $action_object;
  protected $accion_context;

  /**
   * Prepare this event to be triggered
   */
  function prepare() {
    parent::prepare();

    // Load the content now, later it may be different
    $this->content = $this
      ->load_content();

    // Build message template so it can be reused later
    if ($this->content && $this
      ->get_template()) {
      $this->dispatch = TRUE;
    }
    else {

      // If something failed we don't even store this event
      $this->dispatch = FALSE;
    }
    return $this;
  }

  /**
   * Prepare event objects from context parameters
   */
  protected abstract function prepare_context();

  /**
   * Load content, to be implemented by subclasses
   */
  protected abstract function load_content();

  /**
   * Set action parameters
   */
  public function set_action($object, $context) {
    $this->action_object = $object;
    $this->action_context = $context;
    $this
      ->prepare_context();
    return $this;
  }

  /**
   * Get property from action context
   */
  public function get_action_context($name = NULL, $default = NULL) {
    if ($name) {
      return isset($this->action_context[$name]) ? $this->action_context[$name] : $default;
    }
    else {
      return $this->action_context;
    }
  }

  /**
   * Create message template to build this event as text
   *
   * The template can be configured per action
   */
  function create_template() {
    $template_name = $this
      ->get_action_context('template');

    // If no template set, we go for the default for this event type
    $template_name = $template_name ? $template_name : $this
      ->get_type('template', 'default');
    return notifications_template($template_name)
      ->set_event($this);
  }

  /**
   * Process event, send pending notifications. Subscriptions start on $counter (min sid)
   *
   * @param $limit
   *   Maximum number of subscriptions to process
   */
  function process($limit = 10) {
    while ($limit && ($subscriptions = $this
      ->get_subscriptions($limit))) {
      $limit = $limit - count($subscriptions);

      // New value for the counter if this goes right
      $counter = max(array_keys($subscriptions));

      // Turn subscriptions into batches, groups, etc...
      $groups = $this
        ->prepare_subscriptions($subscriptions);

      // Now process groups one at a time
      foreach ($groups as $group) {
        $results = $this
          ->process_group($groups);
      }

      // Update counter on the event record
      $this
        ->update_counter($counter);
    }
    if (!empty($counter)) {

      // We will do more processing later
      $this
        ->release();
    }
    else {

      // Nothing to process, delete all this
      $this
        ->delete();
    }
  }

  /**
   * Groups subscriptions. This one will just create a group for all
   */
  protected function prepare_subscriptions($subscriptions) {
    return array(
      $subscriptions,
    );
  }

  /**
   * Process group, add all to a message and send out
   */
  protected function process_group($group) {
    $result = array(
      'messages' => 0,
      'items' => 0,
      'errors' => 0,
    );
    $message = $this
      ->build_message();
    foreach ($group as $item) {
      if ($destination = $item
        ->get_destination()) {
        $message
          ->add_destination($destination);
      }
      else {
        $result['errors']++;
      }
      $result['items']++;
    }
    if ($message
      ->send()) {
      $result['messages']++;
    }
    return $result;
  }

  /**
   * Build query for subscriptions that match this event type
   */
  function query_subscriptions() {

    // This is the condition for scheduled notifications
    return db_select('notifications_subscription', 's')
      ->condition('s.status', Notifications_Subscription::STATUS_ACTIVE)
      ->condition('s.send_interval', -1);
  }

  /**
   * Check user access to event's objects. Default for mass mailing events is TRUE
   */
  public function user_access($account, $op = 'view') {
    return TRUE;
  }

  /**
   * When done these events cannot be just deleted, we need to keep track of last time it was executed
   */
  public function done() {
    $this
      ->record(TRUE);

    // Now take care of previous events of this type / action
    if (variable_get('notifications_event_log', NOTIFICATIONS_EVENT_LOG)) {

      // If logging enabled, make logs of previous events
      $query = db_update('notifications_event')
        ->fields(array(
        'log' => 1,
      ));
    }
    else {

      // If not logging enabled, delete all previous events
      $query = db_delete('notifications_event');
    }
    $query
      ->condition('type', $this->type)
      ->condition('action', $this->action)
      ->condition('eid', $this->eid, '<')
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Notifications_Scheduler_Event::$accion_context protected property
Notifications_Scheduler_Event::$action_object protected property
Notifications_Scheduler_Event::$counter public property
Notifications_Scheduler_Event::$queue public property
Notifications_Scheduler_Event::$queue_item protected property
Notifications_Scheduler_Event::$subscriptions protected property
Notifications_Scheduler_Event::create_template function Create message template to build this event as text
Notifications_Scheduler_Event::done public function When done these events cannot be just deleted, we need to keep track of last time it was executed
Notifications_Scheduler_Event::get_action_context public function Get property from action context
Notifications_Scheduler_Event::load_content abstract protected function Load content, to be implemented by subclasses 1
Notifications_Scheduler_Event::prepare function Prepare this event to be triggered
Notifications_Scheduler_Event::prepare_context abstract protected function Prepare event objects from context parameters 1
Notifications_Scheduler_Event::prepare_subscriptions protected function Groups subscriptions. This one will just create a group for all
Notifications_Scheduler_Event::process function Process event, send pending notifications. Subscriptions start on $counter (min sid)
Notifications_Scheduler_Event::process_group protected function Process group, add all to a message and send out
Notifications_Scheduler_Event::query_subscriptions function Build query for subscriptions that match this event type
Notifications_Scheduler_Event::set_action public function Set action parameters
Notifications_Scheduler_Event::user_access public function Check user access to event's objects. Default for mass mailing events is TRUE 1