You are here

messaging_debug.module in Messaging 5

Simple messaging using html page. Messaging method plug-in

This is a really simple message viewer and also an illustration of pulling messaging methods

File

messaging_debug/messaging_debug.module
View source
<?php

/**
 * @file
 * Simple messaging using html page. Messaging method plug-in
 * 
 * This is a really simple message viewer and also an illustration of pulling messaging methods
 */

// Number of messages to display per page
define('MESSAGING_DEBUG_PAGER', 10);

/**
 * Implementation of hook_menu().
 */
function messaging_debug_menu($may_cache) {
  global $user;

  // we need the user to to build some urls
  $items = array();
  if (!$may_cache) {
    if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) > 0) {
      $account = user_load(array(
        'uid' => arg(1),
      ));
      if ($account->uid == $user->uid || user_access('administer messaging')) {
        $items[] = array(
          'path' => 'user/' . $account->uid . '/messagelog',
          'type' => MENU_LOCAL_TASK,
          'title' => t('Message log'),
          'callback' => 'messaging_debug_user_page',
          'callback arguments' => array(
            $account,
          ),
        );
      }
    }
  }
  return $items;
}

/**
 * Implementation of hook_form_alter()
 */
function messaging_debug_form_alter($form_id, &$form) {
  if ($form_id == 'messaging_admin_settings') {
    $form['general']['messaging_debug'] = array(
      '#title' => t('Debug mode'),
      '#type' => 'radios',
      '#options' => array(
        t('Disabled'),
        t('Enabled'),
      ),
      '#default_value' => variable_get('messaging_debug', 0),
      '#description' => t('If enabled, messages wont be sent out but logged to watchdog, and displayed in the page footer.'),
    );
  }
}

/**
 * Menu callback. Display pending messages to the user
 * 
 * Sample Implementation of messaging pull methods
 */
function messaging_debug_user_page($account) {
  drupal_set_title(t('Messages for %name', array(
    '%name' => $account->name,
  )));

  // Fetch all pending messages.
  $output = '';

  // Use this method's info for all the messages
  $messages = messaging_store('get', array(
    'uid' => $account->uid,
  ), array(
    'mqid DESC',
  ), MESSAGING_DEBUG_PAGER, 0);
  if ($messages) {
    $header = array(
      t('Method'),
      t('Subject'),
      t('Body'),
    );
    foreach ($messages as $message) {

      // Check plain everything so we can actually see the mark up if any
      $rows[] = array(
        $message['method'],
        check_plain($message['subject']),
        check_plain($message['body']),
      );
    }
    $output .= theme('table', $header, $rows);
    $output .= theme('pager', array(), MESSAGING_DEBUG_PAGER);
  }
  else {
    $output .= t('No logged messages');
  }
  return $output;
}

/**
 * Implementation of hook_messaging
 */
function messaging_debug_messaging($op = 'info') {
  switch ($op) {
    case 'send methods':
      $info['debug'] = array(
        'name' => t('Debug'),
        'destination' => 'name',
        'send' => 'messaging_debug_send_msg',
        'send_user' => 'messaging_debug_send_user',
        'type' => MESSAGING_TYPE_PULL,
        'glue' => '<br />',
        'description' => t('The messages will be just logged to watchdog.'),
      );
      return $info;
  }
}

/**
 * Just show message title to the user. 
 * 
 * This is a pull method though, so this is mainly intended for testing options
 */
function messaging_debug_send_user($account, $message, $method = 'debug') {
  $info = messaging_method_info('debug');
  $destination = array(
    $account,
  );
  return messaging_message_send($destination, $message, $method);
}

/**
 * Just show message title to the user. 
 * 
 * This is a pull method though, so this is mainly intended for testing options
 */
function messaging_debug_send_msg($destination, $message) {

  // Just logs everything.
  $text = '';
  $count = 0;
  $watchdog = array();
  $text = t('Message %key for %name: %subject', array(
    '%name' => $destination->name,
    '%key' => $message['type'],
    '%subject' => $message['subject'],
  ));
  messaging_log($text);
  $watchdog[] = t('Message body:') . '<br /><pre>' . $message['body'] . '</pre>';

  // Just log message body at the end
  watchdog('messaging', implode('<br />', $watchdog));
  return TRUE;
}

/**
 * Implementation of hook_footer()
 * 
 * Only debugging functionality for administrators
 */
function messaging_debug_footer() {
  if (user_access('administer messaging') && ($logs = messaging_log())) {
    $output .= '<div class="messaging-debug">';
    $lines = array();
    foreach ($logs as $log) {
      $lines[] = is_string($log) ? $log : '<pre>' . print_r($log, TRUE) . '</pre>';
    }
    $output .= implode('<br/>', $lines);
    $output .= '</div>';
    return $output;
  }
}

Functions

Namesort descending Description
messaging_debug_footer Implementation of hook_footer()
messaging_debug_form_alter Implementation of hook_form_alter()
messaging_debug_menu Implementation of hook_menu().
messaging_debug_messaging Implementation of hook_messaging
messaging_debug_send_msg Just show message title to the user.
messaging_debug_send_user Just show message title to the user.
messaging_debug_user_page Menu callback. Display pending messages to the user

Constants