You are here

notifications_lite.module in Notifications 5

Simple notifications API

This is a very simple API for other modules to produce notifications with a minimal set up.

This module doesn't have direct dependencies because it uses any available module to send out the notifications. It tries in this order:

1. Notifications (queues messages for delivery) 2. Messaging (just sends messages right away with the user's default method) 3. Drupal standard mail

File

notifications_lite/notifications_lite.module
View source
<?php

/**
 * @file
 *   Simple notifications API
 * 
 * This is a very simple API for other modules to produce notifications with a minimal set up.
 * 
 * @see notifications_lite_send()
 * 
 * This module doesn't have direct dependencies because it uses any available module to send
 * out the notifications. It tries in this order:
 * 
 * 1. Notifications (queues messages for delivery)
 * 2. Messaging (just sends messages right away with the user's default method)
 * 3. Drupal standard mail
 * 
 */

/**
 * Sends out a notification for a user
 * 
 * This is all it takes for a module to send out some notification for a user.
 * 
 * However, a real event is produced so other modules may want to provide secondary subscriptions
 * and notifications for these simple events
 * 
 * @param $uid
 *   User id for destination
 * @param $subject
 *   Notification subject
 * @param $body
 *   Optional notification body
 * @param $action
 *   Optional action name, so other modules can define specific message parts for their actions
 * @param $params
 *   Optional array of additional parameters to be stored with the event. For example, one could store the sender
 *   of a message, as otherwise this information might not be easily retrievable during message sending on cron.
 *   Only available when the full notification system is used and not just the messaging module.
 * 
 * Note: Even when notifications module is enabled, the 'send interval' will be always immediate (0) for this
 * kind of messages.
 */
function notifications_lite_send($uid, $subject, $body = '', $action = 'default', $params = array()) {
  if (module_exists('notifications')) {

    // So we have the full Notifications system enabled
    return notifications_lite_add_to_queue($uid, $subject, $body, $action, $params);
  }
  elseif (module_exists('messaging')) {

    // We just have the Messaging system, that's something
    $account = messaging_load_user($uid);
    $message = array(
      'type' => 'notifications-lite',
      'subject' => $subject,
      'body' => empty($body) ? $subject : $body,
    );
    return messaging_message_send_user($account, $message);
  }
  else {

    // We don't have anything else so we use Drupal's mail
    $account = user_load(array(
      'uid' => $uid,
    ));
    return drupal_mail('notifications-lite-' . $action, $account->mail, $subject, empty($body) ? $subject : $body);
  }
}

/**
 * Put simple notification into queue
 * 
 * @param $uid
 *   User id for destination
 * @param $subject
 *   Notification subject
 * @param $body
 *   Optional notification body
 * @param $action
 *   Optional action name, so other modules can define specific message parts for their actions
 */
function notifications_lite_add_to_queue($uid, $subject, $body = '', $action = 'default', $params = array()) {

  // Build and store simple event
  $event = array(
    'module' => 'notifications',
    'uid' => 0,
    'oid' => $uid,
    'type' => 'lite',
    'action' => $action,
    'params' => array(
      'for-uid' => $uid,
      'subject' => $subject,
      'body' => $body,
    ),
  );
  $event['params'] = array_merge($event['params'], $params);
  $event = notifications_event($event);
}

/**
 * Implementation of hook_notifications()
 * 
 * It handles event texts
 */
function notifications_lite_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
  switch ($op) {
    case 'event load':

      // $arg0 is event
      $event =& $arg0;
      if ($event->type == 'lite') {
        if (!empty($event->params['subject'])) {
          $event->text['subject'] = $event->params['subject'];
          $event->text['digest'] = $event->params['subject'];
        }
        if (!empty($event->params['body'])) {
          $event->text['main'] = $event->params['body'];
        }
      }
      break;

    // By queueing the event here we gain access to some features, like inmediate sending
    case 'event queued':

      // $event is arg0
      $event =& $arg0;
      if ($event->type == 'lite') {
        notifications_lite_queue_event($event);
      }
      break;
    case 'event types':

      // We just define event type 'lite', action 'default'
      $types[] = array(
        'type' => 'lite',
        'action' => 'default',
        'name' => t('Message for [user]'),
        'digest' => NULL,
        // This means grouping by: event type, event action
        'description' => t('Notifications lite message'),
      );
      return $types;
  }
}

/**
 * Insert lite notificaition into queue
 */
function notifications_lite_queue_event($event) {
  $uid = $event->params['for-uid'];
  $account = user_load(array(
    'uid' => $uid,
  ));

  // Just inmediate sending for 5.x, diggest support in 6.x
  $send_interval = 0;
  $send_method = notifications_user_setting('send_method', $account);
  $sql = 'INSERT INTO {notifications_queue} (uid, sid, module, eid, send_interval, send_method, cron, created, conditions) ';
  $sql .= " VALUES(%d, 0, 'notifications', %d, %d, '%s', 1,  %d, 0) ";
  db_query($sql, $uid, $event->eid, $send_interval, $send_method, time());
}

/**
 * Implementation of hook_messaging()
 */
function notifications_lite_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  switch ($op) {
    case 'message groups':

      // Generic notifications event
      $info['notifications-event-lite'] = array(
        'module' => 'notifications_lite',
        'name' => t('Simple notifications'),
        'help' => t('The subject and main body will be provided by the event itself'),
      );
      return $info;
    case 'message keys':
      $type = $arg1;
      switch ($type) {
        case 'notifications-event-lite':

          // The other parts for these messages will be given by the event itself
          return array(
            'header' => t('Header'),
            'footer' => t('Footer'),
          );
          break;
      }
      break;
    case 'messages':
      $type = $arg1;

      // Event notifications
      switch ($type) {
        case 'notifications-event-lite':
          return array(
            'header' => t("Greetings, [user]."),
            'footer' => array(
              t('This is an automatic message from [site-name]'),
              t('To manage your subscriptions, browse to [subscriptions-manage]'),
            ),
          );
      }
      break;
  }
}

Functions

Namesort descending Description
notifications_lite_add_to_queue Put simple notification into queue
notifications_lite_messaging Implementation of hook_messaging()
notifications_lite_notifications Implementation of hook_notifications()
notifications_lite_queue_event Insert lite notificaition into queue
notifications_lite_send Sends out a notification for a user