You are here

function biblio_import_form_submit in Bibliography Module 6

Same name and namespace in other branches
  1. 6.2 includes/biblio.import.export.inc \biblio_import_form_submit()
  2. 7 includes/biblio.import.export.inc \biblio_import_form_submit()
  3. 7.2 includes/biblio.import.export.inc \biblio_import_form_submit()

Implementation of hook_submit() for the biblio_import_form.

File

./biblio.import.export.inc, line 179
Functions that are used to import and export biblio data.

Code

function biblio_import_form_submit($form, &$form_state) {
  global $user;
  if ($form_state['values']['op'] == t('Import') && isset($form_state['values']['filetype'])) {
    if ($import_file = file_save_upload('biblio_import_file')) {
      if ($form_state['values']['batch_process'] == 1) {
        $batch_proc = TRUE;
      }

      // we will use batch import for larger files.
      // Concatenate all the terms of the different vocabularies
      // in a single array to be sent to biblio_import
      $terms = array();
      foreach (array_keys($form_state['values']) as $key) {
        if (preg_match('/(vocabulary[0-9]+)/', $key)) {
          if (!empty($form_state['values'][$key])) {
            if (is_array($form_state['values'][$key])) {
              $terms[] = $form_state['values'][$key];
            }
            else {
              $terms[] = array(
                $form_state['values'][$key],
              );
            }
          }
        }
        if ($key == 'copy_to_biblio') {
          $terms['copy_to_biblio'] = $form_state['values'][$key];
        }
      }

      // Added the $terms argument
      // the array of terms to be attached to the node(s)
      $userid = isset($form_state['values']['userid']) ? $form_state['values']['userid'] : $user->uid;
      $filetype = $form_state['values']['filetype'];
      $filesize = sprintf("%01.1f", $import_file->filesize / 1000);
      $filesize = " ({$filesize} KB)";
      if ($batch_proc) {
        $session_id = md5(microtime());
        $batch_op = array(
          'title' => t('Importing ') . $import_file->filename . $filesize,
          'operations' => array(
            array(
              'biblio_import',
              array(
                $import_file,
                $filetype,
                $userid,
                $terms,
                $batch_proc,
                $session_id,
              ),
            ),
            array(
              'biblio_import_batch_operations',
              array(
                $session_id,
                $user,
                $userid,
                $terms,
              ),
            ),
          ),
          'progressive' => TRUE,
          'finished' => 'biblio_import_batch_finished',
          'init_message' => t('Parsing file...'),
          'progress_message' => t('Saving nodes...'),
          'file' => './' . drupal_get_path('module', 'biblio') . '/biblio.import.export.inc',
        );
        batch_set($batch_op);
        $base = variable_get('biblio_base', 'biblio');
        batch_process("{$base}/import");
      }
      else {

        //not batch processing the file
        $session_id = md5(microtime());
        $context = array();
        biblio_import($import_file, $filetype, $userid, $terms, $batch_proc, $session_id, $context);
        biblio_import_finalize(TRUE, $context['results']);
      }
      file_delete($import_file->filepath);
    }
    else {
      drupal_set_message(t("File was NOT successfully uploaded"), 'error');
    }
  }
}