# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: millennium.admin.inc
--- millennium.admin.inc Base (1.1.2.32)
+++ millennium.admin.inc Locally Modified (Based On 1.1.2.32)
@@ -273,6 +273,25 @@
 function millennium_admin_display() {
   $form = array();
 
+  $form['general'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => false,
+    '#title' => t("General"),
+  );
+  $form['general']['millennium_display_biblio_data'] = array(
+    '#type' => 'radios',
+    '#title' => t('Display bibiographic data table in node'),
+    '#options' => array(0 => "No", "full" => "Full view only", "teaser" => "Teaser & Full view"),
+    '#default_value' => variable_get('millennium_display_biblio_data', "teaser"),
+    '#description' => t('You might want to switch this off if you are mapping values into CCK fields. Recommended setting is "Teaser & Full view".'),
+  );
+  $form['general']['millennium_coverimage_baseurl'] = array(
+    '#type' => 'textfield',
+    '#title' => '('. t('Optional') .') '. t('URL for displaying cover images'),
+    '#default_value' => variable_get('millennium_coverimage_baseurl', ''),
+    '#description' => t('Use the placeholder !id in the URL where the ISBN or ISBN will be placed. You can also specify: !type (Biblio Type number), !tit (Title), !aut (Author(s))') .' '. t('For example') .': http://example.com/getimage?isbn=!id&type=!type',
+  );
+
   $form['holdings'] = array(
     '#type' => 'fieldset',
     '#collapsible' => false,
@@ -301,13 +320,6 @@
     '#description' => t('Comma-separated location names, as they appear on the WebOpac, that will be given priority over other locations. For example, entering "CSU" will match locations like "CSU General stacks" and "CSU Storage", showing only those in teasers and showing those above others in full record views.'),
   );
 
-  $form['millennium_coverimage_baseurl'] = array(
-    '#type' => 'textfield',
-    '#title' => '('. t('Optional') .') '. t('URL for displaying cover images'),
-    '#default_value' => variable_get('millennium_coverimage_baseurl', ''),
-    '#description' => t('Use the placeholder !id in the URL where the ISBN or ISBN will be placed. You can also specify: !type (Biblio Type number), !tit (Title), !aut (Author(s))') .' '. t('For example') .': http://example.com/getimage?isbn=!id&type=!type',
-  );
-
   $form['gbooks'] = array(
     '#type' => 'fieldset',
     '#collapsible' => false,
@@ -339,6 +351,48 @@
 }
 
 /**
+ * Callback for drupal_get_form for cck mapping settings form.
+ */
+function millennium_admin_cck_mapping() {
+  global $_millennium_field_labels;
+  $form = array();
+
+  // Get destination content type
+  $millennium_node_type = variable_get('millennium_marc_node_type', 'page');
+  $content_types_fields = content_types();
+  $content_type_fields = $content_types_fields[$millennium_node_type];
+
+  // Help text
+  $form['help'] = array(
+    '#type' => 'markup',
+    '#value' => t(
+      'Select what data to import into each of the CCK text fields available for the !type-link content type.',
+      array('!type-link' => l($millennium_node_type, 'admin/content/node-type/' . str_replace("_", "-", $millennium_node_type) . '/fields'))
+    ),
+  );
+  // Build mapping destination field options
+  $options = array(
+    -1 => t('<Set field to empty value>'),
+  );
+  foreach ($_millennium_field_labels as $biblio_name => $biblio_label) {
+    $options[$biblio_name] = $biblio_label;
+  }
+
+  // Build form
+  foreach ($content_type_fields['fields'] as $fieldname => $field) {
+    if ($field['type'] == 'text') {
+      $form["millennium_cck_mapping_{$fieldname}"] = array(
+        '#type' => 'select',
+        '#title' => $field['widget']['label'],
+        '#options' => $options,
+        '#default_value' => variable_get("millennium_cck_mapping_{$fieldname}", -1),
+      );
+    }
+  }
+  return system_settings_form($form);
+}
+
+/**
  * Callback for drupal_get_form for mapping settings form.
  */
 function millennium_admin_mapping() {
Index: millennium.install
--- millennium.install Base (1.1.2.5.2.1.2.11)
+++ millennium.install Locally Modified (Based On 1.1.2.5.2.1.2.11)
@@ -258,10 +258,12 @@
    * millennium_import_form_*
    * millennium_range_form_*
    * millennium_marc_vid_*
+   * millennium_cck_mapping_*
    */
   db_query("DELETE FROM {variable} WHERE name LIKE 'millennium_import_form_%'");
   db_query("DELETE FROM {variable} WHERE name LIKE 'millennium_range_form_%'");
   db_query("DELETE FROM {variable} WHERE name LIKE 'millennium_marc_vid_%'");
+  db_query("DELETE FROM {variable} WHERE name LIKE 'millennium_cck_mapping_%'");
\ No newline at end of file
 
   variable_del('millennium_action_on_last_item_deleted');
   variable_del('millennium_sources');
Index: millennium.module
--- millennium.module Base (1.13.2.33.2.2.2.88)
+++ millennium.module Locally Modified (Based On 1.13.2.33.2.2.2.88)
@@ -606,6 +606,16 @@
     'access arguments' => array('administer millennium'),
     'file' => 'millennium.admin.inc',
   );
+  $items[MILLENNIUM_SETTINGS_PATH . '/fieldmap'] = array(
+    'title' => 'CCK field mapping',
+    'description' => 'Configure MARC import mapping settings.',
+    'type' => MENU_LOCAL_TASK,
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('millennium_admin_cck_mapping'),
+    'access callback' => 'user_access',
+    'access arguments' => array('administer millennium'),
+    'file' => 'millennium.admin.inc',
+  );
   $items[MILLENNIUM_SETTINGS_PATH . '/display'] = array(
     'title' => 'Display',
     'description' => 'Configure node view settings: cover images, holdings information, etc.',
@@ -817,7 +827,7 @@
       $gbooks_flag = variable_get('millennium_googlebooks', false);
       if ($gbooks_flag !== false) {
         if (
-            (!$page && $gbooks_flag == "teaser") || ($page && $gbooks_flag != "No")
+            (!$page && $gbooks_flag == "teaser") || ($page && $gbooks_flag !== "0")
           ) {
           drupal_add_js( drupal_get_path('module', 'millennium') .'/millennium_googlebooks.js');
           drupal_add_js(array('millennium' => array('gbooks' => array($search_isbn => $search_isbn))), 'setting');
@@ -859,10 +869,16 @@
         );
       }
 
+      // Show biblio data table
+      $biblio_table_flag = variable_get('millennium_display_biblio_data', 'teaser');
+      if (
+            (!$page && $biblio_table_flag == "teaser") || ($page && $biblio_table_flag !== "0")
+          ) {
       $node->content['millennium_biblio_data'] = array(
         '#value' => theme("millennium_biblio_data", $node->millennium_biblio_data, $page ? "full" : "teaser"),
         '#weight' => 0,
       );
+      }
 
       break;
   }
@@ -912,6 +928,7 @@
       return array(
         'marc' => array('info' => t('Millennium: MARC')),
         //'citation' => array('info' => t('Millennium: APA citation')),
+        'biblio_data' => array('info' => t('Millennium: bibliographic data table')),
       );
 
     case 'view':
@@ -937,6 +954,13 @@
             'content' => '<pre class="millennium marc">'. $marc_text .'</pre>',
           );
           break;
+        case 'biblio_data':
+          $table = theme("millennium_biblio_data", $node->millennium_biblio_data);
+          return array(
+            'subject' => t('Millennium: bibliographic data table'),
+            'content' => $table,
+          );
+          break;
       }
       return $blocks;
 
@@ -1518,6 +1542,9 @@
   // Store custom data in node
   $node["millennium_biblio_data"] = $biblio;
 
+  // Handle CCK mappings
+  millennium_cck_biblio_to_node($node, $biblio);
+
   // Return the node
   $nodeobject = (object)$node;
 
@@ -2791,3 +2818,57 @@
     'api' => 2,
   );
 }
+
+/**
+ * Map biblio data into CCK fields according to configuration
+ */
+function millennium_cck_biblio_to_node(&$node, $biblio_values) {
+  global $_millennium_field_labels;
+
+  // Get destination content type
+  $content_types_fields = content_types();
+  $content_type_fields = $content_types_fields[variable_get('millennium_marc_node_type', 'page')];
+
+  foreach ($content_type_fields['fields'] as $dest_fieldname => $dummy) {
+    // Get mapping. Skip if none assigned
+    $biblio_fieldname = variable_get("millennium_cck_mapping_{$dest_fieldname}", -1);
+    if ($biblio_fieldname === 0) {
+      // Leave untouched
+      continue;
+    }
+
+    if (empty($node[$dest_fieldname])) {
+      $node[$dest_fieldname] = array();
+    }
+    // Clear existing values
+    foreach ($node[$dest_fieldname] as $index => $dummy) {
+      $node[$dest_fieldname][$index]['value'] = '';
+    }
+
+    if ($biblio_fieldname == -1) {
+      // Just clear out the values (which we did already)
+      continue;
+    }
+
+    // Get max num. of values this field can hold.
+    $num_allowed_values = $content_type_fields["fields"][$dest_fieldname]["multiple"];
+    if ($num_allowed_values == 0) {
+      $num_allowed_values = 1;
+    }
+
+    // Get value to write to CCK field value
+    $value = $biblio_values[$biblio_fieldname];
+    if (is_array($value)) {
+      // Handle multi-valued (array) biblio data values
+      // map into multiple CCK values up to that field's # of values limit
+      for ($index = 0; $index < $num_allowed_values; $index++) {
+        $node[$dest_fieldname][$index]['value'] = $value[$index];
+      }
+    }
+    else {
+      // Handle single-valued (string) biblio data value
+      // Set CCK field's first value
+      $node[$dest_fieldname][0]['value'] = $value;
+    }
+  }
+}
