You are here

function _biblio_tagged_import in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 modules/endnote/biblio_tagged.module \_biblio_tagged_import()
  2. 7 modules/endnote/biblio_tagged.module \_biblio_tagged_import()

Export data in tagged format.

Parameters

$result: a database result set pointer

Return value

none

1 call to _biblio_tagged_import()
biblio_tagged_biblio_import in modules/endnote/biblio_tagged.module

File

modules/endnote/biblio_tagged.module, line 146

Code

function _biblio_tagged_import($file, $terms = array(), $batch = FALSE, $session_id = NULL) {
  ini_set('auto_detect_line_endings', true);
  if (!($fp = fopen($file->uri, "r"))) {
    drupal_set_message(t("Could not open EndNote Tagged input"), 'error');
    return;
  }
  $nids = array();
  $dups = array();
  $ignored_tags = array();
  $node = NULL;
  $incite = FALSE;
  $node_id = NULL;
  $contributors = NULL;
  while (!feof($fp)) {
    $line = trim(fgets($fp));
    $line_len = strlen($line);
    if ($line_len) {
      $start = strpos($line, "%");

      // There could be some unprintables at the beginning of the line so fine the location of the %
      if ($start !== FALSE) {
        $tag = drupal_substr($line, $start, 2);
        $value = trim(drupal_substr($line, $start + 3));
      }
      else {
        $value = $line;
      }
    }
    if ($line_len) {

      // if this is not a blank line
      if (!$incite) {
        $incite = TRUE;
        $node = new stdClass();

        //$node->biblio_contributors = array();
      }
      switch ($tag) {
        case '%0':
          $node->biblio_type = _biblio_tagged_type_map($value);
          break;
        case '%A':
          $node->biblio_contributors[] = array(
            'name' => $value,
            'auth_category' => 1,
            'auth_type' => _biblio_get_auth_type(1, $node->biblio_type),
          );
          break;
        case '%E':
          $node->biblio_contributors[] = array(
            'name' => $value,
            'auth_category' => 2,
            'auth_type' => _biblio_get_auth_type(2, $node->biblio_type),
          );
          break;
        case '%T':
          $node->title = $value;
          break;
        case '%Y':
          $node->biblio_contributors[] = array(
            'name' => $value,
            'auth_category' => 3,
            'auth_type' => _biblio_get_auth_type(3, $node->biblio_type),
          );
          break;
        case '%?':
          $node->biblio_contributors[] = array(
            'name' => $value,
            'auth_category' => 4,
            'auth_type' => _biblio_get_auth_type(4, $node->biblio_type),
          );
          break;
        case '%X':
          $node->biblio_abst_e .= $value;
          break;
        case '%Z':
          $node->biblio_notes .= $value;
          break;
        default:
          $field = _biblio_tagged_field_map($tag);
          if (!empty($field)) {
            $node->{$field} = $value;
          }
          else {
            if (!in_array($tag, $ignored_tags)) {
              $ignored_tags[] = $tag;
            }
          }
          break;
      }

      //end switch
    }
    else {
      $incite = FALSE;
      if (!empty($node)) {
        _biblio_tagged_save($node, $terms, $batch, $session_id, $nids, $dups);
        $node = NULL;
      }
    }

    // end if ($start !== FALSE)
  }

  // end while
  fclose($fp);
  if ($incite && !empty($node)) {

    // this catches the case where the file ends without a blank line at the end
    _biblio_tagged_save($node, $terms, $batch, $session_id, $nids, $dups);
  }
  if (!empty($ignored_tags)) {
    $ignored_tags = array_unique($ignored_tags);
    $message = t("The following elements were ignored because they do not map to any biblio fields: ");
    $message .= implode(', ', $ignored_tags);
    if (user_access('administer biblio')) {
      $message .= '. ' . t('Click !url if you wish to check the field mapping', array(
        '!url' => l(t('here'), 'admin/config/content/biblio/iomap/edit/tagged'),
      ));
    }
    drupal_set_message($message, 'warning');
  }
  return array(
    $nids,
    $dups,
  );
}