Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.55
diff -u -p -r1.55 tablesort.inc
--- includes/tablesort.inc	29 Sep 2009 15:31:13 -0000	1.55
+++ includes/tablesort.inc	4 Oct 2009 05:27:47 -0000
@@ -16,7 +16,7 @@
 class TableSort extends SelectQueryExtender {
 
   /**
-   * The array of fields that can be sorted by.
+   * An array of table header columns that can be sorted by, as described in theme_table().
    *
    * @var array
    */
@@ -34,16 +34,17 @@ class TableSort extends SelectQueryExten
   /**
    * Order the query based on a header array.
    *
-   * @see theme_table()
    * @param $header
-   *   Table header array.
+   *   An array of table header columns as described in theme_table().
+   *
+   * @see theme_table()
    */
-  public function orderByHeader(Array $header) {
+  public function orderByHeader(array $header) {
     $this->header = $header;
     $ts = $this->init();
-    if (!empty($ts['sql'])) {
+    if (!empty($ts['field'])) {
       // Based on code from db_escape_table(), but this can also contain a dot.
-      $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['sql']);
+      $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $ts['field']);
 
       // Sort order can only be ASC or DESC.
       $sort = drupal_strtoupper($ts['sort']);
@@ -67,23 +68,12 @@ class TableSort extends SelectQueryExten
    * Determine the current sort direction.
    *
    * @param $headers
-   *   An array of column headers in the format described in theme_table().
+   *   An array of table header columns as described in theme_table().
    * @return
    *   The current sort direction ("asc" or "desc").
    */
   protected function getSort() {
-    if (isset($_GET['sort'])) {
-      return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
-    }
-    // User has not specified a sort. Use default if specified; otherwise use "asc".
-    else {
-      foreach ($this->header as $header) {
-        if (is_array($header) && array_key_exists('sort', $header)) {
-          return $header['sort'];
-        }
-      }
-    }
-    return 'asc';
+    return tablesort_get_sort($this->header);
   }
 
   /**
@@ -102,38 +92,13 @@ class TableSort extends SelectQueryExten
   /**
    * Determine the current sort criterion.
    *
-   * @param $headers
-   *   An array of column headers in the format described in theme_table().
    * @return
    *   An associative array describing the criterion, containing the keys:
-   *   - "name": The localized title of the table column.
-   *   - "sql": The name of the database field to sort on.
+   *   - name: The localized title of the table column.
+   *   - field: The name of the database field to sort on.
    */
   protected function order() {
-    $order = isset($_GET['order']) ? $_GET['order'] : '';
-    foreach ($this->header as $header) {
-      if (isset($header['data']) && $order == $header['data']) {
-        return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
-      }
-
-      if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
-        $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
-      }
-    }
-
-    if (isset($default)) {
-      return $default;
-    }
-    else {
-      // The first column specified is initial 'order by' field unless otherwise specified
-      if (is_array($this->header[0])) {
-        $this->header[0] += array('data' => NULL, 'field' => NULL);
-        return array('name' => $this->header[0]['data'], 'sql' => $this->header[0]['field']);
-      }
-      else {
-        return array('name' => $this->header[0]);
-      }
-    }
+    return tablesort_get_order($this->header);
   }
 }
 
@@ -156,7 +121,7 @@ function tablesort_init($header) {
  * @param $cell
  *   The cell to format.
  * @param $header
- *   An array of column headers in the format described in theme_table().
+ *   An array of table header columns as described in theme_table().
  * @param $ts
  *   The current table sort context as returned from tablesort_init().
  * @return
@@ -191,7 +156,7 @@ function tablesort_header($cell, $header
  * @param $cell
  *   The cell to format.
  * @param $header
- *   An array of column headers in the format described in theme_table().
+ *   An array of table header columns as described in theme_table().
  * @param $ts
  *   The current table sort context as returned from tablesort_init().
  * @param $i
@@ -225,37 +190,37 @@ function tablesort_get_query_parameters(
 /**
  * Determine the current sort criterion.
  *
- * @param $headers
- *   An array of column headers in the format described in theme_table().
+ * @param $header
+ *   An array of table header columns as described in theme_table().
  * @return
  *   An associative array describing the criterion, containing the keys:
- *   - "name": The localized title of the table column.
- *   - "sql": The name of the database field to sort on.
+ *   - name: The localized title of the table column.
+ *   - field: The name of the database field to sort on.
  */
-function tablesort_get_order($headers) {
+function tablesort_get_order($header) {
   $order = isset($_GET['order']) ? $_GET['order'] : '';
-  foreach ($headers as $header) {
-    if (isset($header['data']) && $order == $header['data']) {
-      return array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
+  foreach ($header as $column) {
+    // Use the header column matching the URL parameter.
+    if (isset($column['data']) && $order == $column['data']) {
+      return array('name' => $column['data'], 'field' => isset($column['field']) ? $column['field'] : '');
     }
-
-    if (isset($header['sort']) && ($header['sort'] == 'asc' || $header['sort'] == 'desc')) {
-      $default = array('name' => $header['data'], 'sql' => isset($header['field']) ? $header['field'] : '');
+    // In case no header column will match the URL parameter, and this column
+    // defines 'sort', it is supposed to be the default sorting column.
+    if (isset($column['sort']) && ($column['sort'] == 'asc' || $column['sort'] == 'desc')) {
+      $default = array('name' => $column['data'], 'field' => isset($column['field']) ? $column['field'] : '');
     }
   }
 
+  // If there was a default sorting column, return that.
   if (isset($default)) {
     return $default;
   }
+  // Otherwise, use the first header column that defines a field.
   else {
-    // The first column specified is the initial 'order by' field unless otherwise specified.
-    $first = current($headers);
-    if (is_array($first)) {
-      $first += array('data' => NULL, 'field' => NULL);
-      return array('name' => $first['data'], 'sql' => $first['field']);
-    }
-    else {
-      return array('name' => $first, 'sql' => '');
+    foreach ($header as $column) {
+      if (isset($column['field'])) {
+        return array('name' => $column['data'], 'field' => $column['field']);
+      }
     }
   }
 }
@@ -263,20 +228,20 @@ function tablesort_get_order($headers) {
 /**
  * Determine the current sort direction.
  *
- * @param $headers
- *   An array of column headers in the format described in theme_table().
+ * @param $header
+ *   An array of table header columns as described in theme_table().
  * @return
  *   The current sort direction ("asc" or "desc").
  */
-function tablesort_get_sort($headers) {
+function tablesort_get_sort($header) {
   if (isset($_GET['sort'])) {
     return ($_GET['sort'] == 'desc') ? 'desc' : 'asc';
   }
   // User has not specified a sort. Use default if specified; otherwise use "asc".
   else {
-    foreach ($headers as $header) {
-      if (is_array($header) && array_key_exists('sort', $header)) {
-        return $header['sort'];
+    foreach ($header as $column) {
+      if (is_array($column) && isset($column['sort'])) {
+        return $column['sort'];
       }
     }
   }
