Index: millennium.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/millennium/millennium.module,v
retrieving revision 1.13.2.33.2.2.2.53
diff -u -r1.13.2.33.2.2.2.53 millennium.module
--- millennium.module	9 Dec 2009 22:41:53 -0000	1.13.2.33.2.2.2.53
+++ millennium.module	11 Dec 2009 15:33:30 -0000
@@ -826,14 +826,14 @@
   $output .= $marc_text_msg;
   $output .= "<pre class='millennium marc'>$marc_text</pre>";
 
-  $tmp_node = millennium_marc_to_nodeobject($marc_text, $recnum, $base_url);
+  $tmp_node = millennium_record_to_nodeobject($recnum, $marc_text, $base_url);
 
   $output .= "<h3>" .t('Conversion results') ."</h3>";
-
+  $output .= t('The conversion has just been generated using current settings.');
   $output .= "<h4>" . $GLOBALS["_millennium_field_labels"]["type"] ."</h4>";
   $output .= $tmp_node->millennium_biblio_data["type"];
 
-  $image = millennium_coverimage_widget($tmp_node);
+  $image = millennium_coverimage_widget($tmp_node->millennium_biblio_data);
   $output .= "<h4>" . t('Cover image') . "</h4>";
   $output .= $image ? $image : t("No image");
 
@@ -1316,19 +1316,88 @@
  * @param recnum Millennium record number (b123456, i123456) to fetch
  * @param string $marc_text An optional MARC record to import from.
  */
-function millennium_record_to_nodeobject($recnum, $marc_text = null) {
+function millennium_record_to_nodeobject($recnum, $marc_text = null, $base_url = false) {
   $result = new stdClass();
+  $base_url = millennium_get_real_baseurl($base_url);
   if ($marc_text == null) {
-    $marc_text = millennium_fetch_marc($recnum);
+    $marc_text = millennium_fetch_marc($recnum, $base_url);
     if (!$marc_text) {
       $result->success = FALSE;
-      $result->error = "Could not fetch MARC for $recnum";
+      $result->error = "Could not fetch MARC for $recnum at $base_url";
       return $result;
     }
   }
 
-  //Create a nodeobject from parsed MARC
-  $nodeobject = millennium_marc_to_nodeobject($marc_text, $recnum);
+  // Create a bibliographic information array from parsed MARC
+  $biblio = millennium_marc_to_biblio($marc_text);
+  if (!$biblio) {
+    return false;
+  }
+  
+  // Add Millennium-only info
+  if ($base_url === false) {
+    $base_url = millennium_get_real_baseurl();
+  }
+  $biblio["url"] = l(t('Link to original record'), millennium_permalink($recnum, "plain", $base_url));
+  $biblio["recnum"] = $recnum;
+  $biblio["base_url"] = $base_url;
+  // If no LCC, try to get from item holdings
+  /*
+  if ($lcc == "" && $recnum) {
+    $holdings = millennium_get_holdings_info($recnum, null, $base_url);
+    // Look for first "classnumber" in items
+    if (is_array($holdings)) {
+      foreach ($holdings as $holding) {
+        if (trim($holding["classnumber"])!= "") {
+          $lcc = $holding["classnumber"];
+          break;
+        }
+      }
+    }
+  }
+  */
+  
+  // Let other modules add custom bibliographic data
+  //  function signature: mymodulename_millennium_biblio_data_alter($biblio, $marc)
+  //  where:
+  //    $biblio is the biblio information array
+  //    $marc is the parsed MARC record
+  drupal_alter('millennium_biblio_data', &$biblio, $marc);
+  
+  // Create node array
+  $log = 'Imported automatically by Millennium module on date '. date('g:i:s a');
+  $node_type = variable_get('millennium_marc_node_type', 'page');
+  $node_type_settings = variable_get("node_options_{$node_type}", array());
+  $user = user_load(1);
+
+  $node = array(
+    "type" => $node_type,
+    "title" => $biblio["title"],
+    "status" => in_array("status", $node_type_settings) ? 1 : 0,
+    "comment" => variable_get("comment_{$node_type}", 0),
+    "promote" => in_array("promote", $node_type_settings) ? 1 : 0,
+    "moderate" => in_array("moderate", $node_type_settings) ? 1 : 0,
+    "format" => 2,
+    "uid" => 1,
+    "name" => $user->name,
+    "log" => $log,
+    "teaser" => '',
+    "body" => '',
+  );
+
+  // Store custom data in node
+  $node["millennium_biblio_data"] = $biblio;
+
+  // Return the node
+  $nodeobject = (object)$node;
+
+  // Add taxonomy
+  $parsed_marc = millennium_parse_marc($marc_text);
+  millennium_add_taxonomy_to_node($nodeobject, $parsed_marc);
+
+  // Add availability info
+  millennium_add_availability_to_node($nodeobject, $recnum);
+
   if (!$nodeobject) {
     $result->success = false;
     $result->error = "Could not create node object from parsed MARC for $recnum";
@@ -1473,46 +1542,27 @@
 }
 
 /**
- * Takes a parsed marc record array and generates a nodeobject with type 'biblio'
- * @param bib_recnum Millennium record number (b123456) that contains this MARC
- * @param marc_text MARC record (plaintext)
+ * Takes a parsed marc record array and generates an array of bibliographic info
+ * @param $marc_text 
+ *   MARC record (plaintext)
  */
-function millennium_marc_to_nodeobject($marc_text, $recnum = false, $base_url = false) {
+function millennium_marc_to_biblio($marc_text) {
   global $_millennium_field_labels;
   
-  if ($base_url === false) {
-    $base_url = millennium_get_real_baseurl();
-  }
+  $biblio = array();
 
   $marc = millennium_parse_marc($marc_text);
   if (! $marc) {
-    #return array("success" => false, "error" => "Could not parse MARC for $recnum");
+    #return array("success" => false, "error" => "Could not parse MARC");
     return false;
   }
 
-  $log = 'Imported automatically by Millennium module on date '. date('g:i:s a');
-
   // LCC
   $lcc = trim(millennium_getFieldPlain($marc, "05"));
-  // If no LCC, try to get from items
-  /*
-  if ($lcc == "" && $recnum) {
-    $holdings = millennium_get_holdings_info($recnum, null, $base_url);
-    // Look for first "classnumber" in items
-    if (is_array($holdings)) {
-      foreach ($holdings as $holding) {
-        if (trim($holding["classnumber"])!= "") {
-          $lcc = $holding["classnumber"];
-          break;
-        }
-      }
-    }
-  }
-  */
   $biblio["lcc"] = $lcc;
 
   // Notes
-  $notes="";
+  $notes = "";
   $tmpfields = millennium_getFields_fixed($marc, "50");
   if (is_array($tmpfields)) {
     foreach ($tmpfields as $field) {
@@ -1661,43 +1711,13 @@
   // Others
   #$biblio["bib_recnum"] = $bib_recnum;
   $biblio["imprint"] = $biblio["imprint_place"] ." ". $biblio["imprint_name"] ." ". $biblio["imprint_date"];
-  $biblio["url"] = l(t('Link to original record'), millennium_permalink($recnum, "plain", $base_url));
   $biblio["marc"] = $marc_text;
-  $biblio["recnum"] = $recnum;
-  $biblio["base_url"] = $base_url;
-
-  // Clean up (almost) every $biblio[] value
-  $cant_touch_this = array('item_description', 'links', 'url');
-  foreach ($biblio as $index => $value) {
-    if (!in_array($index, $cant_touch_this)) {
-      $biblio[$index] = millennium_trim_marc_value($value);
-    }
-  }
-
-  $node_type = variable_get('millennium_marc_node_type', 'page');
-  $node_type_settings = variable_get("node_options_{$node_type}", array());
-  $user = user_load(1);
-
-  $node = array(
-    "type" => $node_type,
-    "title" => $biblio["title"],
-    "status" => in_array("status", $node_type_settings) ? 1 : 0,
-    "comment" => variable_get("comment_{$node_type}", 0),
-    "promote" => in_array("promote", $node_type_settings) ? 1 : 0,
-    "moderate" => in_array("moderate", $node_type_settings) ? 1 : 0,
-    "format" => 2,
-    "uid" => 1,
-    "name" => $user->name,
-    "log" => $log,
-    "teaser" => '',
-    "body" => '',
-
-    /* These are for the Biblio module. As of today, that module is not yet released.
+  
+  /* These are for the Biblio module. As of today, that module is not yet released.
     "biblio_alternate_title" => $biblio["alternate_title"],
     "biblio_short_title" =>  str_replace(' /', '', $biblio["short_title"]),
     "biblio_translated_title" => $biblio["translated_title"],
     "biblio_notes" => $body,
-
     "biblio_type" => $biblio["type"],
     "biblio_authors" => $biblio["authors"],
     "biblio_corp_author" => $biblio["corp_author"],
@@ -1715,29 +1735,17 @@
     "biblio_lang" => $biblio["lang"],
     "biblio_url" => $biblio["url"],
     "biblio_custom7" => $biblio["bib_recnum"],
-    */
-  );
+  */
   
-  // Let other modules add custom bibliographic data
-  //  function signature: mymodulename_millennium_biblio_data_alter($biblio, $marc)
-  //  where:
-  //  $biblio is the biblio information array
-  //  $marc is the parsed MARC record
-  drupal_alter('millennium_biblio_data', &$biblio, $marc);
-
-  // Store custom data in node
-  $node["millennium_biblio_data"] = $biblio;
-
-  // Return the node
-  $nodeobject = (object)$node;
-
-  // Add taxonomy
-  millennium_add_taxonomy_to_node($nodeobject, $marc);
-
-  // Add availability info
-  millennium_add_availability_to_node($nodeobject, $recnum);
+  // Clean up (almost) every $biblio[] value
+  $cant_touch_this = array('item_description', 'links', 'url');
+  foreach ($biblio as $index => $value) {
+    if (!in_array($index, $cant_touch_this) && is_string($value)) {
+      $biblio[$index] = millennium_trim_marc_value($value);
+    }
+  }
 
-  return $nodeobject;
+  return $biblio;
 }
 
 /**
@@ -1851,10 +1859,12 @@
 
 /**
  * Scans a parsed MARC record array for important access points and relates them with taxonomy terms in vocabularies stored in module's config.
- * @param nodeobject A drupal node object
- * @param record Array containing parsed marc record
+ * @param $nodeobject 
+ *   A drupal node object
+ * @param $marc 
+ *   Array containing parsed marc record
  */
-function millennium_add_taxonomy_to_node(&$nodeobject, $record) {
+function millennium_add_taxonomy_to_node(&$nodeobject, $marc) {
   #drupal_set_message("millennium_add_taxonomy_to_node()");
   $nid = $nodeobject->nid;
 
@@ -1894,7 +1904,7 @@
     millennium_add_node_taxonomy_terms($nodeobject, variable_get('millennium_marc_vid_year', -1), array($year));
   }
 
-  $tmpfields = millennium_getFields_fixed($record, "008");  #get all 008s
+  $tmpfields = millennium_getFields_fixed($marc, "008");  #get all 008s
   if (is_array($tmpfields)) {
 
     // Language
@@ -1952,7 +1962,7 @@
     */
 
   // Get all 65x's
-  $tmpfields = millennium_getFields_fixed($record, "65");
+  $tmpfields = millennium_getFields_fixed($marc, "65");
   if (is_array($tmpfields)) {
     foreach ($tmpfields as $field) {
       foreach ($field["subfields"] as $subfield) {
@@ -1976,7 +1986,7 @@
   }
 
   // Other subjects: 600s, 61xs, 63xs
-  $tmpfields = millennium_getFields($record, "6[013]...");  //get'em
+  $tmpfields = millennium_getFields($marc, "6[013]...");  //get'em
 
   if (is_array($tmpfields)) {
     foreach ($tmpfields as $field) {
@@ -1993,8 +2003,8 @@
   }
 
   // Series
-  #$tmpfields = millennium_getFields($record, "4901.");  // Only "traced" series; tag 490, indicators "1 "
-  $tmpfields = millennium_getFields_fixed($record, "830");  // 830 - Series Added Entry-Uniform Title
+  #$tmpfields = millennium_getFields($marc, "4901.");  // Only "traced" series; tag 490, indicators "1 "
+  $tmpfields = millennium_getFields_fixed($marc, "830");  // 830 - Series Added Entry-Uniform Title
   if (is_array($tmpfields)) {
     foreach ($tmpfields as $field) {
       list($tmpsub) = millennium_getSubfields($field, "a");
@@ -2223,7 +2233,7 @@
 
     case 'rss item':
       $table = theme("millennium_biblio_data", $node->millennium_biblio_data, "full");
-      $coverimage_widget = millennium_coverimage_widget($node);
+      $coverimage_widget = millennium_coverimage_widget($node->millennium_biblio_data);
       $content = $coverimage_widget . $table;
       return array(array('key' => 'description', 'value' => $content));
       break;
@@ -2332,7 +2342,7 @@
       }
 
       // Coverimage
-      $coverimage_widget = millennium_coverimage_widget($node);
+      $coverimage_widget = millennium_coverimage_widget($node->millennium_biblio_data);
       if ($coverimage_widget) {
         $node->content['millennium_coverimage'] = array(
           '#value' => "<div class='millennium coverimage'>". $coverimage_widget ."</div>\n",
@@ -2442,25 +2452,25 @@
  * @param node a node object
  * @param link flag that dictates whether include a link to the node
  */
-function millennium_coverimage_widget($node, $link = false) {
+function millennium_coverimage_widget($biblio_data, $link = false) {
   $override_url = variable_get("millennium_coverimage_baseurl", false);
   $output = "";
   $cover_url = "";
   if ($override_url) {
-    $number = trim(preg_replace('/[^0-9X -].*$/i', '', trim($node->millennium_biblio_data["isbn"]) ));
+    $number = trim(preg_replace('/[^0-9X -].*$/i', '', trim($biblio_data["isbn"]) ));
     if ($number == "") {
-      $number = trim(preg_replace('/[^0-9X -].*$/i', '', trim($node->millennium_biblio_data["issn"])));
+      $number = trim(preg_replace('/[^0-9X -].*$/i', '', trim($biblio_data["issn"])));
     }
 
     $number = preg_replace('/[^0-9X]+/i', '', $number);
-    $type = $node->millennium_biblio_data["type"];
+    $type = $biblio_data["type"];
     $cover_url = str_replace("!id", $number, $override_url);
     $cover_url = str_replace("!type", $type, $cover_url);
     $cover_url = str_replace("!tit", urlencode($node->title), $cover_url);
-    $cover_url = str_replace("!aut", urlencode($node->millennium_biblio_data["authors"]), $cover_url);
+    $cover_url = str_replace("!aut", urlencode($biblio_data["authors"]), $cover_url);
   } else {
     // No cover image override, use other sources in biblio information
-    $cover_sources = $node->millennium_biblio_data["coverimage_sources"];
+    $cover_sources = $biblio_data["coverimage_sources"];
     #dpm($cover_sources);
     if (is_array($cover_sources)) {
       $cover_info = array_pop($cover_sources);

