You are here

biblio_marc.module in Bibliography Module 7.2

File

modules/marcParse/biblio_marc.module
View source
<?php

/*
 * @file biblio_marc.module
 *
 */

/*
 *   add the marc option to the option list of the biblio_import_form
 *   the key is the module name use by module_invoke to call hook_biblio_import
 *   module_invoke('biblio_marc', 'biblio_import',...)
 */
function biblio_marc_biblio_import_options() {
  return array(
    'biblio_marc' => t('MARC'),
  );
}
function biblio_marc_node_delete($node) {
  if ($node->type != 'biblio') {
    return;
  }
  db_delete('biblio_marc')
    ->condition('nid', $node->nid)
    ->execute();
}
function biblio_marc_node_insert($node) {
  if ($node->type != 'biblio') {
    return;
  }
  if (!isset($node->biblio_marc_md5)) {
    return;
  }
  drupal_write_record('biblio_marc', $node);
}
function biblio_marc_form_biblio_node_form_alter(&$form, &$form_state) {
  global $user;
  if (!$form_state['submitted'] && !isset($form_state['values']) && !isset($form['#node']->nid)) {
    if (!$form_state['submitted']) {
      $form['biblio_cut_paste'] = array(
        '#type' => 'fieldset',
        '#title' => t('Paste MARC Record'),
        '#weight' => -20,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['biblio_cut_paste']['paste_data_marc'] = array(
        '#type' => 'textarea',
        '#title' => t('MARC'),
        '#required' => FALSE,
        '#default_value' => isset($form_state['values']['paste_data_marc']) ? $form_state['values']['paste_data_marc'] : '',
        '#description' => t('Paste a  entry here'),
        '#size' => 60,
        '#weight' => -4,
      );
      $form['biblio_cut_paste']['paste_submit'] = array(
        '#type' => 'submit',
        '#value' => t('Populate using MARC'),
        '#submit' => array(
          'biblio_marc_form_biblio_node_form_submit',
        ),
      );
    }
  }
  $biblio_marc_id = isset($form_state['values']['biblio_marc_id']) ? $form_state['values']['biblio_marc_id'] : '';
  $biblio_marc_md5 = isset($form_state['values']['biblio_marc_md5']) ? $form_state['values']['biblio_marc_md5'] : '';
  $form['biblio_marc_id'] = array(
    '#type' => 'value',
    '#value' => $biblio_marc_id,
  );
  $form['biblio_marc_md5'] = array(
    '#type' => 'value',
    '#value' => $biblio_marc_md5,
  );
}
function biblio_marc_form_biblio_node_form_submit($form, &$form_state) {
  global $user;
  $node_data = array();
  $dups = array();
  if (strlen($form_state['values']['paste_data_marc'])) {
    list($node_data, $dups) = biblio_marc_biblio_import($form_state['values']['paste_data_marc'], array(), FALSE, NULL, FALSE, TRUE);
  }
  if (!empty($node_data) && is_object($node_data[0])) {
    $form_state['values'] = array_merge($form_state['values'], (array) $node_data[0]);
    $form_state['input']['biblio_type'] = $form_state['biblio_type'] = $node_data[0]->biblio_type;

    //    $form_state['storage']['biblio_type']  = $node_data[0]->biblio_type;
  }
  elseif (!empty($dups)) {
    $message = t('The MARC node that you are trying to paste into the form already exists in the database, see !url', array(
      '!url' => l('node/' . $dups[0], 'node/' . $dups[0]),
    ));
    form_set_error('paste_data_marc', $message);
  }
  $form_state['rebuild'] = TRUE;
  return;
}
function biblio_marc_biblio_import($file, $terms = array(), $batch = FALSE, $session_id = NULL, $save = TRUE, $string = FALSE) {
  $nids = array();
  $dups = array();
  module_load_include('php', 'biblio_marc', 'php-marc');
  if (is_object($file) && isset($file->uri) && !$string) {
    $file = $file->uri;
  }
  $marcfile = new File($file);
  while ($record = $marcfile
    ->next()) {
    $node = new stdClass();
    $node->biblio_contributors = array();
    $leader = $record
      ->leader();
    $pubtype = $leader[6];
    $pubtype .= $leader[7];
    $node->biblio_type = _biblio_marc_type_map($pubtype);
    foreach ($record
      ->fields() as $fields) {
      foreach ($fields as $field) {
        $tagnum = $field->tagno;
        switch ($tagnum) {
          case '008':
            $data = $field
              ->data();
            $node->biblio_year = substr($data, 7, 4);
            $node->biblio_lang = substr($data, 35, 3);
            break;
          case '020':
            $node->biblio_isbn = $field
              ->subfield('a');
            break;
          case '022':
            $node->biblio_issn = $field
              ->subfield('a');
            break;
          case '024':
            $node->biblio_other_number = $field
              ->subfield('a');
            break;
          case '050':

          //LIBRARY OF CONGRESS CALL NUMBER
          case '055':

          //CLASSIFICATION NUMBERS ASSIGNED IN CANADA
          case '060':

            //NATIONAL LIBRARY OF MEDICINE CALL NUMBER
            $node->biblio_call_number = $field
              ->subfield('a');
            break;
          case '130':
            $node->title = str_replace(' /', '', $field
              ->subfield('a'));
            break;
          case '210':
            $node->biblio_short_title = str_replace(' /', '', $field
              ->subfield('a'));
            break;
          case '245':
            $node->title = str_replace(' /', '', $field
              ->subfield('a')) . ' ' . $field
              ->subfield('b');
            break;
          case '250':
            $node->biblio_edition = $field
              ->subfield('a');
            break;
          case '260':
            $node->biblio_place_published = str_replace(' :', '', $field
              ->subfield('a'));
            $node->biblio_publisher = $field
              ->subfield('b');
            $node->biblio_date = $field
              ->subfield('c');
            break;
          case '300':
            $node->biblio_pages = $field
              ->subfield('a');
            break;
          case '490':
            $node->biblio_volume = $field
              ->subfield('v');
            break;
          case $tagnum >= 500 && $tagnum <= 599:
            $value = $field
              ->subfield('a');
            if (!empty($value)) {
              $node->biblio_notes .= $value;
            }
            break;
          case '650':
            foreach ($field
              ->subfields() as $subject) {
              $node->biblio_keywords[] = $subject[0];
            }
            break;
          case '100':
          case '700':
            $value = $field
              ->subfield('a');
            if (!empty($value)) {
              $node->biblio_contributors[] = array(
                'name' => $value,
                'auth_category' => 1,
                'auth_type' => 1,
              );
            }
            break;
          case '110':
          case '710':
            $node->biblio_contributors[] = array(
              'name' => $field
                ->subfield('a'),
              'auth_category' => 5,
              'auth_type' => 5,
            );
            break;
          case '856':
            $value = $field
              ->subfield('u');
            if (!empty($value)) {
              $node->biblio_url = $value;
            }
            break;
        }
      }
    }
    if (!empty($node)) {
      $node->biblio_marc_md5 = md5(serialize($node));
      if (!($dup = biblio_marc_check_md5($node->biblio_marc_md5))) {
        if ($save) {
          biblio_save_node($node, $terms, $batch, $session_id);
          if (!empty($node->nid)) {
            $nids[] = $node->nid;
          }
        }
        else {
          $nids[] = $node;
        }
      }
      else {
        $dups[] = $dup;
      }
    }
  }
  return array(
    $nids,
    $dups,
  );
}
function biblio_marc_check_md5($md5) {
  static $marc_md5s = array();
  if (empty($marc_md5s)) {
    $result = db_query("SELECT * FROM {biblio_marc} ");
    foreach ($result as $row) {
      $marc_md5s[$row->biblio_marc_md5] = $row->nid;
    }
  }
  if (isset($marc_md5s[$md5])) {
    return $marc_md5s[$md5];
  }
  else {
    $marc_md5s[$md5] = TRUE;

    // gaurd against duplicates in the same import
    return;
  }
}
function _biblio_marc_type_map($type, $reverse = FALSE) {
  static $map = array();
  if (empty($map)) {
    $map = biblio_get_map('type_map', 'marc');
  }
  if ($reverse) {
    return ($tag = array_search($type, $map)) ? $tag : 'Generic';

    //return the biblio type or 129 (Misc) if type not found
  }
  return isset($map[$type]) ? $map[$type] : 129;

  //return the biblio type or 129 (Misc) if type not found
}