You are here

function _mass_contact_save_node in Mass Contact 6

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

Save a copy of the sent message as a node.

Parameters

subject: The subject of the message and the title of the node.

body: The body of the message and node.

recipients: The users who received a copy of the message.

category: The category of users sent to.

roles: The various roles the category represents.

uid: The user who sent the message is also the user who owns the node.

bcc: A boolean that indicates if the recipients were placed in the BCC field.

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

File

./mass_contact.module, line 2278
This is the main code file for the Mass Contact module. This module enables users to contact multiple users through selected roles.

Code

function _mass_contact_save_node($subject, $body, $recipients, $category, $roles, $uid, $has_attachments, $bcc) {

  // get role names
  foreach ($roles as $role) {
    $roletemp = db_fetch_object(db_query("SELECT name FROM {role} WHERE rid = %d", $role));
    $rolesenta[] = $roletemp->name;
  }
  $rolesent = implode(', ', $rolesenta);

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

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

  // Include the file that has the function we need to use.
  if (include_once realpath('./') . '/' . drupal_get_path('module', 'node') . '/node.pages.inc') {

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

    // The file could not be included, so we put an error in the logs...
    watchdog('mass_contact', 'Could not include the "node.pages.inc" file, which is part of the Node module.', WATCHDOG_ERROR);

    // ...and force some defualts.
    $node->status = FALSE;
    $node->promote = FALSE;
    $node->sticky = FALSE;
  }

  // Fill in the rest of the details
  $node->title = $subject;

  // Add the body to the node.
  $node_body = '';
  if (variable_get('mass_contact_nodecc_include_categories', 1)) {
    $node_body .= '<p><em>' . t('Category: ') . $category . '</em></p>';
  }
  if (variable_get('mass_contact_nodecc_include_roles', 1)) {
    $node_body .= '<p><em>' . t('Roles: ') . $rolesent . '</em></p>';
  }
  if ($bcc) {
    if (variable_get('mass_contact_nodecc_include_bcc_recipients', 1)) {
      $node_body .= '<p><em>' . t('Recipients: ') . $recipients . '</em></p>';
    }
  }
  else {
    if (variable_get('mass_contact_nodecc_include_to_recipients', 1)) {
      $node_body .= '<p><em>' . t('Recipients: ') . $recipients . '</em></p>';
    }
  }
  $node->body = $node_body . $body . '</p>';

  // Set the node summary.
  $node->teaser = node_teaser($node->body);

  // Set the creater of the node.
  $node->uid = $uid;

  // Add any attachments, if they exist.
  if ($has_attachments) {
    for ($i = 1; $i <= variable_get('mass_contact_number_of_attachments', '3'); $i++) {
      if ($_FILES['files']['error']['attachment_' . $i] == UPLOAD_ERR_OK) {

        // Create and populate a file object.
        $file = new stdClass();
        $file->uid = $uid;
        $file->filename = $_FILES['files']['name']['attachment_' . $i];
        $file->filepath = $_FILES['files']['tmp_name']['attachment_' . $i];
        $file->filemime = $_FILES['files']['type']['attachment_' . $i];
        $file->filesize = $_FILES['files']['size']['attachment_' . $i];
        $file->status = FILE_STATUS_TEMPORARY;
        $file->timestamp = time();
        $file->filesource = $_FILES['files']['name']['attachment_' . $i];
        $file->list = 1;
        $file->new = TRUE;

        // Copy the file to Drupal's 'files' directory.
        if (!file_copy($file)) {
          drupal_set_message('Failed to save attachment: ' . $_FILES['files']['name']['attachment_' . $i]);
          continue;
        }

        // Save the file information to the database.
        drupal_write_record('files', $file);

        // Change file's status to permanent.
        file_set_status($file, FILE_STATUS_PERMANENT);

        // Add the attachment to the node.
        $node->files[$file->fid] = $file;
      }
    }
  }

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

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

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