You are here

function _mass_contact_process_mime_mail_attachments in Mass Contact 7

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

Processes attachments for use with Mime Mail.

Parameters

bool $nodecc: Identifies whether a copy of the message will be saved as a node.

Return value

array The attachment information, as Mime Mail expects it.

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

File

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

Code

function _mass_contact_process_mime_mail_attachments($nodecc = FALSE) {
  $files = array();

  // Loop through each possible attachment.
  for ($i = 1; $i <= variable_get('mass_contact_number_of_attachments', '3'); $i++) {

    // Check to see if an attachment exists.
    if ($_FILES['files']['size']['attachment_' . $i] > 0) {

      // If the message is to be saved as a node, we need to save the
      // attachment now so that it is not lost later.
      if ($nodecc) {

        // The name in the global $_FILES array where the information about the
        // attachment is stored.
        $file_source = 'attachment_' . $i;
        $file_validators = array();

        // Get the directory where attachments are to be saved.
        $file_destination = 'public://' . variable_get('mass_contact_attachment_location');
        $file_replace = FILE_EXISTS_RENAME;

        // Check to see that the directory exists.
        if (!file_prepare_directory($file_destination)) {

          // It does not, so create the directoty.
          drupal_mkdir($file_destination);
        }

        // Save the file to the Mass Contact attachments directory.
        $file = file_save_upload($file_source, $file_validators, $file_destination, $file_replace);

        // Check to see if there was an error during the save.
        if ($file === FALSE) {

          // There was an error, so report it.
          $message = 'There was an error uploading the "!attachment" attachment.';
          $variables = array(
            '!attachment' => $_FILES['files']['name']['attachment_' . $i],
          );
          drupal_set_message(t($message, $variables), 'error');
          watchdog('mass_contact', $message, $variables, WATCHDOG_ERROR);
        }
        elseif ($file === NULL) {

          // Nothing happened, so say so.
          $message = 'The "!attachment" attachment was not uploaded.';
          $variables = array(
            '!attachment' => $_FILES['files']['name']['attachment_' . $i],
          );
          drupal_set_message(t($message, $variables), 'error');
          watchdog('mass_contact', $message, $variables, WATCHDOG_ERROR);
        }
        else {

          // Set the status of the uploaded file so it stays around for a while.
          $file->status = FILE_STATUS_PERMANENT;

          // Save the file object to the database.
          $file = file_save($file);

          // Check the state to determine if there was an error or not.
          if (!$file) {

            // If there was an error, report it.
            $message = 'There was an error saving the file object for "!attachment" to the database.';
            $variables = array(
              '!attachment' => $_FILES['files']['name']['attachment_' . $i],
            );
            drupal_set_message(t($message, $variables), 'error');
            watchdog('mass_contact', $message, $variables, WATCHDOG_ERROR);
          }
          else {

            // Add the file object to the files collection array.
            $node_files[] = $file;

            // Save the file information for the message.
            $real_path = drupal_realpath($file->uri);
            $files[] = array(
              'filepath' => $real_path,
              'uri' => $real_path,
              'filecontent' => file_get_contents($real_path),
              'filename' => $_FILES['files']['name']['attachment_' . $i],
              'filemime' => $_FILES['files']['type']['attachment_' . $i],
            );
          }
        }
      }
      else {
        $files[] = array(
          'filepath' => $_FILES['files']['tmp_name']['attachment_' . $i],
          'uri' => $_FILES['files']['tmp_name']['attachment_' . $i],
          'filecontent' => file_get_contents($_FILES['files']['tmp_name']['attachment_' . $i]),
          'filename' => $_FILES['files']['name']['attachment_' . $i],
          'filemime' => $_FILES['files']['type']['attachment_' . $i],
        );
      }
    }
  }
  $files_array['files'] = $files;
  if (!empty($node_files)) {
    $files_array['node_files'] = $node_files;
  }
  return $files_array;
}