From 63a292d53bf04e72ae159ccddbd9a0cc27d25f79 Mon Sep 17 00:00:00 2001
From: Travis Carden <TravisCarden@236758.no-reply.drupal.org>
Date: Fri, 24 Feb 2012 09:29:10 -0600
Subject: [PATCH] Issue #206127: Added ability to sort Views exposed filters SELECT lists in reverse.

---
 date_api.module       |   61 ++++++++++++++++++++++++-------------------------
 date_api_elements.inc |    7 +----
 2 files changed, 32 insertions(+), 36 deletions(-)

diff --git a/date_api.module b/date_api.module
index 67d6d63..3df6dde 100644
--- a/date_api.module
+++ b/date_api.module
@@ -286,21 +286,21 @@ function date_week_days_ordered($weekdays) {
 /**
  * An array of years.
  *
- * @param int $min
- *   the minimum year in the array
- * @param int $max
- *   the maximum year in the array
+ * @param int $start
+ *   the start year in the array
+ * @param int $end
+ *   the end year in the array
  * @param $required
  *   If not required, will include a blank value at the beginning of the array.
  * @return
  *   an array of years in the selected range
  */
-function date_years($min = 0, $max = 0, $required = FALSE) {
-  // Have to be sure $min and $max are valid values;
-  if (empty($min)) $min = intval(date('Y', time()) - 3);
-  if (empty($max)) $max = intval(date('Y', time()) + 3);
+function date_years($start = 0, $end = 0, $required = FALSE) {
+  // Have to be sure $start and $end are valid values;
+  if (empty($start)) $start = intval(date('Y', time()) - 3);
+  if (empty($end)) $end = intval(date('Y', time()) + 3);
   $none = array(0 => '');
-  return !$required ? $none + drupal_map_assoc(range($min, $max)) : drupal_map_assoc(range($min, $max));
+  return !$required ? $none + drupal_map_assoc(range($start, $end)) : drupal_map_assoc(range($start, $end));
 }
 
 /**
@@ -2571,50 +2571,49 @@ function date_range_valid($string) {
 }
 
 /**
- * Split a string like -3:+3 or 2001:2010 into
- * an array of min and max years.
- *
+ * Split a string like -3:+3 or 2001:2010 into 
+ * an array of start and end years.
+ * 
  * Center the range around the current year, if any, but expand it far
  * enough so it will pick up the year value in the field in case
  * the value in the field is outside the initial range.
  */
 function date_range_years($string, $date = NULL) {
   $this_year = date_format(date_now(), 'Y');
-  list($min_year, $max_year) = explode(':', $string);
+  list($start_year, $end_year) = explode(':', $string);
 
   // Valid patterns would be -5:+5, 0:+1, 2008:2010.
   $plus_pattern = '@[\+|\-][0-9]{1,4}@';
   $year_pattern = '@[0-9]{4}@';
-  if (!preg_match($year_pattern, $min_year, $matches)) {
-    if (preg_match($plus_pattern, $min_year, $matches)) {
-      $min_year = $this_year + $matches[0];
+  if (!preg_match($year_pattern, $start_year, $matches)) {
+    if (preg_match($plus_pattern, $start_year, $matches)) {
+      $start_year = $this_year + $matches[0];
     }
     else {
-      $min_year = $this_year;
+      $start_year = $this_year;
     }
   }
-  if (!preg_match($year_pattern, $max_year, $matches)) {
-    if (preg_match($plus_pattern, $max_year, $matches)) {
-      $max_year = $this_year + $matches[0];
+  if (!preg_match($year_pattern, $end_year, $matches)) {
+    if (preg_match($plus_pattern, $end_year, $matches)) {
+      $end_year = $this_year + $matches[0];
     }
     else {
-      $max_year = $this_year;
+      $end_year = $this_year;
     }
   }
-  // We expect the $min year to be less than the $max year.
-  // Some custom values for -99:+99 might not obey that.
-  if ($min_year > $max_year) {
-    $temp = $max_year;
-    $max_year = $min_year;
-    $min_year = $temp;
-  }
   // If there is a current value, stretch the range to include it.
   $value_year = is_object($date) ? date_format($date, 'Y') : '';
   if (!empty($value_year)) {
-    $min_year = min($value_year, $min_year);
-    $max_year = max($value_year, $max_year);
+    if ($start_year < $end_year) {
+      $start_year = (int) min($value_year, $start_year);
+      $end_year = (int) max($value_year, $end_year);
+    }
+    else {
+      $start_year = (int) max($value_year, $start_year);
+      $end_year = (int) min($value_year, $end_year);
+    }
   }
-  return array($min_year, $max_year);
+  return array($start_year, $end_year);
 }
 
 /**
diff --git a/date_api_elements.inc b/date_api_elements.inc
index 02e639a..409fd9b 100644
--- a/date_api_elements.inc
+++ b/date_api_elements.inc
@@ -256,13 +256,10 @@ function date_parts_element($element, $date, $format) {
       );
     switch ($field) {
       case 'year':
-        $range = date_range_years($element['#date_year_range'], $date);
-        $min_year = $range[0];
-        $max_year = $range[1];
-
+        list($start_year, $end_year) = date_range_years($element['#date_year_range'], $date);
         $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'Y') : '';
         if ($part_type == 'select') {
-          $sub_element[$field]['#options'] = drupal_map_assoc(date_years($min_year, $max_year, $part_required));
+          $sub_element[$field]['#options'] = drupal_map_assoc(date_years($start_year, $end_year, $part_required));
         }
         break;
       case 'month':
-- 
1.7.4.1

