You are here

public static function birthdays_field_views_handler_sort::addOrderBy in Birthdays 7

Add an "ORDER BY time to next birthday" to a query.

Parameters

$query: The Views query.

$order: DESC or ASC.

An array with the keys year, month and day where the values are the real: column names in the query.

$fieldname: The name for the sorting column.

2 calls to birthdays_field_views_handler_sort::addOrderBy()
birthdays_field_views_handler_field::click_sort in views/birthdays_field_views_handler_field.inc
Overrides views_handler_field_field::click_sort().
birthdays_field_views_handler_sort::query in views/birthdays_field_views_handler_sort.inc
Overrides views_handler_sort::query(). Add an ORDER_BY to the query.

File

views/birthdays_field_views_handler_sort.inc, line 45
The birthdays_field_views_handler_sort class.

Class

birthdays_field_views_handler_sort
A views handler for sorting by a birthday field.

Code

public static function addOrderBy($query, $order, $columns, $fieldname) {

  // Use the formula to calculate a value for the current time.
  $now = date('m', REQUEST_TIME) * 32 + date('d', REQUEST_TIME);

  // Get the formula for the current database type.
  switch (Database::getConnection()
    ->databaseType()) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $formula = "CASE WHEN {$columns['month']}*32 + {$columns['day']} >= {$now} THEN {$columns['month']}*32 + {$columns['day']} ELSE {$columns['month']}*32 + {$columns['day']} + 1000 END";
      break;
  }

  // Add the ORDER BY.
  if (isset($formula)) {
    $query
      ->add_orderby(NULL, $formula, $order, $fieldname);
  }
}