You are here

abstract class Messaging_Method in Messaging 6.4

Same name and namespace in other branches
  1. 7 messaging.method.inc \Messaging_Method

Base class for all Incoming and Sending methods

Hierarchy

Expanded class hierarchy of Messaging_Method

File

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

View source
abstract class Messaging_Method {

  // Method with push delivery. Messages will be pushed to the user using messaging sending methods.
  const TYPE_PUSH = 1;

  // Method type with pull delivery. Messages will be pulled using messaging pull methods
  const TYPE_PULL = 2;

  // Outgoing method
  const TYPE_OUTGOING = 4;

  // Incoming method
  const TYPE_INCOMING = 8;

  // Disable queueing (sending is more expensive than queueing)
  const TYPE_NOQUEUE = MESSAGING_TYPE_NOQUEUE;

  // Shorthand type: Push + Outgoing
  const TYPE_SEND = MESSAGING_TYPE_SEND;

  // Common properties
  // Method key
  public $method;
  public $type;
  public $title;
  public $name;
  public $description;
  public $group;

  // All sending methods are enabled by default
  public $enabled = TRUE;

  // Remaining info array
  public $info = array();

  /**
   * 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()) {
    $this->method = $method;
    $this->info = $info;
    $this
      ->set_properties($info, 'type', 'title', 'name', 'group', 'description', 'enabled');
  }

  /**
   * Set multiple properties from array
   *
   * @param $data
   *   Array of properties
   * @param $name1, $name2...
   *   Names of properties to set if they're in the array
   */
  function set_properties() {
    $args = func_get_args();
    if ($data = array_shift($args)) {

      // If no parameters passed, set them all
      $properties = $args ? $args : array_keys($data);
      while ($name = array_shift($properties)) {
        if (isset($data[$name])) {
          $this->{$name} = $data[$name];
        }
      }
    }
  }

  /**
   * Get info property
   */
  function get_info($property = NULL) {
    if ($property) {
      return isset($this->info[$property]) ? $this->info[$property] : NULL;
    }
    else {
      return $this->info;
    }
  }

  /**
   * Renders full message with header and body
   *
   * @param $message
   *   Message object
   * @param $info
   *   Sending method info for rendering (glue and filter options)
   */
  static function default_render($message, $info = array()) {
    messaging_include('text.inc');
    messaging_debug('Rendering message', array(
      'message' => $message,
      'info' => $info,
    ));

    // Apply footer prefix if provided and the message has a footer element.
    // Note: If message body is a string the final isset($message['body']['footer']) will be true
    if (!empty($info['footer']) && is_array($message->body) && isset($message->body['footer'])) {
      $message->body['footer'] = array(
        '#prefix' => $info['footer'],
        '#text' => $message->body['footer'],
      );
    }

    // Render separately subject and body info, adding default parameters
    $info += array(
      'glue' => ' ',
      'subject_glue' => ' ',
      'body_format' => NULL,
      'filter' => NULL,
    );
    $message->subject = self::check_subject(self::text_render($message->subject, $info['subject_glue']));
    $message->body = self::text_render($message->body, $info['glue'], $info['body_format'], $info['filter']);
    $message->rendered = TRUE;
    messaging_debug('Rendered message', array(
      'message' => array(
        'subject' => $message->subject,
        'body' => $message->body,
      ),
      'info' => $info,
    ));
    return $message;
  }

  /**
   * Converts strings to plain utf-8 single line
   */
  static function check_subject($text) {
    $text = messaging_text_check_plain($text, NULL);

    // taken from _sanitizeHeaders() in PEAR mail() : http://pear.php.net/package/Mail
    $text = preg_replace('=((0x0A/%0A|0x0D/%0D|\\n|\\r)\\S).*=i', NULL, $text);
    return $text;
  }

  /**
   * Composes message from different parts, recursively and applies filter
   *
   * Filter is applied now only once
   *
   * @param $text
   *   Simple string or array of message parts
   *   It may have named elements like #prefix and #text
   *   or it may be single strings to render straight forward
   * @param $glue
   *   Text to glue all lines together
   * @param $filter
   *   Input format to apply to the results
   */
  static function text_render($text, $glue = '', $format = NULL, $filter = NULL) {
    return messaging_text_render($text, $glue, $format, $filter);
  }

  /**
   * Clean text of HTML stuff and optionally of line endings
   *
   * @param $text
   *   Dirty HTML text to be filtered
   * @param $newline
   *   Optional string to be used as line ending
   */
  static function text_clean($text, $newline = NULL) {
    return messaging_text_clean($text, $newline);
  }

  /**
   * Truncate messages to given length.  Adapted from node_teaser() in node.module
   */
  static function text_truncate($text, $length) {
    return messaging_text_truncate($text, $length);
  }

}

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::$type public property 1
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_Method::__construct function Build send method from info array 1