You are here

function biblio_save_node in Bibliography Module 7.2

Same name and namespace in other branches
  1. 6.2 includes/biblio.import.export.inc \biblio_save_node()
  2. 6 biblio.import.export.inc \biblio_save_node()
  3. 7 includes/biblio.import.export.inc \biblio_save_node()
9 calls to biblio_save_node()
biblio_import_batch_operations in includes/biblio.import.export.inc
biblio_marc_biblio_import in modules/marcParse/biblio_marc.module
biblio_save_imported_nodes in includes/biblio.import.export.inc
Save node imported from a file.
EndNoteXMLParser::endnote7_endElement in modules/endnote/endnote_xml_parser.inc
EndNoteXMLParser::endnote8_endElement in modules/endnote/endnote_xml_parser.inc

... See full list

File

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

Code

function biblio_save_node($biblio, $terms = array(), $batch = FALSE, $session_id = NULL, $save_node = TRUE) {
  global $user;
  $wrapper = biblio_wrapper($biblio);
  module_load_include('inc', 'biblio', 'includes/biblio.fields');
  if ($batch && $session_id) {

    // we are batch processing some import data
    $cache['session_id'] = $session_id;
    $cache['data'] = base64_encode(serialize($biblio));

    // base64_encode to avoid problems unserializing strings with embeded quotes.
    drupal_write_record('biblio_import_cache', $cache);
    return;
  }

  // Persist the node revision log since it will be overridden by
  // node_object_prepare().
  $created = !empty($biblio->created) ? $biblio->created : NULL;
  $revision_log = !empty($biblio->log) ? $biblio->log : NULL;

  //  node_object_prepare($node);
  $biblio->created = $created;
  $biblio->log = $revision_log;
  if (!empty($terms)) {
    foreach ($terms as $key => $value) {
      $biblio->{$key} = $value;
    }
  }
  $biblio->language = 'und';

  //start by setting the language to undefined and then try to refine it.
  if (module_exists('locale')) {
    $biblio->language = locale_language_url_fallback();
  }
  if (module_exists('i18n') && variable_get('i18n_node_biblio', 0) && variable_get('language_content_type_biblio', 0)) {
    $biblio->language = module_invoke('i18n', 'default_language');
  }
  if (!isset($biblio->biblio_type)) {
    $biblio->biblio_type = 'miscellaneous';

    // default to misc if not set.
  }
  if ($save_node) {

    // $save_node = TRUE, the normal save path
    $validation_errors = array();

    //@todo: uncomment validation below and make it work

    //    $validation_errors = biblio_save_node_validate($biblio);
    biblio_create_imported_keywords($biblio);
    biblio_create_imported_contributors($biblio);
    biblio_save($biblio);

    // Get all possible field instances for the current publication type
    $instances = field_info_instances('biblio', $biblio->publication_type);

    // Add field instance data if it exists in both Drupal's stored instances,
    // and those field instances are also properties of the biblio entity
    foreach ($biblio as $field_name => $field_value) {

      //      // @todo: add keywords as taxonomy terms
      if (is_array($field_value)) {

        // Change contributor array values (arrays) to the contributor name
        // @todo: store contributor types somehow
        if ($field_name == 'biblio_contributors') {
          foreach ($field_value as $key => $contributor) {
            $field_value[$key] = $contributor['name'];
          }
        }
        foreach ($field_value as $key => $val) {
          $field_value[$key] = array(
            'value' => $val,
          );
        }
      }
      else {
        $field_value = array(
          array(
            'value' => $field_value,
          ),
        );
      }
    }
    if (!empty($validation_errors)) {
      foreach ($validation_errors as $field => $value) {
        $message = $field . t(' was truncated to fit into the database column');
        $link = l('biblio/' . $biblio->bid, 'biblio/' . $biblio->bid);
        drupal_set_message($message . '; ' . $link, 'warning');
        watchdog('biblio - import', $message, array(), WATCHDOG_ALERT, $link);
      }
    }
    return;

    // (isset($node->nid)) ? $node->nid : 0;
  }
  else {

    // $save_node = FALSE, primarily used to parse data and return it to the input form
    return (array) $biblio;
  }
}