Index: apachesolr_views_query.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_views/apachesolr_views_query.inc,v
retrieving revision 1.32
diff -c -r1.32 apachesolr_views_query.inc
*** apachesolr_views_query.inc	7 Aug 2010 23:41:21 -0000	1.32
--- apachesolr_views_query.inc	22 Sep 2010 16:22:18 -0000
***************
*** 40,45 ****
--- 40,47 ----
      'nid',
      'url',
      'uid', // TODO: skip this probably, but not for user searching...
+ 	'created',
+ 	'updated',
    );
    
    // the template to use on the solr
***************
*** 510,519 ****
     * @return none
     */
    public function add_filter($type, $value, $exclude = FALSE) {
!     $this->_facets[$type][] = array(
        'value' => $value,
        'exclude' => $exclude,
      );
    }
    
    /**
--- 512,530 ----
     * @return none
     */
    public function add_filter($type, $value, $exclude = FALSE) {
!     $filter = array(
        'value' => $value,
        'exclude' => $exclude,
      );
+     
+     /* Check if its a date value */
+     $pattern = '/([\[\{](\S+) TO (\S+)[\]\}])/';
+     if (preg_match($pattern, $value, $matches)) {
+       $filter['start'] = $matches[2];
+       $filter['end'] = $matches[3];
+     }
+     
+     $this->_facets[$type][] = $filter;
    }
    
    /**
***************
*** 651,667 ****
     * @return string the part of the url for Views that matches this facet value
     */
    protected function argument_part($field) {
!     $argument_path = '';
      if (empty($this->_facets[$field])) {
        return '';
      }
!     foreach ($this->_facets[$field] as $defintion) {
!       if (!$defintion['exclude']) {
!         $argument_path .= ',' . $defintion['value'];
        }
      }
      
!     return drupal_substr($argument_path, 1);
    }
    
    /**
--- 662,755 ----
     * @return string the part of the url for Views that matches this facet value
     */
    protected function argument_part($field) {
!     $argument_paths = array();
!     $has_date = FALSE;
      if (empty($this->_facets[$field])) {
        return '';
      }
!     foreach ($this->_facets[$field] as $definition) {
!       if (!$definition['exclude']) {
!         $value = $definition['value'];
!         /* Check if its a date argument */
!         if (!empty($definition['start']) && !empty($definition['end'])) {
!           $has_date = TRUE;
!           $start = $definition['start'];
!           $end = $definition['end'];
!           $value = $this->format_date_argument($start, $end);
!         }
!         $argument_paths[] = $value;
        }
      }
      
!     /* if there are multiple date arguments, merge where possible to keep urls tidy */
!     if ($has_date && count($argument_paths) > 1) {
!       $argument_paths  = $this->merge_date_arguments($argument_paths);
!     }  
!     
!     return implode(',', $argument_paths);
!   }
!  
!   
!   /**
!    * to prevent untidy urls, merge multiple date arguments that share common elements e.g. 2009,2009-11,2009-11-25 would simply be 2009-11-25
!    *
!    * @param string $argument_paths
!    * Argument paths in an array
!    *
!    * @return array the argument paths with dates merged
!    */
!   protected function merge_date_arguments($argument_paths) {    
!     $date_arguments = array();
!     $last_path = '';
!       
!     foreach (array_reverse($argument_paths) as $path) {
!       if (!empty($last_path) && strpos($last_path, $path) === 0) {
!         continue;
!       }
!       else {
!         $date_arguments[] = $path;
!       }
!       $last_path = $path;
!     }
!     
!     if (empty($date_arguments)) {
!       return $argument_paths;
!     }
!     return array_reverse($date_arguments);
!   }
!    
!   /**
!    * return the date argument in a format suitable for views
!    *
!    * @param string $start
!    * Start date in iso format
!    *
!    * @param string $end
!    * End date in iso fomat
!    *
!    * @return string the part of the url for Views that matches this facet value
!    */
!   protected function format_date_argument($start, $end) {    
!     $gap = apachesolr_date_find_query_gap($start, $end);
!     $unix = strtotime($start);
!     if ($unix > 0) {
!       switch ($gap) {
!         case 'YEAR':
!           return gmdate('Y', $unix);
!         case 'MONTH':
!           return gmdate('Y-m', $unix);
!         case 'DAY':
!           return gmdate('Y-m-d', $unix);
!         case 'HOUR':
!           return gmdate('Y-m-d H', $unix);
!         case 'MINUTE':
!           return gmdate('Y-m-d H:i', $unix);
!         case 'SECOND':
!           return gmdate('Y-m-d H:i:s', $unix);
!       }
!     }
!   
!     return $start;
    }
    
    /**
***************
*** 748,757 ****
      $filters = array();
      foreach ($this->_facets as $type => $fields) {
        foreach ($fields as $data) {
!         $filters[] = array(
            '#name' => ($data['exclude']) ? "NOT $type" : $type, 
            '#value' => $data['value']
          );
        }
      }
      
--- 836,850 ----
      $filters = array();
      foreach ($this->_facets as $type => $fields) {
        foreach ($fields as $data) {
!         $filter = array(
            '#name' => ($data['exclude']) ? "NOT $type" : $type, 
            '#value' => $data['value']
          );
+         if (!empty($data['start'])) {
+           $filter['#start'] = $data['start'];
+           $filter['#end'] = $data['end'];
+         }
+         $filters[] = $filter;
        }
      }
      
Index: apachesolr_views.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_views/apachesolr_views.info,v
retrieving revision 1.2
diff -c -r1.2 apachesolr_views.info
*** apachesolr_views.info	12 May 2009 18:07:43 -0000	1.2
--- apachesolr_views.info	22 Sep 2010 16:22:18 -0000
***************
*** 3,8 ****
--- 3,9 ----
  description = Provides Views Integration with Views
  dependencies[] = apachesolr
  dependencies[] = views
+ dependencies[] = date_api
  package = Apache Solr
  core = "6.x"
  php = 5.1.4
Index: apachesolr_views.views.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr_views/apachesolr_views.views.inc,v
retrieving revision 1.18
diff -c -r1.18 apachesolr_views.views.inc
*** apachesolr_views.views.inc	7 Aug 2010 23:47:54 -0000	1.18
--- apachesolr_views.views.inc	22 Sep 2010 16:22:18 -0000
***************
*** 23,28 ****
--- 23,31 ----
        'apachesolr_views_handler_argument' => array(
          'parent' => 'views_handler_argument',
        ),
+       'apachesolr_views_handler_argument_date' => array( 
+         'parent' => 'apachesolr_views_handler_argument', 
+       ), 
        'apachesolr_views_handler_argument_mlt' => array(
          'parent' => 'views_handler_argument_numeric',
        ),
***************
*** 144,149 ****
--- 147,155 ----
      $data['apachesolr_' . $base_table]['created'] = array(
        'title' => t('Creation date'),
        'help' => t('The date the node was created.'),
+ 	  'argument' => array(
+ 		'handler' => 'apachesolr_views_handler_argument_date',
+ 	  ),
        'sort' => array(
          'handler' => 'apachesolr_views_handler_sort',
        ),
***************
*** 151,156 ****
--- 157,165 ----
      $data['apachesolr_' . $base_table]['changed'] = array(
        'title' => t('Updated date'),
        'help' => t('The date the node was last updated.'),
+ 		'argument' => array(
+ 		'handler' => 'apachesolr_views_handler_argument_date',
+ 	  ),
        'sort' => array(
          'handler' => 'apachesolr_views_handler_sort',
        ),
Index: handlers/views_handler_argument_date.inc
===================================================================
RCS file: handlers/views_handler_argument_date.inc
diff -N handlers/views_handler_argument_date.inc
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- handlers/views_handler_argument_date.inc	1 Jan 1970 00:00:00 -0000
***************
*** 0 ****
--- 1,67 ----
+ <?php
+ // $Id: views_handler_argument_date.inc,v 1.3.2.2 2010/03/11 00:49:50 merlinofchaos Exp $
+ /**
+  * Abstract argument handler for dates.
+  *
+  * Adds an option to set a default argument based on the current date.
+  *
+  * @param $arg_format
+  *   The format string to use on the current time when
+  *   creating a default date argument.
+  *
+  * Definitions terms:
+  * - many to one: If true, the "many to one" helper will be used.
+  * - invalid input: A string to give to the user for obviously invalid input.
+  *                  This is deprecated in favor of argument validators.
+  * @see views_many_to_one_helper
+  *
+  * @ingroup views_argument_handlers
+  */
+ class views_handler_argument_date extends views_handler_argument_formula {
+   var $option_name = 'default_argument_date';
+   var $arg_format = 'Y-m-d';
+ 
+   /**
+    * Add an option to set the default value to the current date.
+    */
+   function default_argument_form(&$form, &$form_state) {
+     parent::default_argument_form($form, $form_state);
+     $form['default_argument_type']['#options'] += array('date' => t('Current date'));
+     $form['default_argument_type']['#options'] += array('node_created' => t("Current node's creation time"));
+     $form['default_argument_type']['#options'] += array('node_changed' => t("Current node's update time"));  }
+ 
+   /**
+    * Set the empty argument value to the current date,
+    * formatted appropriately for this argument.
+    */
+   function get_default_argument($raw = FALSE) {
+     if (!$raw && $this->options['default_argument_type'] == 'date') {
+       return date($this->arg_format, time());
+     }
+     else if (!$raw && in_array($this->options['default_argument_type'], array('node_created', 'node_changed'))) {
+       foreach (range(1, 3) as $i) {
+         $node = menu_get_object('node', $i);
+         if (!empty($node)) {
+           continue;
+         }
+       }
+   
+       if (arg(0) == 'node' && is_numeric(arg(1))) {
+         $node = node_load(arg(1));
+       }
+ 
+       if (empty($node)) {
+         return parent::get_default_argument();
+       }
+       else if ($this->options['default_argument_type'] == 'node_created') {
+         return date($this->arg_format, $node->created);
+       }
+       else if ($this->options['default_argument_type'] == 'node_changed') {
+         return date($this->arg_format, $node->changed);
+       }
+     }
+ 
+     return parent::get_default_argument($raw);
+ 
+   }
+ }
Index: handlers/apachesolr_views_handler_argument_date.inc
===================================================================
RCS file: handlers/apachesolr_views_handler_argument_date.inc
diff -N handlers/apachesolr_views_handler_argument_date.inc
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- handlers/apachesolr_views_handler_argument_date.inc	1 Jan 1970 00:00:00 -0000
***************
*** 0 ****
--- 1,96 ----
+ <?php
+ // $Id:
+ 
+ class apachesolr_views_handler_argument_date extends apachesolr_views_handler_argument {
+   var $arg_format = 'Y-m-d';
+   
+   function construct() {
+     parent::construct();
+     /* dependency on the date_api module */
+     require_once('./'. drupal_get_path('module', 'date_api') .'/date_api_sql.inc');
+     $this->date_handler = new date_sql_handler();
+     $this->date_handler->construct();
+   }
+   
+   /**
+    * Override query() and do some fancy manipulation of the argument
+    * so that it is boiled down to the actual field value
+    * instead of the nice title
+    */
+   function query() {
+     $this->value = explode(',', $this->argument);
+     $arg_values = array();
+     
+     foreach ($this->value as $facet_value) {
+       $parsed_values = $this->parse_date_argument($facet_value);
+       if (!empty($parsed_values)) {
+         foreach($parsed_values as $parsed_value){
+           $this->query->add_filter($this->real_field, $parsed_value);
+         }
+       }
+     }
+   }
+   
+   function parse_date_argument($argument) {
+     $range = $this->date_handler->arg_range($argument);
+     $min_date = $range[0];
+     $max_date = $range[1];
+     /* Adjust date to match with apachesolr facets */
+     date_modify($max_date, '1 seconds');
+     $values = $this->get_parent_filters($min_date, $max_date);
+     
+     /* Add the actual date range specified in argument */
+     $values[] = $this->build_date_range($min_date, $max_date);
+     $this->min_date = $min_date;
+     $this->max_date = $max_date;
+ 
+     return $values;
+   }
+   
+   function get_parent_filters($from, $to) {
+     $values = array();
+     $gap = $this->find_date_gap($from, $to);
+     
+     if ($gap == 'YEAR') { return $values; }
+     $values[] = $this->build_date_range($from, '1 year', 'gap');
+     
+     if ($gap == 'MONTH') { return $values; }
+     $values[] = $this->build_date_range($from, '1 month', 'gap');
+     
+     if ($gap == 'DAY') { return $values; }
+     $values[] = $this->build_date_range($from, '1 day', 'gap');
+     
+     if ($gap == 'HOUR') { return $values; }
+     $values[] = $this->build_date_range($from, '1 hour', 'gap');
+     
+     if ($gap == 'SECOND') { return $values; }
+     $values[] = $this->build_date_range($from, '1 minute', 'gap');
+     
+     return $values;
+   }
+   
+   function find_date_gap($from, $to) {
+     $format = 'Y-m-d\TH:i:s\Z';
+     $start = date_format($from, $format);
+     $end = date_format($to, $format);
+     $gap = apachesolr_date_find_query_gap($start, $end);
+     return $gap;
+   }
+   
+   function build_date_range($from, $to, $type = 'max') {
+     $format = 'Y-m-d\TH:i:s\Z';
+     if ($type == 'gap') {
+       $range = $to;
+       $to = drupal_clone($from);
+       date_modify($to, $range);
+     }
+     $start = date_format($from, $format);
+     $end = date_format($to, $format);
+     return '[' . $start . ' TO ' . $end . ']';
+   }
+   
+   
+   function title() {
+    return $this->argument;
+   }
+ }
