--- Solr_Base_Query.php	2010-03-10 13:44:54.000000000 +0100
+++ Solr_Base_Query.php	2010-03-09 09:39:25.000000000 +0100
@@ -1,7 +1,7 @@
 <?php
-// $Id: Solr_Base_Query.php,v 1.1.4.44 2009/12/10 17:43:28 pwolanin Exp $
+// $Id: Solr_Base_Query.php,v 1.1.4.40 2009/07/02 08:50:46 robertDouglass Exp $
 
-class Solr_Base_Query implements Drupal_Solr_Query_Interface {
+class ApacheSolr_Custom_Query implements Drupal_Solr_Query_Interface {
 
   /**
    * Extract all uses of one named field from a filter string e.g. 'type:book'
@@ -128,8 +128,7 @@ class Solr_Base_Query implements Drupal_
     $this->filterstring = trim($filterstring);
     $this->parse_filters();
     $this->available_sorts = $this->default_sorts();
-    $this->sortstring = trim($sortstring);
-    $this->parse_sortstring();
+    $this->parse_sortstring($sortstring);
     $this->base_path = $base_path;
     $this->id = ++self::$idCount;
   }
@@ -138,8 +137,8 @@ class Solr_Base_Query implements Drupal_
     $this->id = ++self::$idCount;
   }
 
-  public function add_filter($field, $value, $exclude = FALSE, $callbacks = array()) {
-    $this->fields[] = array('#exclude' => $exclude, '#name' => $field, '#value' => trim($value), '#callbacks' => $callbacks);
+  public function add_filter($field, $value, $exclude = FALSE) {
+    $this->fields[] = array('#exclude' => $exclude, '#name' => $field, '#value' => trim($value));
   }
 
   /**
@@ -237,9 +236,9 @@ class Solr_Base_Query implements Drupal_
     $this->subqueries = array();
   }
 
-  protected function parse_sortstring() {
+  protected function parse_sortstring($sortstring) {
     // Substitute any field aliases with real field names.
-    $sortstring = strtr($this->sortstring, array_flip($this->field_map));
+    $sortstring = strtr(trim($sortstring), array_flip($this->field_map));
     // Score is a special case - it's the default sort for Solr.
     if ('' == $sortstring) {
       $this->set_solrsort('score', 'asc');
@@ -263,36 +262,53 @@ class Solr_Base_Query implements Drupal_
   public function get_solrsort() {
     return $this->solrsort;
   }
-
+  /*
+   * SVB: changed from orignal file
+   */
   public function get_available_sorts() {
-    return $this->available_sorts;
+    $sorts = array();
+    
+    foreach( $this->available_sorts as $key => $sort ) {
+      if( $sort['status'] ) {
+        $sorts[$key]['title'] = $sort['title'];
+        $sorts[$key]['default'] = $sort['default'];
+      }
+    }
+    
+    return $sorts;
   }
 
   public function set_available_sort($name, $sort) {
     // We expect non-aliased sorts to be added.
     $this->available_sorts[$name] = $sort;
-    // Re-parse the sortstring.
-    $this->parse_sortstring();
+    $this->available_sorts[$name]['standard'] = FALSE;
+    $this->available_sorts[$name]['status'] = TRUE;
+    
+    variable_set('apachesolr_custom_sort_types', $this->available_sorts);
   }
 
   public function remove_available_sort($name) {
     unset($this->available_sorts[$name]);
-    // Re-parse the sortstring.
-    $this->parse_sortstring();
   }
 
   /**
    * Returns a default list of sorts.
+   * 
+   *  SVB: changed from orignal file
    */
   protected function default_sorts() {
     // The array keys must always be real Solr index fields.
-    return array(
+    $defaults = array(
       'score' => array('title' => t('Relevancy'), 'default' => 'asc'),
       'sort_title' => array('title' => t('Title'), 'default' => 'asc'),
       'type' => array('title' => t('Type'), 'default' => 'asc'),
       'sort_name' => array('title' => t('Author'), 'default' => 'asc'),
       'created' => array('title' => t('Date'), 'default' => 'desc'),
     );
+    
+    $sorts = variable_get('apachesolr_custom_sort_types', $defaults);
+    
+    return (empty($sorts) ? $defaults : $sorts);
   }
 
   /**
@@ -332,7 +348,7 @@ class Solr_Base_Query implements Drupal_
    *   Optional. When set, this string overrides the query's current keywords.
    */
   public function get_path($new_keywords = NULL) {
-    if (isset($new_keywords)) {
+    if ($new_keywords) {
       return $this->base_path . '/' . $new_keywords;
     }
     return $this->base_path . '/' . $this->get_query_basic();
