You are here

notifications_digest.module in Notifications 6.3

Same filename and directory in other branches
  1. 6.4 notifications_digest/notifications_digest.module

Notifications message digest

Provides message digesting for Notifications

Development Seed, http://www.developmentseed.org, 2007

File

notifications_digest/notifications_digest.module
View source
<?php

/**
 * @file
 * Notifications message digest
 *
 * Provides message digesting for Notifications
 * 
 * Development Seed, http://www.developmentseed.org, 2007 
 *
 */

/**
 * Implementation of hook_notifications()
 */
function notifications_digest_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'build methods':

      // Return array of digesting engines
      $info['short'] = array(
        'type' => 'short',
        'name' => t('Short digest'),
        'description' => t('Produces one line per event, grouped by object'),
        'build callback' => 'notifications_digest_build_short',
        'digest' => TRUE,
      );
      $info['long'] = array(
        'type' => 'long',
        'name' => t('Long digest'),
        'description' => t('Adds full information for each event'),
        'build callback' => 'notifications_digest_build_long',
        'digest' => TRUE,
      );
      return $info;
  }
}

/**
 * Implementation of hook_messaging_template()
 */
function notifications_digest_messaging_template($op, $type = NULL, $langcode = NULL) {
  switch ($op) {
    case 'templates':
      $info['notifications-digest'] = array(
        'module' => 'notifications',
        'type' => 'notifications',
        'title' => t('Notifications digest', array(), $langcode),
        'description' => t('Depending on your settings for each Send interval, Notifications may be digested, this is grouped and summarized in a single message. These are the common parts for Notifications digests.', array(), $langcode),
      );
      return $info;
    case 'keys':
      if ($type == 'notifications-digest') {
        return array(
          'subject' => t('Subject', array(), $langcode),
          'header' => t('Header', array(), $langcode),
          'main' => t('Line for digested events'),
          'closing' => t('Group closing', array(), $langcode),
          'footer' => t('Footer', array(), $langcode),
        );
      }
      elseif (strpos($type, 'notifications-event') === 0 || strpos($type, 'notifications-content') === 0) {
        return array(
          'digest' => t('Digest line', array(), $langcode),
        );
      }
      elseif (strpos($type, 'notifications-digest') === 0) {
        return array(
          'title' => t('Group title', array(), $langcode),
          'closing' => t('Group footer', array(), $langcode),
        );
      }
      break;
    case 'defaults':

      // Digested messages
      if ($type == 'notifications-digest') {
        return array(
          'subject' => t('[site-name] subscription update for [user]', array(), $langcode),
          'header' => t("Greetings, [user].\n\nThese are your messages", array(), $langcode),
          'main' => t("A [type] has been updated: [title]\n\n[event_list]", array(), $langcode),
          'closing' => '...',
          'footer' => array(
            t('This is an automatic message from [site-name]', array(), $langcode),
            t('To manage your subscriptions, browse to [subscriptions-manage]', array(), $langcode),
          ),
        );
      }
      break;
  }
}

/**
 * Get digest information for an event.
 * 
 * From the event definition (notifications('event types')) we find out 
 * - which event object we'll use for digesting
 * - which field of that object to use for indexing
 * 
 * I.e. for event type = 'node', event action = 'update'
 *  'digest' => ('node', 'nid')
 */
function nofitications_digest_event_info($event, $module = 'notifications') {
  $info = notifications_event_types($event->type, $event->action);
  if (!empty($info['digest'])) {
    $type = $info['digest'][0];
    $field = $info['digest'][1];

    // Check object and values, the object may be the event itself
    if ($type == 'event') {
      $object = $event;
    }
    else {
      $object = !empty($event->objects[$type]) ? $event->objects[$type] : NULL;
    }
  }
  else {

    // No digest info for this event /action so we use event and action itselves.
    $type = $event->type;
    $field = $event->action;
    $object = NULL;
  }
  $value = $object && isset($object->{$field}) ? $object->{$field} : 0;
  return array(
    'type' => $type,
    'field' => $field,
    'value' => $value,
    'object' => $object,
    'module' => $module,
  );
}

/**
 * Digest multiple events in a single message, long format.
 * 
 * We use digest templates for subject, header, footer
 *   digest-subject
 *   digest-header
 *   digest-footer
 * but the regular templates for the message body for each event
 *   event-[type]-[action]-main
 *     or event-[type]-main
 *       or event-main
 * 
 * @return array with messages ready to be sent
 */
function notifications_digest_build_long($params) {
  notifications_log('Digesting long format', array(
    'params' => $params,
  ));

  // Build the base template with full parameters
  $template = Notifications_Template::create_template('digest', NULL, $params->send_method, $params->language, $params->module);

  //$template = notifications_digest_template(NULL, $params->module, $params->send_method, $params->language);
  $template->subscriptions = $params->subscriptions;
  $template->events = $params->events;
  $template
    ->set_params($params);
  $template
    ->add_part('subject');
  $template
    ->add_part('header');

  // Add shared objects, will be inherited by child templates
  $template
    ->set_object('user', $params->account);

  // Build up the digested list with text replacement, body as big array
  // We need text replacement for each line because it depends on different objects
  foreach ($params->events as $event) {

    // Pass only the first subscription for this event
    $subscriptions = !empty($params->subscriptions[$event->eid]) ? $params->subscriptions[$event->eid] : array();
    $event_subscription = ($sid = current($subscriptions)) ? notifications_load_subscription($sid) : NULL;

    // We use the regular template for the events, method and language will be inherited
    if ($event_template = $template
      ->get_event_template($event)) {
      $event_template
        ->set_object('subscription', $event_subscription);
      $event_template
        ->add_part('subject');
      $event_template
        ->add_part('main');
    }
    $template
      ->add_child($event_template, 'main');
  }
  $template
    ->add_part('footer');
  notifications_log('Built long digest template', array(
    'template' => $template,
  ));

  // Build message from template and parameters
  $message = $template
    ->build();

  // Return array of messages instead of message;
  return array(
    $message,
  );
}

/**
 * Digest multiple events in a single message, short format.
 * 
 * @return array with messages ready to be sent
 */
function notifications_digest_build_short($params) {
  notifications_log('Digesting short format', array(
    'params' => $params,
  ));

  // Build the base template with full parameters
  $template = Notifications_Template::create_template('digest', NULL, $params->send_method, $params->language, $params->module);

  // $template = notifications_digest_template(NULL, $params->module, $params->send_method, $params->language);
  $template->subscriptions = $params->subscriptions;
  $template->events = $params->events;
  $template
    ->set_params($params);
  $template
    ->add_part('subject');
  $template
    ->add_part('header');

  // Add shared objects, will be inherited by child templates
  $template
    ->set_object('user', $params->account);

  // Compile list of events for each object and build child templates
  $list = array();
  foreach ($params->events as $event) {

    // Pass only the first subscription for this event
    $subscriptions = !empty($params->subscriptions[$event->eid]) ? $params->subscriptions[$event->eid] : array();
    $event_subscription = ($sid = current($subscriptions)) ? notifications_load_subscription($sid) : NULL;

    // How each event is digested will depend on the event
    $digest = nofitications_digest_event_info($event);
    $digest_type = $digest['type'];
    $digest_value = $digest['value'];
    $digest_field = $digest['field'];

    // Build digest line for the event, then add to the group. Method, language will be from parent
    if ($event_template = $template
      ->get_event_template($event)) {
      $event_template
        ->set_object('subscription', $event_subscription);
      $event_template
        ->add_part('digest');

      // We may have the template for this group already created
      if (isset($list[$digest_type][$digest_value])) {
        $list[$digest_type][$digest_value]
          ->add_child($event_template, 'events');
      }
      else {

        // Template name will look like 'notifications-digest-node-nid'
        $group_template = $template
          ->get_digest_template('digest', $digest_type . '-' . $digest_field);
        $template
          ->add_child($group_template, 'main');
        $group_template
          ->set_object($digest_type, $digest['object']);

        // Just the first event's subscription will be beter than nothing
        $group_template
          ->set_object('subscription', $event_subscription);
        $group_template
          ->add_part('title');
        $group_template
          ->add_child($event_template, 'events');
        $group_template
          ->add_part('closing');
        $list[$digest_type][$digest_value] = $group_template;
      }
    }
  }

  // Close the main template and render
  $template
    ->add_part('footer');

  // We dont pass a subscription object here, won't be too much use anyway
  $template
    ->set_object('subscription', NULL);
  notifications_log('Built short digest template', array(
    'template' => $template,
  ));

  // Build message from template and parameters
  $message = $template
    ->build();

  // Return array of messages instead of message;
  return array(
    $message,
  );
}

/**
 * Implementation of hook_theme()
 */
function notifications_digest_theme() {
  return array(
    'notifications_digest_short_body' => array(
      'arguments' => array(
        'text' => NULL,
        'list' => NULL,
      ),
      'file' => 'notifications_digest.theme.inc',
    ),
    'notifications_digest_short_line' => array(
      'arguments' => array(
        'line' => NULL,
        'group' => NULL,
      ),
      'file' => 'notifications_digest.theme.inc',
    ),
    'notifications_digest_long_body' => array(
      'arguments' => array(
        'header' => NULL,
        'content' => NULL,
        'footer' => NULL,
      ),
      'file' => 'notifications_digest.theme.inc',
    ),
  );
}

/**
 * Digest each line, with some caching for performance
 */
function notifications_digest_line($event, $method) {
  static $digest = array();
  if (!isset($digest[$event->eid][$method])) {

    // The event may have an specific digest line, otherwise use template if present or even information
    if (!empty($event->text['digest'])) {
      $line = $event->text['digest'];
    }
    elseif ($part = notifications_message_part('event', 'digest', $method, $event)) {
      $line = $part;
    }
    else {

      // Get it from event information
      $info = notifications_event_types($event->type, $event->action);
      $line = $info['line'];
    }
    $digest[$event->eid][$method] = $line;
  }
  return $digest[$event->eid][$method];
}

/**
 * Get text parts for digests.
 * 
 * Useful to get the group title and footer given some kind of digesting
 * 
 * @param $digest
 *   Digest information (which object and field we use)
 * @param $part
 *   Template part: header, footer...
 * @param $method
 *   Send method
 */
function notifications_digest_group($digest, $part, $method) {
  static $texts = array();
  $type = $digest['type'];
  $value = $digest['value'];
  if (!isset($texts[$type][$value][$part][$method])) {
    if ($line = notifications_message_part('digest', $part, $method, array(
      $type,
      $digest['field'],
    ), $digest['module'])) {
      $output = $line;
    }
    else {
      $output = '';
    }
    $texts[$type][$value][$part][$method] = $output;
  }
  return $texts[$type][$value][$part][$method];
}

Functions

Namesort descending Description
nofitications_digest_event_info Get digest information for an event.
notifications_digest_build_long Digest multiple events in a single message, long format.
notifications_digest_build_short Digest multiple events in a single message, short format.
notifications_digest_group Get text parts for digests.
notifications_digest_line Digest each line, with some caching for performance
notifications_digest_messaging_template Implementation of hook_messaging_template()
notifications_digest_notifications Implementation of hook_notifications()
notifications_digest_theme Implementation of hook_theme()