You are here

notifications_template.class.inc in Notifications 6.3

Notification Template classes

File

classes/notifications_template.class.inc
View source
<?php

/**
 * @file
 * Notification Template classes
 */

/**
 * Notifications Template
 */
class Notifications_Template extends Messaging_Template {

  // Module to try for templates before notifications
  public $module;
  public $user = NULL;
  public $destination = NULL;

  // Single event or multiple events
  public $event;
  public $events;

  // Subscriptions indexed by $event->eid
  public $subscriptions;

  // Sender uid and options
  public $sender = 0;
  public $sender_option = 0;

  // Whether this is a test so message won't be sent
  public $test;

  // Whether this has already been built
  public $built = FALSE;

  /**
   * Class constructor, create a template of given type
   */
  function __construct($type, $parent = NULL, $method = NULL, $language = NULL, $module = 'notifications') {
    parent::__construct($type, $parent, $method, $language);
    if ($parent) {
      $this->module = $parent->module;
    }
    else {
      $this->module = $module;
      $this->objects['notifications'] = NULL;
    }
  }

  /**
   * Build notifications message
   */
  function build($method = NULL, $language = NULL) {

    // If building for the first time give a chance to modules for altering this
    if (!$this->built) {
      drupal_alter('notifications_template', $this);
      $this->built = TRUE;
    }
    $method = $method ? $method : $this->method;
    $language = $language ? $language : $this
      ->get_language();
    $build = array(
      'template' => $this,
      'method' => $method,
      'language' => $language->language,
      'test' => $this->test,
    );
    $message = new Notifications_Message($build);
    $message
      ->set_sender($this->sender, $this->sender_option);
    $message
      ->set_user($this->user, $this->destination);
    if (isset($this->subscriptions)) {
      $message->notifications['subscriptions'] = $this->subscriptions;
    }
    if (isset($this->events)) {
      $message->notifications['events'] = $this->events;
    }
    return $message;
  }

  /**
   * Get body parts, in this case they are 'header', 'main', 'footer'
   *
   * @see Messaging_Message_Template
   *
   * @param $method
   *   Sending method
   * @param $language
   *   Language code
   * @return string or array()
   *   Body text or text parts for renderin
   */
  function get_body($method = NULL, $language = NULL) {
    $body = array();
    foreach (array(
      'header',
      'main',
      'footer',
    ) as $key) {
      $body[$key] = $this
        ->get_text($key, $method, $language);
    }
    return $body;
  }

  /**
   * Set information from processing parameters
   */
  function set_params($params) {
    $this->method = $params->send_method;
    $this->test = $params->test;
    $this
      ->set_account($params->account);
    $this->language = $params->language;
  }

  /**
   * Set destination user account
   *
   * This will handle notification's fake accounts with a destination set
   */
  function set_account($account) {
    $this->destination = isset($account->destination) ? $account->destination : NULL;
    $this->user = $account;
  }

  /**
   * Get child template object for event
   */
  function get_event_template($event, $method = NULL, $language = NULL) {
    return $this
      ->create_event_template($event, $method, $language, $this->module, $this);
  }

  /**
   * Get child template of type digest
   */
  function get_digest_template($name, $method = NULL, $language = NULL) {
    return $this
      ->get_template('digest', $name, $method, $language);
  }

  /**
   * Create template object for event
   */
  static function create_event_template($event, $method = NULL, $language = NULL, $module = 'notifications', $parent = NULL) {
    $parts[] = $event->type;
    if (!empty($event->subtype)) {
      $parts[] = $event->subtype;
    }
    $parts[] = $event->action;
    $name = implode('-', $parts);
    if ($template = self::create_template('event', $name, $method, $language, $module, $parent)) {
      $template->event = $event;
      $template->sender = $event->uid;

      // Add event objects and predefined texts to template
      if (!empty($event->objects)) {
        $template->objects = $event->objects;
      }
      $template
        ->set_object('event', $event);
      if (!empty($event->text)) {
        $template->presets = $event->text;
      }
      return $template;
    }
  }

  /**
   * Get child template from template engine
   */
  function get_template($type, $name = NULL, $method = NULL, $language = NULL) {

    // Check this is a known type, if not return NULL,
    return self::create_template($type, $name, $method, $language, $this->module, $this);
  }

  /**
   * Create template, just allowed types
   *
   * We will try to find the following templates in this order
   *  module-type-name
   *  module-type
   *  notifications-type-name
   *  notifications-type
   *
   * @param $type
   *   Template type, currently 'event' or 'digest'
   * @param $name
   *   Name prefix for the template, usually related module
   * @param $module
   *   Module name. The fallback module will be always 'notifications'
   */
  static function create_template($type, $name = NULL, $method = NULL, $language = NULL, $module = 'notifications', $parent = NULL) {

    // We try first with name and then without
    if ($name && self::get_info($module . '-' . $type . '-' . $name)) {
      return new Notifications_Template($module . '-' . $type . '-' . $name, $parent, $method, $language, $module);
    }
    elseif (self::get_info($module . '-' . $type)) {
      return new Notifications_Template($module . '-' . $type, $parent, $method, $language, $module);
    }
    elseif ($module != 'notifications') {

      // We retry for module = notifications, still we fix the module in the resulting template
      if ($template = self::create_template($type, $name, $method, $language, 'notifications', $parent)) {
        $template->module = $module;
        return $template;
      }
    }
  }

}

Classes

Namesort descending Description
Notifications_Template Notifications Template