You are here

class Notifications_Template in Notifications 6.3

Notifications Template

Hierarchy

Expanded class hierarchy of Notifications_Template

File

classes/notifications_template.class.inc, line 10
Notification Template classes

View source
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;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Messaging_Template::$debug public property
Messaging_Template::$elements protected property
Messaging_Template::$language public property
Messaging_Template::$method public property
Messaging_Template::$objects protected property
Messaging_Template::$parent protected property
Messaging_Template::$presets public property
Messaging_Template::$texts public property
Messaging_Template::$type public property
Messaging_Template::add_child function
Messaging_Template::add_part function
Messaging_Template::append function Append element: may be a text or another template
Messaging_Template::compose function Compose string
Messaging_Template::debug function Debug, adding instance information
Messaging_Template::get_content function Get all text parts as array Overrides Messaging_Template_Element::get_content
Messaging_Template::get_language function Get language, default to parent's
Messaging_Template::get_method function Get sending method, default to parent's
Messaging_Template::get_objects function Get template objects, included parent's objects
Messaging_Template::get_part function Get template part, FALSE if not found
Messaging_Template::get_string function Get text element as string
Messaging_Template::get_subject function Get subject message parts Overrides Messaging_Message_Template::get_subject
Messaging_Template::get_text function Get text elements as array
Messaging_Template::needs_replace function Check whether this template needs aditional replacement Overrides Messaging_Template_Element::needs_replace
Messaging_Template::render function Render all template elements
Messaging_Template::reset function Reset all template elements for new rendering Overrides Messaging_Template_Element::reset
Messaging_Template::reset_elements function Reset recursively an array of elements
Messaging_Template::set_object function Set object for token replacement
Messaging_Template::text_part function Get template text part, FALSE if not found
Messaging_Template::xget_part function
Messaging_Template::__toString public function
Messaging_Template_Engine::$defaults protected static property
Messaging_Template_Engine::$info protected static property
Messaging_Template_Engine::$keys protected static property
Messaging_Template_Engine::$templates protected static property
Messaging_Template_Engine::$_debug protected static property
Messaging_Template_Engine::build_template static function Build a given template
Messaging_Template_Engine::get_default static function Get default provided by modules
Messaging_Template_Engine::get_info static function Get template type info
Messaging_Template_Engine::get_keys protected function Get part keys for a given template
Messaging_Template_Engine::get_template_part static function Get template part, FALSE if not found
Messaging_Template_Engine::method_default static function Get method fallback
Messaging_Template_Engine::render_elements static function Render recursively an array of elements
Messaging_Template_Engine::text_replace static function Replace text with object tokens
Messaging_Template_Engine::type_fallback static function Get type fallback
Messaging_Template_Engine::_debug static function Debug, static version
Notifications_Template::$built public property
Notifications_Template::$destination public property
Notifications_Template::$event public property
Notifications_Template::$events public property
Notifications_Template::$module public property
Notifications_Template::$sender public property
Notifications_Template::$sender_option public property
Notifications_Template::$subscriptions public property
Notifications_Template::$test public property
Notifications_Template::$user public property
Notifications_Template::build function Build notifications message Overrides Messaging_Template::build
Notifications_Template::create_event_template static function Create template object for event
Notifications_Template::create_template static function Create template, just allowed types Overrides Messaging_Template_Engine::create_template
Notifications_Template::get_body function Get body parts, in this case they are 'header', 'main', 'footer' Overrides Messaging_Template::get_body
Notifications_Template::get_digest_template function Get child template of type digest
Notifications_Template::get_event_template function Get child template object for event
Notifications_Template::get_template function Get child template from template engine Overrides Messaging_Template::get_template
Notifications_Template::set_account function Set destination user account
Notifications_Template::set_params function Set information from processing parameters
Notifications_Template::__construct function Class constructor, create a template of given type Overrides Messaging_Template::__construct