You are here

function _comment_upload_save_files in Comment Upload 5

1 call to _comment_upload_save_files()
comment_upload_comment in ./comment_upload.module
Implementation of hook_comment.

File

./comment_upload.module, line 220

Code

function _comment_upload_save_files($comment) {
  if (!is_array($comment['files'])) {
    return;
  }
  foreach ($comment['files'] as $file) {
    $file = (object) $file;

    // Remove file. Process removals first since no further processing will be required.
    if ($file->remove) {

      // Remove file previews...
      if (strpos($file->fid, 'upload') !== false) {
        file_delete($file->filepath);
      }
      else {
        file_delete($file->filepath);
        db_query('DELETE FROM {comment_upload_files} WHERE fid = %d', $file->fid);
      }
    }
    elseif (strpos($file->fid, 'upload') !== false) {
      if ($file = file_save_upload($file, $file->filename)) {

        // Overwrite existing in single-attachment mode
        if (variable_get('comment_upload_single', 0) && isset($comment['files'][0])) {
          db_query("UPDATE {comment_upload_files} SET filename = '%s', filepath = '%s', filemime = '%s', filesize = %d WHERE fid = %d", $file->filename, $file->filepath, $file->filemime, $file->filesize, $comment['files'][0]['fid']);
        }
        else {
          $file->fid = db_next_id('{comment_upload_files}_fid');
          db_query("INSERT INTO {comment_upload_files} (fid, nid, cid, filename, filepath, filemime, filesize, description, list) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, '%s', %d)", $file->fid, $comment['nid'], $comment['cid'], $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->description, $file->list);
        }
      }
      unset($_SESSION['file_previews'][$fid]);
    }
    else {
      db_query("UPDATE {comment_upload_files} SET list = %d, description = '%s' WHERE fid = %d", $file->list, $file->description, $file->fid);
    }
  }
}