You are here

message_notify.module in Message Notify 7

Same filename and directory in other branches
  1. 8 message_notify.module
  2. 7.2 message_notify.module

Message notify.

File

message_notify.module
View source
<?php

/**
 * @file
 * Message notify.
 */

/**
 * Process and send a message.
 */
function message_notify_send_mail(Message $message, $options = array()) {

  // Set default values.
  $options += array(
    'save on fail' => TRUE,
    'save on success' => TRUE,
    'rendered subject field' => FALSE,
    'rendered body field' => FALSE,
    // Override email address.
    'mail' => FALSE,
    // Override user's language, and use Message language.
    'language override' => FALSE,
  );
  $account = user_load($message->uid);
  $element = $message
    ->buildContent('full', $account->language);
  $message->rendered_subject = $element['subject']['#markup'];
  $message->rendered_body = $element['text']['#markup'];
  $wrapper = entity_metadata_wrapper('message', $message);
  if ($options['rendered subject field']) {
    $wrapper->{$options['rendered subject field']}
      ->set($message->rendered_subject);
  }
  if ($options['rendered body field']) {

    // Get the format from the field. We assume the first delta is the
    // same as the rest.
    if (empty($wrapper->{$options['rendered body field']}->format)) {
      $wrapper->{$options['rendered body field']}
        ->set($message->rendered_body);
    }
    else {
      $format = $wrapper->type->{MESSAGE_FIELD_MESSAGE_TEXT}
        ->get(0)->format
        ->value();
      $wrapper->{$options['rendered body field']}
        ->set(array(
        'value' => $message->rendered_body,
        'format' => $format,
      ));
    }
  }
  $params = array(
    'message_entity' => $message,
    'rendered_subject' => $message->rendered_subject,
    'rendered_body' => $message->rendered_body,
  );
  $mail = $options['mail'] ? $options['mail'] : $account->mail;
  $languages = language_list();
  if (!$options['language override']) {
    $lang = !empty($account->language) && $account->language != LANGUAGE_NONE ? $languages[$account->language] : language_default();
  }
  else {
    $lang = $languages[$message->language];
  }
  $result = drupal_mail('message_notify', $message->type, $mail, $lang, $params);
  if (!$result) {
    watchdog('message_notify', t('Could not send "@type" email to user ID @uid'), array(
      '@message' => $message->type,
      '@uid' => $message->uid,
    ), WATCHDOG_ERROR);
    if ($options['save on fail']) {
      $message
        ->save();
    }
  }
  elseif ($result && $options['save on success']) {
    $message
      ->save();
  }
  return $result;
}

/**
 * Implements hook_message_view().
 */
function message_notify_message_view($message, $view_mode, $langcode) {
  if ($message
    ->getType()->category == 'message_type_email') {
    $message->content['subject'] = array(
      // Add the subject field.
      '#markup' => $message
        ->getText($langcode, array(
        'field name' => 'message_text_subject',
      )),
    );
  }
}

/**
 * Implements hook_default_message_type_category().
 */
function message_notify_default_message_type_category() {
  $items = array();
  $items['message_type_email'] = entity_import('message_type_category', '{
    "category" : "message_type_email",
    "description" : "Message type email",
    "statusKey" : "status",
    "rdf_mapping" : []
  }');
  return $items;
}

/**
 * Implements hook_mail().
 *
 * Set's the message subject and body as configured.
 */
function message_notify_mail($key, &$message, $params) {
  $message['subject'] = $params['rendered_subject'];
  $message['body'][] = $params['rendered_body'];
}