You are here

messaging_simple.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

@ TODO: Add ability to delete queued messages

File

messaging_simple/messaging_simple.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
 * 
 * @ TODO: Add ability to delete queued messages
 */

// Number of messages to display per page
define('MESSAGING_SIMPLE_PAGER', 20);

/**
 * Implementation of hook_menu().
 */
function messaging_simple_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 . '/messages',
          'type' => MENU_LOCAL_TASK,
          'title' => t('Messages'),
          'callback' => 'messaging_simple_user_page',
          'callback arguments' => array(
            $account,
          ),
        );
      }
    }
  }
  return $items;
}

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

  // Fetch all pending messages.
  switch ($op) {
    case 'view':
      return drupal_get_form('messaging_simple_user_message', $account, $id);
      break;
    default:
      return messaging_simple_user_overview($account);
  }
}

/**
 * Display message overview list
 */
function messaging_simple_user_overview($account) {
  $output = '';
  $messages = messaging_store('get', array(
    'method' => 'simple',
    'uid' => $account->uid,
  ), array(
    'mqid DESC',
  ), MESSAGING_SIMPLE_PAGER, 0, TRUE);
  if ($messages) {
    $header = array(
      t('Subject'),
      t('From'),
      t('Date'),
    );
    $rows = array();
    foreach ($messages as $message) {
      $rows[] = array(
        l($message['subject'], 'user/' . $account->uid . '/messages/view/' . $message['mqid']),
        !empty($message['sender_account']) ? theme('username', $message['sender_account']) : t('System'),
        format_date($message['created']),
      );
    }
    $output .= theme('table', $header, $rows);
    $output .= theme('pager', array(), MESSAGING_SIMPLE_PAGER);
  }
  else {
    $output .= t('No pending messages');
  }
  return $output;
}

/**
 * Display single message
 */
function messaging_simple_user_message($account, $id) {
  $form = array();
  if ($messages = messaging_store('get', array(
    'mqid' => $id,
    'method' => 'simple',
    'uid' => $account->uid,
  ))) {
    $message = array_shift($messages);
    $form['account'] = array(
      '#type' => 'value',
      '#value' => $account,
    );
    $form['message'] = array(
      '#type' => 'value',
      '#value' => $message,
    );
    $form['subject'] = array(
      '#type' => 'item',
      '#title' => t('Subject'),
      '#value' => $message['subject'],
    );
    $form['body'] = array(
      '#type' => 'item',
      '#title' => t('Content'),
      '#value' => check_markup($message['body']),
    );
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
    $form['#redirect'] = 'user/' . $account->uid . '/messages';
  }
  return $form;
}

/**
 * Message form submit
 */
function messaging_simple_user_message_submit($form_id, $form_values) {
  messaging_store('del', array(
    'mqid' => $form_values['message']['mqid'],
  ));
  drupal_set_message(t('The message has been deleted'));
}

/**
 * Implementation of hook_messaging
 */
function messaging_simple_messaging($op = 'info') {
  switch ($op) {
    case 'send methods':
      $info['simple'] = array(
        'name' => t('Simple'),
        'group' => 'web',
        'send' => 'messaging_simple_send_msg',
        'type' => MESSAGING_TYPE_PULL,
        'glue' => '<br />',
        'description' => t('Don\'t send messages but display them on a user account tab'),
      );
      return $info;
  }
}

/**
 * Just show message title to the user. 
 * 
 * This is a pull method though, so this is mainly intended for testing options
 * 
 * Do nothing, the message will be retrieved from the queue
 */
function messaging_simple_send_msg($destination, $message) {
  return TRUE;
}

Functions

Namesort descending Description
messaging_simple_menu Implementation of hook_menu().
messaging_simple_messaging Implementation of hook_messaging
messaging_simple_send_msg Just show message title to the user.
messaging_simple_user_message Display single message
messaging_simple_user_message_submit Message form submit
messaging_simple_user_overview Display message overview list
messaging_simple_user_page Menu callback. Display pending messages to the user

Constants