You are here

function notifications_message_part in Notifications 6.2

Same name and namespace in other branches
  1. 5 notifications.cron.inc \notifications_message_part()
  2. 6 notifications.cron.inc \notifications_message_part()

Get message part

It searches for optional message group keys for options defaulting to $type 1. $module-$type-[$event->type]-[$event->action] 2. $module-$type-[$event->type] 3. $module-$type

Parameters

$type: Message type to send, either 'event' or 'digest'

$key: Id of message part, ie 'header'

$method: Method by which message will be sent. Normally 'mail'

$param: Event data if we have a single event (type = event), none if we are digesting multiple events (type = digest)

$module: Module name to be prefixed to the template name. If different than notifications we first try with that module but if not found, try again with 'notifications'

Return value

Part of the message with tokens for replacement.

6 calls to notifications_message_part()
NotificationsTemplatesTests::testNotificationsTemplates in tests/notifications_templates.test
Play with creating, retrieving, deleting a pair subscriptions
notifications_digest_group in ./notifications.cron.inc
Get text parts for digests.
notifications_digest_line in ./notifications.cron.inc
Digest each line, with some caching for performance
notifications_process_digest_long in ./notifications.cron.inc
Digest multiple events in a single message, long format.
notifications_process_digest_short in ./notifications.cron.inc
Digest multiple events in a single message, short format.

... See full list

File

./notifications.cron.inc, line 536

Code

function notifications_message_part($type, $key, $method, $param = NULL, $module = 'notifications') {

  // If event passed check for predefined text or get optional keys from event
  if ($type == 'event' && is_object($param)) {
    if (isset($param->text[$key])) {
      return $param->text[$key];
    }
    else {
      $options = array(
        $param->type,
        $param->action,
      );
    }
  }
  elseif ($method == 'test') {

    // Little trick for this to be testable
    return "{$type} {$key} [type-name] [title] [site-name]";
  }
  else {
    $options = is_array($param) ? $param : array();
  }

  // Buid an array for searching templates, here's where the template fallback happens
  // I.e. $keyparts = array('notifications, 'event', 'node', 'update'),  will search for:
  // - notifications-event-node-update
  // - notifications-event-node
  // - notifications-event
  // - notifications
  $search = $keyparts = array_merge(array(
    $module,
    $type,
  ), $options);
  while ($keyparts) {
    $groupkey = implode('-', $keyparts);
    if ($text = messaging_message_part($groupkey, $key, $method)) {
      $output = $text == MESSAGING_EMPTY ? '' : $text;
      break;
    }

    // If no text trim out latest part of the key and retry
    array_pop($keyparts);
  }

  // If we don't have a template and the module is not notifications, give it another try
  if (isset($output)) {

    // We found something, return it
    return $output;
  }
  elseif ($module != 'notifications') {

    // Found nothing, different module, retry with notifications templates
    return notifications_message_part($type, $key, $method, $param, 'notifications');
  }
  else {

    // Failed to get message part, return information about the template not found, will help debugging
    return "[UNDEFINED module = {$module}, key = {$key}, type = {$type}, method = {$method}, search = " . implode(',', $search) . ']';
  }
}