You are here

function biblio_import_form_submit in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 includes/biblio.import.export.inc \biblio_import_form_submit()
  2. 6 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

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

Code

function biblio_import_form_submit($form, &$form_state) {
  global $user;
  $batch_proc = FALSE;
  $extensions = 'xml bib enw mrc ris txt';
  $validators['file_validate_extensions'] = array();
  $validators['file_validate_extensions'][0] = $extensions;
  if ($form_state['values']['op'] == t('Import') && isset($form_state['values']['filetype'])) {
    if ($import_file = file_save_upload('biblio_import_file', $validators)) {
      if ($form_state['values']['batch_process'] == 1) {

        // We will use batch import for larger files.
        $batch_proc = TRUE;
      }

      // Concatenate all the terms of the different vocabularies
      // in a single array to be sent to biblio_import.
      $terms = array();
      if (isset($form_state['values']['term_refs'])) {
        foreach ($form_state['values']['term_refs'] as $key) {
          if (isset($form_state['values'][$key]) && !empty($form_state['values'][$key])) {
            foreach ($form_state['values'][$key]['und'] as $id => $item) {
              if (!is_array($item) || empty($item['tid']) && (string) $item['tid'] !== '0') {
                unset($form_state['values'][$key]['und'][$id]);
              }
            }
            if (empty($form_state['values'][$key]['und'])) {
              unset($form_state['values'][$key]);
            }
            else {
              $terms[$key] = $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 = microtime();
        $batch_op = array(
          'title' => t('Importing @filename', array(
            '@filename' => $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') . '/includes/biblio.import.export.inc',
        );
        batch_set($batch_op);
        $base = variable_get('biblio_base', 'biblio');
        batch_process("{$base}/import");
      }
      else {
        $session_id = 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);
    }
    else {
      drupal_set_message(t("File was NOT successfully uploaded"), 'error');
    }
  }
}