? 785370_features_integration.patch
Index: apachesolr_biblio.export.inc
===================================================================
RCS file: apachesolr_biblio.export.inc
diff -N apachesolr_biblio.export.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ apachesolr_biblio.export.inc	6 May 2010 14:31:32 -0000
@@ -0,0 +1,155 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Import/Export functionality provided by ApacheSolr Biblio module.
+ */
+
+/**
+ * Export a field to code.
+ *
+ * @param $fields
+ *   An array of fields, or field name.
+ * @param $module
+ *   Optional. The name of the module that will be created if exporting to use
+ *   in hook_default_biblio_fields().
+ */
+function apachesolr_biblio_export_fields($fields = array(), $module = '') {
+  $output = '$fields = array();'. "\n";
+  foreach ($fields as $fid => $field) {
+    if (!is_array($field)) {
+      // Load the field definition
+      $field = apachesolr_biblio_get_field($field);
+    }
+    if(!empty($field)) {
+      $output .= '// Exported field: "'. $field['name'] . '"' . ".\n";
+      $output .= '$fields[] = ' . var_export($field, TRUE) . ";\n";
+    }
+  }
+  $output .= 'return $fields;';
+  return $output;
+}
+
+/**
+ * Form to import default ApacheSolr Biblio fields
+ */
+function apachesolr_biblio_default_fields_import_form() {
+  $form = array();
+
+  $form['import'] = array(
+    '#title' => t('ApacheSolr Biblio default fields import code'),
+    '#type' => 'textarea',
+    '#default_value' => '',
+    '#rows' => 15,
+    '#required' => TRUE,
+    '#description' => t('Paste the code from an <a href="@export-url">ApacheSolr Biblio fields export</a> here to import it into you site. Fields imported with the same name will update existing fields. Fields with a new name will be created.', array('@export-url' => url('admin/settings/biblio/solr/export'))),
+  );
+  $form['submit'] = array(
+    '#value' => t('Import'),
+    '#type' => 'submit',
+  );
+
+  return $form;
+}
+
+/**
+ * Validate handler; Import default ApacheSolr Biblio fields.
+ */
+function apachesolr_biblio_default_fields_import_form_validate($form, &$form_state) {
+  $fields = array();
+  ob_start();
+  eval($form_state['values']['import']);
+  ob_end_clean();
+
+  if (!isset($fields) || !is_array($fields)) {
+    form_set_error('import', t('A valid list of fields could be found in the import code.'));
+    return;
+  }
+
+  $form_state['fields'] = $fields;
+}
+
+/**
+ * Submit handler; Import default ApacheSolr Biblio fields.
+ */
+function apachesolr_biblio_default_fields_import_form_submit($form, &$form_state) {
+  foreach ($form_state['fields'] as $default_field) {
+    db_query("UPDATE {apachesolr_biblio_fields} SET name = '%s', index_type = '%s', indexed = %d WHERE fid = %d", $default_field['name'], $default_field['index_type'], $default_field['indexed'], $default_field['fid']);
+    if (!db_affected_rows()) {
+      db_query("INSERT INTO {apachesolr_biblio_fields} (fid, name, index_type, indexed) VALUES (%d, '%s', '%s', %d)", $default_field['fid'], $default_field['name'], $default_field['index_type'], $default_field['indexed']);
+      drupal_set_message(t('Field @name has been imported.', array('@name' => $default_field['name'])));
+    }
+    else {
+      drupal_set_message(t('Field @name has been updated.', array('@name' => $default_field['name'])));
+    } 
+  }
+  $form_state['redirect'] = 'admin/settings/biblio/solr';
+}
+
+/**
+ * Export a field and display it in a form.
+ */
+function apachesolr_biblio_default_fields_export_form(&$form_state, $field = NULL) {
+  $form = array();
+
+  // Convert a field name (if any) to the list of export fields.
+  if (is_object($field) || ($field = apachesolr_biblio_get_field($field))) {
+    $field = array($field);
+  }
+
+  // Display a list of fields to export.
+  if (!isset($fields)) {
+    if (isset($form_state['values']['fields'])) {
+      $fields = array();
+      foreach ($form_state['values']['fields'] as $field_name) {
+        if ($field_name && $field = apachesolr_biblio_get_field($field_name)) {
+          $fields[] = $field;
+        }
+      }
+    }
+    else {
+      $fields = apachesolr_biblio_get_fields();
+      unset($fields['contributors']);
+      $options = array();
+      foreach($fields as $fid => $field) {
+        $options[$fid] = $field['name'];
+      }
+      $form['fields'] = array(
+        '#type' => 'checkboxes',
+        '#title' => t('Fields to export'),
+        '#options' => drupal_map_assoc($options),
+        '#description' => t('Exporting your fields is useful for moving fields from one site to another, or when including your field definitions in a module.'),
+      );
+      $form['submit'] = array(
+        '#type' => 'submit',
+        '#value' => t('Export'),
+      );
+    }
+  }
+
+  if (isset($fields)) {
+    $code = apachesolr_biblio_export_fields($fields);
+
+    // Link to the Features page if module is present, otherwise link to the
+    // Drupal project page.
+    $features_link = module_exists('features') ? url('admin/build/features') : url('http://drupal.org/project/features');
+
+    $form['export'] = array(
+      '#type' => 'textarea',
+      '#title' => t('ApacheSolr Biblio field exports'),
+      '#description' => t('Use the exported code to later <a href="@import-field">import</a> it. Exports can be included in modules using hook_apachesolr_biblio_default_fields() or using the <a href="@features-url">Features</a> module.', array('@import-field' => url('admin/settings/biblio/solr/import'), '@features-url' => $features_link)),
+      '#value' => $code,
+      '#rows' => 15,
+    );
+  }
+
+  return $form;
+}
+
+/**
+ * Submit handler; Rebuild the export form after the list of fields has been set.
+ */
+function apachesolr_biblio_default_fields_export_form_submit($form, &$form_state) {
+  $form_state['rebuild'] = TRUE;
+}
Index: apachesolr_biblio.features.inc
===================================================================
RCS file: apachesolr_biblio.features.inc
diff -N apachesolr_biblio.features.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ apachesolr_biblio.features.inc	6 May 2010 14:31:32 -0000
@@ -0,0 +1,71 @@
+<?php
+
+/**
+ * @file
+ * Features integration for ApacheSolr Biblio module.
+ */
+
+/**
+ * Implementation of hook_features_export().
+ */
+function apachesolr_biblio_features_export($data, &$export, $module_name = '') {
+  $pipe = array();
+
+  // Add biblio module as a dependency.
+  $export['dependencies']['features'] = 'biblio';
+  foreach ($data as $fid => $field) {
+    if ($solr_field = apachesolr_biblio_get_field($fid)) {
+      $export['features']['apachesolr_biblio_field'][$solr_field['name']] = $solr_field['name'];
+    }
+  } 
+  return $pipe;
+}
+
+/**
+ * Implementation of hook_features_export_options().
+ */
+function apachesolr_biblio_features_export_options() {
+  $options = array();
+  // Get all fields.
+  $fields = apachesolr_biblio_get_fields();
+  unset($fields['contributors']);
+  foreach ($fields as $fid => $field) {
+    $options[$field['name']] = $field['name'];
+  }
+  return $options;
+}
+
+/**
+ * Implementation of hook_features_export_render().
+ */
+function apachesolr_biblio_features_export_render($module = 'foo', $data, $features_export) {
+  module_load_include('inc', 'apachesolr_biblio', '/apachesolr_biblio.export');
+  $code = apachesolr_biblio_export_fields($data, $module);
+  return array('default_biblio_fields' => $code);
+}
+
+/**
+ * Implementation of hook_features_revert().
+ *
+ * @param $module
+ *   The name of module for which to revert content.
+ */
+function apachesolr_biblio_features_revert($module = NULL) {
+  drupal_set_message('Module = '.$module);
+  // Get default fields from features.
+  if (module_hook($module, 'default_biblio_fields')) {
+    $default_fields = module_invoke($module, 'default_biblio_fields');
+
+    // Reset fields that are defined in code.
+    foreach ($default_fields as $default_field) {
+      db_query("UPDATE {apachesolr_biblio_fields} SET name = '%s', index_type = '%s', indexed = %d WHERE fid = %d", $default_field['name'], $default_field['index_type'], $default_field['indexed'], $default_field['fid']);
+      if (!db_affected_rows()) {
+        db_query("INSERT INTO {apachesolr_biblio_fields} (fid, name, index_type, indexed) VALUES (%d, '%s', '%s', %d)", $default_field['fid'], $default_field['name'], $default_field['index_type'], $default_field['indexed']);
+      }
+    }
+  }
+  else {
+    return FALSE;
+  }
+  return TRUE;
+}
Index: apachesolr_biblio.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_biblio/apachesolr_biblio.module,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 apachesolr_biblio.module
--- apachesolr_biblio.module	23 Nov 2009 15:58:21 -0000	1.1.2.2
+++ apachesolr_biblio.module	6 May 2010 14:31:32 -0000
@@ -12,6 +12,24 @@
     'type' => MENU_LOCAL_TASK,
     'weight' => 10
   );
+  $items['admin/settings/biblio/solr/import'] = array(
+    'title' => 'Import Fields',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('apachesolr_biblio_default_fields_import_form'),
+    'access arguments' => array('administer biblio'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'apachesolr_biblio.export.inc',
+    'weight' => 2,
+  );
+  $items['admin/settings/biblio/solr/export'] = array(
+    'title' => 'Export Fields',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('apachesolr_biblio_default_fields_export_form'),
+    'access arguments' => array('administer biblio'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'apachesolr_biblio.export.inc',
+    'weight' => 3,
+  );
   return $items;
 }
 
@@ -43,21 +61,56 @@
 // These are the fields as apachesolr_biblio knows abouit them.
 function apachesolr_biblio_get_fields() {
   // get current settings
-  $result = db_query("SELECT * FROM {apachesolr_biblio_fields");
+  $result = db_query("SELECT * FROM {apachesolr_biblio_fields}");
   $fields = array();
   while ($row = db_fetch_array($result)) {
     $fields[$row['fid']] = $row;
   }
+  // If there aren't any fields defined, load any default fields
+  if(empty($fields)) {
+    foreach(module_implements('default_biblio_fields') as $module) {
+      $default_fields = module_invoke($module, 'default_biblio_fields');
+      if(!empty($default_fields)) {
+        foreach($default_fields as $field) {
+          $fields[$field['fid']] = $field;
+        }
+      }
+    }
+  }
   $fields['contributors'] = variable_get('apachesolr_biblio_index_authors', 1);
   return $fields;
 }
 
+/**
+ * Load a single field either by name or by field ID.
+ *
+ * @param string $name
+ *   The name of the field.
+ * @param int $fid
+ *   The field id.
+ */
+function apachesolr_biblio_get_field($name = NULL, $fid = NULL) {
+  $fields = apachesolr_biblio_get_fields();
+  if (isset($name)) {
+    foreach($fields as $fid => $field) {
+      if($field['name'] == $name) {
+        return $field;
+      }
+    }
+  }
+  elseif (isset($fid)) {
+    return $fields[$fid];
+  }
+  return FALSE;
+}
+
+
 function apachesolr_biblio_apachesolr_update_index(&$document, $node) {
   // Only do stuff with biblio nodes.
   if ($node->type != 'biblio') {
     return;
   }
-  $tz = date_default_timezone();
+  $tz = date_default_timezone_name();
   date_default_timezone_set('UTC');
   $fields = apachesolr_biblio_get_fields();
   foreach ($fields as $fid => $biblio) {
@@ -275,3 +328,16 @@
       break;
   }
 }
+
+/**
+ * Implementation of hook_features_api().
+ */
+function apachesolr_biblio_features_api() {
+  return array(
+    'apachesolr_biblio' => array(
+      'default_hook' => 'default_biblio_fields',
+      'features_source' => TRUE,
+      'file' => drupal_get_path('module', 'apachesolr_biblio') . '/apachesolr_biblio.features.inc',
+    ),
+  );
+}
