You are here

function _biblio_bibtex_import in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 modules/bibtexParse/biblio_bibtex.module \_biblio_bibtex_import()
  2. 7 modules/bibtexParse/biblio_bibtex.module \_biblio_bibtex_import()
1 call to _biblio_bibtex_import()
biblio_bibtex_biblio_import in modules/bibtexParse/biblio_bibtex.module

File

modules/bibtexParse/biblio_bibtex.module, line 213

Code

function _biblio_bibtex_import($entries, $terms = array(), $batch = FALSE, $session_id = NULL, $save = TRUE) {
  module_load_include('inc', 'biblio', 'includes/biblio.fields');
  module_load_include('inc', 'biblio', 'includes/biblio.import.export');
  global $user;
  $bids = array();
  $dups = array();
  foreach ($entries as $entry) {
    $type = _biblio_bibtex_type_map($entry['bibtexEntryType'], 'import');

    // Create a new, empty biblio object
    $biblio = biblio_create($type);
    $wrapper = biblio_wrapper($biblio, 'biblio');
    $biblio->uid = $user->uid;
    $biblio->biblio_contributors = array();
    switch ($entry['bibtexEntryType']) {
      case 'mastersthesis':
        $biblio->biblio_type_of_work = 'masters';
        break;
      case 'phdthesis':
        $biblio->biblio_type_of_work = 'phd';
        break;
    }
    if (!empty($entry['author'])) {

      // split on ' and '
      $authorArray = preg_split("/\\s(and|&)\\s/i", trim($entry['author']));
      foreach ($authorArray as $key => $author) {
        $biblio->biblio_contributors[] = array(
          'name' => $author,
          'category' => 'primary',
        );
      }
    }
    $wrapper->biblio_citekey = !empty($entry['bibtexCitation']) ? $entry['bibtexCitation'] : NULL;
    if (!empty($entry['editor'])) {
      $authorArray = preg_split("/\\s(and|&)\\s/i", trim($entry['editor']));
      foreach ($authorArray as $key => $author) {
        $biblio->biblio_contributors[] = array(
          'name' => $author,
          'category' => 'secondary',
        );
      }
    }

    // // Split contributor data into their own 'auth_category'-specific fields (primary, secondary, tertiary, etc)
    // foreach($biblio->biblio_contributors as $key => $contributor_data) {
    //   switch ($contributor_data['category']) {
    //     case :
    //       $biblio->biblio_authors[] = $contributor_data['name'];
    //       break;
    //     case 2:
    //       $biblio->biblio_secondary_authors[] = $contributor_data['name'];
    //       break;
    //   }
    // }
    $wrapper->biblio_title_secondary = !empty($entry['journal']) ? $entry['journal'] : NULL;
    if (!empty($entry['booktitle'])) {
      $wrapper->biblio_title_secondary = $entry['booktitle'];
    }
    if (!empty($entry['series'])) {
      if (!empty($entry['booktitle'])) {
        $wrapper->biblio_title_tertirary = $entry['series'];
      }
      else {
        $wrapper->biblio_title_secondary = $entry['series'];
      }
    }
    $wrapper->biblio_volume = !empty($entry['volume']) ? $entry['volume'] : NULL;
    $wrapper->biblio_number = !empty($entry['number']) ? $entry['number'] : NULL;
    $wrapper->biblio_year = !empty($entry['year']) ? $entry['year'] : 0;
    $wrapper->biblio_notes = !empty($entry['note']) ? $entry['note'] : NULL;
    $wrapper->biblio_date = !empty($entry['month']) ? $entry['month'] : NULL;
    $wrapper->biblio_pages = !empty($entry['pages']) ? $entry['pages'] : NULL;
    $wrapper->biblio_publisher = !empty($entry['publisher']) ? $entry['publisher'] : NULL;
    if (!empty($entry['organization'])) {
      $wrapper->biblio_publisher = $entry['organization'];
    }
    if (!empty($entry['school'])) {
      $wrapper->biblio_publisher = $entry['school'];
    }
    if (!empty($entry['institution'])) {
      $wrapper->biblio_publisher = $entry['institution'];
    }
    $wrapper->biblio_title = !empty($entry['title']) ? $entry['title'] : NULL;
    $type_of_work = !empty($entry['type']) ? $entry['type'] . $type_of_work : NULL;
    $wrapper->biblio_type_of_work = !empty($type_of_work) ? $type_of_work : NULL;
    $wrapper->biblio_edition = !empty($entry['edition']) ? $entry['edition'] : NULL;
    $wrapper->biblio_section = !empty($entry['chapter']) ? $entry['chapter'] : NULL;
    $wrapper->biblio_place_published = !empty($entry['address']) ? $entry['address'] : NULL;
    $wrapper->biblio_abst_e = !empty($entry['abstract']) ? array(
      'value' => $entry['abstract'],
    ) : NULL;
    if (!empty($entry['keywords'])) {
      if (strpos($entry['keywords'], ';')) {
        $entry['keywords'] = str_replace(';', ',', $entry['keywords']);
      }
      $biblio->biblio_keywords = explode(',', $entry['keywords']);
    }
    $wrapper->biblio_isbn = !empty($entry['isbn']) ? $entry['isbn'] : NULL;
    $wrapper->biblio_issn = !empty($entry['issn']) ? $entry['issn'] : NULL;
    $wrapper->biblio_url = !empty($entry['url']) ? $entry['url'] : NULL;
    $wrapper->biblio_doi = !empty($entry['doi']) ? $entry['doi'] : NULL;
    $temp_properties = biblio_remove_variable_properties($biblio);
    $biblio->biblio_bibtex_md5 = md5(serialize((array) $biblio));
    biblio_retrieve_variable_properties($biblio, $temp_properties);
    $biblio->created = $temp_properties['created'];
    $biblio->changed = $temp_properties['changed'];
    $biblio->biblio_import_type = 'bibtex';
    if (!($dup = biblio_bibtex_check_md5($biblio->biblio_bibtex_md5))) {
      if ($save) {
        entity_get_controller('biblio');
        biblio_save_node($biblio, $terms, $batch, $session_id, $save);
        $bids[] = !empty($biblio->bid) ? $biblio->bid : NULL;
      }
      else {

        // return the whole node if we are not saveing to the DB (used for the paste function on the input form)
        $bids[] = $biblio;
      }
    }
    else {
      $dups[] = $dup;
    }
  }
  return array(
    $bids,
    $dups,
  );
}