You are here

class Messaging_Template in Messaging 6.3

Same name and namespace in other branches
  1. 7 messaging_template/messaging_template.inc \Messaging_Template

Messaging Template class

Hierarchy

Expanded class hierarchy of Messaging_Template

File

messaging_template/messaging_template.inc, line 202
Base classes for messaging templates

View source
class Messaging_Template extends Messaging_Template_Engine implements Messaging_Message_Template, Messaging_Template_Element {

  // Basic template parameters
  public $type = NULL;
  public $language = NULL;
  public $method = NULL;

  // Parent template and template engine
  protected $parent;

  // Template raw elements. Each of them may be:
  // - A text part string, ready for replacement
  // - An array like (text_part_key, default)
  // - A Template object
  protected $elements = array();

  // Objects for replacement
  protected $objects = array();

  // Text templates for each part indexed by method and language code
  public $texts = array();

  // Predefined texts to use instead of text parts
  public $presets = array();

  // Debug option on/off
  public $debug = TRUE;

  /**
   * Class constructor, create a template of given type
   */
  function __construct($type = 'messaging-template', $parent = NULL, $method = NULL, $language = NULL) {
    $this->type = $type;
    if ($parent) {
      $this->parent = $parent;
      $this->method = $method;
      $this->language = $language;
    }
    else {
      $this->language = $language ? $language : language_default();
      $this->method = $method ? $method : 'default';
      $this->objects = array(
        'global' => NULL,
      );
    }
  }

  /**
   * Get child template derived from this one
   */
  function get_template($type = 'messaging-template', $method = NULL, $language = NULL) {

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

  /**
   * Get template part, FALSE if not found
   */
  function get_part($key, $type = NULL, $method = NULL, $language = NULL) {
    return self::get_template_part($key, $type ? $type : $this->type, $method ? $method : $this
      ->get_method(), $language ? $language : $this
      ->get_language());
  }

  /**
   * Set object for token replacement
   */
  function set_object($type, $object = NULL) {
    $this->objects[$type] = $object;
  }

  /**
   * @param $key
   *   Template part key
   * @param $default
   *   Default value if key not found
   * @param $duplicate
   *   Add duplicates (if this part has been already added)
   */
  function add_part($key, $default = NULL, $duplicate = FALSE) {
    if (isset($this->elements[$key]) && !$duplicate) {
      return;
    }
    if (isset($this->presets[$key])) {
      $this
        ->append($this->presets[$key], $key);
    }
    else {

      // Add as Template_Text_Part
      $this
        ->append(new Messaging_Template_Part($this, $key, $default), $key);
    }
  }

  // Add child template
  function add_child($template, $key = NULL) {
    $template->parent = $this;
    $this
      ->append($template, $key);
  }

  // Get text part, possibly from parent
  function xget_part($key) {
    return $this->engine
      ->get_part($key, $this->type, $this
      ->get_method(), $this
      ->get_language());
  }

  /**
   * Append element: may be a text or another template
   */
  function append($value, $key = NULL) {
    if (!isset($key)) {
      $this->elements[] = $value;
    }
    elseif (isset($this->elements[$key])) {

      // If not an array yet, make it into an array
      if (!is_array($this->elements[$key])) {
        $this->elements[$key] = array(
          $this->elements[$key],
        );
      }
      $this->elements[$key][] = $value;
    }
    else {
      $this->elements[$key] = $value;
    }
  }

  /**
   * Render all template elements
   *
   * @param $method
   *   Sending method to render for
   * @param $language
   *   Language object
   */
  function render($method, $language) {
    if (!isset($this->texts[$method][$language->language])) {
      $this->texts[$method][$language->language] = $this
        ->render_elements($this->elements, $method, $language, $this
        ->get_objects());
    }
    return $this->texts;
  }

  /**
   * Reset all template elements for new rendering
   *
   * @param $recurse
   *   Reset child templates too
   */
  function reset($recurse = FALSE) {
    $this->rendered = FALSE;
    $this->texts = array();
    if ($recurse && $this->elements) {
      $this
        ->reset_elements($this->elements);
    }
  }

  /**
   * Reset recursively an array of elements
   */
  function reset_elements(&$elements) {
    foreach ($elements as $element) {
      if (is_object($element)) {
        $element
          ->reset(TRUE);
      }
      elseif (is_array($element)) {
        reset_elements($element);
      }
    }
  }

  /**
   * Get template text part, FALSE if not found
   *
   * Here we translate MESSAGING_EMPTY into an empty string
   */
  function text_part($type, $key, $method = NULL, $language = NULL) {
    $method = $method ? $method : $this
      ->get_method();
    $language = $language ? $language : $this
      ->get_language();
    $this
      ->build_template($type, $method, $language);
    $part = $this
      ->get_part($key, $type, $method);
    $text = $part ? $part->template : FALSE;
    $this
      ->debug('Getting text part', array(
      'type' => $type,
      'key' => $key,
      'method' => $method,
      'text' => $text,
    ));
    return $text;
  }

  /**
   * Get text elements as array
   *
   * @param $key
   *   Text part key to return, like 'subject', 'body'...
   */
  function get_text($key, $method = NULL, $language = NULL) {
    $method = $method ? $method : $this
      ->get_method();
    $language = $language ? $language : $this
      ->get_language();
    $content = $this
      ->get_content($method, $language);
    return isset($content[$key]) ? $content[$key] : NULL;
  }

  /**
   * Get all text parts as array
   */
  function get_content($method, $language) {
    $this
      ->render($method, $language);
    return isset($this->texts[$method][$language->language]) ? $this->texts[$method][$language->language] : NULL;
  }

  /**
   * Get text element as string
   */
  function get_string($key, $glue = "\n", $default = '') {
    if ($text = $this
      ->get_text($key)) {
      return $this
        ->compose($text, $glue);
    }
    else {
      return $default;
    }
  }

  /**
   * Compose string
   */
  function compose($elements, $glue = "\n") {
    $compose = array();
    foreach ($elements as $part) {
      $compose[] = is_string($part) ? $part : $this
        ->compose($part, $glue);
    }
    return implode($glue, $compose);
  }

  /**
   * Check whether this template needs aditional replacement
   */
  function needs_replace() {
    return FALSE;
  }

  // Implementation of Messaging_Template_Parent

  /**
   * Get language, default to parent's
   */
  function get_language($property = NULL) {

    // Either the template has a language or has a parent who's got it
    if (isset($this->language)) {
      return $property ? $this->language->{$property} : $this->language;
    }
    elseif (isset($this->parent)) {
      return $this->parent
        ->get_language($property);
    }
    else {
      return language_default($property);
    }
  }

  /**
   * Get sending method, default to parent's
   */
  function get_method() {
    return $this->method ? $this->method : $this->parent
      ->get_method();
  }

  /**
   * Get template objects, included parent's objects
   */
  function get_objects($get_parent = FALSE) {
    if ($get_parent && isset($this->parent)) {
      return $this->objects + $this->parent
        ->get_objects();
    }
    else {
      return $this->objects;
    }
  }

  /**
   * Debug, adding instance information
   */
  function debug($txt, $variables = array()) {
    if ($this->debug) {
      $variables += array(
        'type' => $this->type,
        'method' => $this
          ->get_method(),
      );
      messaging_debug($txt, $variables);
    }
  }

  // Magic function, format as string
  public function __toString() {
    return "Messaging_Template: type={$this->type}, subject=" . $this
      ->get_string('subject');
  }

  /**
   * Implementation of Messaging_Message_Template
   */

  /**
   * Build a new message for method, destination
   *
   * @param $method
   *   Sending method
   * @param $language
   *   Language object
   * @return Messaging_Message
   *   Message built for method, language
   */
  public function build($method = NULL, $language = NULL) {
    $build = array(
      'template' => $this,
      'method' => $method,
      'language' => $language ? $language : $this
        ->get_language('language'),
    );
    return new Messaging_Message($build);
  }

  /**
   * Get subject message parts
   *
   * @param $method
   *   Sending method
   * @param $language
   *   Language code
   * @return string or array()
   *   Subject text or text parts for rendering
   */
  function get_subject($method = NULL, $language = NULL) {
    return $this
      ->get_text('subject', $method, $language);
  }

  /**
   * Get body parts
   *
   * @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) {
    return $this
      ->get_text('body', $method, $language);
  }

}

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::build public function Build a new message for method, destination Overrides Messaging_Message_Template::build
Messaging_Template::compose function Compose string
Messaging_Template::debug function Debug, adding instance information
Messaging_Template::get_body function Get body parts Overrides Messaging_Message_Template::get_body
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_template function Get child template derived from this one
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::__construct function Class constructor, create a template of given type
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::create_template static function Create template, just allowed types
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