You are here

function notifications_digest_build_short in Notifications 6.4

Same name and namespace in other branches
  1. 6.3 notifications_digest/notifications_digest.module \notifications_digest_build_short()

Digest multiple events in a single message, short format.

Return value

array with messages ready to be sent

1 string reference to 'notifications_digest_build_short'
notifications_digest_notifications in notifications_digest/notifications_digest.module
Implementation of hook_notifications()

File

notifications_digest/notifications_digest.module, line 159
Notifications digest module

Code

function notifications_digest_build_short($template, $events, $subscriptions, $module = 'notifications') {
  $send_method = $template->method;
  $destination = $template
    ->get_destination();
  $account = $template
    ->get_user();
  $language = $template
    ->get_language();

  // Build a unique message from this template
  $message = clone $template;

  // Create message. Do all this in one replacement, then strip out the subject
  $text['subject'] = notifications_digest_message_part('subject', $send_method, NULL, NULL, $language, $module);
  $text['header'] = notifications_digest_message_part('header', $send_method, NULL, NULL, $language, $module);
  $text['footer'] = notifications_digest_message_part('footer', $send_method, NULL, NULL, $language, $module);
  $message->text_parts = $text;

  // We dont pass a subscription object here, won't be too much use anyway
  $text = messaging_template_text_replace($text, array(
    'destination' => $destination,
    'user' => $account,
    'subscription' => NULL,
  ), $language);
  $message->subject = $text['subject'];

  // Compile list of events for each object. Build up the digested list with text replacement
  // We need text replacement for each line because it depends on different objects
  $list = array();
  foreach ($events as $event) {
    notifications_debug('Digesting short format', array(
      'event' => $event->eid,
    ));
    $event_subscriptions = isset($subscriptions[$event->eid]) ? array_filter($subscriptions[$event->eid]) : array();
    $message
      ->add_event($event, $event_subscriptions);
    $subscription = $event_subscriptions ? notifications_load_subscription(current($event_subscriptions)) : NULL;
    $objects = $event->objects + array(
      'destination' => $destination,
      'user' => $account,
      'subscription' => $subscription,
    );
    $digest = notifications_digest_event_info($event);
    notifications_debug('Digest info for event', $digest);
    $digest_type = $digest['type'];
    $digest_value = $digest['value'];
    if (!isset($list[$digest_type][$digest_value]['group'])) {
      $group = array(
        'title' => notifications_digest_group($digest, 'title', $send_method, $language),
        'footer' => notifications_digest_group($digest, 'closing', $send_method, $language),
      );
      $message->text_parts['list'][$digest_type][$digest_value]['group'] = $group;

      // The objects passed here for tokens will be the ones from the first event only
      $list[$digest_type][$digest_value]['group'] = messaging_template_text_replace($group, $objects, $language);
      notifications_debug('Digesting object', array(
        'type' => $digest_type,
        'value' => $digest_value,
      ));
    }

    // Check duplicate notifications for the same event so we do some deduping
    if (!isset($list[$digest_type][$digest_value]['line'][$event->eid])) {
      $line = notifications_digest_line($event, $send_method, $language);
      $messsage->text_parts['list'][$digest_type][$digest_value]['line'][$event->eid] = $line;
      $objects['event'] = $event;
      $list[$digest_type][$digest_value]['line'][$event->eid] = messaging_template_text_replace($line, $objects, $language);
    }
  }

  // Compose body. All these lines have been text replaced
  $message->body = theme('notifications_digest_short_body', $text, $list);
  return array(
    $message,
  );
}