You are here

function _mass_contact_save_node in Mass Contact 5.2

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

Save the sent mail 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.

$has_attachments: An boolean indictaing whether there were attachments in the message.

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 1685
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) {

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

  // Create and populate the node object.
  $node = new stdClass();
  $node->title = $subject;
  $node->body = '<p><i>' . t('Category: ') . $category . '</i><p><i>' . t('Roles: ') . $rolesent . '</i><p><i>' . t('Recipients: ') . $recipients . '</i><p>' . $body;
  $node->teaser = node_teaser($node->body);
  $node->type = 'mass_contact';
  $node->uid = $uid;

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

  // Get new node id.
  $nidobj = db_fetch_object(db_query("SELECT nid FROM {node} WHERE type = 'mass_contact' ORDER BY changed DESC"));
  $node->nid = $nidobj->nid;

  // 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->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->list = 1;
        $file->new = true;

        // Copy the file to Drupal's 'files' directory.
        $new_file = file_save_upload($file, $file->filename);
        if ($new_file) {
          $new_file->fid = db_next_id('{files}_fid');
          $node->files[$new_file->fid] = $new_file;
          node_save($node);
          db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
          db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $file->fid, $node->vid, $file->list, $file->description);
        }
        else {
          drupal_set_message('Failed to save attachment: ' . $_FILES['files']['name']['attachment_' . $i]);
          continue;
        }
      }
    }
  }

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

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