You are here

function _mass_contact_save_node in Mass Contact 7

Same name and namespace in other branches
  1. 5.2 mass_contact.module \_mass_contact_save_node()
  2. 6 mass_contact.module \_mass_contact_save_node()

Archive a copy of the sent message as a node.

Parameters

array $params: The message array.

string $format: The text format the body is to use.

string $from: The email address representing who the message is from.

string $category: The categories of users sent to.

string $recipients: The users who received the message.

bool $result: The result of sending the message.

1 call to _mass_contact_save_node()
mass_contact_mail_page_submit in ./mass_contact.page.inc
Processes the main Mass Contact mail form.

File

./mass_contact.page.inc, line 1249
The main form for creating and sending the messages.

Code

function _mass_contact_save_node(array $params, $format, $from, $category, $recipients, $result) {

  // Create a new node object.
  $node = new stdClass();

  // Set the node type.
  $node->type = 'mass_contact';

  // Prepare the node object for editing and fill in a few default values.
  node_object_prepare($node);

  // Set the language code, if the Locale module is enabled.
  // global $language;
  // $node->language = ($language) ? $language->language : LANGUAGE_NONE;
  // The node data is not completely saved if $node->language is not set to
  // LANGUAGE_NONE.
  // @TODO: Find out if this is a bug in Drupal or not.
  $node->language = LANGUAGE_NONE;

  // Set the title of the node to the subject of the message.
  $node->title = $params['subject'];

  // Convert the body from an array to a string.
  $body = implode("\n\n", $params['body']);

  // Set the body of the node to the body of the message.
  $node->body[$node->language][0]['value'] = $body;

  // Set the teaser or summary of the body.
  $node->body[$node->language][0]['summary'] = text_summary($body);

  // Set the text format of the message body.
  $node->body[$node->language][0]['format'] = $format;

  // Set the From field.
  $node->field_mass_contact_from[$node->language][0]['value'] = $from;

  // Set the Category field.
  $node->field_mass_contact_category[$node->language][0]['value'] = $category;

  // Set the To field.
  $to = explode(',', $recipients);
  foreach ($to as $recipient) {
    $node->field_mass_contact_to[$node->language][]['value'] = $recipient;
  }

  // Set the BCC field.
  if (!empty($params['headers']['Bcc'])) {
    $to = explode(',', $params['headers']['Bcc']);
    foreach ($to as $recipient) {
      $node->field_mass_contact_bcc[$node->language][]['value'] = $recipient;
    }
  }

  // Set the Headers field.
  if (!empty($params['headers'])) {

    // Format the output so that it is readable.
    $headers = '';
    foreach ($params['headers'] as $header => $value) {
      $headers .= $header . ": " . $value . "\n";
    }

    // Set the body of the Headers.
    $node->field_mass_contact_headers[$node->language][0]['value'] = $headers;

    // Set the teaser or summary of the Headers.
    $node->field_mass_contact_headers[$node->language][0]['summary'] = text_summary($headers);

    // Set the text format of the Headers.
    $node->field_mass_contact_headers[$node->language][0]['format'] = 'plain_text';
  }

  // Set the Result field.
  // Set the body of the Result.
  $node->field_mass_contact_result[$node->language][0]['value'] = $result;

  // Set the teaser or summary of the Result.
  $node->field_mass_contact_result[$node->language][0]['summary'] = text_summary($result);

  // Set the text format of the Result.
  $node->field_mass_contact_result[$node->language][0]['format'] = 'plain_text';

  // If any attachments exist, add them.
  if (!empty($params['node_attachments'])) {
    foreach ($params['node_attachments'] as $attachment) {

      // Attach the attachment to the node.
      $node->field_mass_contact_attachments[$node->language][] = array(
        'fid' => $attachment->fid,
        'display' => 1,
        'dscription' => '',
        'uid' => $attachment->uid,
        'filename' => $attachment->filename,
        'uri' => $attachment->uri,
        'filemime' => $attachment->filemime,
        'filesize' => $attachment->filesize,
        'status' => $attachment->status,
        'timestamp' => $attachment->timestamp,
        'rdf_mapping' => array(),
      );
    }
  }

  // Save the node.
  node_save($node);

  // Log the node creation.
  watchdog('content', 'Mass Contact content added "%title".', array(
    '%title' => $params['subject'],
  ), WATCHDOG_NOTICE, l(t('View'), 'node/' . $node->nid));

  // Inform the user.
  drupal_set_message(t('A copy has been created as a node.'));
}