You are here

function block_upload_plupload_form_submit in Block Upload 7

Saves files uploaded via plupload form.

Example taken from plupload_test_submit();

File

./block_upload.module, line 469
Block Upload module.

Code

function block_upload_plupload_form_submit($form, &$form_state) {
  $field = $form_state['field'];
  $path = block_upload_get_upload_destination($field);

  // We can't use file_save_upload() because of
  // http://www.jacobsingh.name/content/tight-coupling-no-not
  // file_uri_to_object();
  $buid = $form_state['buid'];
  $field_name = variable_get('block_upload_' . $buid . '_field', '');
  $node = node_load($form_state['node']->nid);
  if (isset($form_state['input']['remove_files'])) {
    if (array_filter($form_state['input']['remove_files'])) {
      block_upload_deletefiles($node, $field_name, $form_state);
    }
  }
  if (isset($form_state['values']['block_upload_file'])) {
    foreach ($form_state['values']['block_upload_file'] as $uploaded_file) {
      if ($uploaded_file['status'] == 'done') {
        $source = $uploaded_file['tmppath'];
        $destination = file_stream_wrapper_uri_normalize($path . '/' . $uploaded_file['name']);

        // Rename it to its original name, and put it in its final home.
        // Note - not using file_move here because if we call file_get_mime
        // (in file_uri_to_object) while it has a .tmp extension, it horks.
        $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
        $file = plupload_file_uri_to_object($destination);
        if ($field['widget']['module'] == 'file') {
          $file->display = 1;
        }
        file_save($file);
        $node->{$field_name}[LANGUAGE_NONE][] = (array) $file;
        drupal_set_message(t('File @name was successfully uploaded!', array(
          '@name' => $uploaded_file['name'],
        )));
      }
      else {

        // @todo move this to element validate or something and clean up t().
        form_set_error('block_upload_file', t('Upload of') . $uploaded_file['name'] . t('failed'));
      }
    }
  }

  // Do not change existing path alias if pathauto module installed.
  if (module_exists('pathauto')) {
    $node->path['pathauto'] = FALSE;
  }
  node_save($node);
}