upload_save

Versions
4.6
upload_save($node)
4.7 – 6
upload_save(&$node)
7
upload_save($node)

Code

modules/upload/upload.module, line 478

<?php
function upload_save($node) {
  if (empty($node->files) || !is_array($node->files)) {
    return;
  }

  foreach ($node->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)) {
      // Remove the reference from this revision.
      db_delete('upload')->condition('fid', $file->fid)->condition('vid', $node->vid)->execute();
      // Try a soft delete, if the file isn't used elsewhere it'll be deleted.
      file_delete($file);
      // Remove it from the session in the case of new uploads,
      // that you want to disassociate before node submission.
      unset($node->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 (!empty($node->old_vid) || $file->new) {
      db_insert('upload')
        ->fields(array(
          'fid' => $file->fid,
          'nid' => $node->nid,
          'vid' => $node->vid,
          'list' => $file->list,
          'description' => $file->description,
          'weight' => $file->weight,
        ))
        ->execute();
    }
    // Update existing revision.
    else {
      db_update('upload')
        ->fields(array(
          'list' => $file->list,
          'description' => $file->description,
          'weight' => $file->weight,
        ))
        ->condition('fid', $file->fid, '=')
        ->condition('vid', $node->vid, '=')
        ->execute();
    }
    $file->status |= FILE_STATUS_PERMANENT;
    $file = file_save($file);
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.