You are here

public function views_handler_sort_publication_date::query in Publication Date 7.2

Called to add the sort to a query.

Overrides views_handler_sort_date::query

File

includes/views_handler_sort_publication_date.inc, line 52
Definition of views_handler_sort_publication_date.

Class

views_handler_sort_publication_date
Sort handler for publication dates.

Code

public function query() {
  $this
    ->ensure_my_table();
  if ($this->options['null_date'] == 'null') {
    $field_name = $this->real_field;
    $field = $this->table_alias . '.' . $field_name;
  }
  else {
    $field_name = $this->real_field . '_or_' . $this->options['null_date'];
    switch ($this->options['null_date']) {
      case 'now':
        $alt_value = REQUEST_TIME;
        break;
      case 'created':
        $alt_value = "node.created";
        break;
      case 'changed':
        $alt_value = "node.changed";
        break;
    }
    $field = "COALESCE({$this->table_alias}.{$this->real_field}, {$alt_value})";
  }
  switch ($this->options['granularity']) {
    case 'second':
    default:
      $this->query
        ->add_orderby(NULL, $field, $this->options['order'], $this->table_alias . '_' . $field_name);
      return;
    case 'minute':
      $formula = views_date_sql_format('YmdHi', $field);
      break;
    case 'hour':
      $formula = views_date_sql_format('YmdH', $field);
      break;
    case 'day':
      $formula = views_date_sql_format('Ymd', $field);
      break;
    case 'month':
      $formula = views_date_sql_format('Ym', $field);
      break;
    case 'year':
      $formula = views_date_sql_format('Y', $field);
      break;
  }

  // Add the field.
  $this->query
    ->add_orderby(NULL, $formula, $this->options['order'], $this->table_alias . '_' . $field_name . '_' . $this->options['granularity']);
}