You are here

class Messaging_Send_Method in Messaging 6.4

Same name and namespace in other branches
  1. 6.3 classes/messaging_method.class.inc \Messaging_Send_Method
  2. 7 messaging.method.inc \Messaging_Send_Method

Sending method, implements all specific method functionality

Old callback functions are

  • send
  • destination

Hierarchy

Expanded class hierarchy of Messaging_Send_Method

1 string reference to 'Messaging_Send_Method'
messaging_send_method in ./messaging.module
Get send method object

File

includes/messaging_method.class.inc, line 159
Drupal Messaging Framework - Send_Method class file

View source
class Messaging_Send_Method extends Messaging_Method {

  // Default type is send (Outgoing + Push)
  public $type = Messaging_Method::TYPE_SEND;

  // Address type for this method
  public $address_type = 'user';

  // Queue and log settings
  public $queue = FALSE;
  public $log = FALSE;

  // Suitable for anonymous users
  public $anonymous = FALSE;

  /**
   * Build send method from info array
   *
   * Some of the array values will be set as properties for the object. Some others won't as they're just
   * for formatting, so they'll be kept only in the $object->info array
   */
  function __construct($method, $info = array()) {
    parent::__construct($method, $info);
    $this
      ->set_properties($info, 'address_type', 'anonymous', 'queue', 'log');

    // Pull methods will have always queueing enabled
    $this->queue = $this->queue || $this->type & self::TYPE_PULL;
  }

  /**
   * Get default method parameters to be merged with the ones passed on with the message
   */
  static function default_params($message = NULL) {
    return array();
  }

  /**
   * Get address info property
   */
  function get_address_info($property = NULL) {
    return messaging_address_info($this->address_type, $property);
  }

  /**
   * Send message to address, use sending callback
   */
  function send_address($address, $message) {
    if ($send_callback = $this
      ->get_info('send callback')) {

      // For this case, we render it before so it is compatible with old send methods
      $message
        ->render();
      $params = $message
        ->get_method_params($this->method) + $this
        ->default_params();
      return call_user_func($send_callback, $address, $message, $params);
    }
    else {
      watchdog('messaging', 'Message could not be delivered for method %method', array(
        '%method' => $this->method,
      ), WATCHDOG_ERROR);
    }
  }

  /**
   * Send message to destination, use sending callback
   */
  function send_destination($destination, $message) {
    return $this
      ->send_address($destination->address, $message);
  }

  /**
   * Get address for user account
   *
   * @param $account
   *   User account object or uid
   */
  function get_user_address($account) {

    // The method may have its own destination callback. If not, default to address type,
    if ($function = $this
      ->get_info('destination callback')) {
      return $function(messaging_user_object($account));
    }
    elseif ($function = $this
      ->get_address_info('user2address callback')) {
      return $function($account);
    }
    elseif ($property = $this
      ->get_address_info('account_property')) {
      return messaging_user_property($account, $property);
    }
  }

  /**
   * Check user access to this method
   */
  function user_access($account) {
    if (!$account->uid && !$this->anonymous) {
      return FALSE;
    }
    if ($permission = $this
      ->get_info('access')) {
      return user_access($permission, $account);
    }
    else {
      return TRUE;
    }
  }

  /**
   * Get address name
   */
  function address_name() {
    if (empty($this->address_name)) {
      if ($name = $this
        ->get_address_info('name')) {
        $this->address_name = $name;
      }
      else {
        $this->address_name = t('Address');
      }
    }
    return $this->address_name;
  }

  /**
   * Get uid for address
   */
  function get_address_uid($address) {
    if ($this->address_type == 'user') {
      return (int) $address;
    }
    elseif ($function = $this
      ->get_address_info('address2uid callback')) {
      return $function($address);
    }
  }

  /**
   * Validate address
   */
  function address_validate($address) {
    if ($function = $this
      ->get_address_info('validate callback')) {
      return $function($address);
    }
    else {

      // The default address will be valid if not empty
      return !empty($address);
    }
  }

  /**
   * Format address for display
   */
  function format_address($address, $html = FALSE) {
    if ($function = $this
      ->get_address_info('format callback')) {
      return $function($address, $html);
    }
    else {
      return check_plain($address);
    }
  }

  /**
   * Prepare message for specific user. Check availability, redirect, etc..
   *
   * Redirecting is only possible when we are sending to a user account, not for anonymous destinations
   */
  function message_user($message, $account = NULL) {
    if ($callback = $this
      ->get_info('user callback')) {
      $account = $account ? $account : $message
        ->get_user();
      call_user_func($callback, $message, $account);
    }
  }

  /**
   * Message processing: Decide on queue, log, cron and send options, prepare parameters
   *
   * At this stage, the message can be still redirected through other sending method, or marked for discard
   */
  function message_prepare($message) {

    // If this method is disabled, mark for discard, log the error
    if (!$this->enabled) {
      $message->discard = TRUE;
      $message->queue = $message->cron = 0;
      $message
        ->set_error(t('The sending method is disabled.'));
    }
    else {

      // It will be queued always for pull methods, cron disabled though so it will wait till it's pulled
      $message->queue = !($this->type & self::TYPE_NOQUEUE) && ($this->type & self::TYPE_PULL || !$message->priority && ($message->queue || $this->queue));
      $message->log = $message->log || $this->log && variable_get('messaging_log', 0);

      // If the messaging method is of type push, cron processing will be enabled
      $message->cron = $message->cron || $message->queue && $this->type & self::TYPE_PUSH;
    }

    // Aditionally we can have a prepare callback
    if ($function = $this
      ->get_info('prepare callback')) {
      $function($message, $this
        ->get_info());
    }
  }

  /**
   * Renders full message with header and body
   *
   * @param $message
   *   Message object
   */
  function message_render($message) {
    if ($function = $this
      ->get_info('render callback')) {
      return $function($message, $this
        ->get_info());
    }
    else {
      return $this
        ->default_render($message, $this
        ->get_info());
    }
  }

  /**
   * Send message to a single destination
   */
  function message_send($message) {
    return $this
      ->send_address($message->destination, $message);
  }

  /**
   * Message default callback: send iterating over all destinations
   *
   * This can be overridden by any sending method that can send multiple messages in a more efficient way.
   */
  function message_multisend($message) {
    $success = $fail = array();
    foreach ($message
      ->get_addresses() as $to) {
      if ($this
        ->send_address($to, $message)) {
        $success[] = $to;
      }
      else {
        $fail[] = $to;
      }
    }

    // If sent, set time. If failed set error
    if ($success) {
      $message->sent = time();
    }
    if ($fail) {
      $destinations = check_plain(implode(', ', $fail));
      $message
        ->set_error("Sending ({$this->method}) to some destinations failed: {$destinations}.");
    }
    return $success && !$fail;
  }

  /**
   * The message has been sent
   */
  function message_aftersend($message) {
  }

  /**
   * Queue message for next delivery
   *
   * By default it is saved to the store, though some sending methods like 'simple' may not consider queueing.
   */
  function message_queue($message) {
    return messaging_store()
      ->message_queue($message);
  }

  /**
   * The message has been queued
   */
  function message_afterqueue($message) {
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Messaging_Method::$description public property
Messaging_Method::$enabled public property
Messaging_Method::$group public property
Messaging_Method::$info public property
Messaging_Method::$method public property
Messaging_Method::$name public property
Messaging_Method::$title public property
Messaging_Method::check_subject static function Converts strings to plain utf-8 single line
Messaging_Method::default_render static function Renders full message with header and body
Messaging_Method::get_info function Get info property
Messaging_Method::set_properties function Set multiple properties from array
Messaging_Method::text_clean static function Clean text of HTML stuff and optionally of line endings
Messaging_Method::text_render static function Composes message from different parts, recursively and applies filter
Messaging_Method::text_truncate static function Truncate messages to given length. Adapted from node_teaser() in node.module
Messaging_Method::TYPE_INCOMING constant
Messaging_Method::TYPE_NOQUEUE constant
Messaging_Method::TYPE_OUTGOING constant
Messaging_Method::TYPE_PULL constant
Messaging_Method::TYPE_PUSH constant
Messaging_Method::TYPE_SEND constant
Messaging_Send_Method::$address_type public property
Messaging_Send_Method::$anonymous public property
Messaging_Send_Method::$log public property
Messaging_Send_Method::$queue public property
Messaging_Send_Method::$type public property Overrides Messaging_Method::$type
Messaging_Send_Method::address_name function Get address name
Messaging_Send_Method::address_validate function Validate address
Messaging_Send_Method::default_params static function Get default method parameters to be merged with the ones passed on with the message
Messaging_Send_Method::format_address function Format address for display
Messaging_Send_Method::get_address_info function Get address info property
Messaging_Send_Method::get_address_uid function Get uid for address
Messaging_Send_Method::get_user_address function Get address for user account
Messaging_Send_Method::message_afterqueue function The message has been queued
Messaging_Send_Method::message_aftersend function The message has been sent
Messaging_Send_Method::message_multisend function Message default callback: send iterating over all destinations
Messaging_Send_Method::message_prepare function Message processing: Decide on queue, log, cron and send options, prepare parameters
Messaging_Send_Method::message_queue function Queue message for next delivery
Messaging_Send_Method::message_render function Renders full message with header and body
Messaging_Send_Method::message_send function Send message to a single destination
Messaging_Send_Method::message_user function Prepare message for specific user. Check availability, redirect, etc..
Messaging_Send_Method::send_address function Send message to address, use sending callback
Messaging_Send_Method::send_destination function Send message to destination, use sending callback
Messaging_Send_Method::user_access function Check user access to this method
Messaging_Send_Method::__construct function Build send method from info array Overrides Messaging_Method::__construct