You are here

protected function views_aggregator_plugin_style_table::compare_result_rows in Views Aggregator Plus 7

Compare function for aggregated groups, for use in sorting functions.

Parameters

array $row1: The first aggregated group of result rows.

array $row2: The second aggregated group of result rows.

Return value

int The compare code indicating whether $row1 is smaller than (-1), equal to (0) or greater than (1) $row2.

File

views/views_aggregator_plugin_style_table.inc, line 1038
views_aggregator_plugin_style_table.inc

Class

views_aggregator_plugin_style_table
Style plugin to render each item as a row in a table.

Code

protected function compare_result_rows($row1, $row2) {

  // The sorting data may be raw or rendered, while the sorting style may be
  // alphabetical or numeric.
  //
  // Columns that need to be sorted using raw values:
  // o numbers and moneys, so that "$1,000" comes AFTER "$9.99" (ascending)
  // o dates and date ranges (@todo)
  //
  // Columns that need to be sorted using rendered, post-aggregated values:
  // o Views PHP expressions, addresses, taxonomy terms.
  $field_handler = $this->view->field[$this->active];
  $field_type = isset($field_handler->field_info['type']) ? $field_handler->field_info['type'] : '';

  // AddressFields, taxonomy terms and Views PHP expressions are compared in
  // rendered format.
  $compare_rendered = $field_type == 'addressfield' || $field_type == 'taxonomy_term_reference' || is_a($field_handler, 'views_php_handler_field');

  // Get the cells from the raw or rendered fields.
  // Note that raw data may contain HTML tags too, so always strip.
  $cell1 = strip_tags($this
    ->get_cell($field_handler, $row1->num, $compare_rendered));
  $cell2 = strip_tags($this
    ->get_cell($field_handler, $row2->num, $compare_rendered));
  if ((double) $cell1 == (double) $cell2) {

    // If both cells cast to zero, then compare alphabetically.
    $compare = $cell1 == $cell2 ? 0 : ($cell1 < $cell2 ? -1 : 1);
  }
  else {

    // Compare numerically, i.e. "20 km" comes after "9.5 km".
    // The double cast causes a read up to the first non-number related char.
    $compare = (double) $cell1 < (double) $cell2 ? -1 : 1;
  }
  return $this->order == 'asc' ? $compare : -$compare;
}