You are here

class Notifications_Message_Template in Notifications 7

Message template. This should be able to produce a full message by itself

Hierarchy

Expanded class hierarchy of Notifications_Message_Template

1 string reference to 'Notifications_Message_Template'
notifications_template in ./notifications.module
Create notifications template object

File

./notifications.template.inc, line 10
Drupal Notifications Framework - Templates

View source
class Notifications_Message_Template extends Messaging_Message_Template {

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

  // Events that triggered this notifications, indexed by eid
  protected $events = array();

  // Event content to be rendered on the notification
  protected $content;

  // Template information
  protected $info;

  /**
   * Construct from template info
   */
  function __construct($info = NULL) {
    $this->info = $info;
  }

  /**
   * Get Message_Object with this template linked
   */
  public function build_message($options = array()) {
    $message = new Notifications_Message($options);
    $message
      ->set_template($this);
    return $message;
  }

  /**
   * Set notifications event
   */
  public function set_event($event) {
    $this->content = $event
      ->get_content();
    $this->objects = $event
      ->get_objects();
    $this->objects['notifications_event'] = $event;
    return $this;
  }

  /**
   * Add event object and its corresponding template
   */
  function add_event($event) {
    $this->events[$event->eid] = $event;
    $this
      ->add_template('event:' . $event->eid, $event
      ->get_template());
  }

  /**
   * Get objects as Drupal objects (Removing Notifications object wrapper)
   */
  function get_objects() {
    $objects = array();
    foreach (parent::get_objects() as $type => $object) {
      $token_type = is_a($object, 'Notifications_Object') ? $object
        ->get_token_type() : $type;
      $object = is_a($object, 'Notifications_Object') ? $object
        ->get_object() : $object;
      $objects[$token_type] = $object;
    }
    return $objects;
  }

  /**
   * Get defaults for elements. Add some elements to message body to make them available for templates
   */

  /*
  protected function element_defaults($name) {
    $defaults = parent::element_defaults($name);
    switch ($name) {
      case 'body':
        $defaults['#parts'][] = 'user_unsubscribe_url';
        break;
    }
    return $defaults;
  }
  */

  /**
   * Default texts for this template, may need token replacement
   */
  protected function default_text($type, $options) {
    switch ($type) {
      case 'subject':
        return $this
          ->text_subject($options);
      case 'header':
        return $this
          ->text_header($options);
      case 'content':
        return $this
          ->text_content($options);
      case 'footer':
        return $this
          ->text_footer($options);
      case 'break':
        return array(
          '#type' => 'messaging_break',
        );
      default:
        return parent::default_text($type, $options);
    }
  }

  /**
   * Subject text
   */
  protected function text_subject($options) {
    return t('Notification for your subscriptions', array(), $options);
  }

  /**
   * Header text
   */
  protected function text_header($options) {
    return array(
      '#tokens' => TRUE,
      '#markup' => t("Greetings [user:name],", array(), $options),
    );
  }

  /**
   * Content text
   */
  protected function text_content($options) {
    return t("A item to which you are subscribed has been updated", array(), $options);
  }

  /**
   * Footer text
   */
  protected function text_footer($options) {
    return array(
      '#tokens' => TRUE,
      //'from' => t('This is an automatic message from !site_name', array('!site_name' => variable_get('site_name', 'Drupal')), $options),

      // For links add markup and plaintext alternatives
      'from' => array(
        '#type' => 'messaging_link',
        '#text' => t('This is an automatic message from [site:name],', array(), $options),
        '#url' => '[site:url]',
      ),
      'unsubscribe' => array(
        '#type' => 'messaging_link',
        '#text' => t('You can unsubscribe at', array(), $options),
        '#url' => '[user:unsubscribe-url]',
      ),
    );
  }

  /**
   * Declare all tokens used for this template
   */
  public function token_list() {

    // Don't forget parent tokens
    $tokens = parent::token_list();
    $tokens[] = 'user:unsubscribe-url';
    $tokens[] = 'user:name';
    return $tokens;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Messaging_Message_Template::$method public property
Messaging_Message_Template::element_defaults protected function Overrides Messaging_Template::element_defaults
Messaging_Message_Template::get_parts protected function Set message elements Overrides Messaging_Template::get_parts
Messaging_Message_Template::set_destination function Set destination (and reset built elements) Overrides Messaging_Message_Render::set_destination
Messaging_Message_Template::set_format public function Set text format Overrides Messaging_Message_Render::set_format
Messaging_Message_Template::set_method public function Set sending method Overrides Messaging_Message_Render::set_method
Messaging_Template::$elements public property
Messaging_Template::$format public property
Messaging_Template::$objects protected property
Messaging_Template::$options protected property
Messaging_Template::$parent protected property
Messaging_Template::$text public property
Messaging_Template::$tokens protected property
Messaging_Template::add_element function Add element ready for drupal_render()
Messaging_Template::add_item function Add item of unknown type
Messaging_Template::add_object function Add object to the list
Messaging_Template::add_string function Add string
Messaging_Template::add_text function Add text object
Messaging_Template::build public function Build all elements, return array
Messaging_Template::build_element public function Build a named element
Messaging_Template::build_parts public function Build template parts
Messaging_Template::build_text protected function Build a message text element
Messaging_Template::default_elements protected function Get default elements
Messaging_Template::element_build protected function Build a message element with optional text replacement
Messaging_Template::element_replace protected function Perform token replace within an element
Messaging_Template::get_element function Get element from elements or default texts
Messaging_Template::get_options function Get options for texts, translations, etc
Messaging_Template::get_text public function Get text element from this template
Messaging_Template::get_tokens function Get tokens for templates
Messaging_Template::render public function Render elements, return string
Messaging_Template::reset public function Reset built elements
Messaging_Template::set_language function Set language
Messaging_Template::set_option function Set options
Messaging_Template::set_options function Set array of options
Messaging_Template::set_parent function Set parent text
Messaging_Template::token_replace public function Do token replacement with this template's objects
Notifications_Message_Template::$content protected property
Notifications_Message_Template::$events protected property
Notifications_Message_Template::$info protected property
Notifications_Message_Template::$subscriptions protected property
Notifications_Message_Template::add_event function Add event object and its corresponding template
Notifications_Message_Template::build_message public function Get Message_Object with this template linked Overrides Messaging_Message_Template::build_message
Notifications_Message_Template::default_text protected function Default texts for this template, may need token replacement Overrides Messaging_Message_Template::default_text 2
Notifications_Message_Template::get_objects function Get objects as Drupal objects (Removing Notifications object wrapper) Overrides Messaging_Template::get_objects
Notifications_Message_Template::set_event public function Set notifications event 1
Notifications_Message_Template::text_content protected function Content text 1
Notifications_Message_Template::text_footer protected function Footer text
Notifications_Message_Template::text_header protected function Header text
Notifications_Message_Template::text_subject protected function Subject text 1
Notifications_Message_Template::token_list public function Declare all tokens used for this template Overrides Messaging_Template::token_list
Notifications_Message_Template::__construct function Construct from template info