? ab.patch
Index: apachesolr_biblio.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_biblio/apachesolr_biblio.module,v
retrieving revision 1.1
diff -u -p -r1.1 apachesolr_biblio.module
--- apachesolr_biblio.module	23 Sep 2009 12:18:20 -0000	1.1
+++ apachesolr_biblio.module	23 Nov 2009 13:22:42 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: apachesolr_biblio.module,v 1.1 2009/09/23 12:18:20 robertDouglass Exp $
+// $Id$
 
 function apachesolr_biblio_menu() {
   $items = array();
@@ -57,6 +57,8 @@ function apachesolr_biblio_apachesolr_up
   if ($node->type != 'biblio') {
     return;
   }
+  $tz = date_default_timezone();
+  date_default_timezone_set('UTC');
   $fields = apachesolr_biblio_get_fields();
   foreach ($fields as $fid => $biblio) {
     if ($biblio['indexed'] && isset($node->{$biblio['name']})) {
@@ -65,7 +67,26 @@ function apachesolr_biblio_apachesolr_up
       $value = NULL;
       if ($value = apachesolr_biblio_format_value($node->{$biblio['name']}, $biblio['index_type'])) {
         if (is_array($value)) {
+          // The $biblio_sort variable exists just to build the $sort_index_key.
+          // The $sort_index_key exists so that results can later be sorted
+          // by the first value of this field. Citations often have many
+          // values for fields and we therefore use the first value as the
+          // sort value, building a special field just for this purpose.
+          // TODO: Add a checkbox on the field configuration screen that
+          // indicates whether a field should have a sort, or not.
+          // TODO: Add sorts to the $query object.
+          $biblio_sort = FALSE;
+          if ($biblio['multiple']) {
+            $biblio_sort = $biblio;
+            $biblio_sort['multiple'] = FALSE;
+            $sort_index_key = 'sort_ss_' . $biblio['name'];
+          }
+          $first = TRUE;
           foreach($value as $v) {
+            if ($first) {
+              $document->$sort_index_key = $v;
+              $first = FALSE;
+            }
             $document->setMultiValue($index_key, $v);
           }
         }
@@ -75,13 +96,23 @@ function apachesolr_biblio_apachesolr_up
       }
     }
   }
+  date_default_timezone_set($tz);
   // Handle authors.
   if (variable_get('apachesolr_biblio_index_authors', 1)) {
     $value = array();
-    foreach ($node->biblio_contributors as $keys) {
-      foreach ($keys as $key) {
-        if (isset($key['name'])) {
-          $document->setMultiValue('sm_biblio_contributors', apachesolr_clean_text($key['name']));
+
+    foreach ($node->biblio_contributors as $arrays) {
+      $first = TRUE;
+      foreach ($arrays as $contributor_node) {
+        if (isset($contributor_node['name'])) {
+          if ($author = trim(apachesolr_clean_text($contributor_node['name']))) {
+            if ($first) {
+              // Set the first name to sort_ss_biblio_first_author
+              $first = FALSE;
+              $document->sort_ss_biblio_first_author = $author;
+            }
+            $document->setMultiValue('sm_biblio_contributors', $author);
+          }
         }
       }
     }
@@ -101,34 +132,60 @@ function apachesolr_biblio_format_value(
       // 1999 Mar
       // 1999 Mar 6
       // 1999 Mar-Jun   represents a range of months
+      // 2005 Nov 21-27  A range of days.
       $values = array();
-      $parts = split(' ', $value);
-      $months = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
-      $range = split('-', $parts[1]);
+      $parts = array_filter(split(' ', $value));
+      $months_lookup = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 'jan');
+      $months = isset($parts[1]) ? array_filter(split('-', $parts[1])) : NULL;
+      $days = isset($parts[2]) ? array_filter(split('-', $parts[2])) : NULL;
+      // This is the case that just a year is given, in which case we add 12
+      // values, one for each month of the year.
       if (count($parts) == 1) {
-        foreach ($months as $m) {
-          $values[] = date('Y-m-d\TH:i:s\Z', strtotime("$m $value"));
+        foreach (array_slice($months_lookup, 0, 12) as $m) {
+          $values[] = apachesolr_date_iso(strtotime("$m $value"));
         }
       }
-      elseif (count($range) > 1) {
-        $startpos = array_search(strtolower($range[0]), $months);
-        $endpos = array_search(strtolower($range[1]), $months);
-        foreach (array_slice($months, $startpos, $endpos - $startpos + 1) as $m) {
-          if (count($parts) == 2) {
-            $value = "$m {$parts[0]}";
+      // In this case, there is a range of months.
+      else if (count($months) > 1) {
+        $startpos = array_search(strtolower($months[0]), $months_lookup);
+        $endpos = array_search(strtolower($months[1]), $months_lookup);
+        $month_range = range($startpos, $endpos);
+        $m = 0;
+        foreach ($month_range as $m) {
+          $v = $months_lookup[$m] . " {$parts[0]}";
+          $values[] = apachesolr_date_iso(strtotime($v));
+        }
+        if ($m) {
+          // Cap off the last month. Advance the month by one...
+          $v = $months_lookup[$m + 1];
+          // 11 is December!
+          if ($m == 11) {
+            // We have to advance the year, too.
+            $y = $parts[0] +1;
           }
-          $values[] = date('Y-m-d\TH:i:s\Z', strtotime($value));
+          else {
+            $y = $parts[0];
+          }
+          $v .= " $y";
+          // ... and then shave one second off so that it is still in the last month.
+          $values[] = apachesolr_date_iso(strtotime($v) - 1);
         }
       }
+      // In this case there is a range of days. Add the first day.
+      else if (count($days) > 1) {
+        $values[] = apachesolr_date_iso(strtotime("{$parts[1]} {$days[0]}, {$parts[0]}"));
+      }
       else {
+        $parts = array_filter($parts);
         if (count($parts) == 2) {
-          $value = "{$parts[1]} {$parts[0]}";
+          $v = "{$parts[1]} {$parts[0]}";
         }
         elseif (count($parts) == 3) {
-          $value = "{$parts[1]} {$parts[2]}, {$parts[0]}";
+          $v = "{$parts[1]} {$parts[2]}, {$parts[0]}";
         }
-        $values[] = date('Y-m-d\TH:i:s\Z', strtotime($value));
+        $values[] = apachesolr_date_iso(strtotime($v));
       }
+
       return $values;
   }
 }
