You are here

function privatemsg_attachments_privatemsg_message_insert in Privatemsg 6.2

Implements hook_privatemsg_message_insert().

File

privatemsg_attachments/privatemsg_attachments.module, line 283
Allows users to add attachments to messages

Code

function privatemsg_attachments_privatemsg_message_insert($message) {
  if (empty($message['files']) || !is_array($message['files'])) {
    return;
  }
  foreach ($message['files'] as $fid => $file) {

    // Convert file to object for compatibility
    $file = (object) $file;

    // Remove file. Process removals first since no further processing
    // will be required.
    if (!empty($file->remove)) {
      db_query('DELETE FROM {pm_attachments} WHERE fid = %d AND mid = %d', $fid, $message['mid']);
      file_delete($file->filepath);
      db_query('DELETE FROM {files} WHERE fid = %d', $fid);

      // Remove it from the session in the case of new uploads,
      // that you want to disassociate before node submission.
      unset($message['files'][$fid]);

      // Move on, so the removed file won't be added to new revisions.
      continue;
    }

    // Create a new revision, or associate a new file needed.
    if ($file->new) {
      db_query("INSERT INTO {pm_attachments} (fid, mid, list, description, weight) VALUES (%d, %d, %d, '%s', %d)", $file->fid, $message['mid'], $file->list, $file->description, $file->weight);
      file_set_status($file, FILE_STATUS_PERMANENT);
    }
  }
}