You are here

class Messaging_Template_Engine in Messaging 6.3

Static template functions that interact with the module API

Hierarchy

Expanded class hierarchy of Messaging_Template_Engine

File

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

View source
class Messaging_Template_Engine {

  // Template information
  protected static $info;

  // Built templates
  protected static $templates;

  // Part keys indexed by group/type
  protected static $keys;

  // Default templates from modules
  protected static $defaults;

  // Global class debug
  protected static $_debug = FALSE;

  /**
   * Get template type info
   */
  static function get_info($type) {
    if (!isset(self::$info)) {
      self::$info = messaging_template_info(NULL, NULL);
    }
    return isset(self::$info[$type]) ? self::$info[$type] : NULL;
  }

  /**
   * Create template, just allowed types
   */
  static function create_template($type = 'messaging-template', $parent = NULL, $method = NULL, $language = NULL) {

    // Check this is a known type, if not return NULL
    if (self::get_info($type)) {
      return new Message_Template($type, $parent, $method, $language);
    }
    else {
      return NULL;
    }
  }

  /**
   * Get template part, FALSE if not found
   *
   * @param $key
   *   Part key, like 'subject', 'header', 'footer', ...
   * @param $type
   *   Template type, like 'messaging-template', 'notifications-event'...
   */
  static function get_template_part($key, $type, $method, $language) {
    $lang = $language->language;
    self::_debug('Getting template part', array(
      'type' => $type,
      'key' => $key,
      'method' => $method,
      'language' => $lang,
    ));
    self::build_template($type, $method, $language);
    if (!isset(self::$templates[$lang][$type][$method][$key])) {

      // Try method default for the same type
      if ($method_default = self::method_default($method)) {
        $part = self::get_template_part($key, $type, $method_default, $language);
      }
      elseif (!($part = self::get_default($type, $key, $language))) {
        $part = FALSE;
      }
      self::$templates[$lang][$type][$method][$key] = $part;
    }
    elseif (self::$templates[$lang][$type][$method][$key]->options == MESSAGING_TEMPLATE_FALLBACK) {

      // Resolve fallbacks first time the part is retrieved
      if ($fallback = self::type_fallback($type)) {
        self::$templates[$lang][$type][$method][$key] = self::get_template_part($key, $fallback, $method, $language);
      }
      else {
        self::$templates[$lang][$type][$method][$key] = FALSE;
      }
    }
    return self::$templates[$lang][$type][$method][$key];
  }

  /**
   * Build a given template
   */
  static function build_template($type, $method, $language) {
    $lang = $language->language;
    if (!isset(self::$templates[$lang][$type][$method])) {
      self::_debug('Building template', array(
        'type' => $type,
        'method' => $method,
      ));
      self::$templates[$lang][$type][$method] = messaging_template_get_parts($type, $method, $language);
    }
  }

  /**
   * Get default provided by modules
   */
  static function get_default($type, $key, $language) {
    $lang = $language->language;
    self::_debug('Getting template default', array(
      'type' => $type,
      'key' => $key,
      'language' => $lang,
    ));
    if (!isset(self::$defaults[$lang][$type])) {
      self::$defaults[$lang][$type] = messaging_template_get_defaults($type, $language);
    }
    if (!isset(self::$defaults[$lang][$type][$key])) {
      if ($type_fallback = self::type_fallback($type)) {
        self::$defaults[$lang][$type][$key] = self::get_default($type_fallback, $key, $language);
      }
      else {
        self::$defaults[$lang][$type][$key] = FALSE;
      }
    }
    return self::$defaults[$lang][$type][$key];
  }

  /**
   * Get part keys for a given template
   */
  protected function get_keys($type) {
    if (!isset(self::$keys[$type])) {
      self::$keys[$type] = messaging_template_get_keys($type);
    }
    return array_keys(self::$keys[$type]);
  }

  /**
   * Get method fallback
   */
  static function method_default($method) {
    return messaging_template_method_default($method);
  }

  /**
   * Get type fallback
   */
  static function type_fallback($type) {
    return messaging_template_fallback($type);
  }

  /**
   * Render recursively an array of elements
   */
  static function render_elements(&$elements, $method, $language, $objects = array()) {
    $replace = $texts = array();
    foreach ($elements as $key => $element) {
      if (is_object($element)) {
        $needs_replace = $element
          ->needs_replace();
        $value = $element
          ->get_content($method, $language);
      }
      elseif (is_array($element)) {
        $needs_replace = FALSE;
        $value = self::render_elements($element, $method, $language, $objects);
      }
      else {
        $needs_replace = TRUE;
        $value = $element;
      }

      // If needs replace add $texts placeholder to preserve the order
      if ($needs_replace) {
        $replace[$key] = $value;
        $texts[$key] = NULL;
      }
      else {
        $texts[$key] = $value;
      }
    }
    if ($replace) {
      $texts = array_merge($texts, self::text_replace($replace, $objects, $method, $language));
    }
    return $texts;
  }

  /**
   * Replace text with object tokens
   */
  static function text_replace($text, $objects, $language) {
    return messaging_template_text_replace($text, $objects, $language);
  }

  /**
   * Debug, static version
   */
  static function _debug($txt, $variables = array()) {
    if (self::$_debug) {
      messaging_debug($txt, $variables);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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