You are here

message_digest.module in Message Digest 7

Same filename and directory in other branches
  1. 8 message_digest.module

File

message_digest.module
View source
<?php

/**
 * Implements hook_cron().
 * Aggregate, format and send Digest emails.
 */
function message_digest_cron() {
  foreach (message_notify_get_notifiers() as $plugin_name => $plugin) {
    if (strpos($plugin_name, 'digest') === FALSE) {

      // Only load the "Digest" notifiers and skip the rest.
      continue;
    }
    $plugin = message_notify_get_notifier($plugin_name);
    $class = ctools_plugin_load_class('message_notify', 'notifier', $plugin_name, 'class');
    $notifier = new $class($plugin, new Message());
    $interval = $notifier
      ->getInterval();
    $last_run = variable_get('message_digest_' . $interval . '_last_run', 0);
    if ($last_run > strtotime("-" . $interval)) {

      // Not time to run this again yet. Wait until the interval has elapsed.
      continue;
    }

    // Gather up all the messages into neat little digests and send 'em out.
    $digests = $notifier
      ->aggregate();
    foreach ($digests as $uid => $groups) {
      foreach ($groups as $gid => $messages) {
        $context = array(
          'uid' => $uid,
          // reference only, cannot change
          'gid' => $gid,
          'view_modes' => $plugin['view_modes'],
          'deliver' => TRUE,
          // set to FALSE to prevent delivery
          'plugin' => $plugin,
          // reference only
          'messages' => $messages,
        );
        drupal_alter('message_digest_view_mode', $context);
        if ($context['deliver']) {
          $formatted_messages = $notifier
            ->format($context['messages'], $context['view_modes']);
          $result = $notifier
            ->deliverDigest($uid, $context['gid'], $formatted_messages);
        }
      }
      $notifier
        ->markSent($uid, $plugin_name);
    }
    variable_set('message_digest_' . $interval . '_last_run', time());
  }
}

/**
 * Implements hook_mail()
 */
function message_digest_mail($key, &$message, $params) {
  if ($key == 'digest') {
    $title = variable_get('site_name', 'Open Atrium');

    // @TODO: USE THE NAME OF THE GROUP/SPACE HERE IF IT EXISTS.
    if (!empty($params['gid'])) {
      $group = node_load($params['gid']);
      $title = $group->title;
    }
    $message['subject'] = $title . t(' Message Digest');
    $message['body'][] = $params['body'];
  }
}

/**
 * Implements hook_ctools_plugin_api().
 */
function message_digest_ctools_plugin_api($module, $api) {
  if ($module == 'message_notify' && $api == 'notifier') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function message_digest_ctools_plugin_directory($module, $plugin) {
  if ($module == 'message_notify') {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_theme().
 */
function message_digest_theme() {
  return array(
    'message_digest' => array(
      'variables' => array(
        'messages' => NULL,
        'plugin' => NULL,
      ),
    ),
    'message_digest_row' => array(
      'variables' => array(
        'rows' => NULL,
        'plugin' => NULL,
        'message' => NULL,
      ),
    ),
  );
}

/**
 * Theme function for message_digest
 */
function theme_message_digest($vars) {
  return implode("\n<hr />", $vars['messages']);
}

/**
 * Theme function for message_digest_row
 */
function theme_message_digest_row($vars) {
  return implode($vars['rows']);
}

Functions

Namesort descending Description
message_digest_cron Implements hook_cron(). Aggregate, format and send Digest emails.
message_digest_ctools_plugin_api Implements hook_ctools_plugin_api().
message_digest_ctools_plugin_directory Implements hook_ctools_plugin_directory().
message_digest_mail Implements hook_mail()
message_digest_theme Implements hook_theme().
theme_message_digest Theme function for message_digest
theme_message_digest_row Theme function for message_digest_row