You are here

class Notifications_Message in Notifications 7

Same name and namespace in other branches
  1. 6.4 includes/notifications_message.class.inc \Notifications_Message
  2. 6.3 classes/notifications_message.class.inc \Notifications_Message

Wrapper for Notifications messages

Store some more temporary properties on the message that will be dropped if the message is stored

Hierarchy

Expanded class hierarchy of Notifications_Message

File

./notifications.message.inc, line 12
Drupal Notifications Framework - Default class file

View source
class Notifications_Message extends Messaging_Message {

  // Message type, set property
  public $type = 'notifications';

  // Send interval used for grouping
  public $send_interval = 0;

  // Notifications events
  public $events = array();

  // Map events to subscriptions
  public $events_subscriptions = array();

  // Subscriptions that triggered this message, indexed by sid
  public $subscriptions = array();

  // Text parts used to build this, will be useful for debugging
  public $text_parts = array();

  // Digest flag, will be true if this is a digest of multiple events
  public $digest = FALSE;

  // Build method used for this
  public $build_method;

  /**
   * Check parameters and go for alter hook
   */
  protected function do_build() {
    parent::do_build();
    drupal_alter('notifications_message', $this);

    // Clean up Notifications objects so we don't move too much data around (Sometimes the full object gets serialized)
    unset($this->events);
    unset($this->events_subscriptions);
    unset($this->subscriptions);
    unset($this->text_parts);

    // The message must be built, without errors and not for discarding
    return $this
      ->check_status();
  }

  /**
   * Add event to the message. One message can be produced by one or more events.
   */
  public function add_event($event, $subscriptions = array()) {
    $this->events[$event->eid] = $event;
    $this->events_subscriptions[$event->eid] = $subscriptions;
    $this
      ->add_subscriptions($subscriptions);
  }

  /**
   * Add subscriptions
   */
  public function add_subscriptions($subscriptions) {
    foreach ($subscriptions as $sid) {
      if (!isset($this->subscriptions[$sid])) {
        $this->subscriptions[$sid] = $sid;
      }
    }
  }

  /**
   * Set message sender. Depending on options this will set sender account too.
   *
   * Optional sender, if chosen will be the user account who produced the event
   * It will be up to the sending method modules what to do with this information.
   */
  public function set_sender($account) {
    $sender_option = variable_get('notifications_sender', 0);
    if ($sender_option) {
      $sender = is_numeric($account) ? notifications_load_user($account) : $account;
      if ($sender_option == 2) {
        parent::set_sender($sender);
      }
      else {
        $this->sender = 0;
        $this->sender_name = $sender->name;
      }
    }
  }

  /**
   * Digest multiple events in a single message, short format.
   *
   * Was: notifications_process_build_simple()
   *
   * @return array with messages ready to be sent
   */
  public static function build_simple($template, $events, $subscriptions, $module = 'notifications') {
    $messages = array();
    $sender_option = variable_get('notifications_sender', 0);
    foreach ($events as $event) {
      $event_subscriptions = isset($subscriptions[$event->eid]) ? array_filter($subscriptions[$event->eid]) : NULL;
      $message = self::build_simple_message($template, $event, $event_subscriptions, $module);
      $message
        ->set_sender($event->uid);
      $messages[] = $message;
    }
    return $messages;
  }

  /**
   * Creates a single message for a single event
   *
   * @param $account
   *   Destination user account
   * @param $event
   *   Event object which caused this notification
   * @param $subscriptions
   *   Array of subscription ids
   * @param $debug
   *   Return template parts information with the message
   *
   * @return
   *   Message object
   */
  public static function build_simple_message($template, $event, $subscriptions, $module = 'notifications') {
    $send_method = $template->method;
    $destination = $template
      ->get_destination();
    $account = $template
      ->get_user();
    $language = $template
      ->get_language();

    // Start the message by cloning the template
    $message = clone $template;
    $message
      ->add_event($event, $subscriptions);

    // Create message. Do all this in one replacemente
    $parts = array(
      'subject' => $event
        ->message_part('subject', $send_method, $language, $module),
      'header' => $event
        ->message_part('header', $send_method, $language, $module),
      'event' => $event
        ->message_part('main', $send_method, $language, $module),
      'footer' => $event
        ->message_part('footer', $send_method, $language, $module),
    );

    // We pass only the first subscription, which is at least something
    // @ TODO Handle nicely the case where there are more than one subscription
    $subscription = $subscriptions ? notifications_subscription_load(current($subscriptions)) : NULL;
    $objects = array(
      'destination' => $destination,
      'user' => $account,
      'event' => $event,
      'subscription' => $subscription,
    );
    $objects = array_merge($objects, $event
      ->get_objects());
    $text = messaging_template_text_replace($parts, $objects, $language);

    // Get subject out of text and build the message array
    $message->subject = $text['subject'];
    unset($text['subject']);
    $message->body = $text;
    $message->text_parts = $parts;
    return $message;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Messaging_Message::$created public property
Messaging_Message::$deleted public property
Messaging_Message::$destination public property
Messaging_Message::$discard public property
Messaging_Message::$error public property
Messaging_Message::$files public property
Messaging_Message::$key public property
Messaging_Message::$language public property
Messaging_Message::$log public property
Messaging_Message::$method public property
Messaging_Message::$module public property
Messaging_Message::$mqid public property
Messaging_Message::$msid public property
Messaging_Message::$priority public property
Messaging_Message::$process protected property
Messaging_Message::$queue public property
Messaging_Message::$queued public property
Messaging_Message::$redirect public property
Messaging_Message::$result public property
Messaging_Message::$results public property
Messaging_Message::$retry public property
Messaging_Message::$send public property
Messaging_Message::$sender public property
Messaging_Message::$sent public property
Messaging_Message::$status public property
Messaging_Message::$template protected property
Messaging_Message::$text protected property
Messaging_Message::$uid public property
Messaging_Message::$updated public property
Messaging_Message::add_address function Add address or array of addresses as destination
Messaging_Message::add_address_list function Add list of addresses as destinations
Messaging_Message::add_destination function Add destination object
Messaging_Message::add_destination_list function Add destination list, taking care of duplicated destinations
Messaging_Message::add_user function Add user as a destination
Messaging_Message::add_user_list function Add list of users as destinations
Messaging_Message::build_object public static function Build from db object
Messaging_Message::build_template public static function Build from template object
Messaging_Message::check_destination function Get a list of destinations for current method
Messaging_Message::check_status function Check message status, will return FALSE if we should stop processing
Messaging_Message::default_template protected function Get default message template
Messaging_Message::delete public function Delete message from logs and store
Messaging_Message::destination_type function Get list of destinations of this type
Messaging_Message::discard function Discard message
Messaging_Message::dispatch_failed function After the message has been processed with errors
Messaging_Message::dispatch_success function After the message has been processed successfully
Messaging_Message::do_dispatch protected function Check whether the message is to be sent / queued
Messaging_Message::do_prepare protected function Prepare for sending through given method
Messaging_Message::do_queue protected function Queue message using sending method
Messaging_Message::do_render protected function Render through sending method
Messaging_Message::do_send protected function Send message through sending method
Messaging_Message::ERROR constant
Messaging_Message::ERROR_DESTINATION constant
Messaging_Message::get_body function Get rendered body
Messaging_Message::get_content function Get message content rendered
Messaging_Message::get_destinations function Get destinations for sending with current method
Messaging_Message::get_files function Get message files
Messaging_Message::get_language function Get language object
Messaging_Message::get_params function Get generic parameters
Messaging_Message::get_results function Get sending results
Messaging_Message::get_sender function Get sender parameters or single property
Messaging_Message::get_sender_account function Get sender account if uid present
Messaging_Message::get_sender_name function Get sender name
Messaging_Message::get_subject function Get rendered subject
Messaging_Message::get_template function Get message template for current method
Messaging_Message::get_text function Get template text, prepared for this method
Messaging_Message::get_user function Get user, if the message is intended for a single user
Messaging_Message::invoke_hook protected function Invoke hook_messaging_message() on all modules
Messaging_Message::load public static function Load message by id
Messaging_Message::load_multiple public static function Load multiple events
Messaging_Message::log public function Log message
Messaging_Message::message_debug protected function Debug facility
Messaging_Message::message_log protected function Log facility
Messaging_Message::prepare_element protected static function Build text parts for drupal_render()
Messaging_Message::process protected function Run operations on message for current method if not done before
Messaging_Message::process_done protected function Mark operation as done for the current method
Messaging_Message::process_result protected function Return process result for method and operation
Messaging_Message::process_step protected function Run operations on message for current method if not done before
Messaging_Message::process_todo protected function Check whether an operation is still to do for the current method
Messaging_Message::queue public function Queue message using the messaging store.
Messaging_Message::record public function Create or update message record
Messaging_Message::render public function Render for current method
Messaging_Message::save public function Save message to store $name
Messaging_Message::send public function Send message through sending method.
Messaging_Message::send_all public function Send message through all sending methods
Messaging_Message::send_method public function Get send method object
Messaging_Message::set_error function Set error condition and stop processing
Messaging_Message::set_method function Set send method
Messaging_Message::set_results function Mark as sent for this destination key or for all
Messaging_Message::set_status function Set success status and return status check
Messaging_Message::set_template function Set message template
Messaging_Message::set_user function Set user, if the message is intended for a single user try to find suitable method
Messaging_Message::STATUS_BUILD constant
Messaging_Message::STATUS_DISPATCH constant
Messaging_Message::STATUS_ERROR constant
Messaging_Message::STATUS_NONE constant
Messaging_Message::STATUS_PREPARE constant
Messaging_Message::STATUS_PROCESS constant
Messaging_Message::STATUS_QUEUE constant
Messaging_Message::STATUS_SEND constant
Messaging_Message::STATUS_SENT constant
Messaging_Message::__construct function Constructor, with predefined array of data
Messaging_Message::__set public function Magic method, properly set some variables
Messaging_Message::__toString public function
Notifications_Message::$build_method public property
Notifications_Message::$digest public property
Notifications_Message::$events public property
Notifications_Message::$events_subscriptions public property
Notifications_Message::$send_interval public property
Notifications_Message::$subscriptions public property
Notifications_Message::$text_parts public property
Notifications_Message::$type public property
Notifications_Message::add_event public function Add event to the message. One message can be produced by one or more events.
Notifications_Message::add_subscriptions public function Add subscriptions
Notifications_Message::build_simple public static function Digest multiple events in a single message, short format.
Notifications_Message::build_simple_message public static function Creates a single message for a single event
Notifications_Message::do_build protected function Check parameters and go for alter hook Overrides Messaging_Message::do_build
Notifications_Message::set_sender public function Set message sender. Depending on options this will set sender account too.